Search in sources :

Example 16 with UserEntity

use of com.synopsys.integration.alert.database.user.UserEntity in project hub-alert by blackducksoftware.

the class DefaultUserAccessorTest method getUsersTest.

@Test
public void getUsersTest() {
    final Long authenticationTypeId = 1L;
    final String roleName = "userName";
    UserEntity userEntity = new UserEntity(username, password, emailAddress, authenticationTypeId);
    userEntity.setId(1L);
    UserRoleRelation userRoleRelation = new UserRoleRelation(1L, 2L);
    UserRoleModel userRoleModel = createUserRoleModel(1L, roleName, true);
    Mockito.when(userRepository.findAll()).thenReturn(List.of(userEntity));
    createModelMocks(userRoleRelation, userRoleModel, AuthenticationType.DATABASE);
    DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
    List<UserModel> userModelList = defaultUserAccessor.getUsers();
    assertEquals(1, userModelList.size());
    UserModel userModel = userModelList.get(0);
    testUserModel(userEntity.getId(), username, emailAddress, roleName, userModel);
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) UserEntity(com.synopsys.integration.alert.database.user.UserEntity) UserRoleRelation(com.synopsys.integration.alert.database.user.UserRoleRelation) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) Test(org.junit.jupiter.api.Test)

Example 17 with UserEntity

use of com.synopsys.integration.alert.database.user.UserEntity in project hub-alert by blackducksoftware.

the class DefaultUserAccessor method deleteUser.

@Override
@Transactional(propagation = Propagation.REQUIRED)
public void deleteUser(Long userId) throws AlertForbiddenOperationException {
    Optional<UserEntity> optionalUser = userRepository.findById(userId);
    if (optionalUser.isPresent()) {
        UserEntity user = optionalUser.get();
        deleteUserEntity(user);
    }
}
Also used : UserEntity(com.synopsys.integration.alert.database.user.UserEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 18 with UserEntity

use of com.synopsys.integration.alert.database.user.UserEntity in project hub-alert by blackducksoftware.

the class DefaultUserAccessor method changeUserEmailAddress.

private void changeUserEmailAddress(UserEntity oldEntity, String emailAddress) {
    UserEntity updatedEntity = new UserEntity(oldEntity.getUserName(), oldEntity.getPassword(), emailAddress, oldEntity.getAuthenticationType());
    updatedEntity.setId(oldEntity.getId());
    userRepository.save(updatedEntity);
}
Also used : UserEntity(com.synopsys.integration.alert.database.user.UserEntity)

Example 19 with UserEntity

use of com.synopsys.integration.alert.database.user.UserEntity in project hub-alert by blackducksoftware.

the class DefaultUserAccessor method updateUser.

@Override
@Transactional(propagation = Propagation.REQUIRED)
public UserModel updateUser(UserModel user, boolean passwordEncoded) throws AlertConfigurationException, AlertForbiddenOperationException {
    Long userId = user.getId();
    UserEntity existingUser = userRepository.findById(userId).orElseThrow(() -> new AlertConfigurationException(String.format("No user found with id '%s'", userId)));
    Long existingUserId = existingUser.getId();
    UserEntity savedEntity = existingUser;
    // if it isn't an external user then update username, password, and email.
    Optional<AuthenticationType> authenticationType = authenticationTypeAccessor.getAuthenticationType(existingUser.getAuthenticationType());
    if (authenticationType.isEmpty()) {
        throw new AlertRuntimeException("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 AlertForbiddenOperationException("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);
}
Also used : AlertRuntimeException(com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException) UserEntity(com.synopsys.integration.alert.database.user.UserEntity) AlertForbiddenOperationException(com.synopsys.integration.alert.common.exception.AlertForbiddenOperationException) AlertConfigurationException(com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException) AuthenticationType(com.synopsys.integration.alert.common.enumeration.AuthenticationType) Transactional(org.springframework.transaction.annotation.Transactional)

Example 20 with UserEntity

use of com.synopsys.integration.alert.database.user.UserEntity in project hub-alert by blackducksoftware.

the class DefaultUserAccessor method changeUserPassword.

private void changeUserPassword(UserEntity oldEntity, String newPassword) {
    UserEntity updatedEntity = new UserEntity(oldEntity.getUserName(), defaultPasswordEncoder.encode(newPassword), oldEntity.getEmailAddress(), oldEntity.getAuthenticationType());
    updatedEntity.setId(oldEntity.getId());
    userRepository.save(updatedEntity);
}
Also used : UserEntity(com.synopsys.integration.alert.database.user.UserEntity)

Aggregations

UserEntity (com.synopsys.integration.alert.database.user.UserEntity)20 Test (org.junit.jupiter.api.Test)14 UserModel (com.synopsys.integration.alert.common.persistence.model.UserModel)8 UserRoleModel (com.synopsys.integration.alert.common.persistence.model.UserRoleModel)7 UserRoleRelation (com.synopsys.integration.alert.database.user.UserRoleRelation)7 AlertConfigurationException (com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)4 AuthenticationType (com.synopsys.integration.alert.common.enumeration.AuthenticationType)4 Transactional (org.springframework.transaction.annotation.Transactional)4 AlertForbiddenOperationException (com.synopsys.integration.alert.common.exception.AlertForbiddenOperationException)3 AlertRuntimeException (com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException)2 AuthenticationTypeDetails (com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails)2