Search in sources :

Example 11 with UserRoleModel

use of com.synopsys.integration.alert.common.persistence.model.UserRoleModel in project hub-alert by blackducksoftware.

the class RoleActionsTest method getAllTest.

@Test
public void getAllTest() {
    PermissionModel permissionModel = createPermissionModel();
    UserRoleModel userRoleModel = new UserRoleModel(1L, roleName, false, PermissionModelUtil.convertToPermissionMatrixModel(Set.of(permissionModel)));
    Mockito.when(roleAccessor.getRoles()).thenReturn(Set.of(userRoleModel));
    RoleActions roleActions = new RoleActions(userManagementDescriptorKey, roleAccessor, authorizationManager, descriptorMap);
    ActionResponse<MultiRolePermissionModel> rolePermissionModelActionResponse = roleActions.getAll();
    assertTrue(rolePermissionModelActionResponse.isSuccessful());
    assertTrue(rolePermissionModelActionResponse.hasContent());
    assertEquals(HttpStatus.OK, rolePermissionModelActionResponse.getHttpStatus());
}
Also used : UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) Test(org.junit.jupiter.api.Test)

Example 12 with UserRoleModel

use of com.synopsys.integration.alert.common.persistence.model.UserRoleModel 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 13 with UserRoleModel

use of com.synopsys.integration.alert.common.persistence.model.UserRoleModel in project hub-alert by blackducksoftware.

the class DefaultUserAccessorTest method updateUserNonDatabaseAuthTest.

@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);
}
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 14 with UserRoleModel

use of com.synopsys.integration.alert.common.persistence.model.UserRoleModel in project hub-alert by blackducksoftware.

the class DefaultUserAccessorTest method addUserTest.

@Test
public void addUserTest() throws Exception {
    final String roleName = "userName";
    UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L);
    userEntity.setId(1L);
    AuthenticationTypeDetails authenticationTypeDetails = new AuthenticationTypeDetails(2L, "authentication-name");
    UserRoleRelation userRoleRelation = new UserRoleRelation(1L, 2L);
    UserRoleModel userRoleModel = createUserRoleModel(1L, roleName, true);
    Mockito.when(userRepository.findByUserName(Mockito.any())).thenReturn(Optional.empty());
    Mockito.when(authenticationTypeAccessor.getAuthenticationTypeDetails(Mockito.any())).thenReturn(Optional.of(authenticationTypeDetails));
    Mockito.when(userRepository.save(Mockito.any())).thenReturn(userEntity);
    createModelMocks(userRoleRelation, userRoleModel, AuthenticationType.DATABASE);
    DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
    UserModel userModel = defaultUserAccessor.addUser(username, password, emailAddress);
    testUserModel(userEntity.getId(), username, emailAddress, roleName, userModel);
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) AuthenticationTypeDetails(com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails) 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 15 with UserRoleModel

use of com.synopsys.integration.alert.common.persistence.model.UserRoleModel in project hub-alert by blackducksoftware.

the class UserActions method updateWithoutChecks.

