use of ca.corefacility.bioinformatics.irida.model.user.User in project irida by phac-nml.
the class PasswordResetController method resetSuccess.
/**
* Success page for a password reset
*
* @param encodedEmail
* A base64 encoded email address
* @param model
* Model for the view
*
* @return The password reset success view name
*/
@RequestMapping("/success/{encodedEmail}")
public String resetSuccess(@PathVariable String encodedEmail, Model model) {
byte[] decode = Base64.getDecoder().decode(encodedEmail);
String email = new String(decode);
logger.debug("Password reset submitted for " + email);
// Authentication should not need to be set at this point, as the user
// will be logged in
User user = userService.loadUserByEmail(email);
model.addAttribute("user", user);
return PASSWORD_RESET_SUCCESS;
}
use of ca.corefacility.bioinformatics.irida.model.user.User in project irida by phac-nml.
the class PasswordResetController method submitEmail.
/**
* Create a password reset for the given email address
*
* @param email
* The email address to create a password reset for
* @param model
* Model for the view
*
* @return Reset created page if the email exists in the system
*/
@RequestMapping(method = RequestMethod.POST)
public String submitEmail(@RequestParam String email, Model model) {
setAuthentication();
String page;
model.addAttribute("email", email);
try {
User user = userService.loadUserByEmail(email);
try {
createNewPasswordReset(user);
page = CREATED_REDIRECT + Base64.getEncoder().encodeToString(email.getBytes());
} catch (final MailSendException e) {
model.addAttribute("mailSendError", true);
SecurityContextHolder.clearContext();
page = noLoginResetPassword(model);
}
} catch (EntityNotFoundException ex) {
model.addAttribute("emailError", true);
SecurityContextHolder.clearContext();
page = noLoginResetPassword(model);
}
return page;
}
use of ca.corefacility.bioinformatics.irida.model.user.User in project irida by phac-nml.
the class PasswordResetController method adminNewPasswordReset.
/**
* Create a new {@link PasswordReset} for the given {@link User}
*
* @param userId
* The ID of the {@link User}
* @param principal
* a reference to the logged in user.
* @param locale
* a reference to the locale specified by the browser.
* @return a model indicating success or failure of the reset request.
*/
@RequestMapping("/ajax/create/{userId}")
@ResponseBody
@PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_MANAGER')")
public Map<String, Object> adminNewPasswordReset(@PathVariable Long userId, Principal principal, Locale locale) {
User user = userService.read(userId);
User principalUser = userService.getUserByUsername(principal.getName());
Map<String, Object> response;
if (canCreatePasswordReset(principalUser, user)) {
try {
createNewPasswordReset(user);
response = ImmutableMap.of("success", true, "message", messageSource.getMessage("password.reset.success-message", new Object[] { user.getFirstName() }, locale), "title", messageSource.getMessage("password.reset.success-title", null, locale));
} catch (final MailSendException e) {
logger.error("Failed to send password reset e-mail.");
response = ImmutableMap.of("success", false, "message", messageSource.getMessage("password.reset.error-message", null, locale), "title", messageSource.getMessage("password.reset.error-title", null, locale));
}
} else {
response = ImmutableMap.of("success", false, "message", messageSource.getMessage("password.reset.error-message", null, locale), "title", messageSource.getMessage("password.reset.error-title", null, locale));
}
return response;
}
use of ca.corefacility.bioinformatics.irida.model.user.User in project irida by phac-nml.
the class UsersController method getEditUserPage.
/**
* Get the user edit page
*
* @param userId
* The ID of the user to get
* @param model
* The model for the returned view
*
* @return The user edit view
*/
@RequestMapping(value = "/{userId}/edit", method = RequestMethod.GET)
@PreAuthorize("hasPermission(#userId, 'canUpdateUser')")
public String getEditUserPage(@PathVariable Long userId, Model model) {
logger.trace("Getting edit project page for [User " + userId + "]");
User user = userService.read(userId);
model.addAttribute("user", user);
Locale locale = LocaleContextHolder.getLocale();
Map<String, String> roleNames = new HashMap<>();
for (Role role : adminAllowedRoles) {
if (!role.equals(user.getSystemRole())) {
String roleMessageName = ROLE_MESSAGE_PREFIX + role.getName();
String roleName = messageSource.getMessage(roleMessageName, null, locale);
roleNames.put(role.getName(), roleName);
}
}
model.addAttribute("allowedRoles", roleNames);
String currentRoleName = messageSource.getMessage(ROLE_MESSAGE_PREFIX + user.getSystemRole().getName(), null, locale);
model.addAttribute("currentRole", currentRoleName);
if (!model.containsAttribute("errors")) {
model.addAttribute("errors", new HashMap<String, String>());
}
return EDIT_USER_PAGE;
}
use of ca.corefacility.bioinformatics.irida.model.user.User in project irida by phac-nml.
the class UsersController method submitCreateUser.
/**
* Create a new user object
*
* @param user
* User to create as a motel attribute
* @param systemRole
* The system role to give to the user
* @param confirmPassword
* Password confirmation
* @param requireActivation
* Checkbox whether the user account needs to be activated
* @param model
* Model for the view
* @param principal
* The user creating the object
*
* @return A redirect to the user details view
*/
@RequestMapping(value = "/create", method = RequestMethod.POST)
@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MANAGER')")
public String submitCreateUser(@ModelAttribute User user, @RequestParam String systemRole, @RequestParam String confirmPassword, @RequestParam(required = false) String requireActivation, Model model, Principal principal) {
Map<String, String> errors = new HashMap<>();
String returnView = null;
Locale locale = LocaleContextHolder.getLocale();
User creator = userService.getUserByUsername(principal.getName());
// check if we need to generate a password
boolean generateActivation = !Strings.isNullOrEmpty(requireActivation);
if (generateActivation) {
user.setPassword(generatePassword());
confirmPassword = user.getPassword();
user.setCredentialsNonExpired(false);
}
// check validity of password
if (!user.getPassword().equals(confirmPassword)) {
errors.put("password", messageSource.getMessage("user.edit.password.match", null, locale));
}
// Check if there are any errors for the user creation
if (errors.isEmpty()) {
if (isAdmin(principal)) {
user.setSystemRole(Role.valueOf(systemRole));
} else {
user.setSystemRole(Role.ROLE_USER);
}
try {
user = userService.create(user);
Long userId = user.getId();
returnView = "redirect:/users/" + userId;
// if the password isn't set, we'll generate a password reset
PasswordReset passwordReset = null;
if (generateActivation) {
passwordReset = passwordResetService.create(new PasswordReset(user));
logger.trace("Created password reset for activation");
}
emailController.sendWelcomeEmail(user, creator, passwordReset);
} catch (ConstraintViolationException | DataIntegrityViolationException | EntityExistsException ex) {
errors = handleCreateUpdateException(ex, locale);
} catch (final MailSendException e) {
logger.error("Failed to send user activation e-mail.", e);
model.addAttribute("mailFailure", true);
}
}
if (!errors.isEmpty()) {
model.addAttribute("errors", errors);
model.addAttribute("given_username", user.getUsername());
model.addAttribute("given_firstName", user.getFirstName());
model.addAttribute("given_lastName", user.getLastName());
model.addAttribute("given_email", user.getEmail());
model.addAttribute("given_phoneNumber", user.getPhoneNumber());
model.addAttribute("given_requireActivation", generateActivation);
returnView = createUserPage(model);
}
return returnView;
}
Aggregations