Search in sources :

Example 6 with UserGroupProjectJoin

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

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

the class ProjectServiceImpl method updateUserGroupProjectRole.

/**
 * {@inheritDoc}
 */
@Override
@Transactional
@LaunchesProjectEvent(UserGroupRoleSetProjectEvent.class)
@PreAuthorize("hasRole('ROLE_ADMIN') or hasPermission(#project, 'canManageLocalProjectSettings')")
public Join<Project, UserGroup> updateUserGroupProjectRole(Project project, UserGroup userGroup, ProjectRole projectRole) throws ProjectWithoutOwnerException {
    final UserGroupProjectJoin j = ugpjRepository.findByProjectAndUserGroup(project, userGroup);
    if (j == null) {
        throw new EntityNotFoundException("Join between this project and group does not exist. Group: " + userGroup + " Project: " + project);
    }
    if (!allowRoleChange(project, j.getProjectRole())) {
        throw new ProjectWithoutOwnerException("This role change would leave the project without an owner");
    }
    j.setProjectRole(projectRole);
    return ugpjRepository.save(j);
}
Also used : ProjectWithoutOwnerException(ca.corefacility.bioinformatics.irida.exceptions.ProjectWithoutOwnerException) UserGroupProjectJoin(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupProjectJoin) 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 8 with UserGroupProjectJoin

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

the class ReadProjectPermission method customPermissionAllowed.

/**
 * {@inheritDoc}
 */
@Override
public boolean customPermissionAllowed(final Authentication authentication, final Project p) {
    logger.trace("Testing permission for [" + authentication + "] on project [" + p + "]");
    if (authentication.getAuthorities().stream().anyMatch(g -> g.getAuthority().equals(ROLE_SEQUENCER))) {
        logger.trace("Fast pass for sequencer role.");
        return true;
    }
    // if not an administrator, then we need to figure out if the
    // authenticated user is participating in the project.
    final User u = userRepository.loadUserByUsername(authentication.getName());
    final List<Join<Project, User>> projectUsers = pujRepository.getUsersForProject(p);
    for (final Join<Project, User> projectUser : projectUsers) {
        if (projectUser.getObject().equals(u)) {
            logger.trace("Permission GRANTED for [" + authentication + "] on project [" + p + "]");
            // this user is participating in the project.
            return true;
        }
    }
    // if we've made it this far, then that means that the user isn't
    // directly added to the project, so check if the user is in any groups
    // added to the project.
    final Collection<UserGroupProjectJoin> groups = ugpjRepository.findGroupsByProject(p);
    for (final UserGroupProjectJoin group : groups) {
        final Collection<UserGroupJoin> groupMembers = ugRepository.findUsersInGroup(group.getObject());
        final boolean inGroup = groupMembers.stream().anyMatch(j -> j.getSubject().equals(u));
        if (inGroup) {
            logger.trace("Permission GRANTED for [" + authentication + "] on project [" + p + "] by group membership in [" + group.getLabel() + "]");
            return true;
        }
    }
    logger.trace("Permission DENIED for [" + authentication + "] on project [" + p + "]");
    return false;
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) User(ca.corefacility.bioinformatics.irida.model.user.User) UserGroupProjectJoin(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupProjectJoin) UserGroupJoin(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin) UserGroupJoin(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin) Join(ca.corefacility.bioinformatics.irida.model.joins.Join) UserGroupProjectJoin(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupProjectJoin)

Example 9 with UserGroupProjectJoin

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

the class ProjectMembersController method getProjectGroupMembers.

/**
 * Get a page of groups on the project for display in a DataTable.
 *
 * @param params
 *            the datatables criteria for filtering/sorting groups
 * @param projectId
 *            the id of the project we're looking at
 * @return a {@link DataTablesResponseModel} of groups on the project
 */
@RequestMapping(value = "/{projectId}/settings/ajax/groups")
@ResponseBody
public DataTablesResponse getProjectGroupMembers(@DataTablesRequest DataTablesParams params, @PathVariable final Long projectId) {
    final Project project = projectService.read(projectId);
    final Page<UserGroupProjectJoin> userGroupsForProject = userGroupService.getUserGroupsForProject(params.getSearchValue(), project, params.getCurrentPage(), params.getLength(), params.getSort());
    List<DataTablesResponseModel> responseModels = new ArrayList<>();
    for (UserGroupProjectJoin userGroupProjectJoin : userGroupsForProject) {
        responseModels.add(new DTProjectGroup(userGroupProjectJoin));
    }
    return new DataTablesResponse(params, userGroupsForProject, responseModels);
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) DataTablesResponseModel(ca.corefacility.bioinformatics.irida.ria.web.components.datatables.models.DataTablesResponseModel) DTProjectGroup(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTProjectGroup) UserGroupProjectJoin(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupProjectJoin) DataTablesResponse(ca.corefacility.bioinformatics.irida.ria.web.components.datatables.DataTablesResponse)

Aggregations

UserGroupProjectJoin (ca.corefacility.bioinformatics.irida.model.user.group.UserGroupProjectJoin)9 Project (ca.corefacility.bioinformatics.irida.model.project.Project)5 User (ca.corefacility.bioinformatics.irida.model.user.User)4 ProjectWithoutOwnerException (ca.corefacility.bioinformatics.irida.exceptions.ProjectWithoutOwnerException)3 Join (ca.corefacility.bioinformatics.irida.model.joins.Join)3 UserGroupJoin (ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin)3 ArrayList (java.util.ArrayList)3 LaunchesProjectEvent (ca.corefacility.bioinformatics.irida.events.annotations.LaunchesProjectEvent)2 EntityNotFoundException (ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException)2 ProjectUserJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin)2 UserGroup (ca.corefacility.bioinformatics.irida.model.user.group.UserGroup)2 Specification (org.springframework.data.jpa.domain.Specification)2 EntityExistsException (ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException)1 ProjectRole (ca.corefacility.bioinformatics.irida.model.enums.ProjectRole)1 UserGroupRoleSetProjectEvent (ca.corefacility.bioinformatics.irida.model.event.UserGroupRoleSetProjectEvent)1 ProjectSampleJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin)1 RelatedProjectJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.RelatedProjectJoin)1 ProjectReferenceFileJoin (ca.corefacility.bioinformatics.irida.model.project.ProjectReferenceFileJoin)1 ReferenceFile (ca.corefacility.bioinformatics.irida.model.project.ReferenceFile)1 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)1