Search in sources :

Example 1 with UserNotFoundException

use of org.motechproject.security.exception.UserNotFoundException in project motech by motech.

the class UserController method saveUser.

/**
 * Creates user
 *
 * @param user user to be created
 */
@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/users/create", method = RequestMethod.POST)
public void saveUser(@RequestBody UserDto user) {
    int passLength = Math.max(GENERATED_PASSWORD_MIN_LENGTH, settingService.getMinPasswordLength());
    String password = user.isGeneratePassword() ? RandomStringUtils.randomAlphanumeric(passLength) : user.getPassword();
    motechUserService.register(user.getUserName(), password, user.getEmail(), "", user.getRoles(), user.getLocale());
    try {
        if (user.isGeneratePassword() && StringUtils.isNotBlank(user.getEmail())) {
            motechUserService.sendLoginInformation(user.getUserName());
        }
    } catch (UserNotFoundException | NonAdminUserException | ServerUrlIsEmptyException ex) {
        throw new MailSendException("Email was not sent", ex);
    }
}
Also used : UserNotFoundException(org.motechproject.security.exception.UserNotFoundException) ServerUrlIsEmptyException(org.motechproject.security.exception.ServerUrlIsEmptyException) MailSendException(org.springframework.mail.MailSendException) NonAdminUserException(org.motechproject.security.exception.NonAdminUserException) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with UserNotFoundException

use of org.motechproject.security.exception.UserNotFoundException in project motech by motech.

the class PasswordRecoveryServiceImpl method oneTimeTokenOpenId.

@Override
@Transactional
public String oneTimeTokenOpenId(String email, DateTime expiration, boolean notify) throws UserNotFoundException, NonAdminUserException {
    MotechUser user = motechUsersDao.findUserByEmail(email);
    DateTime expirationDate = expiration;
    if (expirationDate == null) {
        expirationDate = DateTime.now().plusHours(DEFAULT_EXPIRATION_HOURS);
    } else if (expirationDate.isBefore(DateTime.now())) {
        throw new IllegalArgumentException("The expiration date shouldn't be a past date!");
    }
    if (user == null) {
        throw new UserNotFoundException("User with email not found: " + email);
    }
    List<String> roles = user.getRoles();
    boolean isAdminUser = false;
    for (String role : roles) {
        if (role.toLowerCase().contains("admin")) {
            isAdminUser = true;
        }
    }
    if (!isAdminUser) {
        throw new NonAdminUserException("You are not admin User: " + user.getUserName());
    }
    String token = RandomStringUtils.randomAlphanumeric(TOKEN_LENGTH);
    PasswordRecovery recovery = createRecovery(user.getUserName(), user.getEmail(), token, expirationDate, user.getLocale());
    if (notify) {
        emailSender.sendOneTimeToken(recovery);
    }
    LOGGER.info("Created a one time token for user " + user.getUserName());
    return token;
}
Also used : UserNotFoundException(org.motechproject.security.exception.UserNotFoundException) MotechUser(org.motechproject.security.domain.MotechUser) NonAdminUserException(org.motechproject.security.exception.NonAdminUserException) PasswordRecovery(org.motechproject.security.domain.PasswordRecovery) DateTime(org.joda.time.DateTime) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with UserNotFoundException

use of org.motechproject.security.exception.UserNotFoundException in project motech by motech.

the class ForgotControllerTest method testInvalidEmail.

@Test
public void testInvalidEmail() throws UserNotFoundException {
    doThrow(new UserNotFoundException()).when(recoveryService).passwordRecoveryRequest(EMAIL);
    when(cookieLocaleResolver.resolveLocale(request)).thenReturn(Locale.ENGLISH);
    when(motechSettings.getLoginMode()).thenReturn(LoginMode.REPOSITORY);
    assertEquals("security.forgot.noSuchUser", controller.forgotPost(EMAIL));
    verify(recoveryService).passwordRecoveryRequest(EMAIL);
}
Also used : UserNotFoundException(org.motechproject.security.exception.UserNotFoundException) Test(org.junit.Test)

Example 4 with UserNotFoundException

use of org.motechproject.security.exception.UserNotFoundException in project motech by motech.

the class PasswordRecoveryServiceImpl method passwordRecoveryRequest.

@Override
@Transactional
public String passwordRecoveryRequest(String email, DateTime expiration, boolean notify) throws UserNotFoundException {
    MotechUser user = motechUsersDao.findUserByEmail(email);
    DateTime expirationDate = expiration;
    if (expirationDate == null) {
        expirationDate = DateTime.now().plusHours(DEFAULT_EXPIRATION_HOURS);
    } else if (expirationDate.isBefore(DateTime.now())) {
        throw new IllegalArgumentException("The expiration date shouldn't be a past date!");
    }
    if (user == null) {
        throw new UserNotFoundException("User with email not found: " + email);
    }
    String token = RandomStringUtils.randomAlphanumeric(TOKEN_LENGTH);
    PasswordRecovery recovery = createRecovery(user.getUserName(), user.getEmail(), token, expirationDate, user.getLocale());
    if (notify) {
        emailSender.sendRecoveryEmail(recovery);
    }
    LOGGER.info("Created a password recovery for user " + user.getUserName());
    return token;
}
Also used : UserNotFoundException(org.motechproject.security.exception.UserNotFoundException) MotechUser(org.motechproject.security.domain.MotechUser) PasswordRecovery(org.motechproject.security.domain.PasswordRecovery) DateTime(org.joda.time.DateTime) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

UserNotFoundException (org.motechproject.security.exception.UserNotFoundException)4 DateTime (org.joda.time.DateTime)2 MotechUser (org.motechproject.security.domain.MotechUser)2 PasswordRecovery (org.motechproject.security.domain.PasswordRecovery)2 NonAdminUserException (org.motechproject.security.exception.NonAdminUserException)2 Transactional (org.springframework.transaction.annotation.Transactional)2 Test (org.junit.Test)1 ServerUrlIsEmptyException (org.motechproject.security.exception.ServerUrlIsEmptyException)1 MailSendException (org.springframework.mail.MailSendException)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)1