Search in sources :

Example 6 with RoleEntity

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

the class RoleService method getRoleByName.

/**
 * Gets the role by name.
 *
 * @param role the role
 * @return the role by name
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public List<Role> getRoleByName(final Set<String> role) {
    List<Role> roles = new ArrayList<Role>();
    List<RoleEntity> roleEntities = roleRepository.findByNameIn(role);
    if (ValidatorUtil.isNull(roleEntities)) {
        LOGGER.warn("Roles with name '" + role + "' not found. Error: " + messageUtil.getAttributeInvalid("role", role + ""));
        throw new RequestValidationException(messageUtil.getAttributeInvalid("role", role + ""));
    }
    for (RoleEntity roleEntity : roleEntities) {
        if (Status.ACTIVE.getStatusEntity().getStatus().equalsIgnoreCase(roleEntity.getStatusEntity().getStatus())) {
            roles.add(RoleConversionUtil.toRole(roleEntity, true, false));
        }
    }
    return roles;
}
Also used : Role(org.usermanagement.model.Role) RoleEntity(org.usermanagement.dao.entity.RoleEntity) ArrayList(java.util.ArrayList) RequestValidationException(org.usermanagement.exception.RequestValidationException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with RoleEntity

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

the class UserService method assignUserByRoleId.

/**
 * Assign user by role id.
 *
 * @param roleId the role id
 * @param role the role
 * @return the role
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Role assignUserByRoleId(final Long roleId, final Role role) {
    RoleEntity roleEntity = roleRepository.findByRoleId(roleId);
    if (ValidatorUtil.isNull(roleEntity)) {
        LOGGER.warn("Role with role id '" + roleId + "' not found. Error: " + messageUtil.getAttributeInvalid("role_id", roleId + ""));
        throw new RequestValidationException(messageUtil.getAttributeInvalid("role_id", roleId + ""));
    }
    roleEntity.getUsers().clear();
    if (role.getUserInfo() != null) {
        for (UserInfo user : role.getUserInfo()) {
            UserEntity userEntity = userRepository.findByUserId(user.getUserId());
            roleEntity.getUsers().add(userEntity);
        }
    }
    roleEntity = roleRepository.save(roleEntity);
    activityLogger.log(ActivityType.ASSIGN_USERS_BY_ROLE, roleEntity.getName());
    LOGGER.info("Users assigned with role successfully (role id: " + roleId + ")");
    return RoleConversionUtil.toRole(roleEntity, false, true);
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) UserInfo(org.usermanagement.model.UserInfo) RequestValidationException(org.usermanagement.exception.RequestValidationException) UserEntity(org.usermanagement.dao.entity.UserEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with RoleEntity

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

the class UserService method createUser.

/**
 * Creates the user.
 *
 * @param userRequest the user request
 * @return the user info
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public UserInfo createUser(final UserInfo userRequest) {
    userValidator.validateCreateUser(userRequest);
    Set<RoleEntity> roleEntities = roleService.getRolesById(userRequest.getRoleIds());
    UserEntity userEntity = UserConversionUtil.toUserEntity(userRequest, roleEntities);
    String password = ValidatorUtil.randomAlphaNumeric(16);
    userEntity.setPassword(StringUtil.encodeString(password));
    userEntity.setIs2FaEnabled(userRequest.getIs2FaEnabled());
    userEntity = userRepository.save(userEntity);
    LOGGER.info("User with username '" + userEntity.getUsername() + "' created successfully.");
    activityLogger.log(ActivityType.CREATE_USER, userRequest.getUsername());
    try {
        if (userEntity.getUserId() != null) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name", userEntity.getName());
            map.put("username", userEntity.getUsername());
            map.put("password", password);
            mailService.send(userEntity.getEmail(), mailUtils.getSubjectAccountUsername(), TemplateService.Template.ACCOUNT_USERNAME, map);
            mailService.send(userEntity.getEmail(), mailUtils.getSubjectAccountPassword(), TemplateService.Template.ACCOUNT_PASSWORD, map);
            LOGGER.info("Username and password email sent successfully to user(username: " + userEntity.getUsername() + ").");
        }
    } catch (Exception ex) {
        LOGGER.warn("User registration email failed for username:'" + userEntity.getUsername());
    }
    return UserConversionUtil.toUserInfo(userEntity);
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) HashMap(java.util.HashMap) UserEntity(org.usermanagement.dao.entity.UserEntity) OtpRequiredException(org.openkilda.exception.OtpRequiredException) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) InvalidOtpException(org.openkilda.exception.InvalidOtpException) TwoFaKeyNotSetException(org.openkilda.exception.TwoFaKeyNotSetException) AccessDeniedException(java.nio.file.AccessDeniedException) RequestValidationException(org.usermanagement.exception.RequestValidationException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with RoleEntity

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

the class PermissionService method getPermissionById.

/**
 * Gets the permission by id.
 *
 * @param permissionId the permission id
 * @return the permission by id
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Permission getPermissionById(final Long permissionId) {
    PermissionEntity permissionEntity = permissionRepository.findByPermissionId(permissionId);
    if (ValidatorUtil.isNull(permissionEntity)) {
        LOGGER.warn("Permission with permissionId '" + permissionId + "' not found. Error: " + messageUtil.getAttributeInvalid("permission_id", permissionId + ""));
        throw new RequestValidationException(messageUtil.getAttributeInvalid("permission_id", permissionId + ""));
    }
    Set<RoleEntity> roleEntityList = roleRepository.findByPermissions_permissionId(permissionId);
    return PermissionConversionUtil.toPermission(permissionEntity, roleEntityList);
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) PermissionEntity(org.usermanagement.dao.entity.PermissionEntity) RequestValidationException(org.usermanagement.exception.RequestValidationException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with RoleEntity

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

the class PermissionService method deletePermissionById.

/**
 * Delete permission by id.
 *
 * @param permissionId the permission id
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void deletePermissionById(final Long permissionId) {
    PermissionEntity permissionEntity = permissionRepository.findByPermissionId(permissionId);
    if (ValidatorUtil.isNull(permissionEntity)) {
        throw new RequestValidationException(messageUtil.getAttributeInvalid("permission_id", permissionId + ""));
    }
    Set<RoleEntity> roleEntityList = roleRepository.findByPermissions_permissionId(permissionId);
    if (roleEntityList.size() > 0) {
        String roles = "";
        for (RoleEntity roleEntity : roleEntityList) {
            roles += !"".equals(roles) ? "," + roleEntity.getName() : roleEntity.getName();
        }
        LOGGER.warn("Permission with permissionId '" + permissionId + "' not allowed to delete. Error: " + messageUtil.getAttributeDeletionNotAllowed(permissionEntity.getName(), roles));
        throw new RequestValidationException(messageUtil.getAttributeDeletionNotAllowed(permissionEntity.getName(), roles));
    }
    permissionRepository.delete(permissionEntity);
    LOGGER.info("Permission(permissionId: " + permissionId + ") deleted successfully.");
    activityLogger.log(ActivityType.DELETE_PERMISSION, permissionEntity.getName());
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) PermissionEntity(org.usermanagement.dao.entity.PermissionEntity) RequestValidationException(org.usermanagement.exception.RequestValidationException) Transactional(org.springframework.transaction.annotation.Transactional)

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