3030import edu .lternet .pasta .common .audit .AuditRecord ;
3131import edu .lternet .pasta .common .eml .DataPackage ;
3232import edu .lternet .pasta .common .eml .EMLParser ;
33+ import edu .lternet .pasta .common .security .access .ForbiddenException ;
3334import edu .lternet .pasta .common .security .access .UnauthorizedException ;
3435import edu .lternet .pasta .common .security .authorization .AccessMatrix ;
3536import edu .lternet .pasta .common .security .authorization .InvalidPermissionException ;
@@ -1997,12 +1998,10 @@ public Response appendProvenance(@Context HttpHeaders headers,
19971998 }
19981999 */
19992000
2000- /**
2001- * <strong>Is Authorized</strong> (to <em>READ</em> resource) operation,
2002- * determines whether the user as defined in the authentication token has
2003- * permission to access the specified data package resource. Allowed permissions
2004- * are "read", "write", or "changePermission" and must be verbatim.
2005- *
2001+ /**
2002+ * <strong>Is Authorized</strong> operation, determines whether the user as, defined in the
2003+ * authentication token, has the provided permission (read, write, or changePermission) on the
2004+ * specified data package resource. *
20062005 * <h4>Requests:</h4>
20072006 * <table border="1" cellspacing="0" cellpadding="3">
20082007 * <tr>
@@ -2097,6 +2096,7 @@ public Response isAuthorized (
20972096 ) {
20982097
20992098 AuthToken authToken = null ;
2099+
21002100 String entryText = String .format ("/package/authz?resourceId=%s&permission=%s" , resourceId , permission );
21012101 ResponseBuilder responseBuilder = null ;
21022102 Response response = null ;
@@ -2107,37 +2107,32 @@ public Response isAuthorized (
21072107 authToken = getAuthToken (headers );
21082108 String userId = authToken .getUserId ();
21092109
2110- // Is user authorized to run the service method?
2111- boolean serviceMethodAuthorized =
2112- isServiceMethodAuthorized (serviceMethodName , servicePermission , authToken );
2113- if (!serviceMethodAuthorized ) {
2114- throw new UnauthorizedException (
2115- "User " + userId + " is not authorized to execute service method " +
2116- serviceMethodName );
2117- }
2118-
21192110 try {
2111+ // Is user authorized to run the service method?
2112+ boolean serviceMethodAuthorized = isServiceMethodAuthorized (serviceMethodName , servicePermission , authToken );
2113+ if (!serviceMethodAuthorized ) {
2114+ String msg = String .format ("User %s is not authorized to execute service method %s" , userId , serviceMethodName );
2115+ throw new ForbiddenException (msg );
2116+ }
21202117
21212118 DataPackageManager dpm = new DataPackageManager ();
2122- Boolean isAuthorized = dpm .isAuthorized (authToken , resourceId , resourcePermission );
2119+ dpm .isAuthorized (authToken , resourceId , resourcePermission );
21232120
2124- if (isAuthorized != null && isAuthorized ) {
2125- responseBuilder = Response .ok (resourceId );
2126- response = responseBuilder .build ();
2127- }
2121+ responseBuilder = Response .ok (resourceId );
2122+ response = responseBuilder .build ();
21282123 } catch (IllegalArgumentException e ) {
21292124 entryText = e .getMessage ();
21302125 response = WebExceptionFactory .makeBadRequest (e ).getResponse ();
21312126 } catch (UnauthorizedException e ) {
21322127 entryText = e .getMessage ();
21332128 response = WebExceptionFactory .makeUnauthorized (e ).getResponse ();
2129+ } catch (ForbiddenException e ) {
2130+ entryText = e .getMessage ();
2131+ response = WebExceptionFactory .makeForbidden (e ).getResponse ();
21342132 } catch (ResourceNotFoundException e ) {
21352133 entryText = e .getMessage ();
21362134 response = WebExceptionFactory .makeNotFound (e ).getResponse ();
2137- } catch (ResourceDeletedException e ) {
2138- entryText = e .getMessage ();
2139- response = WebExceptionFactory .makeConflict (e ).getResponse ();
2140- } catch (ResourceExistsException e ) {
2135+ } catch (ResourceDeletedException | ResourceExistsException e ) {
21412136 entryText = e .getMessage ();
21422137 response = WebExceptionFactory .makeConflict (e ).getResponse ();
21432138 } catch (UserErrorException e ) {
0 commit comments