Search in sources :

Example 16 with UserRoleModel

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

the class UserActions method createWithoutChecks.

@Override
protected ActionResponse<UserConfig> createWithoutChecks(UserConfig resource) {
    try {
        String userName = resource.getUsername();
        String password = resource.getPassword();
        String emailAddress = resource.getEmailAddress();
        logger.debug(actionMessageCreator.createStartMessage("user", userName));
        UserModel userModel = userAccessor.addUser(userName, password, emailAddress);
        Long userId = userModel.getId();
        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(userId, roleNames);
        }
        userModel = userAccessor.getUser(userId).orElse(userModel);
        logger.debug(actionMessageCreator.createSuccessMessage("User", userName));
        return new ActionResponse<>(HttpStatus.CREATED, convertDatabaseModelToRestModel(userModel));
    } catch (AlertException ex) {
        logger.error(actionMessageCreator.createErrorMessage("user", resource.getUsername()));
        return new ActionResponse<>(HttpStatus.INTERNAL_SERVER_ERROR, String.format("There was an issue creating the user. %s", ex.getMessage()));
    }
}
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)

Example 17 with UserRoleModel

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

the class RoleActions method createWithoutChecks.

@Override
protected ActionResponse<RolePermissionModel> createWithoutChecks(RolePermissionModel resource) {
    String roleName = resource.getRoleName();
    Set<PermissionModel> permissions = resource.getPermissions();
    PermissionMatrixModel permissionMatrixModel = PermissionModelUtil.convertToPermissionMatrixModel(permissions);
    logger.debug(actionMessageCreator.createStartMessage("role", roleName));
    UserRoleModel userRoleModel = authorizationManager.createRoleWithPermissions(roleName, permissionMatrixModel);
    logger.debug(actionMessageCreator.createSuccessMessage("Role", roleName));
    return new ActionResponse<>(HttpStatus.OK, convertDatabaseModelToRestModel(userRoleModel));
}
Also used : PermissionMatrixModel(com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel) 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)

Example 18 with UserRoleModel

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

the class DefaultRoleAccessor method getRoles.

@Override
@Transactional(readOnly = true)
public Set<UserRoleModel> getRoles() {
    List<RoleEntity> roleList = roleRepository.findAll();
    Set<UserRoleModel> userRoles = new LinkedHashSet<>();
    for (RoleEntity entity : roleList) {
        userRoles.add(new UserRoleModel(entity.getId(), entity.getRoleName(), entity.getCustom(), readPermissionsForRole(entity.getId())));
    }
    return userRoles;
}
Also used : RoleEntity(com.synopsys.integration.alert.database.user.RoleEntity) LinkedHashSet(java.util.LinkedHashSet) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) Transactional(org.springframework.transaction.annotation.Transactional)

Example 19 with UserRoleModel

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

the class DefaultRoleAccessorTest method updateUserRolesTest.

@Test
public void updateUserRolesTest() {
    final Long userId = 1L;
    final String roleName = "roleName";
    final Long roleId = 1L;
    RoleEntity roleEntity = new RoleEntity(roleName, true);
    roleEntity.setId(1L);
    UserRoleModel userRoleModel = createUserRoleModel(roleId, roleName, true);
    Collection<UserRoleModel> userRoleModelCollection = List.of(userRoleModel);
    Mockito.when(roleRepository.findRoleEntitiesByRoleNames(Mockito.any())).thenReturn(List.of(roleEntity));
    DefaultRoleAccessor authorizationUtility = new DefaultRoleAccessor(roleRepository, userRoleRepository, permissionMatrixRepository, registeredDescriptorRepository, configContextRepository);
    authorizationUtility.updateUserRoles(userId, userRoleModelCollection);
    Mockito.verify(userRoleRepository).bulkDeleteAllByUserId(Mockito.any());
    Mockito.verify(userRoleRepository).saveAll(Mockito.any());
}
Also used : RoleEntity(com.synopsys.integration.alert.database.user.RoleEntity) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) Test(org.junit.jupiter.api.Test)

Example 20 with UserRoleModel

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

the class DefaultUserAccessorTest method getUserByUserIdTest.

@Test
public void getUserByUserIdTest() {
    final Long userId = 1L;
    final Long emptyUserId = 5L;
    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.findById(userId)).thenReturn(Optional.of(userEntity));
    Mockito.when(userRepository.findById(emptyUserId)).thenReturn(Optional.empty());
    createModelMocks(userRoleRelation, userRoleModel, AuthenticationType.DATABASE);
    DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
    Optional<UserModel> userModelOptional = defaultUserAccessor.getUser(userId);
    Optional<UserModel> userModelOptionalEmpty = defaultUserAccessor.getUser(emptyUserId);
    assertTrue(userModelOptional.isPresent());
    assertFalse(userModelOptionalEmpty.isPresent());
    UserModel userModel = userModelOptional.get();
    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)

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