Search in sources :

Example 1 with UserRoles

use of org.broadinstitute.consent.http.enumeration.UserRoles in project consent by DataBiosphere.

the class DarCollectionResource method cancelDarCollectionByCollectionId.

@PUT
@Path("{id}/cancel")
@Produces("application/json")
@RolesAllowed({ ADMIN, CHAIRPERSON, RESEARCHER })
public Response cancelDarCollectionByCollectionId(@Auth AuthUser authUser, @PathParam("id") Integer collectionId, @QueryParam("roleName") String roleName) {
    try {
        User user = userService.findUserByEmail(authUser.getEmail());
        DarCollection collection = darCollectionService.getByCollectionId(collectionId);
        isCollectionPresent(collection);
        // Default to the least impactful role if none provided.
        UserRoles actingRole = UserRoles.RESEARCHER;
        if (Objects.nonNull(roleName)) {
            validateUserHasRoleName(user, roleName);
            UserRoles requestedRole = UserRoles.getUserRoleFromName(roleName);
            if (Objects.nonNull(requestedRole)) {
                actingRole = requestedRole;
            }
        }
        DarCollection cancelledCollection;
        switch(actingRole) {
            case ADMIN:
                cancelledCollection = darCollectionService.cancelDarCollectionElectionsAsAdmin(collection);
                break;
            case CHAIRPERSON:
                cancelledCollection = darCollectionService.cancelDarCollectionElectionsAsChair(collection, user);
                break;
            default:
                validateUserIsCreator(user, collection);
                cancelledCollection = darCollectionService.cancelDarCollectionAsResearcher(collection);
                break;
        }
        return Response.ok().entity(cancelledCollection).build();
    } catch (Exception e) {
        return createExceptionResponse(e);
    }
}
Also used : AuthUser(org.broadinstitute.consent.http.models.AuthUser) User(org.broadinstitute.consent.http.models.User) UserRoles(org.broadinstitute.consent.http.enumeration.UserRoles) BadRequestException(javax.ws.rs.BadRequestException) ForbiddenException(javax.ws.rs.ForbiddenException) NotFoundException(javax.ws.rs.NotFoundException) DarCollection(org.broadinstitute.consent.http.models.DarCollection) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 2 with UserRoles

use of org.broadinstitute.consent.http.enumeration.UserRoles in project consent by DataBiosphere.

the class DarCollectionService method getCollectionsForUserByRoleName.

public List<DarCollection> getCollectionsForUserByRoleName(User user, String roleName) {
    List<DarCollection> collections = new ArrayList<>();
    UserRoles selectedRole = UserRoles.getUserRoleFromName(roleName);
    if (Objects.nonNull(selectedRole) && user.hasUserRole(selectedRole)) {
        switch(selectedRole) {
            case ADMIN:
                collections.addAll(getAllCollections());
                break;
            case CHAIRPERSON:
            case MEMBER:
                collections.addAll(getCollectionsByUserDacs(user));
                break;
            case SIGNINGOFFICIAL:
                collections.addAll(getCollectionsByUserInstitution(user));
                break;
            default:
                collections.addAll(getCollectionsForUser(user));
        }
    } else {
        collections.addAll(getCollectionsForUser(user));
    }
    return collections;
}
Also used : UserRoles(org.broadinstitute.consent.http.enumeration.UserRoles) ArrayList(java.util.ArrayList) DarCollection(org.broadinstitute.consent.http.models.DarCollection)

Example 3 with UserRoles

use of org.broadinstitute.consent.http.enumeration.UserRoles in project consent by DataBiosphere.

the class DataAccessRequestResource method describeManageDataAccessRequestsV2.

@GET
@Produces("application/json")
@Path("/manage/v2")
@RolesAllowed({ ADMIN, CHAIRPERSON, MEMBER, SIGNINGOFFICIAL, RESEARCHER })
public Response describeManageDataAccessRequestsV2(@Auth AuthUser authUser, @QueryParam("roleName") Optional<String> roleName) {
    try {
        User user = userService.findUserByEmail(authUser.getEmail());
        String roleNameValue = roleName.orElse(null);
        UserRoles queriedUserRole = UserRoles.getUserRoleFromName(roleNameValue);
        if (roleName.isPresent()) {
            // if a roleName was passed in but it is not in the UserRoles enum throw exception
            if (Objects.isNull(queriedUserRole)) {
                throw new BadRequestException("Invalid role name: " + roleNameValue);
            } else {
                // if there is a valid roleName but it is not SO or Researcher then throw an exception
                if (queriedUserRole != UserRoles.RESEARCHER && queriedUserRole != UserRoles.SIGNINGOFFICIAL) {
                    throw new BadRequestException("Unsupported role name: " + queriedUserRole.getRoleName());
                }
                // if the user does not have the given roleName throw NotFoundException
                if (!user.hasUserRole(queriedUserRole)) {
                    throw new NotFoundException("User: " + user.getDisplayName() + ", does not have " + queriedUserRole.getRoleName() + " role.");
                }
            }
        // if no roleName was passed in, find the user's role
        } else {
            if (user.hasUserRole(UserRoles.ADMIN)) {
                queriedUserRole = UserRoles.ADMIN;
            } else if (user.hasUserRole(UserRoles.CHAIRPERSON)) {
                queriedUserRole = UserRoles.CHAIRPERSON;
            } else if (user.hasUserRole(UserRoles.MEMBER)) {
                queriedUserRole = UserRoles.MEMBER;
            }
        }
        List<DataAccessRequestManage> dars = dataAccessRequestService.describeDataAccessRequestManageV2(user, queriedUserRole);
        return Response.ok().entity(dars).build();
    } catch (Exception e) {
        return createExceptionResponse(e);
    }
}
Also used : AuthUser(org.broadinstitute.consent.http.models.AuthUser) User(org.broadinstitute.consent.http.models.User) DataAccessRequestManage(org.broadinstitute.consent.http.models.DataAccessRequestManage) UserRoles(org.broadinstitute.consent.http.enumeration.UserRoles) BadRequestException(javax.ws.rs.BadRequestException) NotFoundException(javax.ws.rs.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

UserRoles (org.broadinstitute.consent.http.enumeration.UserRoles)3 RolesAllowed (javax.annotation.security.RolesAllowed)2 BadRequestException (javax.ws.rs.BadRequestException)2 NotFoundException (javax.ws.rs.NotFoundException)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 AuthUser (org.broadinstitute.consent.http.models.AuthUser)2 DarCollection (org.broadinstitute.consent.http.models.DarCollection)2 User (org.broadinstitute.consent.http.models.User)2 ArrayList (java.util.ArrayList)1 ForbiddenException (javax.ws.rs.ForbiddenException)1 GET (javax.ws.rs.GET)1 PUT (javax.ws.rs.PUT)1 DataAccessRequestManage (org.broadinstitute.consent.http.models.DataAccessRequestManage)1