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);
}
}
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;
}
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);
}
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;
}
Aggregations