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);
}
}
};
}
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);
}
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;
}
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);
}
Aggregations