Search in sources :

Example 11 with User

use of ca.corefacility.bioinformatics.irida.model.user.User 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 12 with User

use of ca.corefacility.bioinformatics.irida.model.user.User in project irida by phac-nml.

the class UsersController method getAjaxUserList.

/**
 * Get a list of users based on search criteria.
 * @param params {@link DataTablesParams} for the current Users DataTables.
 * @param locale {@link Locale}
 * @return {@link DataTablesResponse} of the filtered users list.
 */
@RequestMapping(value = "/ajax/list", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public DataTablesResponse getAjaxUserList(@DataTablesRequest DataTablesParams params, Locale locale) {
    Page<User> userPage = userService.search(UserSpecification.searchUser(params.getSearchValue()), new PageRequest(params.getCurrentPage(), params.getLength(), params.getSort()));
    List<DataTablesResponseModel> usersData = new ArrayList<>();
    for (User user : userPage) {
        // getting internationalized system role from the message source
        String roleMessageName = "systemrole." + user.getSystemRole().getName();
        String systemRole = messageSource.getMessage(roleMessageName, null, locale);
        usersData.add(new DTUser(user.getId(), user.getUsername(), user.getFirstName(), user.getLastName(), user.getEmail(), systemRole, user.getCreatedDate(), user.getModifiedDate(), user.getLastLogin()));
    }
    return new DataTablesResponse(params, userPage, usersData);
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) DTUser(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTUser) User(ca.corefacility.bioinformatics.irida.model.user.User) DataTablesResponseModel(ca.corefacility.bioinformatics.irida.ria.web.components.datatables.models.DataTablesResponseModel) ArrayList(java.util.ArrayList) DTUser(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTUser) DataTablesResponse(ca.corefacility.bioinformatics.irida.ria.web.components.datatables.DataTablesResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 13 with User

use of ca.corefacility.bioinformatics.irida.model.user.User in project irida by phac-nml.

the class ProjectEventsController method getRecentEventsForUser.

/**
 * Get recent {@link ProjectEvent}s for the currently logged in user
 *
 * @param model
 *            Model for the view. Contains a list named "events". This will
 *            be a map which will contain "name" which is the name of the
 *            view fragment to use, and "event" which is a reference to the
 *            event itself
 * @param principal
 *            currently logged in principal
 * @param size
 *            Number of events to show
 * @return The name of the events view
 */
@RequestMapping("/current_user")
public String getRecentEventsForUser(Model model, Principal principal, @RequestParam(required = false, defaultValue = DEFAULT_PAGE_SIZE) Integer size) {
    String userName = principal.getName();
    User user = userService.getUserByUsername(userName);
    Page<ProjectEvent> events = eventService.getEventsForUser(user, new PageRequest(0, size, Direction.DESC, "createdDate"));
    List<Map<String, Object>> eventInfo = buildEventsListFromPage(events);
    model.addAttribute("events", eventInfo);
    return EVENTS_VIEW;
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) User(ca.corefacility.bioinformatics.irida.model.user.User) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) UserGroupRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.enums.UserGroupRemovedProjectEvent) UserRoleSetProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRoleSetProjectEvent) UserRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRemovedProjectEvent) DataAddedToSampleProjectEvent(ca.corefacility.bioinformatics.irida.model.event.DataAddedToSampleProjectEvent) SampleRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.SampleRemovedProjectEvent) UserGroupRoleSetProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserGroupRoleSetProjectEvent) ProjectEvent(ca.corefacility.bioinformatics.irida.model.event.ProjectEvent) SampleAddedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.SampleAddedProjectEvent) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 14 with User

use of ca.corefacility.bioinformatics.irida.model.user.User in project irida by phac-nml.

the class ProjectEventsController method addSubscription.

/**
 * Update the subscription status on a {@link Project} for a {@link User}
 *
 * @param userId
 *            The {@link User} id to update
 * @param projectId
 *            the {@link Project} to subscribe to
 * @param subscribe
 *            boolean whether to be subscribed to the project or not
 * @param locale
 *            locale of the request
 * @return Map success message if the subscription status was updated
 */
@RequestMapping(value = "/projects/{projectId}/subscribe/{userId}", method = RequestMethod.POST)
public Map<String, String> addSubscription(@PathVariable Long userId, @PathVariable Long projectId, @RequestParam boolean subscribe, Locale locale) {
    User user = userService.read(userId);
    Project project = projectService.read(projectId);
    userService.updateEmailSubscription(user, project, subscribe);
    String message;
    if (subscribe) {
        message = messageSource.getMessage("user.projects.subscriptions.added", new Object[] { project.getLabel() }, locale);
    } else {
        message = messageSource.getMessage("user.projects.subscriptions.removed", new Object[] { project.getLabel() }, locale);
    }
    return ImmutableMap.of("success", "true", "message", message);
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) User(ca.corefacility.bioinformatics.irida.model.user.User) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 15 with User

use of ca.corefacility.bioinformatics.irida.model.user.User in project irida by phac-nml.

the class ProjectMembersController method updateUserRole.

/**
 * Update a user's role on a project
 *
 * @param projectId   The ID of the project
 * @param userId      The ID of the user
 * @param projectRole The role to set
 * @param locale      Locale of the logged in user
 * @return Success or error message
 */
@RequestMapping(path = "{projectId}/settings/members/editrole/{userId}", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> updateUserRole(@PathVariable final Long projectId, @PathVariable final Long userId, @RequestParam final String projectRole, final Locale locale) {
    final Project project = projectService.read(projectId);
    final User user = userService.read(userId);
    final ProjectRole role = ProjectRole.fromString(projectRole);
    final String roleName = messageSource.getMessage("projectRole." + projectRole, new Object[] {}, locale);
    try {
        projectService.updateUserProjectRole(project, user, role);
        return ImmutableMap.of("success", messageSource.getMessage("project.members.edit.role.success", new Object[] { user.getLabel(), roleName }, locale));
    } catch (final ProjectWithoutOwnerException e) {
        return ImmutableMap.of("failure", messageSource.getMessage("project.members.edit.role.failure.nomanager", new Object[] { user.getLabel(), roleName }, locale));
    }
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) User(ca.corefacility.bioinformatics.irida.model.user.User) ProjectWithoutOwnerException(ca.corefacility.bioinformatics.irida.exceptions.ProjectWithoutOwnerException) ProjectRole(ca.corefacility.bioinformatics.irida.model.enums.ProjectRole)

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