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