vona-module-a-user 5.0.25 → 5.0.27
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/bean/guard.roleName.d.ts +2 -0
- package/dist/bean/guard.userName.d.ts +2 -0
- package/dist/index.js +33 -15
- package/package.json +1 -1
|
@@ -5,7 +5,9 @@ import { BeanBase } from 'vona';
|
|
|
5
5
|
export interface IGuardOptionsRoleName extends IDecoratorGuardOptions {
|
|
6
6
|
name?: keyof IRoleNameRecord | (keyof IRoleNameRecord)[];
|
|
7
7
|
passWhenMatched: boolean;
|
|
8
|
+
rejectWhenDismatched: boolean;
|
|
8
9
|
}
|
|
9
10
|
export declare class GuardRoleName extends BeanBase implements IGuardExecute {
|
|
10
11
|
execute(options: IGuardOptionsRoleName, next: Next): Promise<boolean>;
|
|
12
|
+
private _check;
|
|
11
13
|
}
|
|
@@ -5,7 +5,9 @@ import { BeanBase } from 'vona';
|
|
|
5
5
|
export interface IGuardOptionsUserName extends IDecoratorGuardOptions {
|
|
6
6
|
name?: keyof IUserNameRecord | (keyof IUserNameRecord)[];
|
|
7
7
|
passWhenMatched: boolean;
|
|
8
|
+
rejectWhenDismatched: boolean;
|
|
8
9
|
}
|
|
9
10
|
export declare class GuardUserName extends BeanBase implements IGuardExecute {
|
|
10
11
|
execute(options: IGuardOptionsUserName, next: Next): Promise<boolean>;
|
|
12
|
+
private _check;
|
|
11
13
|
}
|
package/dist/index.js
CHANGED
|
@@ -52,41 +52,59 @@ let GuardPassport = (_dec$f = Guard({
|
|
|
52
52
|
|
|
53
53
|
var _dec$e, _dec2$e, _class$e;
|
|
54
54
|
let GuardRoleName = (_dec$e = Guard({
|
|
55
|
-
passWhenMatched: true
|
|
55
|
+
passWhenMatched: true,
|
|
56
|
+
rejectWhenDismatched: true
|
|
56
57
|
}), _dec2$e = BeanInfo({
|
|
57
58
|
module: "a-user"
|
|
58
59
|
}), _dec$e(_class$e = _dec2$e(_class$e = class GuardRoleName extends BeanBase {
|
|
59
60
|
async execute(options, next) {
|
|
60
|
-
|
|
61
|
+
const result = await this._check(options);
|
|
62
|
+
if (!result) {
|
|
63
|
+
if (options.rejectWhenDismatched) return this.app.throw(403);
|
|
64
|
+
} else {
|
|
65
|
+
if (options.passWhenMatched) return true;
|
|
66
|
+
}
|
|
67
|
+
// next
|
|
68
|
+
return next();
|
|
69
|
+
}
|
|
70
|
+
async _check(options) {
|
|
71
|
+
if (!options.name) return false;
|
|
61
72
|
const user = this.bean.passport.getCurrentUser();
|
|
62
|
-
if (!user || user.anonymous) return
|
|
73
|
+
if (!user || user.anonymous) return false;
|
|
63
74
|
const roles = this.bean.passport.getCurrentRoles();
|
|
64
|
-
if (!roles) return
|
|
75
|
+
if (!roles) return false;
|
|
65
76
|
const roleNames = roles?.map(item => item.name);
|
|
66
77
|
const optionsName = Array.isArray(options.name) ? options.name : [options.name];
|
|
67
|
-
if (!roleNames.some(roleName => optionsName.includes(roleName))) return
|
|
68
|
-
|
|
69
|
-
// next
|
|
70
|
-
return next();
|
|
78
|
+
if (!roleNames.some(roleName => optionsName.includes(roleName))) return false;
|
|
79
|
+
return true;
|
|
71
80
|
}
|
|
72
81
|
}) || _class$e) || _class$e);
|
|
73
82
|
|
|
74
83
|
var _dec$d, _dec2$d, _class$d;
|
|
75
84
|
let GuardUserName = (_dec$d = Guard({
|
|
76
|
-
passWhenMatched: true
|
|
85
|
+
passWhenMatched: true,
|
|
86
|
+
rejectWhenDismatched: true
|
|
77
87
|
}), _dec2$d = BeanInfo({
|
|
78
88
|
module: "a-user"
|
|
79
89
|
}), _dec$d(_class$d = _dec2$d(_class$d = class GuardUserName extends BeanBase {
|
|
80
90
|
async execute(options, next) {
|
|
81
|
-
|
|
91
|
+
const result = await this._check(options);
|
|
92
|
+
if (!result) {
|
|
93
|
+
if (options.rejectWhenDismatched) return this.app.throw(403);
|
|
94
|
+
} else {
|
|
95
|
+
if (options.passWhenMatched) return true;
|
|
96
|
+
}
|
|
97
|
+
// next
|
|
98
|
+
return next();
|
|
99
|
+
}
|
|
100
|
+
async _check(options) {
|
|
101
|
+
if (!options.name) return false;
|
|
82
102
|
const user = this.bean.passport.getCurrentUser();
|
|
83
|
-
if (!user || user.anonymous) return
|
|
103
|
+
if (!user || user.anonymous) return false;
|
|
84
104
|
const userName = user.name;
|
|
85
105
|
const optionsName = Array.isArray(options.name) ? options.name : [options.name];
|
|
86
|
-
if (!optionsName.includes(userName)) return
|
|
87
|
-
|
|
88
|
-
// next
|
|
89
|
-
return next();
|
|
106
|
+
if (!optionsName.includes(userName)) return false;
|
|
107
|
+
return true;
|
|
90
108
|
}
|
|
91
109
|
}) || _class$d) || _class$d);
|
|
92
110
|
|