@Override
protected ActionResponse<UserConfig> updateWithoutChecks(Long id, UserConfig resource) {
    Optional<UserModel> userModel = userAccessor.getUser(id);
    if (userModel.isPresent()) {
        UserModel existingUser = userModel.get();
        boolean passwordMissing = StringUtils.isBlank(resource.getPassword());
        String userName = resource.getUsername();
        String password = passwordMissing ? existingUser.getPassword() : resource.getPassword();
        String emailAddress = resource.getEmailAddress();
        UserModel newUserModel = UserModel.existingUser(existingUser.getId(), userName, password, emailAddress, existingUser.getAuthenticationType(), existingUser.getRoles(), existingUser.isEnabled());
        try {
            logger.debug(actionMessageCreator.updateStartMessage("user", userName));
            userAccessor.updateUser(newUserModel, passwordMissing);
            Set<String> configuredRoleNames = resource.getRoleNames();
            if (null != configuredRoleNames && !configuredRoleNames.isEmpty()) {
                Collection<UserRoleModel> roleNames = roleAccessor.getRoles().stream().filter(role -> configuredRoleNames.contains(role.getName())).collect(Collectors.toList());
                authorizationManager.updateUserRoles(existingUser.getId(), roleNames);
            }
            userSystemValidator.validateDefaultAdminUser(id);
            UserConfig user = userAccessor.getUser(id).map(this::convertDatabaseModelToRestModel).orElse(resource);
            logger.debug(actionMessageCreator.updateSuccessMessage("User", userName));
            return new ActionResponse<>(HttpStatus.NO_CONTENT);
        } catch (AlertException ex) {
            logger.error(actionMessageCreator.updateErrorMessage("User", userName));
            return new ActionResponse<>(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
        }
    }
    logger.warn(actionMessageCreator.updateNotFoundMessage("User", id));
    return new ActionResponse<>(HttpStatus.NOT_FOUND);
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) UserManagementDescriptorKey(com.synopsys.integration.alert.component.users.UserManagementDescriptorKey) AbstractResourceActions(com.synopsys.integration.alert.common.action.api.AbstractResourceActions) StringUtils(org.apache.commons.lang.StringUtils) ValidationActionResponse(com.synopsys.integration.alert.common.action.ValidationActionResponse) AlertFieldStatus(com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus) UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) Autowired(org.springframework.beans.factory.annotation.Autowired) DefaultUserRole(com.synopsys.integration.alert.common.enumeration.DefaultUserRole) UserSystemValidator(com.synopsys.integration.alert.component.users.UserSystemValidator) ArrayList(java.util.ArrayList) ActionResponse(com.synopsys.integration.alert.common.action.ActionResponse) AlertLoggerFactory(com.synopsys.integration.alert.common.logging.AlertLoggerFactory) ValidationResponseModel(com.synopsys.integration.alert.common.rest.model.ValidationResponseModel) ActionMessageCreator(com.synopsys.integration.alert.common.action.api.ActionMessageCreator) AuthenticationTypeDetails(com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails) UserAccessor(com.synopsys.integration.alert.common.persistence.accessor.UserAccessor) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) Logger(org.slf4j.Logger) Collection(java.util.Collection) Set(java.util.Set) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) Collectors(java.util.stream.Collectors) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) HttpStatus(org.springframework.http.HttpStatus) RoleAccessor(com.synopsys.integration.alert.common.descriptor.accessor.RoleAccessor) List(java.util.List) Component(org.springframework.stereotype.Component) AuthenticationTypeAccessor(com.synopsys.integration.alert.common.persistence.accessor.AuthenticationTypeAccessor) NumberUtils(org.apache.commons.lang3.math.NumberUtils) Optional(java.util.Optional) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) Transactional(org.springframework.transaction.annotation.Transactional) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) ValidationActionResponse(com.synopsys.integration.alert.common.action.ValidationActionResponse) ActionResponse(com.synopsys.integration.alert.common.action.ActionResponse) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Aggregations

UserRoleModel (com.synopsys.integration.alert.common.persistence.model.UserRoleModel)36 Test (org.junit.jupiter.api.Test)24 UserModel (com.synopsys.integration.alert.common.persistence.model.UserModel)17 UserRoleRelation (com.synopsys.integration.alert.database.user.UserRoleRelation)8 UserEntity (com.synopsys.integration.alert.database.user.UserEntity)7 RoleEntity (com.synopsys.integration.alert.database.user.RoleEntity)6 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)4 ActionResponse (com.synopsys.integration.alert.common.action.ActionResponse)4 ValidationActionResponse (com.synopsys.integration.alert.common.action.ValidationActionResponse)4 AuthenticationType (com.synopsys.integration.alert.common.enumeration.AuthenticationType)4 PermissionMatrixModel (com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel)4 LinkedHashSet (java.util.LinkedHashSet)4 Transactional (org.springframework.transaction.annotation.Transactional)4 AlertConfigurationException (com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)3 RoleAccessor (com.synopsys.integration.alert.common.descriptor.accessor.RoleAccessor)3 DefaultUserRole (com.synopsys.integration.alert.common.enumeration.DefaultUserRole)3 AlertForbiddenOperationException (com.synopsys.integration.alert.common.exception.AlertForbiddenOperationException)3 UserAccessor (com.synopsys.integration.alert.common.persistence.accessor.UserAccessor)3 AuthenticationTypeDetails (com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails)3 Collection (java.util.Collection)3