Search in sources :

Example 1 with NonAdminUserException

use of org.motechproject.security.exception.NonAdminUserException 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 NonAdminUserException

use of org.motechproject.security.exception.NonAdminUserException 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)

Aggregations

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