Search in sources :

Example 16 with RoleEntity

use of org.usermanagement.dao.entity.RoleEntity in project open-kilda by telstra.

the class RoleService method createRole.

/**
 * Creates the role.
 *
 * @param role the role
 * @return the role
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Role createRole(final Role role) {
    roleValidator.validateRole(role);
    Set<PermissionEntity> permissionEntities = new HashSet<>();
    List<PermissionEntity> permissionEntityList = permissionRepository.findAll();
    for (Long permissionId : role.getPermissionId()) {
        PermissionEntity permissionEntity = permissionEntityList.parallelStream().filter((entity) -> entity.getPermissionId().equals(permissionId)).findFirst().orElse(null);
        if (!ValidatorUtil.isNull(permissionEntity)) {
            permissionEntities.add(permissionEntity);
        } else {
            LOGGER.warn("Permission with id '" + permissionId + "' not found.");
            throw new RequestValidationException(messageUtil.getAttributeNotFound("permission"));
        }
    }
    RoleEntity roleEntity = RoleConversionUtil.toRoleEntity(role, permissionEntities);
    roleRepository.save(roleEntity);
    activityLogger.log(ActivityType.CREATE_ROLE, role.getName());
    LOGGER.info("Role with name '" + roleEntity.getName() + "' created successfully.");
    return RoleConversionUtil.toRole(roleEntity, true, false);
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) PermissionEntity(org.usermanagement.dao.entity.PermissionEntity) RequestValidationException(org.usermanagement.exception.RequestValidationException) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 17 with RoleEntity

use of org.usermanagement.dao.entity.RoleEntity in project open-kilda by telstra.

the class RoleService method getRolesByPermissionId.

/**
 * Gets the roles by permission id.
 *
 * @param permissionId the permission id
 * @return the roles by permission id
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Permission getRolesByPermissionId(final Long permissionId) {
    PermissionEntity permissionEntity = permissionRepository.findByPermissionId(permissionId);
    Set<RoleEntity> roleEntityList = roleRepository.findByPermissions_permissionId(permissionId);
    return RoleConversionUtil.toPermissionByRole(roleEntityList, permissionEntity);
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) PermissionEntity(org.usermanagement.dao.entity.PermissionEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 18 with RoleEntity

use of org.usermanagement.dao.entity.RoleEntity in project open-kilda by telstra.

the class UserService method updateUser.

/**
 * Update user.
 *
 * @param userInfo the user info
 * @param userId the user id
 * @return the user info
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public UserInfo updateUser(final UserInfo userInfo, final Long userId) {
    UserEntity userEntity = userValidator.validateUpdateUser(userInfo);
    StringBuilder activityMessage = new StringBuilder(userEntity.getUsername() + " updated with:\n");
    if (userInfo.getRoleIds() != null) {
        StringBuilder roles = new StringBuilder();
        userEntity.getRoles().clear();
        Set<RoleEntity> roleEntities = roleService.getRolesById(userInfo.getRoleIds());
        userEntity.getRoles().addAll(roleEntities);
        for (RoleEntity role : roleEntities) {
            roles = roles.length() > 0 ? roles.append("," + role.getName()) : roles.append(role.getName());
        }
        activityMessage.append("roles:" + roles.toString() + "\n");
    }
    UserConversionUtil.toUpateUserEntity(userInfo, userEntity, activityMessage);
    userEntity = userRepository.save(userEntity);
    activityLogger.log(ActivityType.UPDATE_USER, activityMessage.toString());
    LOGGER.info("User updated successfully (id: " + userId + ")");
    return UserConversionUtil.toUserInfo(userEntity);
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) UserEntity(org.usermanagement.dao.entity.UserEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 19 with RoleEntity

use of org.usermanagement.dao.entity.RoleEntity in project open-kilda by telstra.

the class UserService method populateUserInfo.

/**
 * Adds user information in session.
 *
 * @param userInfo the userInfo
 * @param username who's information is added in session.
 */
