Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(readme): correct docs about hasPermissionForUser #165

Merged
merged 1 commit into from
Mar 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ Only when the user is granted both permissions of reading any user address and r
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:

```typescript
import { Controller, Get, UnauthorizedException } from '@nestjs/common';
import { Controller, Get, UnauthorizedException, Req } from '@nestjs/common';
import {
AuthZGuard,
AuthZRBACService,
Expand All @@ -169,14 +169,20 @@ export class AppController {
constructor(private readonly rbacSrv: AuthZRBACService) {}

@Get('users')
async findAllUsers() {
const isPermitted = await this.rbacSrv.hasPermissionForUser();
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.rbacSrv.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
// ...
}
}
```
Expand All @@ -189,9 +195,3 @@ For more detailed information, checkout the working example in
## License

This project is licensed under the MIT license.

## Contact

If you have any issues or feature requests, contact me. PR is welcomed.

- dreamdeviloo@163.com
Loading