Search in sources :

Example 1 with AuthService

use of com.axelor.auth.AuthService in project axelor-open-suite by axelor.

the class UserServiceImpl method generateRandomPasswordForUsers.

@Override
@Transactional(rollbackOn = { AxelorException.class, Exception.class })
public void generateRandomPasswordForUsers(List<Long> userIds) {
    AuthService authService = Beans.get(AuthService.class);
    LocalDateTime todayDateTime = Beans.get(AppBaseService.class).getTodayDateTime().toLocalDateTime();
    for (Long userId : userIds) {
        User user = userRepo.find(userId);
        String password = this.generateRandomPassword().toString();
        user.setTransientPassword(password);
        password = authService.encrypt(password);
        user.setPassword(password);
        user.setPasswordUpdatedOn(todayDateTime);
        userRepo.save(user);
    }
    // Update login date in session so that user changing own password doesn't get logged out.
    if (userIds.contains(getUserId())) {
        Session session = AuthUtils.getSubject().getSession();
        session.setAttribute("loginDate", todayDateTime);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) User(com.axelor.auth.db.User) AuthService(com.axelor.auth.AuthService) Session(org.apache.shiro.session.Session) Transactional(com.google.inject.persist.Transactional)

Example 2 with AuthService

use of com.axelor.auth.AuthService in project axelor-open-suite by axelor.

the class UserServiceImpl method verifyCurrentUserPassword.

@Override
public boolean verifyCurrentUserPassword(String password) {
    if (!StringUtils.isBlank(password)) {
        final User current = AuthUtils.getUser();
        final AuthService authService = AuthService.getInstance();
        if (authService.match(password, current.getPassword())) {
            return true;
        }
    }
    return false;
}
Also used : User(com.axelor.auth.db.User) AuthService(com.axelor.auth.AuthService)

Example 3 with AuthService

use of com.axelor.auth.AuthService in project axelor-open-suite by axelor.

the class UserServiceImpl method changeUserPassword.

@Override
public User changeUserPassword(User user, Map<String, Object> values) throws ClassNotFoundException, InstantiationException, IllegalAccessException, MessagingException, IOException, AxelorException {
    Preconditions.checkNotNull(user, I18n.get("User cannot be null."));
    Preconditions.checkNotNull(values, I18n.get("User context cannot be null."));
    final String oldPassword = (String) values.get("oldPassword");
    final String newPassword = (String) values.get("newPassword");
    final String chkPassword = (String) values.get("chkPassword");
    // no password change
    if (StringUtils.isBlank(newPassword)) {
        return user;
    }
    if (StringUtils.isBlank(oldPassword)) {
        throw new ValidationException(I18n.get("Current user password is not provided."));
    }
    if (!newPassword.equals(chkPassword)) {
        throw new ValidationException(I18n.get("Confirm password doesn't match with new password."));
    }
    if (!matchPasswordPattern(newPassword)) {
        throw new ValidationException(I18n.get(PATTERN_DESCRIPTION));
    }
    final User current = AuthUtils.getUser();
    final AuthService authService = AuthService.getInstance();
    if (!authService.match(oldPassword, current.getPassword())) {
        throw new ValidationException(I18n.get("Current user password is wrong."));
    }
    user.setTransientPassword(newPassword);
    return user;
}
Also used : ValidationException(javax.validation.ValidationException) User(com.axelor.auth.db.User) AuthService(com.axelor.auth.AuthService)

Aggregations

AuthService (com.axelor.auth.AuthService)3 User (com.axelor.auth.db.User)3 Transactional (com.google.inject.persist.Transactional)1 LocalDateTime (java.time.LocalDateTime)1 ValidationException (javax.validation.ValidationException)1 Session (org.apache.shiro.session.Session)1