public void populateUserInfo(final UserInfo userInfo, final String username) {
    UserEntity user = getUserByUsername(username);
    Set<RoleEntity> roleEntities = user.getRoles();
    Set<String> roles = new HashSet<String>();
    Set<String> permissions = new HashSet<String>();
    for (RoleEntity roleEntity : roleEntities) {
        roles.add(roleEntity.getName());
        userInfo.setRole("ROLE_ADMIN");
        if (user.getUserId() != 1) {
            Set<PermissionEntity> permissionEntities = roleEntity.getPermissions();
            for (PermissionEntity permissionEntity : permissionEntities) {
                if (permissionEntity.getStatusEntity().getStatusCode().equalsIgnoreCase(Status.ACTIVE.getCode()) && !permissionEntity.getIsAdminPermission()) {
                    permissions.add(permissionEntity.getName());
                }
            }
        }
    }
    if (user.getUserId() == 1) {
        List<PermissionEntity> permissionEntities = permissionRepository.findAll();
        for (PermissionEntity permissionEntity : permissionEntities) {
            permissions.add(permissionEntity.getName());
        }
    }
    userInfo.setUserId(user.getUserId());
    userInfo.setUsername(user.getUsername());
    userInfo.setName(user.getName());
    userInfo.setRoles(roles);
    userInfo.setPermissions(permissions);
    userInfo.setIs2FaEnabled(user.getIs2FaEnabled());
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) PermissionEntity(org.usermanagement.dao.entity.PermissionEntity) UserEntity(org.usermanagement.dao.entity.UserEntity) HashSet(java.util.HashSet)

Example 20 with RoleEntity

use of org.usermanagement.dao.entity.RoleEntity in project open-kilda by telstra.

the class RoleConversionUtil method toRoleEntity.

/**
 * To role entity.
 *
 * @param role the role
 * @param permissionEntitySet the permission entity set
 * @return the role entity
 */
public static RoleEntity toRoleEntity(final Role role, final Set<PermissionEntity> permissionEntitySet) {
    RoleEntity roleEntity = new RoleEntity();
    roleEntity.setName(role.getName());
    roleEntity.setPermissions(permissionEntitySet);
    roleEntity.setDescription(role.getDescription());
    StatusEntity statusEntity = Status.ACTIVE.getStatusEntity();
    roleEntity.setStatusEntity(statusEntity);
    return roleEntity;
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) StatusEntity(org.usermanagement.dao.entity.StatusEntity)

Aggregations

RoleEntity (org.usermanagement.dao.entity.RoleEntity)21 Transactional (org.springframework.transaction.annotation.Transactional)12 RequestValidationException (org.usermanagement.exception.RequestValidationException)10 PermissionEntity (org.usermanagement.dao.entity.PermissionEntity)7 HashSet (java.util.HashSet)5 UserEntity (org.usermanagement.dao.entity.UserEntity)5 Role (org.usermanagement.model.Role)4 ArrayList (java.util.ArrayList)3 UserInfo (org.usermanagement.model.UserInfo)3 SamlConfigEntity (org.openkilda.saml.dao.entity.SamlConfigEntity)2 SamlConfig (org.openkilda.saml.model.SamlConfig)2 Permission (org.usermanagement.model.Permission)2 AccessDeniedException (java.nio.file.AccessDeniedException)1 HashMap (java.util.HashMap)1 InvalidOtpException (org.openkilda.exception.InvalidOtpException)1 OtpRequiredException (org.openkilda.exception.OtpRequiredException)1 TwoFaKeyNotSetException (org.openkilda.exception.TwoFaKeyNotSetException)1 NameID (org.opensaml.saml2.core.NameID)1 MetadataProviderException (org.opensaml.saml2.metadata.provider.MetadataProviderException)1 AnonymousAuthenticationToken (org.springframework.security.authentication.AnonymousAuthenticationToken)1