@Transactional(propagation = Propagation.NOT_SUPPORTED) public List<AccessKey> list( Long userId, String label, String labelPattern, Integer type, String sortField, Boolean sortOrderAsc, Integer take, Integer skip) { CriteriaBuilder cb = genericDAO.criteriaBuilder(); CriteriaQuery<AccessKey> cq = cb.createQuery(AccessKey.class); Root<AccessKey> from = cq.from(AccessKey.class); Predicate[] predicates = CriteriaHelper.accessKeyListPredicates( cb, from, userId, ofNullable(label), ofNullable(labelPattern), ofNullable(type)); cq.where(predicates); CriteriaHelper.order(cb, cq, from, ofNullable(sortField), Boolean.TRUE.equals(sortOrderAsc)); TypedQuery<AccessKey> query = genericDAO.createQuery(cq); ofNullable(skip).ifPresent(query::setFirstResult); ofNullable(take).ifPresent(query::setMaxResults); genericDAO.cacheQuery(query, of(CacheConfig.bypass())); return query.getResultList(); }
@Transactional public AccessKey authenticate(@NotNull String key) { Optional<AccessKey> accessKeyOpt = genericDAO .createNamedQuery(AccessKey.class, "AccessKey.getByKey", Optional.of(CacheConfig.get())) .setParameter("someKey", key) .getResultList() .stream() .findFirst(); if (!accessKeyOpt.isPresent()) { return null; } AccessKey accessKey = accessKeyOpt.get(); final Long expirationPeriod = configurationService.getLong(Constants.SESSION_TIMEOUT, Constants.DEFAULT_SESSION_TIMEOUT); if (accessKey.getExpirationDate() != null) { final Long expiresIn = accessKey.getExpirationDate().getTime() - timestampService.getTimestamp().getTime(); if (AccessKeyType.SESSION == accessKey.getType() && expiresIn > 0 && expiresIn < expirationPeriod / 2) { em.refresh(accessKey, LockModeType.PESSIMISTIC_WRITE); accessKey.setExpirationDate( new Date(timestampService.getTimestamp().getTime() + expirationPeriod)); return genericDAO.merge(accessKey); } } return accessKey; }
@Transactional(propagation = Propagation.SUPPORTS) public AccessKey find(@NotNull Long keyId, @NotNull Long userId) { return genericDAO .createNamedQuery(AccessKey.class, "AccessKey.getById", Optional.of(CacheConfig.refresh())) .setParameter("userId", userId) .setParameter("accessKeyId", keyId) .getResultList() .stream() .findFirst() .orElse(null); }
@Transactional(propagation = Propagation.SUPPORTS) public boolean hasAccessToDevice(AccessKey accessKey, String deviceGuid) { Set<AccessKeyPermission> permissions = accessKey.getPermissions(); Set<String> allowedDevices = new HashSet<>(); Set<Long> allowedNetworks = new HashSet<>(); User accessKeyUser = userService.findUserWithNetworks(accessKey.getUser().getId()); Set<AccessKeyPermission> toRemove = new HashSet<>(); Device device = genericDAO .createNamedQuery(Device.class, "Device.findByUUID", of(CacheConfig.refresh())) .setParameter("guid", deviceGuid) .getSingleResult(); for (AccessKeyPermission currentPermission : permissions) { if (currentPermission.getDeviceGuidsAsSet() == null) { allowedDevices.add(null); } else { if (!currentPermission.getDeviceGuidsAsSet().contains(deviceGuid)) { toRemove.add(currentPermission); } else { allowedDevices.addAll(currentPermission.getDeviceGuidsAsSet()); } } if (currentPermission.getNetworkIdsAsSet() == null) { allowedNetworks.add(null); } else { if (device.getNetwork() != null) { if (!currentPermission.getNetworkIdsAsSet().contains(device.getNetwork().getId())) { toRemove.add(currentPermission); } else { allowedNetworks.addAll(currentPermission.getNetworkIdsAsSet()); } } } } permissions.removeAll(toRemove); boolean hasAccess; hasAccess = allowedDevices.contains(null) ? userService.hasAccessToDevice(accessKeyUser, device.getGuid()) : allowedDevices.contains(device.getGuid()) && userService.hasAccessToDevice(accessKeyUser, device.getGuid()); hasAccess = hasAccess && allowedNetworks.contains(null) ? accessKeyUser.isAdmin() || accessKeyUser.getNetworks().contains(device.getNetwork()) : (accessKeyUser.isAdmin() || accessKeyUser.getNetworks().contains(device.getNetwork())) && allowedNetworks.contains(device.getNetwork().getId()); return hasAccess; }