nest-access-control 3.2.0 → 3.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -164,7 +164,7 @@ So let's discuss what's going on!
|
|
|
164
164
|
First we introduced two new decorators, actually they are three, but let's see what they can do:
|
|
165
165
|
|
|
166
166
|
- `@UseRoles({ ... })`: this the most used decorator, it define what roles should user have to access this route.
|
|
167
|
-
|
|
167
|
+
Apply it to a controller to require permissions on every handler, or to a handler to add requirements for that route. When both are present, all requirements **must** be satisfied; handler metadata does not override the controller.
|
|
168
168
|
The structure of the role is really simple, for example, here we define what resources we have, and the **ACGuard\*** - _Damn, it's a good name for a guard :joy:_ - will check for the user roles, then if the user roles have the permissions to access this resource the guard will return `true`, else it will throw a `ForbiddenException`.
|
|
169
169
|
For more information about the structure of roles see `roles.interface.ts` file or read the original [documentation](https://onury.io/accesscontrol/) form `accesscontrol` library [here](https://onury.io/accesscontrol/?api=ac#AccessControl~IQueryInfo).
|
|
170
170
|
> \*note: for those who are asking what ACGuard stands for, it of course stands for Access Control Guard :smile:
|
package/access-control.guard.js
CHANGED
|
@@ -30,18 +30,17 @@ let ACGuard = class ACGuard {
|
|
|
30
30
|
return user['roles'];
|
|
31
31
|
}
|
|
32
32
|
async canActivate(context) {
|
|
33
|
-
const
|
|
34
|
-
|
|
33
|
+
const controllerRoles = this.reflector.get('roles', context.getClass());
|
|
34
|
+
const handlerRoles = this.reflector.get('roles', context.getHandler());
|
|
35
|
+
if (!controllerRoles && !handlerRoles) {
|
|
35
36
|
return true;
|
|
36
37
|
}
|
|
37
38
|
const userRoles = await this.getUserRoles(context);
|
|
38
|
-
|
|
39
|
-
const queryInfo = role;
|
|
40
|
-
queryInfo.role = userRoles;
|
|
39
|
+
return [...(controllerRoles || []), ...(handlerRoles || [])].every((role) => {
|
|
40
|
+
const queryInfo = Object.assign(Object.assign({}, role), { role: userRoles });
|
|
41
41
|
const permission = this.roleBuilder.permission(queryInfo);
|
|
42
42
|
return permission.granted;
|
|
43
43
|
});
|
|
44
|
-
return hasRoles;
|
|
45
44
|
}
|
|
46
45
|
};
|
|
47
46
|
exports.ACGuard = ACGuard;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Role } from '../role.interface';
|
|
2
2
|
/**
|
|
3
|
-
* Define
|
|
4
|
-
*
|
|
3
|
+
* Define permissions required for a controller or handler.
|
|
4
|
+
* All controller and handler requirements must be satisfied.
|
|
5
5
|
*/
|
|
6
6
|
export declare const UseRoles: (...roles: Role[]) => import("@nestjs/common").CustomDecorator<string>;
|
|
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.UseRoles = void 0;
|
|
4
4
|
const common_1 = require("@nestjs/common");
|
|
5
5
|
/**
|
|
6
|
-
* Define
|
|
7
|
-
*
|
|
6
|
+
* Define permissions required for a controller or handler.
|
|
7
|
+
* All controller and handler requirements must be satisfied.
|
|
8
8
|
*/
|
|
9
9
|
const UseRoles = (...roles) => (0, common_1.SetMetadata)('roles', roles);
|
|
10
10
|
exports.UseRoles = UseRoles;
|