public static Predicate[] deviceTypeListPredicates(CriteriaBuilder cb, Root<DeviceType> from, Optional<String> nameOpt, Optional<String> namePatternOpt, Optional<HivePrincipal> principalOpt) { List<Predicate> predicates = new LinkedList<>(); nameOpt.ifPresent(name -> predicates.add(cb.equal(from.get("name"), name))); namePatternOpt.ifPresent(pattern -> predicates.add(cb.like(from.get("name"), pattern))); principalOpt.flatMap(principal -> { UserVO user = principal.getUser(); return ofNullable(user); }).ifPresent(user -> { if (!user.isAdmin() && !user.getAllDeviceTypesAvailable()) { User usr = User.convertToEntity(user); predicates.add(from.join("users").in(usr)); } }); principalOpt.flatMap(principal -> { Set<Long> deviceTypes = principal.getDeviceTypeIds(); return ofNullable(deviceTypes); }).ifPresent(deviceTypes -> predicates.add(from.<Long>get("id").in(deviceTypes))); return predicates.toArray(new Predicate[predicates.size()]); }
/** * Allows user access to given device type * * @param userId id of user * @param deviceTypeId id of device type */ @Transactional(propagation = Propagation.REQUIRED) public void assignDeviceType(@NotNull long userId, @NotNull long deviceTypeId) { UserVO existingUser = userDao.find(userId); if (existingUser == null) { logger.error("Can't assign device type with id {}: user {} not found", deviceTypeId, userId); throw new HiveException(String.format(Messages.USER_NOT_FOUND, userId), NOT_FOUND.getStatusCode()); } if (existingUser.getAllDeviceTypesAvailable()) { throw new HiveException(String.format(Messages.DEVICE_TYPE_ASSIGNMENT_NOT_ALLOWED, userId), FORBIDDEN.getStatusCode()); } DeviceTypeWithUsersAndDevicesVO existingDeviceType = deviceTypeDao.findWithUsers(deviceTypeId).orElse(null); if (Objects.isNull(existingDeviceType)) { throw new HiveException(String.format(Messages.DEVICE_TYPE_NOT_FOUND, deviceTypeId), NOT_FOUND.getStatusCode()); } deviceTypeDao.assignToDeviceType(existingDeviceType, existingUser); }
/** * Revokes user access to given device type * * @param userId id of user * @param deviceTypeId id of device type */ @Transactional(propagation = Propagation.REQUIRED) public void unassignDeviceType(@NotNull long userId, @NotNull long deviceTypeId) { UserVO existingUser = userDao.find(userId); if (existingUser == null) { logger.error("Can't unassign device type with id {}: user {} not found", deviceTypeId, userId); throw new HiveException(String.format(Messages.USER_NOT_FOUND, userId), NOT_FOUND.getStatusCode()); } if (existingUser.getAllDeviceTypesAvailable()) { throw new HiveException(String.format(Messages.DEVICE_TYPE_ASSIGNMENT_NOT_ALLOWED, userId), FORBIDDEN.getStatusCode()); } DeviceTypeVO existingDeviceType = deviceTypeDao.find(deviceTypeId); if (existingDeviceType == null) { logger.error("Can't unassign user with id {}: device type {} not found", userId, deviceTypeId); throw new HiveException(String.format(Messages.DEVICE_TYPE_NOT_FOUND, deviceTypeId), NOT_FOUND.getStatusCode()); } userDao.unassignDeviceType(existingUser, deviceTypeId); }
if (user.getAllDeviceTypesAvailable()) { idForFiltering = null;
if (!userVO.getAllDeviceTypesAvailable()) { principal.setAllDeviceTypesAvailable(false); if (deviceTypeIds.contains("*")) { principal.setAllDeviceTypesAvailable(true); } else if (userVO != null && userVO.getAllDeviceTypesAvailable()) { principal.setAllDeviceTypesAvailable(true); } else {
if (user.getAllDeviceTypesAvailable() == null) { user.setAllDeviceTypesAvailable(true);
public static UserWithNetworkVO fromUserVO(UserVO dc) { UserWithNetworkVO vo = null; if (dc != null) { vo = new UserWithNetworkVO(); vo.setData(dc.getData()); vo.setId(dc.getId()); vo.setData(dc.getData()); vo.setLastLogin(dc.getLastLogin()); vo.setLogin(dc.getLogin()); vo.setLoginAttempts(dc.getLoginAttempts()); vo.setNetworks(new HashSet<>()); vo.setPasswordHash(dc.getPasswordHash()); vo.setPasswordSalt(dc.getPasswordSalt()); vo.setRole(dc.getRole()); vo.setStatus(dc.getStatus()); vo.setIntroReviewed(dc.getIntroReviewed()); vo.setAllDeviceTypesAvailable(dc.getAllDeviceTypesAvailable()); } return vo; }
public static UserWithDeviceTypeVO fromUserVO(UserVO dc) { UserWithDeviceTypeVO vo = null; if (dc != null) { vo = new UserWithDeviceTypeVO(); vo.setData(dc.getData()); vo.setId(dc.getId()); vo.setData(dc.getData()); vo.setLastLogin(dc.getLastLogin()); vo.setLogin(dc.getLogin()); vo.setLoginAttempts(dc.getLoginAttempts()); vo.setDeviceTypes(new HashSet<>()); vo.setPasswordHash(dc.getPasswordHash()); vo.setPasswordSalt(dc.getPasswordSalt()); vo.setRole(dc.getRole()); vo.setStatus(dc.getStatus()); vo.setIntroReviewed(dc.getIntroReviewed()); vo.setAllDeviceTypesAvailable(dc.getAllDeviceTypesAvailable()); } return vo; }
public static User convertToEntity(UserVO dc) { User vo = null; if (dc != null) { vo = new User(); vo.setData(dc.getData()); vo.setId(dc.getId()); vo.setLastLogin(dc.getLastLogin()); vo.setLogin(dc.getLogin()); vo.setLoginAttempts(dc.getLoginAttempts()); //TODO [rafa] ??? vo.setNetworks(dc.getN); vo.setPasswordHash(dc.getPasswordHash()); vo.setPasswordSalt(dc.getPasswordSalt()); vo.setRole(dc.getRole()); vo.setStatus(dc.getStatus()); vo.setIntroReviewed(dc.getIntroReviewed()); vo.setAllDeviceTypesAvailable(dc.getAllDeviceTypesAvailable()); } return vo; }