@@ -294,11 +294,43 @@ export class Enforcer extends ManagementEnforcer {
294294 * getPermissionsForUser gets permissions for a user or role.
295295 *
296296 * @param user the user.
297+ * @param domain the domain, optional. When given, only the permissions of that domain are returned.
297298 * @return the permissions, a permission is usually like (obj, act). It is actually the rule without the subject.
298299 */
299- public async getPermissionsForUser ( user : string ) : Promise < string [ ] [ ] > {
300- const subIndex = this . getFieldIndex ( 'p' , FieldIndex . Subject ) ;
301- return this . getFilteredPolicy ( subIndex , user ) ;
300+ public async getPermissionsForUser ( user : string , ...domain : string [ ] ) : Promise < string [ ] [ ] > {
301+ return this . getNamedPermissionsForUser ( 'p' , user , ...domain ) ;
302+ }
303+
304+ /**
305+ * getNamedPermissionsForUser gets permissions for a user or role by the named policy.
306+ *
307+ * @param ptype the policy type, can be "p", "p2", "p3", ..
308+ * @param user the user.
309+ * @param domain the domain, optional. When given, only the permissions of that domain are returned.
310+ * @return the permissions, a permission is usually like (obj, act). It is actually the rule without the subject.
311+ */
312+ public async getNamedPermissionsForUser ( ptype : string , user : string , ...domain : string [ ] ) : Promise < string [ ] [ ] > {
313+ const subIndex = this . getFieldIndex ( ptype , FieldIndex . Subject ) ;
314+ if ( subIndex === - 1 ) {
315+ return [ ] ;
316+ }
317+ if ( domain . length === 0 ) {
318+ return this . getFilteredNamedPolicy ( ptype , subIndex , user ) ;
319+ }
320+
321+ // The domain is not necessarily the token right after the subject, so it has to be
322+ // looked up in the model instead of being assumed to sit at a fixed index.
323+ const domIndex = this . getFieldIndex ( ptype , FieldIndex . Domain ) ;
324+ if ( domIndex === - 1 ) {
325+ return this . getFilteredNamedPolicy ( ptype , subIndex , user ) ;
326+ }
327+
328+ const start = Math . min ( subIndex , domIndex ) ;
329+ // "" means not to match that field.
330+ const fieldValues = new Array < string > ( Math . abs ( subIndex - domIndex ) + 1 ) . fill ( '' ) ;
331+ fieldValues [ subIndex - start ] = user ;
332+ fieldValues [ domIndex - start ] = domain [ 0 ] ;
333+ return this . getFilteredNamedPolicy ( ptype , start , ...fieldValues ) ;
302334 }
303335
304336 /**
@@ -357,16 +389,10 @@ export class Enforcer extends ManagementEnforcer {
357389 const roles = await this . getImplicitRolesForUser ( user , ...domain ) ;
358390 roles . unshift ( user ) ;
359391 const res : string [ ] [ ] = [ ] ;
360- const withDomain = domain && domain . length !== 0 ;
361392
362393 for ( const n of roles ) {
363- if ( withDomain ) {
364- const p = await this . getFilteredPolicy ( 0 , n , ...domain ) ;
365- res . push ( ...p ) ;
366- } else {
367- const p = await this . getPermissionsForUser ( n ) ;
368- res . push ( ...p ) ;
369- }
394+ const p = await this . getPermissionsForUser ( n , ...domain ) ;
395+ res . push ( ...p ) ;
370396 }
371397
372398 return res ;
0 commit comments