@Test public void updateUserNonDatabaseAuthTest() throws Exception { final String roleName = "roleName"; AuthenticationType authenticationType = AuthenticationType.LDAP; UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L); userEntity.setId(1L); UserRoleModel roles = createUserRoleModel(1L, roleName, true); UserModel userModel = UserModel.existingUser(1L, username, "", emailAddress, authenticationType, Set.of(roles), true); UserRoleRelation userRoleRelation = new UserRoleRelation(1L, 2L); UserRoleModel userRoleModel = createUserRoleModel(1L, roleName, true); Mockito.when(userRepository.findById(Mockito.any())).thenReturn(Optional.of(userEntity)); Mockito.when(authenticationTypeAccessor.getAuthenticationType(Mockito.any())).thenReturn(Optional.of(authenticationType)); Mockito.when(userRepository.save(Mockito.any())).thenReturn(userEntity); createModelMocks(userRoleRelation, userRoleModel, authenticationType); DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor); UserModel updatedUserModel = defaultUserAccessor.updateUser(userModel, false); Mockito.verify(roleAccessor).updateUserRoles(Mockito.eq(userEntity.getId()), Mockito.any()); testUserModel(userEntity.getId(), username, emailAddress, roleName, updatedUserModel); }
@Override public UserModel updateUser(UserModel user, boolean passwordEncoded) throws AlertDatabaseConstraintException { Long userId = user.getId(); if (null == userId) { throw new AlertDatabaseConstraintException("A user id must be specified"); } UserEntity existingUser = userRepository.findById(userId) .orElseThrow(() -> new AlertDatabaseConstraintException(String.format("No user found with id '%s'", userId))); Long existingUserId = existingUser.getId(); UserEntity savedEntity = existingUser; Optional<AuthenticationType> authenticationType = authenticationTypeAccessor.getAuthenticationType(existingUser.getAuthenticationType()); if (!authenticationType.isPresent()) { throw new AlertDatabaseConstraintException("Unknown Authentication Type, user not updated."); } else if (AuthenticationType.DATABASE != authenticationType.get()) { boolean isUserNameInvalid = !StringUtils.equals(existingUser.getUserName(), user.getName()); boolean isEmailInvalid = !StringUtils.equals(existingUser.getEmailAddress(), user.getEmailAddress()); boolean isPasswordSet = StringUtils.isNotBlank(user.getPassword()); if (isUserNameInvalid || isEmailInvalid || isPasswordSet) { throw new AlertDatabaseConstraintException("An external user cannot change its credentials."); } } else { String password = passwordEncoded ? user.getPassword() : defaultPasswordEncoder.encode(user.getPassword()); UserEntity newEntity = new UserEntity(user.getName(), password, user.getEmailAddress(), user.isExpired(), user.isLocked(), user.isPasswordExpired(), user.isEnabled(), existingUser.getAuthenticationType()); newEntity.setId(existingUserId); savedEntity = userRepository.save(newEntity); } roleAccessor.updateUserRoles(existingUserId, user.getRoles()); return createModel(savedEntity); }
DefaultUserAccessor implements UserAccessor { @Override public UserModel updateUser(UserModel user, boolean passwordEncoded) throws AlertDatabaseConstraintException { Long userId = user.getId(); if (null == userId) { throw new AlertDatabaseConstraintException("A user id must be specified"); } UserEntity existingUser = userRepository.findById(userId) .orElseThrow(() -> new AlertDatabaseConstraintException(String.format("No user found with id '%s'", userId))); Long existingUserId = existingUser.getId(); UserEntity savedEntity = existingUser; Optional<AuthenticationType> authenticationType = authenticationTypeAccessor.getAuthenticationType(existingUser.getAuthenticationType()); if (!authenticationType.isPresent()) { throw new AlertDatabaseConstraintException("Unknown Authentication Type, user not updated."); } else if (AuthenticationType.DATABASE != authenticationType.get()) { boolean isUserNameInvalid = !StringUtils.equals(existingUser.getUserName(), user.getName()); boolean isEmailInvalid = !StringUtils.equals(existingUser.getEmailAddress(), user.getEmailAddress()); boolean isPasswordSet = StringUtils.isNotBlank(user.getPassword()); if (isUserNameInvalid || isEmailInvalid || isPasswordSet) { throw new AlertDatabaseConstraintException("An external user cannot change its credentials."); } } else { String password = passwordEncoded ? user.getPassword() : defaultPasswordEncoder.encode(user.getPassword()); UserEntity newEntity = new UserEntity(user.getName(), password, user.getEmailAddress(), user.isExpired(), user.isLocked(), user.isPasswordExpired(), user.isEnabled(), existingUser.getAuthenticationType()); newEntity.setId(existingUserId); savedEntity = userRepository.save(newEntity); } roleAccessor.updateUserRoles(existingUserId, user.getRoles()); return createModel(savedEntity); } }
DefaultUserAccessor implements UserAccessor { @Override public UserModel updateUser(UserModel user, boolean passwordEncoded) throws AlertDatabaseConstraintException { Long userId = user.getId(); if (null == userId) { throw new AlertDatabaseConstraintException("A user id must be specified"); } UserEntity existingUser = userRepository.findById(userId) .orElseThrow(() -> new AlertDatabaseConstraintException(String.format("No user found with id '%s'", userId))); Long existingUserId = existingUser.getId(); UserEntity savedEntity = existingUser; Optional<AuthenticationType> authenticationType = authenticationTypeAccessor.getAuthenticationType(existingUser.getAuthenticationType()); if (!authenticationType.isPresent()) { throw new AlertDatabaseConstraintException("Unknown Authentication Type, user not updated."); } else if (AuthenticationType.DATABASE != authenticationType.get()) { boolean isUserNameInvalid = !StringUtils.equals(existingUser.getUserName(), user.getName()); boolean isEmailInvalid = !StringUtils.equals(existingUser.getEmailAddress(), user.getEmailAddress()); boolean isPasswordSet = StringUtils.isNotBlank(user.getPassword()); if (isUserNameInvalid || isEmailInvalid || isPasswordSet) { throw new AlertDatabaseConstraintException("An external user cannot change its credentials."); } } else { String password = passwordEncoded ? user.getPassword() : defaultPasswordEncoder.encode(user.getPassword()); UserEntity newEntity = new UserEntity(user.getName(), password, user.getEmailAddress(), user.isExpired(), user.isLocked(), user.isPasswordExpired(), user.isEnabled(), existingUser.getAuthenticationType()); newEntity.setId(existingUserId); savedEntity = userRepository.save(newEntity); } roleAccessor.updateUserRoles(existingUserId, user.getRoles()); return createModel(savedEntity); } @Autowired  DefaultUserAccessor(UserRepository userRepository, UserRoleRepository userRoleRepository, PasswordEncoder defaultPasswordEncoder, DefaultRoleAccessor roleAccessor,
        AuthenticationTypeAccessor authenticationTypeAccessor); }
