Search in sources :

Example 16 with UserEntity

use of org.usermanagement.dao.entity.UserEntity 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 17 with UserEntity

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

the class UserService method createSamlUser.

/**
 * Creates the saml user.
 * @param username the username
 * @param roles the user roles
 */
public void createSamlUser(String username, Set<RoleEntity> roles) {
    UserEntity userEntity = new UserEntity();
    userEntity.setUsername(username);
    userEntity.setEmail(username);
    userEntity.setName(username);
    userEntity.setRoles(roles);
    userEntity.setActiveFlag(true);
    userEntity.setLoginTime(new Timestamp(System.currentTimeMillis()));
    userEntity.setLogoutTime(new Timestamp(System.currentTimeMillis()));
    userEntity.setIsAuthorized(true);
    userEntity.setIs2FaEnabled(false);
    userEntity.setIs2FaConfigured(false);
    StatusEntity statusEntity = Status.ACTIVE.getStatusEntity();
    userEntity.setStatusEntity(statusEntity);
    String password = ValidatorUtil.randomAlphaNumeric(16);
    userEntity.setPassword(StringUtil.encodeString(password));
    userRepository.save(userEntity);
}
Also used : Timestamp(java.sql.Timestamp) StatusEntity(org.usermanagement.dao.entity.StatusEntity) UserEntity(org.usermanagement.dao.entity.UserEntity)

Example 18 with UserEntity

use of org.usermanagement.dao.entity.UserEntity 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 19 with UserEntity

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

the class UserService method updateLoginDetail.

/**
 * Update login detail.
 *
 * @param userName the user name
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void updateLoginDetail(final String userName) {
    UserEntity userEntity = userRepository.findByUsernameIgnoreCase(userName);
    if (ValidatorUtil.isNull(userEntity)) {
        LOGGER.warn("User with username '" + userName + "' not found. Error: " + messageUtil.getAttributeInvalid("username", userName + ""));
        throw new RequestValidationException(messageUtil.getAttributeInvalid("username", userName + ""));
    }
    userEntity.setLoginTime(Calendar.getInstance().getTime());
    userEntity.setFailedLoginCount(null);
    userEntity.setUnlockTime(null);
    if (userEntity.getIs2FaEnabled()) {
        userEntity.setIs2FaConfigured(true);
    }
    userRepository.save(userEntity);
    LOGGER.info("User last login updated successfully (username: " + userName + ")");
}
Also used : RequestValidationException(org.usermanagement.exception.RequestValidationException) UserEntity(org.usermanagement.dao.entity.UserEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 20 with UserEntity

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

the class RoleConversionUtil method toRole.

/**
 * To role.
 *
 * @param roleEntity the role entity
 * @param withPermissions the with permissions
 * @param withUsers the with users
 * @return the role
 */
public static Role toRole(final RoleEntity roleEntity, final boolean withPermissions, final boolean withUsers) {
    Role role = new Role();
    role.setName(roleEntity.getName());
    role.setRoleId(roleEntity.getRoleId());
    role.setStatus(roleEntity.getStatusEntity().getStatus());
    role.setDescription(roleEntity.getDescription());
    if (withPermissions) {
        List<Permission> permissionList = new ArrayList<Permission>();
        if (!ValidatorUtil.isNull(roleEntity.getPermissions())) {
            for (PermissionEntity permissionEntity : roleEntity.getPermissions()) {
                permissionList.add(PermissionConversionUtil.toPermission(permissionEntity, null));
            }
            role.setPermissions(permissionList);
        }
    }
    if (withUsers) {
        List<UserInfo> userInfoList = new ArrayList<>();
        for (UserEntity userEntity : roleEntity.getUsers()) {
            if (userEntity.getUserId() != 1) {
                UserInfo userInfo = new UserInfo();
                userInfo.setUserId(userEntity.getUserId());
                userInfo.setName(userEntity.getName());
                userInfoList.add(userInfo);
            }
        }
        role.setUserInfo(userInfoList);
    }
    return role;
}
Also used : Role(org.usermanagement.model.Role) Permission(org.usermanagement.model.Permission) ArrayList(java.util.ArrayList) UserInfo(org.usermanagement.model.UserInfo) PermissionEntity(org.usermanagement.dao.entity.PermissionEntity) UserEntity(org.usermanagement.dao.entity.UserEntity)

Aggregations

UserEntity (org.usermanagement.dao.entity.UserEntity)21 Transactional (org.springframework.transaction.annotation.Transactional)10 RequestValidationException (org.usermanagement.exception.RequestValidationException)8 InvalidOtpException (org.openkilda.exception.InvalidOtpException)6 OtpRequiredException (org.openkilda.exception.OtpRequiredException)6 TwoFaKeyNotSetException (org.openkilda.exception.TwoFaKeyNotSetException)6 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)5 RoleEntity (org.usermanagement.dao.entity.RoleEntity)5 AccessDeniedException (java.nio.file.AccessDeniedException)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 UserInfo (org.usermanagement.model.UserInfo)4 Timestamp (java.sql.Timestamp)3 Authentication (org.springframework.security.core.Authentication)3 ArrayList (java.util.ArrayList)2 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 PermissionEntity (org.usermanagement.dao.entity.PermissionEntity)2 StatusEntity (org.usermanagement.dao.entity.StatusEntity)2 Permission (org.usermanagement.model.Permission)2