@@ -33,18 +33,11 @@ export class CasbinAuthorizationProvider
3333 resource : string ,
3434 request ?: Request ,
3535 ) : Promise < boolean > {
36- let authDecision = false ;
3736 try {
3837 // fetch decorator metadata
3938 const metadata : AuthorizationMetadata = await this . getCasbinMetadata ( ) ;
4039
41- if ( request && this . checkIfAllowedAlways ( request ) ) {
42- return true ;
43- }
44-
45- if ( metadata ?. permissions ?. indexOf ( '*' ) === 0 ) {
46- // Return immediately with true, if allowed to all
47- // This is for publicly open routes only
40+ if ( this . isAlwaysAllowed ( request , metadata ) ) {
4841 return true ;
4942 }
5043
@@ -96,16 +89,15 @@ export class CasbinAuthorizationProvider
9689 return false ;
9790 }
9891
99- // Use casbin enforce method to get authorization decision
100- for ( const permission of desiredPermissions ) {
101- const decision = await enforcer . enforce ( subject , resource , permission ) ;
102- authDecision = authDecision || decision ;
103- }
92+ return await this . checkPermissions (
93+ enforcer ,
94+ subject ,
95+ resource ,
96+ desiredPermissions ,
97+ ) ;
10498 } catch ( err ) {
10599 throw new HttpErrors . Unauthorized ( err . message ) ;
106100 }
107-
108- return authDecision ;
109101 }
110102
111103 // Generate the user name according to the naming convention
@@ -125,6 +117,33 @@ export class CasbinAuthorizationProvider
125117 }
126118 }
127119
120+ isAlwaysAllowed (
121+ request ?: Request ,
122+ metadata ?: AuthorizationMetadata ,
123+ ) : boolean {
124+ if ( request && this . checkIfAllowedAlways ( request ) ) {
125+ return true ;
126+ }
127+ if ( metadata ?. permissions ?. indexOf ( '*' ) === 0 ) {
128+ return true ;
129+ }
130+ return false ;
131+ }
132+
133+ async checkPermissions (
134+ enforcer : casbin . Enforcer ,
135+ subject : string ,
136+ resource : string ,
137+ permissions : string [ ] ,
138+ ) : Promise < boolean > {
139+ for ( const permission of permissions ) {
140+ if ( await enforcer . enforce ( subject , resource , permission ) ) {
141+ return true ;
142+ }
143+ }
144+ return false ;
145+ }
146+
128147 // Create casbin policy for user based on ResourcePermission data provided by extension client
129148 createCasbinPolicy (
130149 resPermObj : ResourcePermissionObject [ ] ,
0 commit comments