DefaultUserAccessor implements UserAccessor { @Override public UserModel updateUser(UserModel user, boolean passwordEncoded) throws AlertDatabaseConstraintException { Long userId = user.getId(); if (null == userId) { throw new AlertDatabaseConstraintException("A user id must be specified"); } UserEntity existingUser = userRepository.findById(userId) .orElseThrow(() -> new AlertDatabaseConstraintException(String.format("No user found with id '%s'", userId))); Long existingUserId = existingUser.getId(); UserEntity savedEntity = existingUser; Optional<AuthenticationType> authenticationType = authenticationTypeAccessor.getAuthenticationType(existingUser.getAuthenticationType()); if (!authenticationType.isPresent()) { throw new AlertDatabaseConstraintException("Unknown Authentication Type, user not updated."); } else if (AuthenticationType.DATABASE != authenticationType.get()) { boolean isUserNameInvalid = !StringUtils.equals(existingUser.getUserName(), user.getName()); boolean isEmailInvalid = !StringUtils.equals(existingUser.getEmailAddress(), user.getEmailAddress()); boolean isPasswordSet = StringUtils.isNotBlank(user.getPassword()); if (isUserNameInvalid || isEmailInvalid || isPasswordSet) { throw new AlertDatabaseConstraintException("An external user cannot change its credentials."); } } else { String password = passwordEncoded ? user.getPassword() : defaultPasswordEncoder.encode(user.getPassword()); UserEntity newEntity = new UserEntity(user.getName(), password, user.getEmailAddress(), user.isExpired(), user.isLocked(), user.isPasswordExpired(), user.isEnabled(), existingUser.getAuthenticationType()); newEntity.setId(existingUserId); savedEntity = userRepository.save(newEntity); } roleAccessor.updateUserRoles(existingUserId, user.getRoles()); return createModel(savedEntity); } @Autowired  DefaultUserAccessor(UserRepository userRepository, UserRoleRepository userRoleRepository, PasswordEncoder defaultPasswordEncoder, DefaultRoleAccessor roleAccessor,
        AuthenticationTypeAccessor authenticationTypeAccessor); @Override List<UserModel> getUsers(); @Override Optional<UserModel> getUser(Long userId); @Override Optional<UserModel> getUser(String username); @Override UserModel addUser(String userName, String password, String emailAddress); @Override UserModel addUser(UserModel user, boolean passwordEncoded); @Override UserModel updateUser(UserModel user, boolean passwordEncoded); @Override boolean assignRoles(String username, Set<Long> roleIds); @Override boolean changeUserPassword(String username, String newPassword); @Override boolean changeUserEmailAddress(String username, String emailAddress); @Override void deleteUser(String userName); @Override void deleteUser(Long userId); }
