chyz 2.0.0-rc.8 → 2.0.1-rc.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.
Files changed (153) hide show
  1. package/BaseChyz.d.ts +73 -56
  2. package/BaseChyz.d.ts.map +1 -1
  3. package/BaseChyz.js +569 -404
  4. package/README.md +1 -1
  5. package/base/ActionFilter.d.ts +32 -32
  6. package/base/ActionFilter.d.ts.map +1 -1
  7. package/base/ActionFilter.js +82 -82
  8. package/base/BadRequestHttpException.d.ts +6 -0
  9. package/base/BadRequestHttpException.d.ts.map +1 -0
  10. package/base/BadRequestHttpException.js +17 -0
  11. package/base/BaseError.d.ts +43 -10
  12. package/base/BaseError.d.ts.map +1 -1
  13. package/base/BaseError.js +58 -26
  14. package/base/Behavior.d.ts +3 -3
  15. package/base/Behavior.js +7 -7
  16. package/base/CBaseObject.d.ts +4 -4
  17. package/base/CBaseObject.d.ts.map +1 -1
  18. package/base/CBaseObject.js +16 -16
  19. package/base/CEvents.d.ts +13 -0
  20. package/base/CEvents.d.ts.map +1 -0
  21. package/base/CEvents.js +24 -0
  22. package/base/CRequest.d.ts +1 -1
  23. package/base/CRequest.js +10 -10
  24. package/base/CWebController.d.ts +43 -43
  25. package/base/CWebController.d.ts.map +1 -1
  26. package/base/CWebController.js +81 -74
  27. package/base/Component.d.ts +20 -20
  28. package/base/Component.d.ts.map +1 -1
  29. package/base/Component.js +57 -57
  30. package/base/Configurable.d.ts +2 -2
  31. package/base/Configurable.js +8 -8
  32. package/base/DataErrorDbException.d.ts +5 -5
  33. package/base/DataErrorDbException.d.ts.map +1 -1
  34. package/base/DataErrorDbException.js +17 -17
  35. package/base/DbConnection.d.ts +12 -12
  36. package/base/DbConnection.js +53 -53
  37. package/base/ForbiddenHttpException.d.ts +4 -4
  38. package/base/ForbiddenHttpException.d.ts.map +1 -1
  39. package/base/ForbiddenHttpException.js +16 -16
  40. package/base/InvalidArgumentException.d.ts +5 -5
  41. package/base/InvalidArgumentException.d.ts.map +1 -1
  42. package/base/InvalidArgumentException.js +17 -17
  43. package/base/InvalidConfigException.d.ts +5 -5
  44. package/base/InvalidConfigException.d.ts.map +1 -1
  45. package/base/InvalidConfigException.js +17 -17
  46. package/base/Logs.d.ts +21 -21
  47. package/base/Logs.d.ts.map +1 -1
  48. package/base/Logs.js +57 -57
  49. package/base/Model.d.ts +301 -298
  50. package/base/Model.d.ts.map +1 -1
  51. package/base/Model.js +545 -507
  52. package/base/ModelManager.d.ts +1 -1
  53. package/base/ModelManager.js +19 -19
  54. package/base/NotFoundHttpException.d.ts +5 -5
  55. package/base/NotFoundHttpException.d.ts.map +1 -1
  56. package/base/NotFoundHttpException.js +17 -17
  57. package/base/RestClient.d.ts +6 -6
  58. package/base/RestClient.d.ts.map +1 -1
  59. package/base/RestClient.js +25 -25
  60. package/base/UnauthorizedHttpException.d.ts +5 -5
  61. package/base/UnauthorizedHttpException.d.ts.map +1 -1
  62. package/base/UnauthorizedHttpException.js +17 -17
  63. package/base/ValidationHttpException.d.ts +5 -5
  64. package/base/ValidationHttpException.d.ts.map +1 -1
  65. package/base/ValidationHttpException.js +17 -17
  66. package/base/db/Exception.d.ts +6 -6
  67. package/base/db/Exception.js +15 -15
  68. package/base/index.d.ts +19 -18
  69. package/base/index.d.ts.map +1 -1
  70. package/base/index.js +41 -39
  71. package/decorator/Middleware.d.ts +3 -3
  72. package/decorator/Middleware.js +11 -11
  73. package/decorator/controller.d.ts +2 -2
  74. package/decorator/controller.js +24 -24
  75. package/decorator/delete.d.ts +3 -0
  76. package/decorator/delete.d.ts.map +1 -0
  77. package/decorator/delete.js +36 -0
  78. package/decorator/enums/ControllerDecoratorParams.d.ts +5 -5
  79. package/decorator/enums/ControllerDecoratorParams.js +9 -9
  80. package/decorator/get.d.ts +2 -2
  81. package/decorator/get.js +36 -36
  82. package/decorator/index.d.ts +5 -3
  83. package/decorator/index.d.ts.map +1 -1
  84. package/decorator/index.js +13 -9
  85. package/decorator/post.d.ts +2 -2
  86. package/decorator/post.js +36 -36
  87. package/decorator/put.d.ts +3 -0
  88. package/decorator/put.d.ts.map +1 -0
  89. package/decorator/put.js +36 -0
  90. package/filters/AccessControl.d.ts +12 -12
  91. package/filters/AccessControl.d.ts.map +1 -1
  92. package/filters/AccessControl.js +83 -83
  93. package/filters/AccessRule.d.ts +82 -82
  94. package/filters/AccessRule.d.ts.map +1 -1
  95. package/filters/AccessRule.js +136 -137
  96. package/filters/auth/AuthInterface.d.ts +25 -25
  97. package/filters/auth/AuthInterface.js +2 -2
  98. package/filters/auth/AuthMethod.d.ts +37 -37
  99. package/filters/auth/AuthMethod.d.ts.map +1 -1
  100. package/filters/auth/AuthMethod.js +82 -82
  101. package/filters/auth/HttpBasicAuth.d.ts +23 -22
  102. package/filters/auth/HttpBasicAuth.d.ts.map +1 -1
  103. package/filters/auth/HttpBasicAuth.js +78 -72
  104. package/filters/auth/HttpBearerAuth.d.ts +17 -17
  105. package/filters/auth/HttpBearerAuth.d.ts.map +1 -1
  106. package/filters/auth/HttpBearerAuth.js +33 -32
  107. package/filters/auth/HttpHeaderAuth.d.ts +18 -14
  108. package/filters/auth/HttpHeaderAuth.d.ts.map +1 -1
  109. package/filters/auth/HttpHeaderAuth.js +64 -57
  110. package/filters/auth/JwtHttpBearerAuth.d.ts +20 -20
  111. package/filters/auth/JwtHttpBearerAuth.d.ts.map +1 -1
  112. package/filters/auth/JwtHttpBearerAuth.js +87 -81
  113. package/filters/auth/KeyCloakHttpBearerAuth.d.ts +22 -22
  114. package/filters/auth/KeyCloakHttpBearerAuth.js +116 -116
  115. package/filters/auth/index.d.ts +4 -4
  116. package/filters/auth/index.js +11 -11
  117. package/filters/index.d.ts +2 -2
  118. package/filters/index.js +7 -7
  119. package/index.d.ts +28 -21
  120. package/index.d.ts.map +1 -1
  121. package/index.js +71 -60
  122. package/model/RouteDefinition.d.ts +6 -6
  123. package/model/RouteDefinition.js +8 -8
  124. package/package.json +22 -24
  125. package/rbac/AuthAssignment.d.ts +18 -18
  126. package/rbac/AuthAssignment.js +44 -44
  127. package/rbac/AuthItem.d.ts +26 -26
  128. package/rbac/AuthItem.js +51 -51
  129. package/rbac/AuthItemChild.d.ts +18 -18
  130. package/rbac/AuthItemChild.js +43 -43
  131. package/rbac/AuthManager.d.ts +111 -111
  132. package/rbac/AuthManager.d.ts.map +1 -1
  133. package/rbac/AuthManager.js +356 -355
  134. package/rbac/index.d.ts +5 -0
  135. package/rbac/index.d.ts.map +1 -0
  136. package/rbac/index.js +28 -0
  137. package/requiments/Glob.d.ts +2 -2
  138. package/requiments/Glob.js +10 -10
  139. package/requiments/ReflectUtil.js +24 -24
  140. package/requiments/Utils.d.ts +1 -8
  141. package/requiments/Utils.d.ts.map +1 -1
  142. package/requiments/Utils.js +115 -112
  143. package/validators/BooleanValidator.js +1 -1
  144. package/validators/CompareValidator.js +1 -1
  145. package/validators/DateValidator.js +1 -1
  146. package/validators/EmailValidator.js +1 -1
  147. package/validators/Validator.d.ts +17 -17
  148. package/validators/Validator.js +27 -27
  149. package/web/IdentityInterface.d.ts +55 -54
  150. package/web/IdentityInterface.d.ts.map +1 -1
  151. package/web/IdentityInterface.js +8 -8
  152. package/web/WebUser.d.ts +71 -71
  153. package/web/WebUser.js +165 -165
