Search in sources :

Example 1 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(String userName) throws AlertForbiddenOperationException {
    Optional<UserEntity> optionalUser = userRepository.findByUserName(userName);
    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 2 with UserEntity

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

the class DefaultUserAccessor method addUser.

@Override
@Transactional(propagation = Propagation.REQUIRED)
public UserModel addUser(UserModel user, boolean passwordEncoded) throws AlertConfigurationException {
    String username = user.getName();
    Optional<UserEntity> userWithSameUsername = userRepository.findByUserName(username);
    if (userWithSameUsername.isPresent()) {
        throw new AlertConfigurationException(String.format("A user with username '%s' is already present", username));
    }
    String password = passwordEncoded ? user.getPassword() : defaultPasswordEncoder.encode(user.getPassword());
    AuthenticationTypeDetails authenticationType = authenticationTypeAccessor.getAuthenticationTypeDetails(user.getAuthenticationType()).orElseThrow(() -> new AlertRuntimeException("Cannot find Authentication Type."));
    UserEntity newEntity = new UserEntity(username, password, user.getEmailAddress(), authenticationType.getId());
    UserEntity savedEntity = userRepository.save(newEntity);
    UserModel model = createModel(savedEntity);
    roleAccessor.updateUserRoles(model.getId(), user.getRoles());
    return model;
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) AlertRuntimeException(com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException) AuthenticationTypeDetails(com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails) UserEntity(com.synopsys.integration.alert.database.user.UserEntity) AlertConfigurationException(com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with UserEntity

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

the class DefaultUserAccessorTest method updateUserTest.

@Test
public void updateUserTest() throws Exception {
    final String roleName = "userName";
    AuthenticationType authenticationType = AuthenticationType.DATABASE;
    UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L);
    userEntity.setId(1L);
    UserRoleModel roles = createUserRoleModel(1L, roleName, true);
    UserModel userModel = UserModel.existingUser(1L, username, password, 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 newUserModel = defaultUserAccessor.updateUser(userModel, false);
    Mockito.verify(roleAccessor).updateUserRoles(Mockito.eq(userEntity.getId()), Mockito.any());
    testUserModel(userEntity.getId(), username, emailAddress, roleName, newUserModel);
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) UserEntity(com.synopsys.integration.alert.database.user.UserEntity) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) UserRoleRelation(com.synopsys.integration.alert.database.user.UserRoleRelation) AuthenticationType(com.synopsys.integration.alert.common.enumeration.AuthenticationType) Test(org.junit.jupiter.api.Test)

Example 4 with UserEntity

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

the class DefaultUserAccessorTest method deleteUserByIdTest.

@Test
public void deleteUserByIdTest() throws Exception {
    UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L);
    userEntity.setId(5L);
    Mockito.when(userRepository.findById(Mockito.eq(userEntity.getId()))).thenReturn(Optional.of(userEntity));
    DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
    defaultUserAccessor.deleteUser(userEntity.getId());
    Mockito.verify(roleAccessor).updateUserRoles(Mockito.any(), Mockito.any());
    Mockito.verify(userRepository).deleteById(Mockito.any());
}
Also used : UserEntity(com.synopsys.integration.alert.database.user.UserEntity) Test(org.junit.jupiter.api.Test)

Example 5 with UserEntity

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

the class DefaultUserAccessorTest method changeUserEmailAddressTest.

@Test
public void changeUserEmailAddressTest() {
    final String badUsername = "badUsername";
    final String newEmailAddress = "newemail.noreplay@blackducksoftware.com";
    UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L);
    userEntity.setId(1L);
    UserEntity newUserEntity = new UserEntity(username, password, newEmailAddress, 2L);
    userEntity.setId(1L);
    Mockito.when(userRepository.findByUserName(Mockito.eq(username))).thenReturn(Optional.of(userEntity));
    Mockito.when(userRepository.findByUserName(Mockito.eq(badUsername))).thenReturn(Optional.empty());
    Mockito.when(userRepository.save(Mockito.any())).thenReturn(newUserEntity);
    DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
    assertTrue(defaultUserAccessor.changeUserEmailAddress(username, newEmailAddress));
    assertFalse(defaultUserAccessor.changeUserEmailAddress(badUsername, newEmailAddress));
}
Also used : UserEntity(com.synopsys.integration.alert.database.user.UserEntity) Test(org.junit.jupiter.api.Test)

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