use of ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin 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.UserGroupJoin in project irida by phac-nml.
the class GroupsController method isGroupOwner.
/**
* Convenience method for checking whether or not the specified user is an
* owner of the group.
*
* @param user
* the {@link User} to check.
* @param group
* the {@link UserGroup} to check.
* @return true if owner, false otherwise.
*/
private boolean isGroupOwner(final User user, final UserGroup group) {
final Collection<UserGroupJoin> groupUsers = userGroupService.getUsersForGroup(group);
final Optional<UserGroupJoin> currentUserGroup = groupUsers.stream().filter(j -> j.getSubject().equals(user)).findAny();
if (currentUserGroup.isPresent()) {
final UserGroupJoin j = currentUserGroup.get();
return j.getRole().equals(UserGroupRole.GROUP_OWNER);
} else {
return false;
}
}
use of ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin in project irida by phac-nml.
the class GroupsController method getDetailsPage.
/**
* Get the details page for a {@link UserGroup}.
*
* @param userGroupId
* the {@link UserGroup} to retrieve.
* @param principal
* the user that's currently logged in.
* @param model
* the model to write attributes to.
* @return the route to the group details page.
*/
@RequestMapping("/{userGroupId}")
public String getDetailsPage(@PathVariable final Long userGroupId, final Principal principal, final Model model) {
final UserGroup group = userGroupService.read(userGroupId);
final Collection<UserGroupJoin> groupUsers = userGroupService.getUsersForGroup(group);
final User currentUser = userService.getUserByUsername(principal.getName());
final boolean isOwner = isGroupOwner(currentUser, group);
model.addAttribute("group", group);
model.addAttribute("isAdmin", currentUser.getSystemRole().equals(Role.ROLE_ADMIN));
model.addAttribute("isOwner", isOwner);
model.addAttribute("users", groupUsers);
model.addAttribute("groupRoles", ImmutableList.of(UserGroupRole.GROUP_MEMBER, UserGroupRole.GROUP_OWNER));
return GROUP_DETAILS;
}
Aggregations