Skip to content

Commit

Permalink
feat: Unify RBAC and Management services into single service (#171)
Browse files Browse the repository at this point in the history
* feat/unified-services

* feat: Unify RBAC/Management services into one service

* adds getImplicitResourcesForUser

* Fixes formatting
  • Loading branch information
Dallin343 authored Aug 6, 2024
1 parent e1e6cce commit 2c9fbb3
Show file tree
Hide file tree
Showing 8 changed files with 3,528 additions and 1,625 deletions.
44 changes: 42 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,49 @@ You can define multiple permissions, but only when all of them satisfied, could
Only when the user is granted both permissions of reading any user address and reading any roles, could he/she access the route.
#### Using `AuthzRBACService` or `AuthzManagementService`
#### Using `AuthZService`
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:
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:
```typescript
import { Controller, Get, UnauthorizedException, Req } from '@nestjs/common';
import {
AuthZGuard,
AuthZService,
AuthActionVerb,
AuthPossession,
UsePermissions
} from 'nest-authz';

@Controller()
export class AppController {
constructor(private readonly authzSrv: AuthZService) {}

@Get('users')
async findAllUsers(@Req() request: Request) {
let username = request.user['username'];
// If there is a policy `p, root, user, read:any` in policy.csv
// then user `root` can do this operation

// Using string literals for simplicity.
const isPermitted = await this.authzSrv.hasPermissionForUser(username, "user", "read:any");
if (!isPermitted) {
throw new UnauthorizedException(
'You are not authorized to read users list'
);
}
// A user can not reach this point if he/she is not granted for permission read users
// ...
}
}
```
#### (Deprecated) Using `AuthZRBACService` or `AuthZManagementService`
> The functionality provided by `AuthZRBACService` and `AuthZManagementService` has been unified in `AuthZService`, so these services will be removed in a later release.
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:
```typescript
import { Controller, Get, UnauthorizedException, Req } from '@nestjs/common';
Expand Down
12 changes: 9 additions & 3 deletions src/authz.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import * as casbin from 'casbin';
import { AuthZModuleOptions } from './interfaces';
import { AuthZGuard } from './authz.guard';
import { AUTHZ_MODULE_OPTIONS, AUTHZ_ENFORCER } from './authz.constants';
import { AuthZRBACService, AuthZManagementService } from './services';
import {
AuthZRBACService,
AuthZManagementService,
AuthZService
} from './services';

@Global()
@Module({
Expand Down Expand Up @@ -57,15 +61,17 @@ export class AuthZModule {
enforcerProvider,
AuthZGuard,
AuthZRBACService,
AuthZManagementService
AuthZManagementService,
AuthZService
],
imports: importsModule,
exports: [
moduleOptionsProvider,
enforcerProvider,
AuthZGuard,
AuthZRBACService,
AuthZManagementService
AuthZManagementService,
AuthZService
]
};
}
Expand Down
Loading

0 comments on commit 2c9fbb3

Please sign in to comment.