vona-module-a-user 5.0.25 → 5.0.26

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.
@@ -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
- if (!options.name) return this.app.throw(403);
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 this.app.throw(403);
73
+ if (!user || user.anonymous) return false;
63
74
  const roles = this.bean.passport.getCurrentRoles();
64
- if (!roles) return this.app.throw(403);
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 this.app.throw(403);
68
- if (options.passWhenMatched) return true;
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
- if (!options.name) return this.app.throw(403);
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 this.app.throw(403);
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 this.app.throw(403);
87
- if (options.passWhenMatched) return true;
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
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vona-module-a-user",
3
3
  "type": "module",
4
- "version": "5.0.25",
4
+ "version": "5.0.26",
5
5
  "title": "a-user",
6
6
  "vonaModule": {
7
7
  "dependencies": {}