@saihu/common 1.4.4 → 1.4.6
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/auth/index.d.ts +1 -0
- package/dist/auth/index.d.ts.map +1 -1
- package/dist/auth/index.js +1 -0
- package/dist/auth/redis-permission.evaluator.d.ts +10 -0
- package/dist/auth/redis-permission.evaluator.d.ts.map +1 -0
- package/dist/auth/redis-permission.evaluator.js +36 -0
- package/dist/saihu-common.module.d.ts.map +1 -1
- package/dist/saihu-common.module.js +23 -6
- package/dist/services/redis.service.d.ts.map +1 -1
- package/dist/services/redis.service.js +6 -0
- package/package.json +1 -1
package/dist/auth/index.d.ts
CHANGED
package/dist/auth/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC;AACjC,cAAc,8BAA8B,CAAC"}
|
package/dist/auth/index.js
CHANGED
|
@@ -20,3 +20,4 @@ __exportStar(require("./auth.types"), exports);
|
|
|
20
20
|
__exportStar(require("./rbac.interface"), exports);
|
|
21
21
|
__exportStar(require("./rbac.guard"), exports);
|
|
22
22
|
__exportStar(require("./user.decorator"), exports);
|
|
23
|
+
__exportStar(require("./redis-permission.evaluator"), exports);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { PermissionEvaluator } from './rbac.interface';
|
|
2
|
+
import { RedisService } from '../interfaces/service.interface';
|
|
3
|
+
export declare class RedisPermissionEvaluator implements PermissionEvaluator {
|
|
4
|
+
private readonly redis;
|
|
5
|
+
constructor(redis: RedisService);
|
|
6
|
+
hasPerms(user: {
|
|
7
|
+
sub: number;
|
|
8
|
+
}, required: string[]): Promise<boolean>;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=redis-permission.evaluator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redis-permission.evaluator.d.ts","sourceRoot":"","sources":["../../src/auth/redis-permission.evaluator.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAEvD,OAAO,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AAE/D,qBACa,wBAAyB,YAAW,mBAAmB;IAC/B,OAAO,CAAC,QAAQ,CAAC,KAAK;gBAAL,KAAK,EAAE,YAAY;IAEjE,QAAQ,CAAC,IAAI,EAAE;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC;CAQ5E"}
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.RedisPermissionEvaluator = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const common_consts_1 = require("../consts/common.consts");
|
|
18
|
+
let RedisPermissionEvaluator = class RedisPermissionEvaluator {
|
|
19
|
+
constructor(redis) {
|
|
20
|
+
this.redis = redis;
|
|
21
|
+
}
|
|
22
|
+
async hasPerms(user, required) {
|
|
23
|
+
if (!required.length)
|
|
24
|
+
return true;
|
|
25
|
+
const cached = await this.redis.get(`auth:perms:staff:${user.sub}`);
|
|
26
|
+
if (!cached)
|
|
27
|
+
return false;
|
|
28
|
+
return required.every((p) => cached.includes(p));
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
exports.RedisPermissionEvaluator = RedisPermissionEvaluator;
|
|
32
|
+
exports.RedisPermissionEvaluator = RedisPermissionEvaluator = __decorate([
|
|
33
|
+
(0, common_1.Injectable)(),
|
|
34
|
+
__param(0, (0, common_1.Inject)(common_consts_1.REDIS_SERVICE)),
|
|
35
|
+
__metadata("design:paramtypes", [Object])
|
|
36
|
+
], RedisPermissionEvaluator);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"saihu-common.module.d.ts","sourceRoot":"","sources":["../src/saihu-common.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,aAAa,EAAY,MAAM,gBAAgB,CAAC;AAEjE,OAAO,EACL,wBAAwB,EACxB,mBAAmB,EACpB,MAAM,gCAAgC,CAAC;
|
|
1
|
+
{"version":3,"file":"saihu-common.module.d.ts","sourceRoot":"","sources":["../src/saihu-common.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,aAAa,EAAY,MAAM,gBAAgB,CAAC;AAEjE,OAAO,EACL,wBAAwB,EACxB,mBAAmB,EACpB,MAAM,gCAAgC,CAAC;AAaxC,qBACa,iBAAiB;IAC5B,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,mBAAmB,GAAG,aAAa;IAqC3D,MAAM,CAAC,YAAY,CAAC,OAAO,EAAE,wBAAwB,GAAG,aAAa;IAwCrE,MAAM,CAAC,sBAAsB,IAAI,aAAa;CAa/C"}
|
|
@@ -15,6 +15,8 @@ const rabbitmq_service_1 = require("./services/rabbitmq.service");
|
|
|
15
15
|
const core_1 = require("@nestjs/core");
|
|
16
16
|
const interceptors_1 = require("./interceptors");
|
|
17
17
|
const config_1 = require("@nestjs/config");
|
|
18
|
+
const redis_permission_evaluator_1 = require("./auth/redis-permission.evaluator");
|
|
19
|
+
const rbac_interface_1 = require("./auth/rbac.interface");
|
|
18
20
|
let SaihuCommonModule = SaihuCommonModule_1 = class SaihuCommonModule {
|
|
19
21
|
static forRoot(options) {
|
|
20
22
|
const providers = [
|
|
@@ -33,15 +35,23 @@ let SaihuCommonModule = SaihuCommonModule_1 = class SaihuCommonModule {
|
|
|
33
35
|
// Add OperlogInterceptor as a regular provider so it can be used manually
|
|
34
36
|
interceptors_1.OperlogInterceptor,
|
|
35
37
|
];
|
|
38
|
+
const exports = [
|
|
39
|
+
common_consts_1.COMMON_MODULE_OPTIONS,
|
|
40
|
+
common_consts_1.REDIS_SERVICE,
|
|
41
|
+
common_consts_1.RABBITMQ_SERVICE,
|
|
42
|
+
interceptors_1.OperlogInterceptor,
|
|
43
|
+
];
|
|
44
|
+
if (options.usePerms) {
|
|
45
|
+
providers.push({
|
|
46
|
+
provide: rbac_interface_1.PERMISSION_EVALUATOR,
|
|
47
|
+
useClass: redis_permission_evaluator_1.RedisPermissionEvaluator,
|
|
48
|
+
});
|
|
49
|
+
exports.push(rbac_interface_1.PERMISSION_EVALUATOR);
|
|
50
|
+
}
|
|
36
51
|
return {
|
|
37
52
|
module: SaihuCommonModule_1,
|
|
38
53
|
providers,
|
|
39
|
-
exports
|
|
40
|
-
common_consts_1.COMMON_MODULE_OPTIONS,
|
|
41
|
-
common_consts_1.REDIS_SERVICE,
|
|
42
|
-
common_consts_1.RABBITMQ_SERVICE,
|
|
43
|
-
interceptors_1.OperlogInterceptor,
|
|
44
|
-
],
|
|
54
|
+
exports,
|
|
45
55
|
global: true,
|
|
46
56
|
};
|
|
47
57
|
}
|
|
@@ -62,6 +72,12 @@ let SaihuCommonModule = SaihuCommonModule_1 = class SaihuCommonModule {
|
|
|
62
72
|
},
|
|
63
73
|
// Add OperlogInterceptor as a regular provider so it can be used manually
|
|
64
74
|
interceptors_1.OperlogInterceptor,
|
|
75
|
+
// Always register so downstream services get RedisPermissionEvaluator;
|
|
76
|
+
// with the isEnabled guard, it returns null/false harmlessly when Redis is off
|
|
77
|
+
{
|
|
78
|
+
provide: rbac_interface_1.PERMISSION_EVALUATOR,
|
|
79
|
+
useClass: redis_permission_evaluator_1.RedisPermissionEvaluator,
|
|
80
|
+
},
|
|
65
81
|
];
|
|
66
82
|
return {
|
|
67
83
|
module: SaihuCommonModule_1,
|
|
@@ -72,6 +88,7 @@ let SaihuCommonModule = SaihuCommonModule_1 = class SaihuCommonModule {
|
|
|
72
88
|
common_consts_1.REDIS_SERVICE,
|
|
73
89
|
common_consts_1.RABBITMQ_SERVICE,
|
|
74
90
|
interceptors_1.OperlogInterceptor,
|
|
91
|
+
rbac_interface_1.PERMISSION_EVALUATOR,
|
|
75
92
|
],
|
|
76
93
|
global: true, // 设置为全局模块
|
|
77
94
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redis.service.d.ts","sourceRoot":"","sources":["../../src/services/redis.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,YAAY,EACZ,eAAe,EAGhB,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AACtE,OAAO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I,CAAC;IAKrC,eAAe;CAMhB"}
|
|
@@ -66,6 +66,8 @@ let RedisServiceImpl = RedisServiceImpl_1 = class RedisServiceImpl {
|
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
async get(key) {
|
|
69
|
+
if (!this.isEnabled || !this.redisClient)
|
|
70
|
+
return null;
|
|
69
71
|
const value = await this.redisClient.get(key);
|
|
70
72
|
if (!value) {
|
|
71
73
|
return null;
|
|
@@ -78,6 +80,8 @@ let RedisServiceImpl = RedisServiceImpl_1 = class RedisServiceImpl {
|
|
|
78
80
|
}
|
|
79
81
|
}
|
|
80
82
|
async set(key, value, ttl) {
|
|
83
|
+
if (!this.isEnabled || !this.redisClient)
|
|
84
|
+
return;
|
|
81
85
|
const serializedValue = typeof value === 'object' ? JSON.stringify(value) : String(value);
|
|
82
86
|
if (ttl) {
|
|
83
87
|
await this.redisClient.setex(key, ttl, serializedValue);
|
|
@@ -87,6 +91,8 @@ let RedisServiceImpl = RedisServiceImpl_1 = class RedisServiceImpl {
|
|
|
87
91
|
}
|
|
88
92
|
}
|
|
89
93
|
async del(key) {
|
|
94
|
+
if (!this.isEnabled || !this.redisClient)
|
|
95
|
+
return;
|
|
90
96
|
await this.redisClient.del(key);
|
|
91
97
|
}
|
|
92
98
|
onModuleDestroy() {
|