Search in sources :

Example 31 with ProjectUserJoin

use of ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin in project irida by phac-nml.

the class ProjectServiceImpl method searchForProjects.

/**
 * Search for projects using a few different types of search fields.
 *
 * @param allFields
 *            the search criteria to apply to all fields
 * @param projectNameFilter
 *            the filter to apply specifically to project name
 * @param organismNameFilter
 *            the filter to apply specifically to organism name
 * @param user
 *            the filter to apply for user filtering
 * @return the specification
 */
private static final Specification<Project> searchForProjects(final String allFields, final String projectNameFilter, final String organismNameFilter, final User user) {
    return new Specification<Project>() {

        /**
         * This {@link Predicate} considers *all* fields on a
         * {@link Project} with an OR filter.
         *
         * @param root
         *            the root of the query
         * @param query
         *            the query
         * @param cb
         *            the builder
         * @return a {@link Predicate} that covers all fields with OR.
         */
        private Predicate allFieldsPredicate(final Root<Project> root, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
            final List<Predicate> allFieldsPredicates = new ArrayList<>();
            allFieldsPredicates.add(cb.like(root.get("name"), "%" + allFields + "%"));
            allFieldsPredicates.add(cb.like(root.get("organism"), "%" + allFields + "%"));
            allFieldsPredicates.add(cb.like(root.get("id").as(String.class), "%" + allFields + "%"));
            return cb.or(allFieldsPredicates.toArray(new Predicate[0]));
        }

        /**
         * This {@link Predicate} considers each specific field on
         * {@link Project} separately and joins them with an AND filter.
         *
         * @param root
         *            the root of the query
         * @param query
         *            the query
         * @param cb
         *            the builder
         * @return a {@link Predicate} that covers individual fields with an
         *         AND.
         */
        private Predicate specificFiltersPredicate(final Root<Project> root, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
            final List<Predicate> filterPredicates = new ArrayList<>();
            if (!StringUtils.isEmpty(projectNameFilter)) {
                filterPredicates.add(cb.like(root.get("name"), "%" + projectNameFilter + "%"));
            }
            if (!StringUtils.isEmpty(organismNameFilter)) {
                filterPredicates.add(cb.like(root.get("organism"), "%" + organismNameFilter + "%"));
            }
            return cb.and(filterPredicates.toArray(new Predicate[0]));
        }

        /**
         * This {@link Predicate} filters out {@link Project}s for the
         * specific user where they are assigned individually as a member.
         *
         * @param root
         *            the root of the query
         * @param query
         *            the query
         * @param cb
         *            the builder
         * @return a {@link Predicate} that filters {@link Project}s where
         *         users are individually assigned.
         */
        private Predicate individualProjectMembership(final Root<Project> root, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
            final Subquery<Long> userMemberSelect = query.subquery(Long.class);
            final Root<ProjectUserJoin> userMemberJoin = userMemberSelect.from(ProjectUserJoin.class);
            userMemberSelect.select(userMemberJoin.get("project").get("id")).where(cb.equal(userMemberJoin.get("user"), user));
            return cb.in(root.get("id")).value(userMemberSelect);
        }

        /**
         * This {@link Predicate} filters out {@link Project}s for the
         * specific user where they are assigned transitively through a
         * {@link UserGroup}.
         *
         * @param root
         *            the root of the query
         * @param query
         *            the query
         * @param cb
         *            the builder
         * @return a {@link Predicate} that filters {@link Project}s where
         *         users are assigned transitively through {@link UserGroup}
         *         .
         */
        private Predicate groupProjectMembership(final Root<Project> root, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
            final Subquery<Long> groupMemberSelect = query.subquery(Long.class);
            final Root<UserGroupProjectJoin> groupMemberJoin = groupMemberSelect.from(UserGroupProjectJoin.class);
            groupMemberSelect.select(groupMemberJoin.get("project").get("id")).where(cb.equal(groupMemberJoin.join("userGroup").join("users").get("user"), user));
            return cb.in(root.get("id")).value(groupMemberSelect);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Predicate toPredicate(final Root<Project> root, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
            final Predicate allFieldsPredicate = allFieldsPredicate(root, query, cb);
            final Predicate specificFiltersPredicate = specificFiltersPredicate(root, query, cb);
            final Predicate projectMember = cb.or(individualProjectMembership(root, query, cb), groupProjectMembership(root, query, cb));
            if (user != null) {
                return cb.and(allFieldsPredicate, specificFiltersPredicate, projectMember);
            } else {
                return cb.and(allFieldsPredicate, specificFiltersPredicate);
            }
        }
    };
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Root(javax.persistence.criteria.Root) ProjectUserJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) ArrayList(java.util.ArrayList) UserGroupProjectJoin(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupProjectJoin) Specification(org.springframework.data.jpa.domain.Specification) Predicate(javax.persistence.criteria.Predicate)

Example 32 with ProjectUserJoin

use of ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin in project irida by phac-nml.

the class ProjectServiceImpl method updateUserProjectRole.

/**
 * {@inheritDoc}
 */
@Override
@Transactional
@LaunchesProjectEvent(UserRoleSetProjectEvent.class)
@PreAuthorize("hasRole('ROLE_ADMIN') or hasPermission(#project,'canManageLocalProjectSettings')")
public Join<Project, User> updateUserProjectRole(Project project, User user, ProjectRole projectRole) throws ProjectWithoutOwnerException {
    ProjectUserJoin projectJoinForUser = pujRepository.getProjectJoinForUser(project, user);
    if (projectJoinForUser == null) {
        throw new EntityNotFoundException("Join between this project and user does not exist. User: " + user + " Project: " + project);
    }
    if (!allowRoleChange(projectJoinForUser.getSubject(), projectJoinForUser.getProjectRole())) {
        throw new ProjectWithoutOwnerException("This role change would leave the project without an owner");
    }
    projectJoinForUser.setProjectRole(projectRole);
    return pujRepository.save(projectJoinForUser);
}
Also used : ProjectUserJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin) ProjectWithoutOwnerException(ca.corefacility.bioinformatics.irida.exceptions.ProjectWithoutOwnerException) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) LaunchesProjectEvent(ca.corefacility.bioinformatics.irida.events.annotations.LaunchesProjectEvent) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Example 33 with ProjectUserJoin

