Search in sources :

Example 11 with UserEntity

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

the class UserService method unlockUserAccount.

/**
 * Unlock user account.
 *
 * @param userId the user id
 */
public void unlockUserAccount(Long userId) {
    UserEntity userEntity = userValidator.validateUserId(userId);
    userEntity.setStatusEntity(Status.ACTIVE.getStatusEntity());
    userEntity.setFailedLoginCount(null);
    userEntity.setUnlockTime(null);
    userEntity.setLoginTime(new Timestamp(System.currentTimeMillis()));
    userRepository.save(userEntity);
}
Also used : Timestamp(java.sql.Timestamp) UserEntity(org.usermanagement.dao.entity.UserEntity)

Example 12 with UserEntity

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

the class UserActivityLogService method getActivityLog.

/**
 * Gets the activity log.
 *
 * @param users the users
 * @param activities the activities
 * @param start the start
 * @param end the end
 * @return the activity log
 */
public List<LogInfo> getActivityLog(final List<Long> users, final List<String> activities, final String start, final String end) {
    List<LogInfo> logs = userActivityService.getLogs(users, activities, start, end);
    List<LogInfo> appAdminlogs = new ArrayList<LogInfo>();
    if (!ValidatorUtil.isNull(logs)) {
        Set<Long> userIds = new HashSet<Long>();
        for (LogInfo log : logs) {
            if (serverContext.getRequestContext().getUserId() != 1 && log.getUserId() == 1) {
                appAdminlogs.add(log);
            }
            userIds.add(log.getUserId());
        }
        logs.removeAll(appAdminlogs);
        List<UserEntity> usersList = userRepository.findByUserIdIn(userIds);
        for (int i = 0; i < logs.size(); i++) {
            UserEntity userEntity = getUser(logs.get(i).getUserId(), usersList);
            if (userEntity != null) {
                logs.get(i).setUsername(userEntity.getUsername());
            } else {
                logs.get(i).setUsername(String.valueOf(logs.get(i).getUserId()));
            }
        }
    }
    return logs;
}
Also used : LogInfo(org.openkilda.log.model.LogInfo) ArrayList(java.util.ArrayList) UserEntity(org.usermanagement.dao.entity.UserEntity) HashSet(java.util.HashSet)

Example 13 with UserEntity

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

the class CustomAuthenticationProvider method authenticate.

/*
     * (non-Javadoc)
     * 
     * @see org.springframework.security.authentication.dao.
     * AbstractUserDetailsAuthenticationProvider#authenticate(org.
     * springframework.security.core.Authentication)
     */
@Override
public Authentication authenticate(final Authentication auth) throws org.springframework.security.core.AuthenticationException {
    CustomWebAuthenticationDetails customWebAuthenticationDetails = ((CustomWebAuthenticationDetails) auth.getDetails());
    String verificationCode = customWebAuthenticationDetails.getVerificationCode();
    UserEntity user = userRepository.findByUsernameIgnoreCase(auth.getName());
    if (user == null || !user.getActiveFlag()) {
        throw new BadCredentialsException("Login failed; Invalid email or password.");
    }
    String loginCount = null;
    String unlockTime = null;
    if (user.getUserId() != 1) {
        loginCount = applicationSettingService.getApplicationSetting(ApplicationSetting.INVALID_LOGIN_ATTEMPT);
        unlockTime = applicationSettingService.getApplicationSetting(ApplicationSetting.USER_ACCOUNT_UNLOCK_TIME);
        if (!user.getStatusEntity().getStatus().equalsIgnoreCase("ACTIVE")) {
            checkUserLoginAttempts(user, loginCount, unlockTime);
        }
    }
    try {
        final Authentication result = super.authenticate(auth);
        if (user.getIs2FaEnabled()) {
            if (!user.getIs2FaConfigured() && !customWebAuthenticationDetails.isConfigure2Fa()) {
                throw new TwoFaKeyNotSetException();
            } else {
                if (verificationCode == null || verificationCode.isEmpty()) {
                    throw new OtpRequiredException();
                } else if (!TwoFactorUtility.validateOtp(verificationCode, user.getTwoFaKey())) {
                    throw new InvalidOtpException("Invalid verfication code");
                }
            }
        }
        return new UsernamePasswordAuthenticationToken(user, result.getCredentials(), result.getAuthorities());
    } catch (BadCredentialsException e) {
        String error = null;
        if (user.getUserId() != 1) {
            error = updateInvalidLoginAttempts(user, loginCount, unlockTime);
        } else {
            error = "Login Failed.Invalid email or password.";
        }
        throw new BadCredentialsException(error);
    }
}
Also used : Authentication(org.springframework.security.core.Authentication) InvalidOtpException(org.openkilda.exception.InvalidOtpException) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) TwoFaKeyNotSetException(org.openkilda.exception.TwoFaKeyNotSetException) UserEntity(org.usermanagement.dao.entity.UserEntity) OtpRequiredException(org.openkilda.exception.OtpRequiredException)

Example 14 with UserEntity

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

the class UserConversionUtil method toUserEntity.

/**
 * To user entity.
 *
 * @param userInfo the user info
 * @param roleEntities the role entities
 * @return the user entity
 */
public static UserEntity toUserEntity(final UserInfo userInfo, final Set<RoleEntity> roleEntities) {
    UserEntity userEntity = new UserEntity();
    userEntity.setUsername(userInfo.getUsername().toLowerCase());
    userEntity.setPassword(StringUtil.encodeString(password));
    userEntity.setEmail(userInfo.getEmail().toLowerCase());
    userEntity.setName(userInfo.getName());
    userEntity.setRoles(roleEntities);
    userEntity.setActiveFlag(true);
    userEntity.setLoginTime(new Timestamp(System.currentTimeMillis()));
    userEntity.setLogoutTime(new Timestamp(System.currentTimeMillis()));
    userEntity.setIsAuthorized(true);
    userEntity.setIs2FaEnabled(true);
    userEntity.setIs2FaConfigured(false);
    StatusEntity statusEntity = Status.ACTIVE.getStatusEntity();
    userEntity.setStatusEntity(statusEntity);
    return userEntity;
}
Also used : Timestamp(java.sql.Timestamp) StatusEntity(org.usermanagement.dao.entity.StatusEntity) UserEntity(org.usermanagement.dao.entity.UserEntity)

Example 15 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