Search in sources :

Example 6 with User

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;
}
Also used : User(ca.corefacility.bioinformatics.irida.model.user.User) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with User

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;
}
Also used : User(ca.corefacility.bioinformatics.irida.model.user.User) MailSendException(org.springframework.mail.MailSendException) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with User

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;
}
Also used : User(ca.corefacility.bioinformatics.irida.model.user.User) MailSendException(org.springframework.mail.MailSendException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 9 with User

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;
}
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 10 with User

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;
}
Also used : Locale(java.util.Locale) DTUser(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTUser) User(ca.corefacility.bioinformatics.irida.model.user.User) MailSendException(org.springframework.mail.MailSendException) HashMap(java.util.HashMap) EntityExistsException(ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) PasswordReset(ca.corefacility.bioinformatics.irida.model.user.PasswordReset) ConstraintViolationException(javax.validation.ConstraintViolationException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

User (ca.corefacility.bioinformatics.irida.model.user.User)252 Test (org.junit.Test)153 Project (ca.corefacility.bioinformatics.irida.model.project.Project)84 WithMockUser (org.springframework.security.test.context.support.WithMockUser)57 Authentication (org.springframework.security.core.Authentication)45 Join (ca.corefacility.bioinformatics.irida.model.joins.Join)34 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)27 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)27 PageRequest (org.springframework.data.domain.PageRequest)26 UserGroup (ca.corefacility.bioinformatics.irida.model.user.group.UserGroup)25 ProjectRole (ca.corefacility.bioinformatics.irida.model.enums.ProjectRole)24 ProjectUserJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin)24 AnalysisSubmission (ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission)19 Principal (java.security.Principal)19 ProjectSampleJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin)18 RelatedProjectJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.RelatedProjectJoin)18 List (java.util.List)18 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)17 EntityNotFoundException (ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException)16 ArrayList (java.util.ArrayList)16