/** * Revokes user access to given network * * @param userId id of user * @param networkId id of network */ @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) public void unassignNetwork(@NotNull long userId, @NotNull long networkId) { User existingUser = userDAO.findById(userId); if (existingUser == null) { throw new HiveException( String.format(Messages.USER_NOT_FOUND, userId), NOT_FOUND.getStatusCode()); } Network existingNetwork = networkDAO.getByIdWithUsers(networkId); if (existingNetwork != null) { existingNetwork.getUsers().remove(existingUser); networkDAO.merge(existingNetwork); } }
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) public User updateUser(@NotNull Long id, UserUpdate userToUpdate) { User existing = userDAO.findById(id); if (existing == null) { throw new HiveException( String.format(Messages.USER_NOT_FOUND, id), NOT_FOUND.getStatusCode()); } if (userToUpdate == null) { return existing; } if (userToUpdate.getLogin() != null) { String newLogin = StringUtils.trim(userToUpdate.getLogin().getValue()); User withSuchLogin = userDAO.findByLogin(newLogin); if (withSuchLogin != null && !withSuchLogin.getId().equals(id)) { throw new HiveException(Messages.DUPLICATE_LOGIN, FORBIDDEN.getStatusCode()); } existing.setLogin(newLogin); } if (userToUpdate.getPassword() != null) { if (StringUtils.isEmpty(userToUpdate.getPassword().getValue())) { throw new HiveException(Messages.PASSWORD_REQUIRED, BAD_REQUEST.getStatusCode()); } String salt = passwordService.generateSalt(); String hash = passwordService.hashPassword(userToUpdate.getPassword().getValue(), salt); existing.setPasswordSalt(salt); existing.setPasswordHash(hash); } if (userToUpdate.getRole() != null) { existing.setRole(userToUpdate.getRoleEnum()); } if (userToUpdate.getStatus() != null) { existing.setStatus(userToUpdate.getStatusEnum()); } hiveValidator.validate(existing); return userDAO.update(existing); }
/** * 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 */ @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) public User findById(@NotNull long id) { return userDAO.findById(id); }