DefaultUserAccessor implements UserAccessor { @Override public UserModel updateUser(UserModel user, boolean passwordEncoded) throws AlertDatabaseConstraintException { Long userId = user.getId(); if (null == userId) { throw new AlertDatabaseConstraintException("A user id must be specified"); } UserEntity existingUser = userRepository.findById(userId) .orElseThrow(() -> new AlertDatabaseConstraintException(String.format("No user found with id '%s'", userId))); Long existingUserId = existingUser.getId(); UserEntity savedEntity = existingUser; Optional<AuthenticationType> authenticationType = authenticationTypeAccessor.getAuthenticationType(existingUser.getAuthenticationType()); if (!authenticationType.isPresent()) { throw new AlertDatabaseConstraintException("Unknown Authentication Type, user not updated."); } else if (AuthenticationType.DATABASE != authenticationType.get()) { boolean isUserNameInvalid = !StringUtils.equals(existingUser.getUserName(), user.getName()); boolean isEmailInvalid = !StringUtils.equals(existingUser.getEmailAddress(), user.getEmailAddress()); boolean isPasswordSet = StringUtils.isNotBlank(user.getPassword()); if (isUserNameInvalid || isEmailInvalid || isPasswordSet) { throw new AlertDatabaseConstraintException("An external user cannot change its credentials."); } } else { String password = passwordEncoded ? user.getPassword() : defaultPasswordEncoder.encode(user.getPassword()); UserEntity newEntity = new UserEntity(user.getName(), password, user.getEmailAddress(), user.isExpired(), user.isLocked(), user.isPasswordExpired(), user.isEnabled(), existingUser.getAuthenticationType()); newEntity.setId(existingUserId); savedEntity = userRepository.save(newEntity); } roleAccessor.updateUserRoles(existingUserId, user.getRoles()); return createModel(savedEntity); } @Autowired  DefaultUserAccessor(UserRepository userRepository, UserRoleRepository userRoleRepository, PasswordEncoder defaultPasswordEncoder, DefaultRoleAccessor roleAccessor,
        AuthenticationTypeAccessor authenticationTypeAccessor); @Override List<UserModel> getUsers(); @Override Optional<UserModel> getUser(Long userId); @Override Optional<UserModel> getUser(String username); @Override UserModel addUser(String userName, String password, String emailAddress); @Override UserModel addUser(UserModel user, boolean passwordEncoded); @Override UserModel updateUser(UserModel user, boolean passwordEncoded); @Override boolean assignRoles(String username, Set<Long> roleIds); @Override boolean changeUserPassword(String username, String newPassword); @Override boolean changeUserEmailAddress(String username, String emailAddress); @Override void deleteUser(String userName); @Override void deleteUser(Long userId);  }