@@ -16,6 +16,7 @@ import { ManagementEnforcer } from './managementEnforcer';
16
16
import { Model , newModel } from './model' ;
17
17
import { Adapter , FileAdapter , StringAdapter } from './persist' ;
18
18
import { getLogger } from './log' ;
19
+ import { arrayRemoveDuplicates } from './util' ;
19
20
20
21
/**
21
22
* Enforcer = ManagementEnforcer + RBAC API.
@@ -320,6 +321,32 @@ export class Enforcer extends ManagementEnforcer {
320
321
321
322
return res ;
322
323
}
324
+
325
+ /**
326
+ * getImplicitUsersForPermission gets implicit users for a permission.
327
+ * For example:
328
+ * p, admin, data1, read
329
+ * p, bob, data1, read
330
+ * g, alice, admin
331
+ *
332
+ * getImplicitUsersForPermission("data1", "read") will get: ["alice", "bob"].
333
+ * Note: only users will be returned, roles (2nd arg in "g") will be excluded.
334
+ */
335
+ public async getImplicitUsersForPermission ( ...permission : string [ ] ) : Promise < string [ ] > {
336
+ const res : string [ ] = [ ] ;
337
+ const policySubjects = await this . getAllSubjects ( ) ;
338
+ const subjects = arrayRemoveDuplicates ( [ ...policySubjects , ...this . model . getValuesForFieldInPolicyAllTypes ( 'g' , 0 ) ] ) ;
339
+ const inherits = this . model . getValuesForFieldInPolicyAllTypes ( 'g' , 1 ) ;
340
+
341
+ for ( const user of subjects ) {
342
+ const allowed = await this . enforce ( user , ...permission ) ;
343
+ if ( allowed ) {
344
+ res . push ( user ) ;
345
+ }
346
+ }
347
+
348
+ return res . filter ( n => ! inherits . some ( m => n === m ) ) ;
349
+ }
323
350
}
324
351
325
352
export async function newEnforcerWithClass < T extends Enforcer > ( enforcer : new ( ) => T , ...params : any [ ] ) : Promise < T > {
0 commit comments