Search in sources :

Example 1 with Role

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;
}
Also used : Locale(java.util.Locale) ProjectRole(ca.corefacility.bioinformatics.irida.model.enums.ProjectRole) Role(ca.corefacility.bioinformatics.irida.model.user.Role) DTUser(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTUser) User(ca.corefacility.bioinformatics.irida.model.user.User) HashMap(java.util.HashMap) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with Role

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;
}
Also used : Locale(java.util.Locale) DTUser(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTUser) User(ca.corefacility.bioinformatics.irida.model.user.User) HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) PasswordReusedException(ca.corefacility.bioinformatics.irida.exceptions.PasswordReusedException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) ProjectRole(ca.corefacility.bioinformatics.irida.model.enums.ProjectRole) Role(ca.corefacility.bioinformatics.irida.model.user.Role) ConstraintViolationException(javax.validation.ConstraintViolationException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with Role

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;
}
Also used : Role(ca.corefacility.bioinformatics.irida.model.user.Role)

Example 4 with Role

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;
}
Also used : Locale(java.util.Locale) ProjectRole(ca.corefacility.bioinformatics.irida.model.enums.ProjectRole) Role(ca.corefacility.bioinformatics.irida.model.user.Role) HashMap(java.util.HashMap) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Role (ca.corefacility.bioinformatics.irida.model.user.Role)4 ProjectRole (ca.corefacility.bioinformatics.irida.model.enums.ProjectRole)3 HashMap (java.util.HashMap)3 Locale (java.util.Locale)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 User (ca.corefacility.bioinformatics.irida.model.user.User)2 DTUser (ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTUser)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 PasswordReusedException (ca.corefacility.bioinformatics.irida.exceptions.PasswordReusedException)1 HttpSession (javax.servlet.http.HttpSession)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)1