use of ca.corefacility.bioinformatics.irida.model.user.Role 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.Role in project irida by phac-nml.
the class UsersController method updateUser.
/**
* Submit a user edit
*
* @param userId
* The id of the user to edit (required)
* @param firstName
* The firstname to update
* @param lastName
* the lastname to update
* @param email
* the email to update
* @param phoneNumber
* the phone number to update
* @param systemRole
* the role to update
* @param password
* the password to update
* @param confirmPassword
* password confirmation
* @param model
* The model to work on
* @param enabled
* whether the user account should be enabled or disabled.
* @param principal
* a reference to the logged in user.
* @param request
* the request
* @return The name of the user view
*/
@RequestMapping(value = "/{userId}/edit", method = RequestMethod.POST)
public String updateUser(@PathVariable Long userId, @RequestParam(required = false) String firstName, @RequestParam(required = false) String lastName, @RequestParam(required = false) String email, @RequestParam(required = false) String phoneNumber, @RequestParam(required = false) String systemRole, @RequestParam(required = false) String password, @RequestParam(required = false) String enabled, @RequestParam(required = false) String confirmPassword, Model model, Principal principal, HttpServletRequest request) {
logger.debug("Updating user " + userId);
Locale locale = LocaleContextHolder.getLocale();
Map<String, String> errors = new HashMap<>();
Map<String, Object> updatedValues = new HashMap<>();
if (!Strings.isNullOrEmpty(firstName)) {
updatedValues.put("firstName", firstName);
}
if (!Strings.isNullOrEmpty(lastName)) {
updatedValues.put("lastName", lastName);
}
if (!Strings.isNullOrEmpty(email)) {
updatedValues.put("email", email);
}
if (!Strings.isNullOrEmpty(phoneNumber)) {
updatedValues.put("phoneNumber", phoneNumber);
}
if (!Strings.isNullOrEmpty(password) || !Strings.isNullOrEmpty(confirmPassword)) {
if (!password.equals(confirmPassword)) {
errors.put("password", messageSource.getMessage("user.edit.password.match", null, locale));
} else {
updatedValues.put("password", password);
}
}
if (isAdmin(principal)) {
logger.debug("User is admin");
if (!Strings.isNullOrEmpty(enabled)) {
updatedValues.put("enabled", true);
} else {
updatedValues.put("enabled", false);
}
if (!Strings.isNullOrEmpty(systemRole)) {
Role newRole = Role.valueOf(systemRole);
updatedValues.put("systemRole", newRole);
}
}
String returnView;
if (errors.isEmpty()) {
try {
User user = userService.updateFields(userId, updatedValues);
returnView = "redirect:/users/" + userId;
// this will update the users gravatar!
if (user != null && principal.getName().equals(user.getUsername())) {
HttpSession session = request.getSession();
session.setAttribute(UserSecurityInterceptor.CURRENT_USER_DETAILS, user);
}
} catch (ConstraintViolationException | DataIntegrityViolationException | PasswordReusedException ex) {
errors = handleCreateUpdateException(ex, locale);
model.addAttribute("errors", errors);
returnView = getEditUserPage(userId, model);
}
} else {
model.addAttribute("errors", errors);
returnView = getEditUserPage(userId, model);
}
return returnView;
}
use of ca.corefacility.bioinformatics.irida.model.user.Role in project irida by phac-nml.
the class PasswordResetController method canCreatePasswordReset.
/**
* Test if a user should be able to click the password reset button
*
* @param principalUser
* The currently logged in principal
* @param user
* The user being edited
* @return true if the principal can create a password reset for the user
*/
public static boolean canCreatePasswordReset(User principalUser, User user) {
Role userRole = user.getSystemRole();
Role principalRole = principalUser.getSystemRole();
if (principalRole.equals(Role.ROLE_ADMIN)) {
return true;
} else if (principalRole.equals(Role.ROLE_MANAGER)) {
if (userRole.equals(Role.ROLE_ADMIN)) {
return false;
} else {
return true;
}
}
return false;
}
use of ca.corefacility.bioinformatics.irida.model.user.Role in project irida by phac-nml.
the class UsersController method createUserPage.
/**
* Get the user creation view
* @param model Model for the view
* @return user creation view
*/
@RequestMapping(value = "/create", method = RequestMethod.GET)
@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MANAGER')")
public String createUserPage(Model model) {
Locale locale = LocaleContextHolder.getLocale();
Map<String, String> roleNames = new HashMap<>();
for (Role role : adminAllowedRoles) {
String roleMessageName = "systemrole." + role.getName();
String roleName = messageSource.getMessage(roleMessageName, null, locale);
roleNames.put(role.getName(), roleName);
}
model.addAttribute("allowedRoles", roleNames);
if (!model.containsAttribute("given_requireActivation")) {
model.addAttribute("given_requireActivation", true);
}
if (!model.containsAttribute("errors")) {
model.addAttribute("errors", new HashMap<String, String>());
}
model.addAttribute("emailConfigured", emailController.isMailConfigured());
return CREATE_USER_PAGE;
}
Aggregations