plac-micro-common 1.2.0 → 1.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/dist/http/constants/index.d.ts +1 -0
- package/dist/http/constants/index.js +4 -0
- package/dist/http/decorators/index.d.ts +1 -0
- package/dist/http/decorators/index.js +1 -0
- package/dist/http/decorators/require_permission.decorator.d.ts +1 -0
- package/dist/http/decorators/require_permission.decorator.js +7 -0
- package/dist/http/guards/index.d.ts +1 -0
- package/dist/http/guards/index.js +1 -0
- package/dist/http/guards/permission.guard.d.ts +7 -0
- package/dist/http/guards/permission.guard.js +44 -0
- package/dist/types/permission.type.d.ts +14 -6
- package/dist/types/permission.type.js +15 -8
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const REQUIRE_PERMISSIONS_KEY = "require_permissions";
|
|
@@ -17,3 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./client_ctx.decorator"), exports);
|
|
18
18
|
__exportStar(require("./current_app.decorator"), exports);
|
|
19
19
|
__exportStar(require("./current_app_client.decorator"), exports);
|
|
20
|
+
__exportStar(require("./require_permission.decorator"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const RequirePermissions: (...permissions: string[]) => import("@nestjs/common").CustomDecorator<string>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RequirePermissions = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const constants_1 = require("../constants");
|
|
6
|
+
const RequirePermissions = (...permissions) => (0, common_1.SetMetadata)(constants_1.REQUIRE_PERMISSIONS_KEY, permissions);
|
|
7
|
+
exports.RequirePermissions = RequirePermissions;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { CanActivate, ExecutionContext } from "@nestjs/common";
|
|
2
|
+
import { Reflector } from "@nestjs/core";
|
|
3
|
+
export declare class PermissionsGuard implements CanActivate {
|
|
4
|
+
private readonly reflector;
|
|
5
|
+
constructor(reflector: Reflector);
|
|
6
|
+
canActivate(context: ExecutionContext): boolean;
|
|
7
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.PermissionsGuard = void 0;
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
const core_1 = require("@nestjs/core");
|
|
15
|
+
const constants_1 = require("../constants");
|
|
16
|
+
let PermissionsGuard = class PermissionsGuard {
|
|
17
|
+
constructor(reflector) {
|
|
18
|
+
this.reflector = reflector;
|
|
19
|
+
}
|
|
20
|
+
canActivate(context) {
|
|
21
|
+
const required = this.reflector.getAllAndOverride(constants_1.REQUIRE_PERMISSIONS_KEY, [context.getHandler(), context.getClass()]);
|
|
22
|
+
// No permissions required => allow
|
|
23
|
+
if (!required || required.length === 0)
|
|
24
|
+
return true;
|
|
25
|
+
const req = context.switchToHttp().getRequest();
|
|
26
|
+
// Must have req.user set by JwtAuthGuard (passport-jwt or custom)
|
|
27
|
+
const user = req?.user;
|
|
28
|
+
if (!user)
|
|
29
|
+
throw new common_1.UnauthorizedException("Missing auth user");
|
|
30
|
+
const userPerms = Array.isArray(user.permissions)
|
|
31
|
+
? user.permissions
|
|
32
|
+
: [];
|
|
33
|
+
// require ALL permissions by default
|
|
34
|
+
const hasAll = required.every((p) => userPerms.includes(p));
|
|
35
|
+
if (!hasAll)
|
|
36
|
+
throw new common_1.ForbiddenException("Insufficient permissions");
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
exports.PermissionsGuard = PermissionsGuard;
|
|
41
|
+
exports.PermissionsGuard = PermissionsGuard = __decorate([
|
|
42
|
+
(0, common_1.Injectable)(),
|
|
43
|
+
__metadata("design:paramtypes", [core_1.Reflector])
|
|
44
|
+
], PermissionsGuard);
|
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
export declare enum PermissionAction {
|
|
2
|
+
Approve = "approve",
|
|
2
3
|
Create = "create",
|
|
4
|
+
Delete = "delete",
|
|
5
|
+
Export = "export",
|
|
3
6
|
Read = "read",
|
|
4
|
-
Update = "update"
|
|
5
|
-
Delete = "delete"
|
|
7
|
+
Update = "update"
|
|
6
8
|
}
|
|
7
|
-
export declare
|
|
8
|
-
Permission
|
|
9
|
-
Quotation
|
|
10
|
-
User
|
|
9
|
+
export declare const PermissionResource: {
|
|
10
|
+
readonly Permission: "permission";
|
|
11
|
+
readonly Quotation: "quotation";
|
|
12
|
+
readonly User: "user";
|
|
13
|
+
};
|
|
14
|
+
export type PermissionResource = (typeof PermissionResource)[keyof typeof PermissionResource];
|
|
15
|
+
export declare enum PermissionModule {
|
|
16
|
+
Admin = "admin",
|
|
17
|
+
Claim = "claim",
|
|
18
|
+
Policy = "policy"
|
|
11
19
|
}
|
|
@@ -1,16 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PermissionResource = exports.PermissionAction = void 0;
|
|
3
|
+
exports.PermissionModule = exports.PermissionResource = exports.PermissionAction = void 0;
|
|
4
4
|
var PermissionAction;
|
|
5
5
|
(function (PermissionAction) {
|
|
6
|
+
PermissionAction["Approve"] = "approve";
|
|
6
7
|
PermissionAction["Create"] = "create";
|
|
8
|
+
PermissionAction["Delete"] = "delete";
|
|
9
|
+
PermissionAction["Export"] = "export";
|
|
7
10
|
PermissionAction["Read"] = "read";
|
|
8
11
|
PermissionAction["Update"] = "update";
|
|
9
|
-
PermissionAction["Delete"] = "delete";
|
|
10
12
|
})(PermissionAction || (exports.PermissionAction = PermissionAction = {}));
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
exports.PermissionResource = {
|
|
14
|
+
Permission: "permission",
|
|
15
|
+
Quotation: "quotation",
|
|
16
|
+
User: "user",
|
|
17
|
+
};
|
|
18
|
+
var PermissionModule;
|
|
19
|
+
(function (PermissionModule) {
|
|
20
|
+
PermissionModule["Admin"] = "admin";
|
|
21
|
+
PermissionModule["Claim"] = "claim";
|
|
22
|
+
PermissionModule["Policy"] = "policy";
|
|
23
|
+
})(PermissionModule || (exports.PermissionModule = PermissionModule = {}));
|