use of ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin in project irida by phac-nml.

the class UserServiceImpl method updateEmailSubscription.

/**
 * {@inheritDoc}
 */
@PreAuthorize("hasPermission(#user, 'canUpdateUser')")
public ProjectUserJoin updateEmailSubscription(User user, Project project, boolean subscribed) {
    ProjectUserJoin projectJoinForUser = pujRepository.getProjectJoinForUser(project, user);
    projectJoinForUser.setEmailSubscription(subscribed);
    return pujRepository.save(projectJoinForUser);
}
Also used : ProjectUserJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 34 with ProjectUserJoin

use of ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin in project irida by phac-nml.

the class UsersController method getUserSpecificPage.

/**
 * Request for a specific user details page.
 *
 * @param userId
 *            The id for the user to show details for.
 * @param model
 *            Spring model to populate the html page
 * @param principal
 *            the currently logged in user
 * @param mailFailure
 * 			  if sending a user activation e-mail passed or failed
 *
 * @return The name of the user/details page
 */
@RequestMapping(value = "/{userId}", method = RequestMethod.GET)
public String getUserSpecificPage(@PathVariable("userId") Long userId, @RequestParam(value = "mailFailure", required = false, defaultValue = "false") final Boolean mailFailure, final Model model, Principal principal) {
    logger.debug("Getting project information for [User " + userId + "]");
    // add the user to the model
    User user = userService.read(userId);
    model.addAttribute("user", user);
    model.addAttribute("mailFailure", mailFailure);
    User principalUser = userService.getUserByUsername(principal.getName());
    Locale locale = LocaleContextHolder.getLocale();
    // add the user's role to the model
    String roleMessageName = "systemrole." + user.getSystemRole().getName();
    String systemRole = messageSource.getMessage(roleMessageName, null, locale);
    model.addAttribute("systemRole", systemRole);
    // check if we should show an edit button
    boolean canEditUser = canEditUser(principalUser, user);
    model.addAttribute("canEditUser", canEditUser);
    model.addAttribute("mailConfigured", emailController.isMailConfigured());
    model.addAttribute("canCreatePasswordReset", PasswordResetController.canCreatePasswordReset(principalUser, user));
    // show the user's projects
    List<Join<Project, User>> projectsForUser = projectService.getProjectsForUser(user);
    // add the projects to the model list
    List<Map<String, Object>> projects = new ArrayList<>();
    for (Join<Project, User> join : projectsForUser) {
        ProjectUserJoin pujoin = (ProjectUserJoin) join;
        Project project = join.getSubject();
        Map<String, Object> map = new HashMap<>();
        map.put("identifier", project.getId());
        map.put("name", project.getName());
        map.put("isManager", pujoin.getProjectRole().equals(ProjectRole.PROJECT_OWNER));
        map.put("subscribed", pujoin.isEmailSubscription());
        String proleMessageName = "projectRole." + pujoin.getProjectRole().toString();
        map.put("role", messageSource.getMessage(proleMessageName, null, locale));
        map.put("date", pujoin.getCreatedDate());
        projects.add(map);
    }
    model.addAttribute("projects", projects);
    return SPECIFIC_USER_PAGE;
}
Also used : Locale(java.util.Locale) DTUser(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTUser) User(ca.corefacility.bioinformatics.irida.model.user.User) ProjectUserJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Join(ca.corefacility.bioinformatics.irida.model.joins.Join) ProjectUserJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin) Project(ca.corefacility.bioinformatics.irida.model.project.Project) Map(java.util.Map) HashMap(java.util.HashMap) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ProjectUserJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin)34 Project (ca.corefacility.bioinformatics.irida.model.project.Project)26 User (ca.corefacility.bioinformatics.irida.model.user.User)23 Join (ca.corefacility.bioinformatics.irida.model.joins.Join)20 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)12 ProjectRole (ca.corefacility.bioinformatics.irida.model.enums.ProjectRole)8 ProjectSampleJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin)7 UserGroupProjectJoin (ca.corefacility.bioinformatics.irida.model.user.group.UserGroupProjectJoin)7 RelatedProjectJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.RelatedProjectJoin)6 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)6 Authentication (org.springframework.security.core.Authentication)6 ProjectEvent (ca.corefacility.bioinformatics.irida.model.event.ProjectEvent)4 UserRoleSetProjectEvent (ca.corefacility.bioinformatics.irida.model.event.UserRoleSetProjectEvent)4 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)4 ProjectWithoutOwnerException (ca.corefacility.bioinformatics.irida.exceptions.ProjectWithoutOwnerException)3 ProjectReferenceFileJoin (ca.corefacility.bioinformatics.irida.model.project.ProjectReferenceFileJoin)3 DTUser (ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTUser)3 List (java.util.List)3 Locale (java.util.Locale)3