/** * Retrieves user by id (no networks fetched in this case) * * @param id user id * @return User model without networks, or null if there is no such user */ @Transactional(propagation = Propagation.NOT_SUPPORTED) public UserVO findById(@NotNull long id) { return userDao.find(id); }
@Transactional(propagation = Propagation.REQUIRED) public UserVO updateUser(@NotNull Long id, UserUpdate userToUpdate, UserVO curUser) { UserVO existing = userDao.find(id);
@Transactional public UserVO disallowAllDeviceTypes(@NotNull long userId) { UserVO existingUser = userDao.find(userId); if (existingUser == null) { logger.error("Can't disallow all device types: user {} not found", userId); throw new HiveException(String.format(Messages.USER_NOT_FOUND, userId), NOT_FOUND.getStatusCode()); } return userDao.disallowAllDeviceTypes(existingUser); }
/** * Allows user access to given network * * @param userId id of user * @param networkId id of network */ @Transactional(propagation = Propagation.REQUIRED) public void assignNetwork(@NotNull long userId, @NotNull long networkId) { UserVO existingUser = userDao.find(userId); if (existingUser == null) { logger.error("Can't assign network with id {}: user {} not found", networkId, userId); throw new HiveException(String.format(Messages.USER_NOT_FOUND, userId), NOT_FOUND.getStatusCode()); } NetworkWithUsersAndDevicesVO existingNetwork = networkDao.findWithUsers(networkId).orElse(null); if (Objects.isNull(existingNetwork)) { throw new HiveException(String.format(Messages.NETWORK_NOT_FOUND, networkId), NOT_FOUND.getStatusCode()); } networkDao.assignToNetwork(existingNetwork, existingUser); }
/** * Revokes user access to given network * * @param userId id of user * @param networkId id of network */ @Transactional(propagation = Propagation.REQUIRED) public void unassignNetwork(@NotNull long userId, @NotNull long networkId) { UserVO existingUser = userDao.find(userId); if (existingUser == null) { logger.error("Can't unassign network with id {}: user {} not found", networkId, userId); throw new HiveException(String.format(Messages.USER_NOT_FOUND, userId), NOT_FOUND.getStatusCode()); } NetworkVO existingNetwork = networkDao.find(networkId); if (existingNetwork == null) { logger.error("Can't unassign user with id {}: network {} not found", userId, networkId); throw new HiveException(String.format(Messages.NETWORK_NOT_FOUND, networkId), NOT_FOUND.getStatusCode()); } userDao.unassignNetwork(existingUser, networkId); }
/** * 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); }