@@ -1,355 +1,356 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.AuthManager = void 0;
13
- const base_1 = require("../base");
14
- const index_1 = require("../index");
15
- const Utils_1 = require("../requiments/Utils");
16
- class AuthManager extends base_1.Component {
17
- constructor() {
18
- super(...arguments);
19
- this.checkAccessAssignments = {};
20
- this.defaultRoles = {};
21
- }
22
- init() {
23
- index_1.BaseChyz.info("Auth Manager init....");
24
- }
25
- /**
26
- *
27
- */
28
- checkAccess(userId, permissionName, params = []) {
29
- return __awaiter(this, void 0, void 0, function* () {
30
- let assignments;
31
- if (!this.checkAccessAssignments[userId.toString()]) {
32
- assignments = yield this.getAssignments(userId);
33
- this.checkAccessAssignments[userId.toString()] = assignments;
34
- }
35
- else {
36
- assignments = this.checkAccessAssignments[userId.toString()];
37
- }
38
- // BaseChyz.info("assignments",assignments)
39
- if (this.hasNoAssignments(assignments)) {
40
- return false;
41
- }
42
- return yield this.checkAccessRecursive(userId, permissionName, params, assignments);
43
- });
44
- }
45
- checkAccessFromCache() {
46
- return __awaiter(this, void 0, void 0, function* () {
47
- });
48
- }
49
- checkAccessRecursive(user, itemname, params, assignments) {
50
- return __awaiter(this, void 0, void 0, function* () {
51
- let item = yield this.getItem(itemname);
52
- if (!item)
53
- return false;
54
- /**
55
- * @todo
56
- * Rule test edilmeli
57
- */
58
- if (assignments[itemname] || Utils_1.Utils.find(this.defaultRoles, itemname)) {
59
- return true;
60
- }
61
- /**
62
- * item child
63
- */
64
- let parents = yield base_1.ModelManager.AuthItemChild.findAll({ attributes: ["parent"], where: { child: itemname } });
65
- for (const parent of parents) {
66
- let r = yield this.checkAccessRecursive(user, parent.parent, params, assignments);
67
- if (r) {
68
- return true;
69
- }
70
- }
71
- return false;
72
- });
73
- }
74
- /**
75
- *
76
- * @param name
77
- */
78
- getItem(name) {
79
- return __awaiter(this, void 0, void 0, function* () {
80
- if (!name)
81
- return null;
82
- return yield base_1.ModelManager.AuthItem.findOne({ where: { name: name } });
83
- });
84
- }
85
- getItems(type) {
86
- return __awaiter(this, void 0, void 0, function* () {
87
- let items = {};
88
- let _items = yield base_1.ModelManager.AuthItem.findAll({ where: { type: type } });
89
- if (_items)
90
- _items.forEach((item) => {
91
- items[item["name"]] = item.dataValues;
92
- });
93
- return items;
94
- });
95
- }
96
- getRolesByUser(userId) {
97
- return __awaiter(this, void 0, void 0, function* () {
98
- if (this.isEmptyUserId(userId.toString())) {
99
- return [];
100
- }
101
- let roles = {};
102
- let items = yield base_1.ModelManager.AuthAssignment.findAll({
103
- where: {
104
- user_id: userId.toString(),
105
- '$AuthItemClasses.type$': AuthManager.TYPE_ROLE
106
- },
107
- include: [{
108
- model: base_1.ModelManager.AuthItem.model()
109
- }]
110
- });
111
- for (const item of items) {
112
- for (const i of item.AuthItemClasses)
113
- roles[i["name"]] = i.dataValues;
114
- }
115
- return roles;
116
- });
117
- }
118
- /**
119
- *
120
- * @param roleName
121
- */
122
- getChildRoles(roleName) {
123
- return __awaiter(this, void 0, void 0, function* () {
124
- let role = yield this.getRole(roleName);
125
- if (role === null) {
126
- throw new index_1.InvalidArgumentException(`Role "${roleName}" not found.`);
127
- }
128
- const result = { result: {} };
129
- yield this.getChildrenRecursive(roleName, yield this.getChildrenList(), result);
130
- let roles = {};
131
- roles[roleName] = role;
132
- let _roles = yield this.getRoles();
133
- let _r = {};
134
- Utils_1.Utils.forEach(_roles, (item) => {
135
- if (result.result[item.name])
136
- _r[item.name] = item;
137
- });
138
- return Utils_1.Utils.merge(roles, _r);
139
- });
140
- }
141
- /**
142
- *
143
- * @param roleName
144
- */
145
- getPermissionsByRole(roleName) {
146
- return __awaiter(this, void 0, void 0, function* () {
147
- let childrenList = this.getChildrenList();
148
- const result = { result: {} };
149
- let permissions = {};
150
- yield this.getChildrenRecursive(roleName, childrenList, result);
151
- if (Utils_1.Utils.isEmpty(result.result)) {
152
- return {};
153
- }
154
- let itemResult = yield base_1.ModelManager.AuthItem.findAll({
155
- where: {
156
- type: AuthManager.TYPE_PERMISSION,
157
- name: Object.keys(result.result)
158
- }
159
- });
160
- for (const itemElement of itemResult) {
161
- permissions[itemElement["name"]] = itemElement.dataValues;
162
- }
163
- return permissions;
164
- });
165
- }
166
- /**
167
- *
168
- * @param userId
169
- */
170
- getPermissionsByUser(userId) {
171
- return __awaiter(this, void 0, void 0, function* () {
172
- if (this.isEmptyUserId(userId.toString())) {
173
- return {};
174
- }
175
- let directPermission = yield this.getDirectPermissionsByUser(userId);
176
- let inheritedPermission = yield this.getInheritedPermissionsByUser(userId);
177
- return Utils_1.Utils.merge(directPermission, inheritedPermission);
178
- });
179
- }
180
- /**
181
- * Returns all permissions that are directly assigned to user.
182
- * @return Permission[] all direct permissions that the user has. The array is indexed by the permission names.
183
- */
184
- getDirectPermissionsByUser(userId) {
185
- return __awaiter(this, void 0, void 0, function* () {
186
- let permissions = {};
187
- let result = yield base_1.ModelManager.AuthAssignment.findAll({
188
- where: {
189
- user_id: userId.toString(),
190
- '$AuthItemClasses.type$': AuthManager.TYPE_PERMISSION
191
- },
192
- include: [
193
- {
194
- model: base_1.ModelManager.AuthItem.model()
195
- }
196
- ]
197
- });
198
- for (const resultElement of result) {
199
- for (const i of resultElement.AuthItemClasses)
200
- permissions[i["name"]] = i.dataValues;
201
- }
202
- return permissions;
203
- });
204
- }
205
- getInheritedPermissionsByUser(userId) {
206
- return __awaiter(this, void 0, void 0, function* () {
207
- let userAssignment = yield base_1.ModelManager.AuthAssignment.findAll({ where: { user_id: userId.toString() }, attributes: ["item_name"] });
208
- let childrenList = yield this.getChildrenList();
209
- const result = { result: {} };
210
- let permissions = {};
211
- for (const userAssignmentElement of userAssignment) {
212
- this.getChildrenRecursive(userAssignmentElement.item_name, childrenList, result);
213
- }
214
- if (Utils_1.Utils.isEmpty(result.result)) {
215
- return {};
216
- }
217
- let itemResult = yield base_1.ModelManager.AuthItem.findAll({
218
- where: {
219
- type: AuthManager.TYPE_PERMISSION,
220
- name: Object.keys(result.result)
221
- }
222
- });
223
- for (const itemElement of itemResult) {
224
- permissions[itemElement["name"]] = itemElement.dataValues;
225
- }
226
- return permissions;
227
- });
228
- }
229
- /**
230
- *
231
- * @param userId
232
- */
233
- getItemsByUser(userId) {
234
- return __awaiter(this, void 0, void 0, function* () {
235
- let items = yield base_1.ModelManager.AuthAssignment.findAll({
236
- where: {
237
- user_id: userId.toString()
238
- },
239
- include: [{
240
- model: base_1.ModelManager.AuthItem.model()
241
- }]
242
- });
243
- return items;
244
- });
245
- }
246
- /**
247
- * Returns all role assignment information for the specified role.
248
- * @param $roleName
249
- */
250
- getUserIdsByRole(roleName) {
251
- return __awaiter(this, void 0, void 0, function* () {
252
- if (!roleName)
253
- return [];
254
- return yield base_1.ModelManager.AuthAssignment.findAll({ where: { "item_name": roleName }, attributes: ["user_id"] });
255
- });
256
- }
257
- /**
258
- * {@inheritdoc}
259
- */
260
- getRole(name) {
261
- return __awaiter(this, void 0, void 0, function* () {
262
- let item = yield this.getItem(name);
263
- return item && item.type == AuthManager.TYPE_ROLE ? item : null;
264
- });
265
- }
266
- /**
267
- * {@inheritdoc}
268
- */
269
- getRoles() {
270
- return this.getItems(AuthManager.TYPE_ROLE);
271
- }
272
- /**
273
- * Recursively finds all children and grand children of the specified item.
274
- * @param string $name the name of the item whose children are to be looked for.
275
- * @param array $childrenList the child list built via [[getChildrenList()]]
276
- * @param array $result the children and grand children (in array keys)
277
- */
278
- getChildrenRecursive(name, childrenList, model) {
279
- if (childrenList[name]) {
280
- for (const child of childrenList[name]) {
281
- model.result[child] = true;
282
- this.getChildrenRecursive(child, childrenList, model);
283
- }
284
- }
285
- }
286
- /**
287
- *
288
- * @param roleName
289
- * @param userId
290
- */
291
- getAssignment(roleName, userId) {
292
- if (this.isEmptyUserId(userId)) {
293
- return [];
294
- }
295
- return base_1.ModelManager.AuthAssignment.findAll({ where: { user_id: userId, items_name: roleName } });
296
- }
297
- /**
298
- *
299
- */
300
- getAssignments(userId) {
301
- return __awaiter(this, void 0, void 0, function* () {
302
- if (this.isEmptyUserId(userId.toString())) {
303
- return {};
304
- }
305
- let assignments = {};
306
- try {
307
- let as = yield base_1.ModelManager.AuthAssignment.findAll({ where: { user_id: userId.toString() } });
308
- for (const a of as) {
309
- assignments[a["item_name"]] = a;
310
- }
311
- }
312
- catch (e) {
313
- throw new base_1.InvalidConfigException('The user application component must be available to specify roles in AccessRule.');
314
- }
315
- return assignments;
316
- });
317
- }
318
- /**
319
- * Returns the children for every parent.
320
- * @return array the children list. Each array key is a parent item name,
321
- * and the corresponding array value is a list of child item names.
322
- */
323
- getChildrenList() {
324
- return __awaiter(this, void 0, void 0, function* () {
325
- let items = yield base_1.ModelManager.AuthItemChild.findAll();
326
- let parents = {};
327
- for (const item of items) {
328
- parents[item["parent"]] = Utils_1.Utils.concat(parents[item["parent"]] || [], [item["child"]]);
329
- }
330
- return parents;
331
- });
332
- }
333
- /**
334
- * Check whether $userId is empty.
335
- * @param mixed $userId
336
- * @return bool
337
- * @since 2.0.26
338
- */
339
- isEmptyUserId(userId) {
340
- return !userId || userId === '';
341
- }
342
- /**
343
- * Checks whether array of $assignments is empty and [[defaultRoles]] property is empty as well.
344
- *
345
- * @param Assignment[] $assignments array of user's assignments
346
- * @return bool whether array of $assignments is empty and [[defaultRoles]] property is empty as well
347
- * @since 2.0.11
348
- */
349
- hasNoAssignments(assignments) {
350
- return Utils_1.Utils.isEmpty(assignments) && Utils_1.Utils.isEmpty(this.defaultRoles);
351
- }
352
- }
353
- exports.AuthManager = AuthManager;
354
- AuthManager.TYPE_ROLE = 1;
355
- AuthManager.TYPE_PERMISSION = 2;
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.AuthManager = void 0;
13
+ const base_1 = require("../base");
14
+ const index_1 = require("../index");
15
+ const Utils_1 = require("../requiments/Utils");
16
+ class AuthManager extends base_1.Component {
17
+ constructor() {
18
+ super(...arguments);
19
+ this.checkAccessAssignments = {};
20
+ this.defaultRoles = {};
21
+ }
22
+ init() {
23
+ index_1.BaseChyz.info("Auth Manager init....");
24
+ }
25
+ /**
26
+ *
27
+ */
28
+ checkAccess(userId, permissionName, params = []) {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ let assignments;
31
+ if (!userId)
32
+ return false;
33
+ if (!this.checkAccessAssignments[userId.toString()]) {
34
+ assignments = yield this.getAssignments(userId);
35
+ this.checkAccessAssignments[userId.toString()] = assignments;
36
+ }
37
+ else {
38
+ assignments = this.checkAccessAssignments[userId.toString()];
39
+ }
40
+ // BaseChyz.info("assignments",assignments)
41
+ if (this.hasNoAssignments(assignments)) {
42
+ return false;
43
+ }
44
+ return yield this.checkAccessRecursive(userId, permissionName, params, assignments);
45
+ });
46
+ }
47
+ checkAccessFromCache() {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ });
50
+ }
51
+ checkAccessRecursive(user, itemname, params, assignments) {
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ let item = yield this.getItem(itemname);
54
+ if (!item)
55
+ return false;
56
+ /**
57
+ * @todo
58
+ * Rule test edilmeli
59
+ */
60
+ if (assignments[itemname] || Utils_1.Utils.find(this.defaultRoles, itemname)) {
61
+ return true;
62
+ }
63
+ /**
64
+ * item child
65
+ */
66
+ let parents = yield base_1.ModelManager.AuthItemChild.findAll({ attributes: ["parent"], where: { child: itemname } });
67
+ for (const parent of parents) {
68
+ let r = yield this.checkAccessRecursive(user, parent.parent, params, assignments);
69
+ if (r) {
70
+ return true;
71
+ }
72
+ }
73
+ return false;
74
+ });
75
+ }
76
+ /**
77
+ *
78
+ * @param name
79
+ */
80
+ getItem(name) {
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ if (!name)
83
+ return null;
84
+ return yield base_1.ModelManager.AuthItem.findOne({ where: { name: name } });
85
+ });
86
+ }
87
+ getItems(type) {
88
+ return __awaiter(this, void 0, void 0, function* () {
89
+ let items = {};
90
+ let _items = yield base_1.ModelManager.AuthItem.findAll({ where: { type: type } });
91
+ if (_items)
92
+ _items.forEach((item) => {
93
+ items[item["name"]] = item.dataValues;
94
+ });
95
+ return items;
96
+ });
97
+ }
98
+ getRolesByUser(userId) {
99
+ return __awaiter(this, void 0, void 0, function* () {
100
+ if (this.isEmptyUserId(userId.toString())) {
101
+ return [];
102
+ }
103
+ let roles = {};
104
+ let items = yield base_1.ModelManager.AuthAssignment.findAll({
105
+ where: {
106
+ user_id: userId.toString(),
107
+ '$AuthItemClasses.type$': AuthManager.TYPE_ROLE
108
+ },
109
+ include: [{
110
+ model: base_1.ModelManager.AuthItem.model()
111
+ }]
112
+ });
113
+ for (const item of items) {
114
+ for (const i of item.AuthItemClasses)
115
+ roles[i["name"]] = i.dataValues;
116
+ }
117
+ return roles;
118
+ });
119
+ }
120
+ /**
121
+ *
122
+ * @param roleName
123
+ */
124
+ getChildRoles(roleName) {
125
+ return __awaiter(this, void 0, void 0, function* () {
126
+ let role = yield this.getRole(roleName);
127
+ if (role === null) {
128
+ throw new index_1.InvalidArgumentException(`Role "${roleName}" not found.`);
129
+ }
130
+ const result = { result: {} };
131
+ yield this.getChildrenRecursive(roleName, yield this.getChildrenList(), result);
132
+ let roles = {};
133
+ roles[roleName] = role;
134
+ let _roles = yield this.getRoles();
135
+ let _r = {};
136
+ Utils_1.Utils.forEach(_roles, (item) => {
137
+ if (result.result[item.name])
138
+ _r[item.name] = item;
139
+ });
140
+ return Utils_1.Utils.merge(roles, _r);
141
+ });
142
+ }
143
+ /**
144
+ *
145
+ * @param roleName
146
+ */
147
+ getPermissionsByRole(roleName) {
148
+ return __awaiter(this, void 0, void 0, function* () {
149
+ let childrenList = this.getChildrenList();
150
+ const result = { result: {} };
151
+ let permissions = {};
152
+ yield this.getChildrenRecursive(roleName, childrenList, result);
153
+ if (Utils_1.Utils.isEmpty(result.result)) {
154
+ return {};
155
+ }
156
+ let itemResult = yield base_1.ModelManager.AuthItem.findAll({
157
+ where: {
158
+ type: AuthManager.TYPE_PERMISSION,
159
+ name: Object.keys(result.result)
160
+ }
161
+ });
162
+ for (const itemElement of itemResult) {
163
+ permissions[itemElement["name"]] = itemElement.dataValues;
164
+ }
165
+ return permissions;
166
+ });
167
+ }
168
+ /**
169
+ *
170
+ * @param userId
171
+ */
172
+ getPermissionsByUser(userId) {
173
+ return __awaiter(this, void 0, void 0, function* () {
174
+ if (this.isEmptyUserId(userId.toString())) {
175
+ return {};
176
+ }
177
+ let directPermission = yield this.getDirectPermissionsByUser(userId);
178
+ let inheritedPermission = yield this.getInheritedPermissionsByUser(userId);
179
+ return Utils_1.Utils.merge(directPermission, inheritedPermission);
180
+ });
181
+ }
182
+ /**
183
+ * Returns all permissions that are directly assigned to user.
184
+ * @return Permission[] all direct permissions that the user has. The array is indexed by the permission names.
185
+ */
186
+ getDirectPermissionsByUser(userId) {
187
+ return __awaiter(this, void 0, void 0, function* () {
188
+ let permissions = {};
189
+ let result = yield base_1.ModelManager.AuthAssignment.findAll({
190
+ where: {
191
+ user_id: userId.toString(),
192
+ '$AuthItemClasses.type$': AuthManager.TYPE_PERMISSION
193
+ },
194
+ include: [
195
+ {
196
+ model: base_1.ModelManager.AuthItem.model()
197
+ }
198
+ ]
199
+ });
200
+ for (const resultElement of result) {
201
+ for (const i of resultElement.AuthItemClasses)
202
+ permissions[i["name"]] = i.dataValues;
203
+ }
204
+ return permissions;
205
+ });
206
+ }
207
+ getInheritedPermissionsByUser(userId) {
208
+ return __awaiter(this, void 0, void 0, function* () {
209
+ let userAssignment = yield base_1.ModelManager.AuthAssignment.findAll({ where: { user_id: userId.toString() }, attributes: ["item_name"] });
210
+ let childrenList = yield this.getChildrenList();
211
+ const result = { result: {} };
212
+ let permissions = {};
213
+ for (const userAssignmentElement of userAssignment) {
214
+ this.getChildrenRecursive(userAssignmentElement.item_name, childrenList, result);
215
+ }
216
+ if (Utils_1.Utils.isEmpty(result.result)) {
217
+ return {};
218
+ }
219
+ let itemResult = yield base_1.ModelManager.AuthItem.findAll({
220
+ where: {
221
+ type: AuthManager.TYPE_PERMISSION,
222
+ name: Object.keys(result.result)
223
+ }
224
+ });
225
+ for (const itemElement of itemResult) {
226
+ permissions[itemElement["name"]] = itemElement.dataValues;
227
+ }
228
+ return permissions;
229
+ });
230
+ }
231
+ /**
232
+ *
233
+ * @param userId
234
+ */
235
+ getItemsByUser(userId) {
236
+ return __awaiter(this, void 0, void 0, function* () {
237
+ return yield base_1.ModelManager.AuthAssignment.findAll({
238
+ where: {
239
+ user_id: userId.toString()
240
+ },
241
+ include: [{
242
+ model: base_1.ModelManager.AuthItem.model()
243
+ }]
244
+ });
245
+ });
246
+ }
247
+ /**
248
+ * Returns all role assignment information for the specified role.
249
+ * @param $roleName
250
+ */
251
+ getUserIdsByRole(roleName) {
252
+ return __awaiter(this, void 0, void 0, function* () {
253
+ if (!roleName)
254
+ return [];
255
+ return yield base_1.ModelManager.AuthAssignment.findAll({ where: { "item_name": roleName }, attributes: ["user_id"] });
256
+ });
257
+ }
258
+ /**
259
+ * {@inheritdoc}
260
+ */
261
+ getRole(name) {
262
+ return __awaiter(this, void 0, void 0, function* () {
263
+ let item = yield this.getItem(name);
264
+ return item && item.type == AuthManager.TYPE_ROLE ? item : null;
265
+ });
266
+ }
267
+ /**
268
+ * {@inheritdoc}
269
+ */
270
+ getRoles() {
271
+ return this.getItems(AuthManager.TYPE_ROLE);
272
+ }
273
+ /**
274
+ * Recursively finds all children and grand children of the specified item.
275
+ * @param string $name the name of the item whose children are to be looked for.
276
+ * @param array $childrenList the child list built via [[getChildrenList()]]
277
+ * @param array $result the children and grand children (in array keys)
278
+ */
279
+ getChildrenRecursive(name, childrenList, model) {
280
+ if (childrenList[name]) {
281
+ for (const child of childrenList[name]) {
282
+ model.result[child] = true;
283
+ this.getChildrenRecursive(child, childrenList, model);
284
+ }
285
+ }
286
+ }
287
+ /**
288
+ *
289
+ * @param roleName
290
+ * @param userId
291
+ */
292
+ getAssignment(roleName, userId) {
293
+ if (this.isEmptyUserId(userId)) {
294
+ return [];
295
+ }
296
+ return base_1.ModelManager.AuthAssignment.findAll({ where: { user_id: userId, items_name: roleName } });
297
+ }
298
+ /**
299
+ *
300
+ */
301
+ getAssignments(userId) {
302
+ return __awaiter(this, void 0, void 0, function* () {
303
+ if (this.isEmptyUserId(userId.toString())) {
304
+ return {};
305
+ }
306
+ let assignments = {};
307
+ try {
308
+ let as = yield base_1.ModelManager.AuthAssignment.findAll({ where: { user_id: userId.toString() } });
309
+ for (const a of as) {
310
+ assignments[a["item_name"]] = a;
311
+ }
312
+ }
313
+ catch (e) {
314
+ throw new base_1.InvalidConfigException('The user application component must be available to specify roles in AccessRule.');
315
+ }
316
+ return assignments;
317
+ });
318
+ }
319
+ /**
320
+ * Returns the children for every parent.
321
+ * @return array the children list. Each array key is a parent item name,
322
+ * and the corresponding array value is a list of child item names.
323
+ */
324
+ getChildrenList() {
325
+ return __awaiter(this, void 0, void 0, function* () {
326
+ let items = yield base_1.ModelManager.AuthItemChild.findAll();
327
+ let parents = {};
328
+ for (const item of items) {
329
+ parents[item["parent"]] = Utils_1.Utils.concat(parents[item["parent"]] || [], [item["child"]]);
330
+ }
331
+ return parents;
332
+ });
333
+ }
334
+ /**
335
+ * Check whether $userId is empty.
336
+ * @param mixed $userId
337
+ * @return bool
338
+ * @since 2.0.26
339
+ */
340
+ isEmptyUserId(userId) {
341
+ return !userId || userId === '';
342
+ }
343
+ /**
344
+ * Checks whether array of $assignments is empty and [[defaultRoles]] property is empty as well.
345
+ *
346
+ * @param Assignment[] $assignments array of user's assignments
347
+ * @return bool whether array of $assignments is empty and [[defaultRoles]] property is empty as well
348
+ * @since 2.0.11
349
+ */
350
+ hasNoAssignments(assignments) {
351
+ return Utils_1.Utils.isEmpty(assignments) && Utils_1.Utils.isEmpty(this.defaultRoles);
352
+ }
353
+ }
354
+ AuthManager.TYPE_ROLE = 1;
355
+ AuthManager.TYPE_PERMISSION = 2;
356
+ exports.AuthManager = AuthManager;