chyz 1.0.13-rc.11 → 1.0.13-rc.16

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/package.json CHANGED
@@ -1,13 +1,12 @@
1
1
  {
2
2
  "name": "chyz",
3
- "version": " 1.0.13-rc.11",
3
+ "version": " 1.0.13-rc.16",
4
4
  "description": "Nodejs Micro service Framework",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
7
7
  "dev": "nodemon -t --trace-warnings index.ts",
8
8
  "debug": "ts-node index.ts",
9
9
  "build": "rmdir /S /Q .\\dist && npx tsc && xcopy .\\log .\\dist\\log /e /i /h /Y && copy .\\package.json .\\dist\\package.json",
10
- "publish": "npm publish",
11
10
  "test": "echo \"Error: no test specified\" && exit 1",
12
11
  "postversion": "git push && git push --tags"
13
12
  },
@@ -1,4 +1,4 @@
1
- import {Component, ModelManager} from "../base";
1
+ import {Component, DataErrorDbException, InvalidConfigException, ModelManager, UnauthorizedHttpException} from "../base";
2
2
  import {BaseChyz, InvalidArgumentException} from "../index";
3
3
  import Utils from "../requiments/Utils";
4
4
 
@@ -9,7 +9,7 @@ interface Role {
9
9
  description: string;
10
10
  ruleName: string;
11
11
  data: string;
12
- params:string;
12
+ params: string;
13
13
  }
14
14
 
