@@ -30,17 +30,23 @@ export class AccessController {
3030 resourceAdapter : ResourceAdapter ;
3131 redisClient : RedisClientType < any , any > ;
3232 userTopic : Topic ;
33- waiting : any [ ] ;
33+ waiting : any ;
3434 cfg : any ;
3535 userService : UserServiceClient ;
36- constructor ( private logger : Logger , opts : AccessControlConfiguration ,
37- userTopic : Topic , cfg : any , userService : UserServiceClient ) {
36+
37+ constructor (
38+ private logger : Logger ,
39+ opts : AccessControlConfiguration ,
40+ userTopic : Topic ,
41+ cfg : any ,
42+ userService : UserServiceClient
43+ ) {
3844 this . policySets = new Map < string , PolicySetWithCombinables > ( ) ;
3945 this . combiningAlgorithms = new Map < string , any > ( ) ;
4046
4147 logger . info ( 'Parsing combining algorithms from access control configuration...' ) ;
4248 // parsing URNs and mapping them to functions
43- const combiningAlgorithms : CombiningAlgorithm [ ] = opts ?. combiningAlgorithms || [ ] ;
49+ const combiningAlgorithms : CombiningAlgorithm [ ] = opts ?. combiningAlgorithms ?? [ ] ;
4450 for ( let ca of combiningAlgorithms ) {
4551 const urn = ca . urn ;
4652 const method = ca . method ;
@@ -115,22 +121,29 @@ export class AccessController {
115121
116122 // policyEffect needed to evalute if the properties should be PERMIT / DENY
117123 let policyEffect : Effect ;
118- if ( ( ! ! policySet . target && await this . targetMatches ( policySet . target , request , 'isAllowed' , obligations ) )
119- || ! policySet . target ) {
124+ if (
125+ ! policySet . target
126+ || await this . targetMatches ( policySet . target , request , 'isAllowed' , obligations )
127+ ) {
120128 let exactMatch = false ;
121129 for ( let [ , policyValue ] of policySet . combinables ) {
122130 const policy : Policy = policyValue ;
123131 if ( policy . effect ) {
124132 policyEffect = policy . effect ;
125- } else if ( policy . combining_algorithm ) {
133+ }
134+ else if ( policy . combining_algorithm ) {
126135 const method = this . combiningAlgorithms . get ( policy . combining_algorithm ) ;
127136 if ( method === 'permitOverrides' ) {
128137 policyEffect = Effect . PERMIT ;
129138 } else if ( method === 'denyOverrides' ) {
130139 policyEffect = Effect . DENY ;
131140 }
132141 }
133- if ( ! ! policy . target && await this . targetMatches ( policy . target , request , 'isAllowed' , obligations , policyEffect ) ) {
142+
143+ if (
144+ policy . target
145+ && await this . targetMatches ( policy . target , request , 'isAllowed' , obligations , policyEffect )
146+ ) {
134147 exactMatch = true ;
135148 break ;
136149 }
@@ -151,11 +164,18 @@ export class AccessController {
151164 continue ;
152165 }
153166 const ruleEffects : EffectEvaluation [ ] = [ ] ;
154- if ( ( ! ! policy . target && exactMatch && await this . targetMatches ( policy . target , request , 'isAllowed' , obligations , policyEffect ) )
167+ if (
168+ ! policy . target
169+ || (
170+ exactMatch
171+ && await this . targetMatches ( policy . target , request , 'isAllowed' , obligations , policyEffect )
172+ )
155173 // regex match
156- || ( ! ! policy . target && ! exactMatch && await this . targetMatches ( policy . target , request , 'isAllowed' , obligations , policyEffect , true ) )
157- || ! policy . target ) {
158-
174+ || (
175+ ! exactMatch
176+ && await this . targetMatches ( policy . target , request , 'isAllowed' , obligations , policyEffect , true )
177+ )
178+ ) {
159179 const rules : Map < string , Rule > = policy . combinables ;
160180 this . logger . verbose ( `Checking policy ${ policy . name } ` ) ;
161181 // only apply a policy effect if there are no rules
@@ -184,19 +204,26 @@ export class AccessController {
184204 }
185205
186206 if ( matches ) {
187- this . logger . verbose ( `Checking rule ${ rule . name } ` ) ;
207+ this . logger . verbose ( `Checking rule HR Scope for ${ rule . name } ` ) ;
188208 if ( matches && rule . target ) {
189209 matches = await checkHierarchicalScope ( rule . target , request , this . urns , this , this . logger ) ;
190210 }
191211
192212 try {
193- if ( matches && ! _ . isEmpty ( rule . condition ) ) {
213+ if ( matches && rule . condition ?. length ) {
194214 // context query is only checked when a rule exists
195215 let context : any ;
196- if ( ! _ . isEmpty ( rule . context_query ) && this . resourceAdapter ) {
216+ if (
217+ this . resourceAdapter
218+ && (
219+ rule . context_query ?. filters ?. length
220+ || rule . context_query ?. query ?. length
221+ )
222+ ) {
197223 context = await this . pullContextResources ( rule . context_query , request ) ;
198224
199225 if ( _ . isNil ( context ) ) {
226+ this . logger . debug ( 'Context query response is empty!' ) ;
200227 return { // deny by default
201228 decision : Response_Decision . DENY ,
202229 obligations,
@@ -209,12 +236,12 @@ export class AccessController {
209236 }
210237 }
211238
212- request . context = context || request . context ;
239+ request . context = context ?? request . context ;
213240 this . logger . debug ( 'Validating rule condition' , { name : rule . name , condition : rule . condition } ) ;
214241 matches = conditionMatches ( rule . condition , request ) ;
215242 this . logger . debug ( 'condition validation response' , { matches } ) ;
216243 }
217- } catch ( err ) {
244+ } catch ( err : any ) {
218245 this . logger . error ( 'Caught an exception while applying rule condition to request' , { code : err . code , message : err . message , stack : err . stack } ) ;
219246 return { // if an exception is caught deny by default
220247 decision : Response_Decision . DENY ,
@@ -296,7 +323,10 @@ export class AccessController {
296323 let obligations : Attribute [ ] = [ ] ;
297324 for ( let [ , value ] of this . policySets ) {
298325 let pSet : PolicySetRQ ;
299- if ( _ . isEmpty ( value . target ) || await this . targetMatches ( value . target , request , 'whatIsAllowed' , obligations ) ) {
326+ if (
327+ _ . isEmpty ( value . target )
328+ || await this . targetMatches ( value . target , request , 'whatIsAllowed' , obligations )
329+ ) {
300330 pSet = _ . merge ( { } , { combining_algorithm : value . combining_algorithm } , _ . pick ( value , [ 'id' , 'target' , 'effect' ] ) ) as any ;
301331 pSet . policies = [ ] ;
302332
0 commit comments