Skip to content

Commit 2c9fbb3

Browse files
authored
feat: Unify RBAC and Management services into single service (#171)
* feat/unified-services * feat: Unify RBAC/Management services into one service * adds getImplicitResourcesForUser * Fixes formatting
1 parent e1e6cce commit 2c9fbb3

File tree

8 files changed

+3528
-1625
lines changed

8 files changed

+3528
-1625
lines changed

README.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,49 @@ You can define multiple permissions, but only when all of them satisfied, could
172172
173173
Only when the user is granted both permissions of reading any user address and reading any roles, could he/she access the route.
174174
175-
#### Using `AuthzRBACService` or `AuthzManagementService`
175+
#### Using `AuthZService`
176176
177-
While the `@UsePermissions` decorator is good enough for most cases, there are situations where we may want to check for a permission in a method's body. We can inject and use `AuthzRBACService` or `AuthzManagementService` which are wrappers of casbin api for that as shown in the example below:
177+
While the `@UsePermissions` decorator is good enough for most cases, there are situations where we may want to check for a permission in a method's body. We can inject and use `AuthZService` which is a wrapper of the Casbin RBAC + Management API for that as shown in the example below:
178+
179+
```typescript
180+
import { Controller, Get, UnauthorizedException, Req } from '@nestjs/common';
181+
import {
182+
AuthZGuard,
183+
AuthZService,
184+
AuthActionVerb,
185+
AuthPossession,
186+
UsePermissions
187+
} from 'nest-authz';
188+
189+
@Controller()
190+
export class AppController {
191+
constructor(private readonly authzSrv: AuthZService) {}
192+
193+
@Get('users')
194+
async findAllUsers(@Req() request: Request) {
195+
let username = request.user['username'];
196+
// If there is a policy `p, root, user, read:any` in policy.csv
197+
// then user `root` can do this operation
198+
199+
// Using string literals for simplicity.
200+
const isPermitted = await this.authzSrv.hasPermissionForUser(username, "user", "read:any");
201+
if (!isPermitted) {
202+
throw new UnauthorizedException(
203+
'You are not authorized to read users list'
204+
);
205+
}
206+
// A user can not reach this point if he/she is not granted for permission read users
207+
// ...
208+
}
209+
}
210+
```
211+
212+
213+
#### (Deprecated) Using `AuthZRBACService` or `AuthZManagementService`
214+
215+
> The functionality provided by `AuthZRBACService` and `AuthZManagementService` has been unified in `AuthZService`, so these services will be removed in a later release.
216+
217+
We can inject and use `AuthZRBACService` or `AuthZManagementService` which are wrappers of the Casbin RBAC and Management APIs, respectively, as shown in the example below:
178218
179219
```typescript
180220
import { Controller, Get, UnauthorizedException, Req } from '@nestjs/common';

src/authz.module.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import * as casbin from 'casbin';
44
import { AuthZModuleOptions } from './interfaces';
55
import { AuthZGuard } from './authz.guard';
66
import { AUTHZ_MODULE_OPTIONS, AUTHZ_ENFORCER } from './authz.constants';
7-
import { AuthZRBACService, AuthZManagementService } from './services';
7+
import {
8+
AuthZRBACService,
9+
AuthZManagementService,
10+
AuthZService
11+
} from './services';
812

913
@Global()
1014
@Module({
@@ -57,15 +61,17 @@ export class AuthZModule {
5761
enforcerProvider,
5862
AuthZGuard,
5963
AuthZRBACService,
60-
AuthZManagementService
64+
AuthZManagementService,
65+
AuthZService
6166
],
6267
imports: importsModule,
6368
exports: [
6469
moduleOptionsProvider,
6570
enforcerProvider,
6671
AuthZGuard,
6772
AuthZRBACService,
68-
AuthZManagementService
73+
AuthZManagementService,
74+
AuthZService
6975
]
7076
};
7177
}

0 commit comments

Comments
 (0)