vona-module-a-permission 5.0.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/LICENSE +21 -0
- package/dist/.metadata/index.d.ts +121 -0
- package/dist/.metadata/this.d.ts +2 -0
- package/dist/bean/bean.permission.d.ts +10 -0
- package/dist/bean/event.retrievePermissions.d.ts +8 -0
- package/dist/bean/summerCache.permission.d.ts +7 -0
- package/dist/config/config.d.ts +6 -0
- package/dist/dto/permissions.d.ts +10 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +122 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016-present Vona
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { TypeSymbolKeyFieldsMore } from 'vona-module-a-orm';
|
|
2
|
+
import type { TypeEntityOptionsFields } from 'vona-module-a-openapi';
|
|
3
|
+
/** bean: begin */
|
|
4
|
+
export * from '../bean/bean.permission.ts';
|
|
5
|
+
import 'vona';
|
|
6
|
+
declare module 'vona' {
|
|
7
|
+
}
|
|
8
|
+
declare module 'vona-module-a-permission' {
|
|
9
|
+
interface BeanPermission {
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/** bean: end */
|
|
13
|
+
/** bean: begin */
|
|
14
|
+
import type { BeanPermission } from '../bean/bean.permission.ts';
|
|
15
|
+
import 'vona';
|
|
16
|
+
declare module 'vona' {
|
|
17
|
+
interface IBeanRecordGlobal {
|
|
18
|
+
'permission': BeanPermission;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** bean: end */
|
|
22
|
+
/** event: begin */
|
|
23
|
+
export * from '../bean/event.retrievePermissions.ts';
|
|
24
|
+
import 'vona';
|
|
25
|
+
declare module 'vona' {
|
|
26
|
+
}
|
|
27
|
+
declare module 'vona-module-a-permission' {
|
|
28
|
+
interface EventRetrievePermissions {
|
|
29
|
+
}
|
|
30
|
+
interface EventRetrievePermissions {
|
|
31
|
+
get $beanFullName(): 'a-permission.event.retrievePermissions';
|
|
32
|
+
get $onionName(): 'a-permission:retrievePermissions';
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/** event: end */
|
|
36
|
+
/** event: begin */
|
|
37
|
+
import type { EventRetrievePermissions } from '../bean/event.retrievePermissions.ts';
|
|
38
|
+
export interface IModuleEvent {
|
|
39
|
+
'retrievePermissions': EventRetrievePermissions;
|
|
40
|
+
}
|
|
41
|
+
/** event: end */
|
|
42
|
+
/** event: begin */
|
|
43
|
+
import type { TypeEventRetrievePermissionsData, TypeEventRetrievePermissionsResult } from '../bean/event.retrievePermissions.ts';
|
|
44
|
+
import type { EventOn } from 'vona-module-a-event';
|
|
45
|
+
declare module 'vona-module-a-event' {
|
|
46
|
+
interface IEventRecord {
|
|
47
|
+
'a-permission:retrievePermissions': EventOn<TypeEventRetrievePermissionsData, TypeEventRetrievePermissionsResult>;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** event: end */
|
|
51
|
+
/** summerCache: begin */
|
|
52
|
+
export * from '../bean/summerCache.permission.ts';
|
|
53
|
+
import { type IDecoratorSummerCacheOptions } from 'vona-module-a-summer';
|
|
54
|
+
declare module 'vona-module-a-summer' {
|
|
55
|
+
interface ISummerCacheRecord {
|
|
56
|
+
'a-permission:permission': IDecoratorSummerCacheOptions;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
declare module 'vona-module-a-permission' {
|
|
60
|
+
interface SummerCachePermission {
|
|
61
|
+
}
|
|
62
|
+
interface SummerCachePermission {
|
|
63
|
+
get $beanFullName(): 'a-permission.summerCache.permission';
|
|
64
|
+
get $onionName(): 'a-permission:permission';
|
|
65
|
+
get $onionOptions(): IDecoratorSummerCacheOptions;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/** summerCache: end */
|
|
69
|
+
/** summerCache: begin */
|
|
70
|
+
import type { SummerCachePermission } from '../bean/summerCache.permission.ts';
|
|
71
|
+
export interface IModuleSummerCache {
|
|
72
|
+
'permission': SummerCachePermission;
|
|
73
|
+
}
|
|
74
|
+
/** summerCache: end */
|
|
75
|
+
/** dto: begin */
|
|
76
|
+
export * from '../dto/permissions.tsx';
|
|
77
|
+
import type { IDtoOptionsPermissions } from '../dto/permissions.tsx';
|
|
78
|
+
import 'vona-module-a-web';
|
|
79
|
+
declare module 'vona-module-a-web' {
|
|
80
|
+
interface IDtoRecord {
|
|
81
|
+
'a-permission:permissions': IDtoOptionsPermissions;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
declare module 'vona-module-a-permission' {
|
|
85
|
+
}
|
|
86
|
+
/** dto: end */
|
|
87
|
+
/** dto: begin */
|
|
88
|
+
import type { DtoPermissions } from '../dto/permissions.tsx';
|
|
89
|
+
declare module 'vona-module-a-permission' {
|
|
90
|
+
interface IDtoOptionsPermissions {
|
|
91
|
+
fields?: TypeEntityOptionsFields<DtoPermissions, IDtoOptionsPermissions[TypeSymbolKeyFieldsMore]>;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** dto: end */
|
|
95
|
+
/** config: begin */
|
|
96
|
+
export * from '../config/config.ts';
|
|
97
|
+
import type { config } from '../config/config.ts';
|
|
98
|
+
/** config: end */
|
|
99
|
+
/** scope: begin */
|
|
100
|
+
import { BeanScopeBase, type BeanScopeUtil, type TypeModuleConfig } from 'vona';
|
|
101
|
+
export declare class ScopeModuleAPermission extends BeanScopeBase {
|
|
102
|
+
}
|
|
103
|
+
export interface ScopeModuleAPermission {
|
|
104
|
+
util: BeanScopeUtil;
|
|
105
|
+
config: TypeModuleConfig<typeof config>;
|
|
106
|
+
event: IModuleEvent;
|
|
107
|
+
summerCache: IModuleSummerCache;
|
|
108
|
+
}
|
|
109
|
+
import 'vona';
|
|
110
|
+
declare module 'vona' {
|
|
111
|
+
interface IBeanScopeRecord {
|
|
112
|
+
'a-permission': ScopeModuleAPermission;
|
|
113
|
+
}
|
|
114
|
+
interface IBeanScopeContainer {
|
|
115
|
+
permission: ScopeModuleAPermission;
|
|
116
|
+
}
|
|
117
|
+
interface IBeanScopeConfig {
|
|
118
|
+
'a-permission': ReturnType<typeof config>;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/** scope: end */
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ICachingActionKeyInfo } from 'vona-module-a-caching';
|
|
2
|
+
import type { IOpenapiPermissions, IResourceRecord } from 'vona-module-a-openapi';
|
|
3
|
+
import { BeanBase } from 'vona';
|
|
4
|
+
export declare class BeanPermission extends BeanBase {
|
|
5
|
+
clearAllCaches(): Promise<void>;
|
|
6
|
+
protected retrievePermissionsCacheKey(info: ICachingActionKeyInfo): string;
|
|
7
|
+
retrievePermissions(resource: keyof IResourceRecord): Promise<IOpenapiPermissions>;
|
|
8
|
+
getPermissionsDefault(resource: keyof IResourceRecord): Promise<IOpenapiPermissions>;
|
|
9
|
+
private _getPermissionOfAction;
|
|
10
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { IOpenapiPermissions, IResourceRecord } from 'vona-module-a-openapi';
|
|
2
|
+
import { BeanEventBase } from 'vona-module-a-event';
|
|
3
|
+
export interface TypeEventRetrievePermissionsData {
|
|
4
|
+
resource: keyof IResourceRecord;
|
|
5
|
+
}
|
|
6
|
+
export type TypeEventRetrievePermissionsResult = IOpenapiPermissions;
|
|
7
|
+
export declare class EventRetrievePermissions extends BeanEventBase<TypeEventRetrievePermissionsData, TypeEventRetrievePermissionsResult> {
|
|
8
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ISummerCacheGet, TSummerCacheActionOptions } from 'vona-module-a-summer';
|
|
2
|
+
import { BeanSummerCacheBase } from 'vona-module-a-summer';
|
|
3
|
+
export type TSummerCachePermissionKey = any;
|
|
4
|
+
export type TSummerCachePermissionData = any;
|
|
5
|
+
export declare class SummerCachePermission extends BeanSummerCacheBase<TSummerCachePermissionKey, TSummerCachePermissionData> implements ISummerCacheGet<TSummerCachePermissionKey, TSummerCachePermissionData> {
|
|
6
|
+
getNative(_key?: TSummerCachePermissionKey, _options?: TSummerCacheActionOptions<TSummerCachePermissionKey, TSummerCachePermissionData>): Promise<TSummerCachePermissionData | undefined>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { IOpenapiPermissionModeActionActions, IOpenapiPermissions } from 'vona-module-a-openapi';
|
|
2
|
+
import type { IRoleIdRecord, IRoleNameRecord } from 'vona-module-a-user';
|
|
3
|
+
import type { IDecoratorDtoOptions } from 'vona-module-a-web';
|
|
4
|
+
export interface IDtoOptionsPermissions extends IDecoratorDtoOptions {
|
|
5
|
+
}
|
|
6
|
+
export declare class DtoPermissions implements IOpenapiPermissions {
|
|
7
|
+
roleIds?: (keyof IRoleIdRecord)[];
|
|
8
|
+
roleNames?: (keyof IRoleNameRecord)[];
|
|
9
|
+
actions?: IOpenapiPermissionModeActionActions;
|
|
10
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './.metadata/index.ts';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { BeanInfo, BeanBase, beanFullNameFromOnionName, appResource, appMetadata, BeanScopeBase } from 'vona';
|
|
2
|
+
import { __decorate, __metadata } from 'tslib';
|
|
3
|
+
import { catchError } from '@cabloy/utils';
|
|
4
|
+
import { Bean, Scope } from 'vona-module-a-bean';
|
|
5
|
+
import { Caching } from 'vona-module-a-caching';
|
|
6
|
+
import { SymbolUseOnionOptionsRouteReal } from 'vona-module-a-onion';
|
|
7
|
+
import { recordResourceNameToRoutePath, composeGuards, Dto } from 'vona-module-a-web';
|
|
8
|
+
import { Event, BeanEventBase } from 'vona-module-a-event';
|
|
9
|
+
import { SummerCache, BeanSummerCacheBase } from 'vona-module-a-summer';
|
|
10
|
+
import { Api, v } from 'vona-module-a-openapiutils';
|
|
11
|
+
import z from 'zod';
|
|
12
|
+
|
|
13
|
+
let BeanPermission = class BeanPermission extends BeanBase {
|
|
14
|
+
async clearAllCaches() {
|
|
15
|
+
const cacheOpenapiSchema = this.bean.summer.cache(beanFullNameFromOnionName('a-permission:permission', 'summerCache'));
|
|
16
|
+
await cacheOpenapiSchema.clear();
|
|
17
|
+
}
|
|
18
|
+
retrievePermissionsCacheKey(info) {
|
|
19
|
+
const resource = info.args[0];
|
|
20
|
+
const userId = this.ctx.passport.user?.id;
|
|
21
|
+
return `${resource}_${userId}`;
|
|
22
|
+
}
|
|
23
|
+
async retrievePermissions(resource) {
|
|
24
|
+
return await this.scope.event.retrievePermissions.emit({
|
|
25
|
+
resource
|
|
26
|
+
}, async () => {
|
|
27
|
+
return await this.getPermissionsDefault(resource);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
async getPermissionsDefault(resource) {
|
|
31
|
+
const routePathInfo = recordResourceNameToRoutePath[resource];
|
|
32
|
+
if (!routePathInfo) throw new Error(`not found routePath of resource: ${resource}`);
|
|
33
|
+
// controller options
|
|
34
|
+
const controller = routePathInfo.controller;
|
|
35
|
+
// controller options
|
|
36
|
+
const beanOptions = appResource.getBean(controller);
|
|
37
|
+
if (!beanOptions) throw new Error('invalid controller');
|
|
38
|
+
const controllerBeanFullName = beanOptions.beanFullName;
|
|
39
|
+
// descs
|
|
40
|
+
const descs = Object.getOwnPropertyDescriptors(controller.prototype);
|
|
41
|
+
const actionsIgnore = this.scope.config.permission.actionsIgnore;
|
|
42
|
+
const actionKeys = Object.keys(descs).filter(actionKey => !['constructor'].includes(actionKey) && !actionsIgnore.includes(actionKey));
|
|
43
|
+
// actions
|
|
44
|
+
const permissionsActions = {};
|
|
45
|
+
for (const actionKey of actionKeys) {
|
|
46
|
+
const desc = descs[actionKey];
|
|
47
|
+
if (!desc.value || typeof desc.value !== 'function') continue;
|
|
48
|
+
const routeReal = appMetadata.getMetadata(SymbolUseOnionOptionsRouteReal, controller.prototype, actionKey);
|
|
49
|
+
const route = {
|
|
50
|
+
controller,
|
|
51
|
+
controllerBeanFullName,
|
|
52
|
+
action: actionKey,
|
|
53
|
+
route: routeReal
|
|
54
|
+
};
|
|
55
|
+
permissionsActions[actionKey] = await this._getPermissionOfAction(route);
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
actions: permissionsActions
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
async _getPermissionOfAction(route) {
|
|
62
|
+
const routePrev = this.ctx.route;
|
|
63
|
+
this.ctx.route = route;
|
|
64
|
+
const composer = composeGuards(this.app, route);
|
|
65
|
+
const [_, error] = await catchError(() => {
|
|
66
|
+
return composer(this.ctx);
|
|
67
|
+
});
|
|
68
|
+
this.ctx.route = routePrev;
|
|
69
|
+
return !error;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
__decorate([Caching.get({
|
|
73
|
+
cacheName: 'a-permission:permission',
|
|
74
|
+
cacheKeyFn: 'retrievePermissionsCacheKey'
|
|
75
|
+
}), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise)], BeanPermission.prototype, "retrievePermissions", null);
|
|
76
|
+
BeanPermission = __decorate([Bean(), BeanInfo({
|
|
77
|
+
module: "a-permission"
|
|
78
|
+
})], BeanPermission);
|
|
79
|
+
|
|
80
|
+
let EventRetrievePermissions = class EventRetrievePermissions extends BeanEventBase {};
|
|
81
|
+
EventRetrievePermissions = __decorate([Event(), BeanInfo({
|
|
82
|
+
module: "a-permission"
|
|
83
|
+
})], EventRetrievePermissions);
|
|
84
|
+
|
|
85
|
+
let SummerCachePermission = class SummerCachePermission extends BeanSummerCacheBase {
|
|
86
|
+
async getNative(_key, _options) {}
|
|
87
|
+
};
|
|
88
|
+
SummerCachePermission = __decorate([SummerCache({
|
|
89
|
+
preset: 'mem'
|
|
90
|
+
}), BeanInfo({
|
|
91
|
+
module: "a-permission"
|
|
92
|
+
})], SummerCachePermission);
|
|
93
|
+
|
|
94
|
+
let DtoPermissions = class DtoPermissions {
|
|
95
|
+
constructor() {
|
|
96
|
+
this.roleIds = void 0;
|
|
97
|
+
this.roleNames = void 0;
|
|
98
|
+
this.actions = void 0;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
__decorate([Api.field(v.optional(), v.array(v.tableIdentity())), __metadata("design:type", Array)], DtoPermissions.prototype, "roleIds", void 0);
|
|
102
|
+
__decorate([Api.field(v.optional(), v.array(z.string())), __metadata("design:type", Array)], DtoPermissions.prototype, "roleNames", void 0);
|
|
103
|
+
__decorate([Api.field(v.optional()), __metadata("design:type", Object)], DtoPermissions.prototype, "actions", void 0);
|
|
104
|
+
DtoPermissions = __decorate([Dto(), BeanInfo({
|
|
105
|
+
module: "a-permission"
|
|
106
|
+
})], DtoPermissions);
|
|
107
|
+
|
|
108
|
+
function config(_app) {
|
|
109
|
+
return {
|
|
110
|
+
permission: {
|
|
111
|
+
actionsIgnore: ['select']
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
let ScopeModuleAPermission = class ScopeModuleAPermission extends BeanScopeBase {};
|
|
117
|
+
ScopeModuleAPermission = __decorate([Scope(), BeanInfo({
|
|
118
|
+
module: "a-permission"
|
|
119
|
+
})], ScopeModuleAPermission);
|
|
120
|
+
/** scope: end */
|
|
121
|
+
|
|
122
|
+
export { BeanPermission, DtoPermissions, EventRetrievePermissions, ScopeModuleAPermission, SummerCachePermission, config };
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vona-module-a-permission",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "5.0.1",
|
|
5
|
+
"title": "a-permission",
|
|
6
|
+
"vonaModule": {
|
|
7
|
+
"dependencies": {}
|
|
8
|
+
},
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": [
|
|
12
|
+
"./src/index.ts",
|
|
13
|
+
"./dist/index.d.ts"
|
|
14
|
+
],
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./package.json": "./package.json"
|
|
18
|
+
},
|
|
19
|
+
"description": "",
|
|
20
|
+
"keywords": [
|
|
21
|
+
"Vona Module"
|
|
22
|
+
],
|
|
23
|
+
"author": "",
|
|
24
|
+
"files": [
|
|
25
|
+
"assets",
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"clean": "rimraf dist tsconfig.build.tsbuildinfo",
|
|
30
|
+
"tsc:publish": "npm run clean && vona :bin:buildModule && tsc -p tsconfig.build.json"
|
|
31
|
+
}
|
|
32
|
+
}
|