15
15
  interface Permission {
@@ -18,7 +18,7 @@ interface Permission {
18
18
  description: string;
19
19
  ruleName: string;
20
20
  data: string;
21
- params:string;
21
+ params: string;
22
22
  }
23
23
 
24
24
 
@@ -42,7 +42,7 @@ export class AuthManager extends Component {
42
42
  *
43
43
  */
44
44
 
45
- public async checkAccess(userId: number, permissionName: string, params: any[] = []) {
45
+ public async checkAccess(userId: number, permissionName: string, params: any[] = []): Promise<boolean> {
46
46
  let assignments: any;
47
47
  if (!this.checkAccessAssignments[userId.toString()]) {
48
48
  assignments = await this.getAssignments(userId);
@@ -66,7 +66,7 @@ export class AuthManager extends Component {
66
66
 
67
67
  }
68
68
 
69
- public async checkAccessRecursive(user: string | number, itemname: string, params: any[], assignments: any[]) {
69
+ public async checkAccessRecursive(user: string | number, itemname: string, params: any[], assignments: any): Promise<boolean> {
70
70
  let item: any = await this.getItem(itemname);
71
71
  if (!item) return false;
72
72
 
@@ -75,6 +75,10 @@ export class AuthManager extends Component {
75
75
  * Rule test edilmeli
76
76
  */
77
77
 
78
+ if (assignments[itemname] || Utils.find(this.defaultRoles, itemname)) {
79
+ return true;
80
+ }
81
+
78
82
  /**
79
83
  * item child
80
84
  */
@@ -344,11 +348,15 @@ export class AuthManager extends Component {
344
348
  }
345
349
 
346
350
  let assignments: any = {};
347
- let as = await ModelManager.AuthAssignment.findAll({where: {user_id: userId.toString()}});
348
- for (const a of as) {
349
- assignments[a["item_name"]] = a;
350
- }
351
+ try {
351
352
 
353
+ let as = await ModelManager.AuthAssignment.findAll({where: {user_id: userId.toString()}});
354
+ for (const a of as) {
355
+ assignments[a["item_name"]] = a;
356
+ }
357
+ } catch (e) {
358
+ throw new InvalidConfigException('The user application component must be available to specify roles in AccessRule.');
359
+ }
352
360
  return assignments;
353
361
  }
354
362
 
package/web/WebUser.ts CHANGED
@@ -9,6 +9,8 @@ import {Component} from "../base/Component";
9
9
  import {ForbiddenHttpException} from "../base/ForbiddenHttpException";
10
10
  import {InvalidConfigException} from "../base/InvalidConfigException";
11
11
  import {IdentityInterface} from "./IdentityInterface";
12
+ import Utils from "../requiments/Utils";
13
+ import {AuthManager} from "../rbac/AuthManager";
12
14
 
13
15
  export class WebUser extends Component {
14
16
 
@@ -17,6 +19,13 @@ export class WebUser extends Component {
17
19
  */
18
20
  public identityClass: any;
19
21
  private _identity: any;
22
+ /**
23
+ * @var CheckAccessInterface|string|array The access checker object to use for checking access or the application
24
+ * component ID of the access checker.
25
+ * If not set the application auth manager will be used.
26
+ * @since 2.0.9
27
+ */
28
+ public accessChecker: any = null;
20
29
 
21
30
 
22
31
  get identity() {
@@ -75,7 +84,7 @@ export class WebUser extends Component {
75
84
  if (this.identity && this.login(this.identity)) {
76
85
  return this.identity;
77
86
  }
78
- }else{
87
+ } else {
79
88
  BaseChyz.error("WebUser::findIdentityByAccessToken undefined")
80
89
  }
81
90
  return null;
@@ -97,4 +106,82 @@ export class WebUser extends Component {
97
106
  public afterLogin() {
98
107
 
99
108
  }
109
+
110
+ public getId() {
111
+ let identity = this.getIdentity();
112
+ return identity !== null ? identity.getId() : null;
113
+ }
114
+
115
+ /**
116
+ * Checks if the user can perform the operation as specified by the given permission.
117
+ *
118
+ * Note that you must configure "authManager" application component in order to use this method.
119
+ * Otherwise it will always return false.
120
+ *
121
+ * @param string $permissionName the name of the permission (e.g. "edit post") that needs access check.
122
+ * @param array $params name-value pairs that would be passed to the rules associated
123
+ * with the roles and permissions assigned to the user.
124
+ * @param bool $allowCaching whether to allow caching the result of access check.
125
+ * When this parameter is true (default), if the access check of an operation was performed
126
+ * before, its result will be directly returned when calling this method to check the same
127
+ * operation. If this parameter is false, this method will always call
128
+ * [[\yii\rbac\CheckAccessInterface::checkAccess()]] to obtain the up-to-date access result. Note that this
129
+ * caching is effective only within the same request and only works when `$params = []`.
130
+ * @return bool whether the user can perform the operation as specified by the given permission.
131
+ */
132
+ // public function can($permissionName, $params = [], $allowCaching = true)
133
+ // {
134
+ // if ($allowCaching && empty($params) && isset($this->_access[$permissionName])) {
135
+ // return $this->_access[$permissionName];
136
+ // }
137
+ // if (($accessChecker = $this->getAccessChecker()) === null) {
138
+ // return false;
139
+ // }
140
+ // $access = $accessChecker->checkAccess($this->getId(), $permissionName, $params);
141
+ // if ($allowCaching && empty($params)) {
142
+ // $this->_access[$permissionName] = $access;
143
+ // }
144
+ //
145
+ // return $access;
146
+ // }
147
+
148
+ public async can(permissionName: string, params = [], allowCaching = true) {
149
+
150
+ let access;
151
+ let accessChecker: AuthManager;
152
+ if ((accessChecker = this.getAccessChecker()) == null)
153
+ return false;
154
+
155
+
156
+ access = await accessChecker.checkAccess(this.getIdentity().id, permissionName, params);
157
+
158
+ if (allowCaching && Utils.isEmpty(params)) {
159
+ // this._access[$permissionName] = $access;
160
+ }
161
+
162
+ return access;
163
+
164
+ }
165
+
166
+ /**
167
+ * Returns auth manager associated with the user component.
168
+ *
169
+ * By default this is the `authManager` application component.
170
+ * You may override this method to return a different auth manager instance if needed.
171
+ * @return \yii\rbac\ManagerInterface
172
+ * @since 2.0.6
173
+ */
174
+ protected getAuthManager(): AuthManager {
175
+ return BaseChyz.getComponent('authManager');
176
+ }
177
+
178
+ /**
179
+ * Returns the access checker used for checking access.
180
+ * @return CheckAccessInterface
181
+ */
182
+ protected getAccessChecker():AuthManager {
183
+ return this.accessChecker !== null ? this.accessChecker : this.getAuthManager();
184
+ }
185
+
186
+
100
187
  }