Search in sources :

Example 6 with UserEntity

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

the class UserService method resetPassword.

/**
 * Reset password.
 *
 * @param userId the user id
 * @param adminFlag the admin flag
 * @return the user info
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public UserInfo resetPassword(final long userId, final boolean adminFlag) {
    UserInfo userinfo = new UserInfo();
    userinfo.setUserId(userId);
    UserEntity userEntity = userRepository.findByUserId(userId);
    if (ValidatorUtil.isNull(userEntity)) {
        LOGGER.warn("User Entity not found for user(id: " + userId + ")");
        throw new RequestValidationException(messageUtil.getAttributeInvalid("user_id", userId + ""));
    }
    String randomPassword = ValidatorUtil.randomAlphaNumeric(16);
    userEntity = UserConversionUtil.toResetPwdUserEntity(userEntity, randomPassword);
    if (adminFlag) {
        userEntity.setIs2FaConfigured(false);
        userEntity.setTwoFaKey(null);
    }
    userEntity = userRepository.save(userEntity);
    if (adminFlag) {
        activityLogger.log(ActivityType.ADMIN_RESET_PASSWORD, userEntity.getUsername());
    } else {
        activityLogger.log(ActivityType.RESET_PASSWORD, userEntity.getUsername());
    }
    LOGGER.info("Password reset successfully for user(userId: " + userId + ").");
    try {
        if (!adminFlag) {
            Map<String, Object> context = new HashMap<>();
            context.put("name", userEntity.getName());
            context.put("password", randomPassword);
            mailService.send(userEntity.getEmail(), mailUtils.getSubjectResetPassword(), TemplateService.Template.RESET_ACCOUNT_PASSWORD, context);
            LOGGER.info("Reset password mail sent successfully for user(userId: " + userId + ").");
        }
    } catch (Exception e) {
        LOGGER.warn("Reset password mail failed for username: " + userEntity.getUsername());
    }
    userinfo.setPassword(randomPassword);
    return userinfo;
}
Also used : HashMap(java.util.HashMap) UserInfo(org.usermanagement.model.UserInfo) RequestValidationException(org.usermanagement.exception.RequestValidationException) 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 7 with UserEntity

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

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

the class UserService method updateUser2FaKey.

/**
 * Update user 2 FA key.
 *
 * @param userName the user name
 * @param secretKey the secret key
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void updateUser2FaKey(final String userName, final String secretKey) {
    UserEntity userEntity = userRepository.findByUsernameIgnoreCase(userName);
    userEntity.setTwoFaKey(secretKey);
    userRepository.save(userEntity);
    LOGGER.info("User 2FA updated successfully (username: " + userName + ")");
}
Also used : UserEntity(org.usermanagement.dao.entity.UserEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with UserEntity

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

the class UserService method reset2fa.

/**
 * Reset 2 fa.
 *
 * @param userId the user id
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void reset2fa(final long userId) {
    UserEntity userEntity = userRepository.findByUserId(userId);
    if (ValidatorUtil.isNull(userEntity)) {
        LOGGER.warn("User Entity not found for user(user_id: " + userId + ")");
        throw new RequestValidationException(messageUtil.getAttributeInvalid("user_id", userId + ""));
    }
    userEntity.setIs2FaConfigured(false);
    userEntity.setTwoFaKey(null);
    userEntity = userRepository.save(userEntity);
    activityLogger.log(ActivityType.RESET_2FA, userEntity.getUsername());
    LOGGER.info("2FA reset successfully for user(user_id: " + userId + ").");
    try {
        if (!userEntity.getIs2FaConfigured()) {
            Map<String, Object> context = new HashMap<>();
            context.put("name", userEntity.getName());
            mailService.send(userEntity.getEmail(), mailUtils.getSubjectReset2fa(), TemplateService.Template.RESET_2FA, context);
            LOGGER.info("Reset 2FA mail sent successfully for user(user_id: " + userId + ").");
        }
    } catch (Exception e) {
        LOGGER.warn("Reset 2FA mail failed for user: " + userId);
    }
}
Also used : HashMap(java.util.HashMap) RequestValidationException(org.usermanagement.exception.RequestValidationException) 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 10 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)

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