Search in sources :

Example 1 with MolgenisUserException

use of org.molgenis.security.user.MolgenisUserException in project molgenis by molgenis.

the class AccountControllerTest method activateUserMolgenisUserException.

@Test
public void activateUserMolgenisUserException() throws Exception {
    doThrow(new MolgenisUserException("message")).when(accountService).activateUser("123");
    this.mockMvc.perform(get("/account/activate/123")).andExpect(view().name("forward:/")).andExpect(model().attribute("warningMessage", "message"));
    verify(accountService).activateUser("123");
}
Also used : MolgenisUserException(org.molgenis.security.user.MolgenisUserException) Test(org.testng.annotations.Test)

Example 2 with MolgenisUserException

use of org.molgenis.security.user.MolgenisUserException in project molgenis by molgenis.

the class AccountServiceImpl method changePassword.

@Override
@RunAsSystem
public void changePassword(String username, String newPassword) {
    User user = dataService.query(USER, User.class).eq(USERNAME, username).findOne();
    if (user == null) {
        throw new MolgenisUserException(format("Unknown user [%s]", username));
    }
    user.setPassword(newPassword);
    user.setChangePassword(false);
    dataService.update(USER, user);
    LOG.info("Changed password of user [{}]", username);
}
Also used : User(org.molgenis.data.security.auth.User) MolgenisUserException(org.molgenis.security.user.MolgenisUserException) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem)

Example 3 with MolgenisUserException

use of org.molgenis.security.user.MolgenisUserException in project molgenis by molgenis.

the class AccountServiceImpl method activateUser.

@Override
@RunAsSystem
public void activateUser(String activationCode) {
    User user = dataService.query(USER, User.class).eq(ACTIVE, false).and().eq(ACTIVATIONCODE, activationCode).findOne();
    if (user != null) {
        user.setActive(true);
        dataService.update(USER, user);
        // send activated email to user
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo(user.getEmail());
        mailMessage.setSubject("Your registration request for " + appSettings.getTitle());
        mailMessage.setText(createActivatedEmailText(user, appSettings.getTitle()));
        mailSender.send(mailMessage);
    } else {
        throw new MolgenisUserException("Invalid activation code or account already activated.");
    }
}
Also used : User(org.molgenis.data.security.auth.User) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MolgenisUserException(org.molgenis.security.user.MolgenisUserException) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem)

Example 4 with MolgenisUserException

use of org.molgenis.security.user.MolgenisUserException in project molgenis by molgenis.

the class AccountServiceImpl method resetPassword.

@Override
@RunAsSystem
public void resetPassword(String userEmail) {
    User user = dataService.query(USER, User.class).eq(EMAIL, userEmail).findOne();
    if (user != null) {
        String newPassword = idGenerator.generateId(SHORT_SECURE_RANDOM);
        user.setPassword(newPassword);
        user.setChangePassword(true);
        dataService.update(USER, user);
        // send password reseted email to user
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo(user.getEmail());
        mailMessage.setSubject("Your new password request");
        mailMessage.setText(createPasswordResettedEmailText(newPassword));
        mailSender.send(mailMessage);
    } else {
        throw new MolgenisUserException("Invalid email address.");
    }
}
Also used : User(org.molgenis.data.security.auth.User) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MolgenisUserException(org.molgenis.security.user.MolgenisUserException) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem)

Example 5 with MolgenisUserException

use of org.molgenis.security.user.MolgenisUserException in project molgenis by molgenis.

the class UserAccountController method validatePasswordInUpdateRequest.

private String validatePasswordInUpdateRequest(@Valid @NotNull AccountUpdateRequest updateRequest) {
    // validate new password
    String newPassword = updateRequest.getNewpwd();
    if (!StringUtils.isEmpty(newPassword)) {
        String oldPassword = updateRequest.getOldpwd();
        String newPasswordConfirm = updateRequest.getNewpwd2();
        // validate password for current user
        if (oldPassword == null || oldPassword.isEmpty()) {
            throw new MolgenisUserException("Please enter old password to update your password.");
        }
        boolean valid = userAccountService.validateCurrentUserPassword(oldPassword);
        if (!valid)
            throw new MolgenisUserException("The password you entered is incorrect.");
        // validate new password against new password confirmation
        if (!newPassword.equals(newPasswordConfirm)) {
            throw new MolgenisUserException("'New password' does not match 'Repeat new password'.");
        }
        // TODO implement http://www.molgenis.org/ticket/2145
        if (newPassword.length() < MIN_PASSWORD_LENGTH) {
            throw new MolgenisUserException("New password must consist of at least 6 characters.");
        }
    }
    return newPassword;
}
Also used : MolgenisUserException(org.molgenis.security.user.MolgenisUserException)

Aggregations

MolgenisUserException (org.molgenis.security.user.MolgenisUserException)6 RunAsSystem (org.molgenis.security.core.runas.RunAsSystem)4 User (org.molgenis.data.security.auth.User)3 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)3 URI (java.net.URI)1 MolgenisDataException (org.molgenis.data.MolgenisDataException)1 Group (org.molgenis.data.security.auth.Group)1 GroupMember (org.molgenis.data.security.auth.GroupMember)1 MailException (org.springframework.mail.MailException)1 Transactional (org.springframework.transaction.annotation.Transactional)1 Test (org.testng.annotations.Test)1