chyz 1.0.13-rc.2 → 1.0.13-rc.20

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 (89) hide show
  1. package/BaseChyz.ts +53 -15
  2. package/Doc/Moel kullanma.md +13 -0
  3. package/Examples/Controllers/ApiController.ts +35 -26
  4. package/Examples/Controllers/BasicApiController.ts +121 -0
  5. package/Examples/Controllers/KeyCloakController.ts +4 -4
  6. package/Examples/Controllers/SiteController.ts +18 -8
  7. package/Examples/Models/AuthAssignment.ts +50 -0
  8. package/Examples/Models/AuthItem.ts +59 -0
  9. package/Examples/Models/AuthItemChild.ts +49 -0
  10. package/Examples/Models/Categories.ts +14 -3
  11. package/Examples/Models/Customer.ts +2 -2
  12. package/Examples/Models/KeycloakUser.ts +4 -0
  13. package/Examples/Models/Order.ts +5 -5
  14. package/Examples/Models/OrderItem.ts +2 -2
  15. package/Examples/Models/ProductModels.ts +4 -5
  16. package/Examples/Models/ProductToCategories.ts +15 -4
  17. package/Examples/Models/Products.ts +9 -8
  18. package/Examples/Models/Stocks.ts +2 -2
  19. package/Examples/Models/User.ts +8 -1
  20. package/Examples/Models/UserPermission.ts +2 -2
  21. package/Examples/Models/index.ts +19 -0
  22. package/Examples/index.ts +6 -0
  23. package/Examples/log/app.log +9456 -0
  24. package/Examples/log/errors.log +1904 -0
  25. package/Examples/tsconfig.json +2 -1
  26. package/README.md +267 -16
  27. package/base/ActionFilter.ts +1 -1
  28. package/base/BaseError.ts +3 -1
  29. package/base/InvalidArgumentException.ts +16 -0
  30. package/base/Model.ts +69 -51
  31. package/base/ModelManager.ts +19 -0
  32. package/base/RestClient.ts +4 -4
  33. package/base/ValidationHttpException.ts +1 -1
  34. package/base/index.ts +2 -0
  35. package/dist/BaseChyz.js +46 -8
  36. package/dist/BaseChyz.js.map +1 -1
  37. package/dist/base/ActionFilter.js +1 -1
  38. package/dist/base/ActionFilter.js.map +1 -1
  39. package/dist/base/BaseError.js +5 -1
  40. package/dist/base/BaseError.js.map +1 -1
  41. package/dist/base/InvalidArgumentException.js +18 -0
  42. package/dist/base/InvalidArgumentException.js.map +1 -0
  43. package/dist/base/Model.js +44 -30
  44. package/dist/base/Model.js.map +1 -1
  45. package/dist/base/ModelManager.js +9 -0
  46. package/dist/base/ModelManager.js.map +1 -0
  47. package/dist/base/RestClient.js +4 -4
  48. package/dist/base/RestClient.js.map +1 -1
  49. package/dist/base/ValidationHttpException.js +1 -1
  50. package/dist/base/index.js +2 -0
  51. package/dist/base/index.js.map +1 -1
  52. package/dist/filters/AccessControl.js +15 -3
  53. package/dist/filters/AccessControl.js.map +1 -1
  54. package/dist/filters/AccessRule.js +99 -38
  55. package/dist/filters/AccessRule.js.map +1 -1
  56. package/dist/filters/auth/HttpBasicAuth.js +65 -0
  57. package/dist/filters/auth/HttpBasicAuth.js.map +1 -1
  58. package/dist/filters/auth/JwtHttpBearerAuth.js +1 -1
  59. package/dist/filters/auth/JwtHttpBearerAuth.js.map +1 -1
  60. package/dist/filters/auth/index.js +1 -0
  61. package/dist/filters/auth/index.js.map +1 -1
  62. package/dist/index.js +2 -3
  63. package/dist/index.js.map +1 -1
  64. package/dist/package.json +55 -0
  65. package/dist/rbac/AuthAssignment.js +45 -0
  66. package/dist/rbac/AuthAssignment.js.map +1 -0
  67. package/dist/rbac/AuthItem.js +52 -0
  68. package/dist/rbac/AuthItem.js.map +1 -0
  69. package/dist/rbac/AuthItemChild.js +44 -0
  70. package/dist/rbac/AuthItemChild.js.map +1 -0
  71. package/dist/rbac/AuthManager.js +359 -0
  72. package/dist/rbac/AuthManager.js.map +1 -0
  73. package/dist/web/WebUser.js +78 -0
  74. package/dist/web/WebUser.js.map +1 -1
  75. package/filters/AccessControl.ts +19 -6
  76. package/filters/AccessRule.ts +61 -16
  77. package/filters/auth/HttpBasicAuth.ts +68 -0
  78. package/filters/auth/JwtHttpBearerAuth.ts +1 -1
  79. package/filters/auth/index.ts +1 -0
  80. package/index.ts +2 -2
  81. package/package-lock.json +5259 -0
  82. package/package.json +6 -6
  83. package/rbac/AuthAssignment.ts +50 -0
  84. package/rbac/AuthItem.ts +57 -0
  85. package/rbac/AuthItemChild.ts +50 -0
  86. package/rbac/AuthManager.ts +398 -0
  87. package/web/IdentityInterface.ts +6 -0
  88. package/web/WebUser.ts +88 -1
  89. package/Examples/yarn.lock +0 -2549
@@ -10,35 +10,72 @@ var _ = require('lodash');
10
10
  import {Component} from "../base/Component";
11
11
  import {InvalidConfigException} from "../base/InvalidConfigException";
12
12
  import {Request, Response} from "express";
13
+ import Utils from "../requiments/Utils";
13
14
 
14
15
  export class AccessRule extends Component {
15
16
 
16
17
  /**
17
18
  * @var bool whether this is an 'allow' rule or 'deny' rule.
18
19
  */
19
- public allow:any;
20
+ public allow: any;
20
21
  /**
21
22
  * @var array list of action IDs that this rule applies to. The comparison is case-sensitive.
22
23
  * If not set or empty, it means this rule applies to all actions.
23
24
  */
24
- public actions:any;
25
+ public actions: any;
25
26
 
26
27
  /**
27
28
  * @var array list of the controller IDs that this rule applies to.
28
29
  */
29
- public controllers:any;
30
+ public controllers: any;
30
31
 
31
32
  /**
32
33
  * - `?`: matches a guest user (not authenticated yet)
33
34
  * - `@`: matches an authenticated user
34
35
  */
35
36
 
36
- public roles:any;
37
+ public roles: any;
37
38
 
38
39
  /**
39
40
  * @var array list of RBAC (Role-Based Access Control) permissions that this rules applies to.
40
41
  */
41
- public permissions:any;
42
+ public permissions: any;
43
+
44
+ /**
45
+ * @var array|Closure parameters to pass to the [[User::can()]] function for evaluating
46
+ * user permissions in [[$roles]].
47
+ *
48
+ * If this is an array, it will be passed directly to [[User::can()]]. For example for passing an
49
+ * ID from the current request, you may use the following:
50
+ *
51
+ * ```php
52
+ * ['postId' => Yii::$app->request->get('id')]
53
+ * ```
54
+ *
55
+ * You may also specify a closure that returns an array. This can be used to
56
+ * evaluate the array values only if they are needed, for example when a model needs to be
57
+ * loaded like in the following code:
58
+ *
59
+ * ```php
60
+ * 'rules' => [
61
+ * [
62
+ * 'allow' => true,
63
+ * 'actions' => ['update'],
64
+ * 'roles' => ['updatePost'],
65
+ * 'roleParams' => function($rule) {
66
+ * return ['post' => Post::findOne(Yii::$app->request->get('id'))];
67
+ * },
68
+ * ],
69
+ * ],
70
+ * ```
71
+ *
72
+ * A reference to the [[AccessRule]] instance will be passed to the closure as the first parameter.
73
+ *
74
+ * @see roles
75
+ * @since 2.0.12
76
+ */
77
+ public roleParams: any = [];
78
+
42
79
 
43
80
  /**
44
81
  * @var array list of user IP addresses that this rule applies to. An IP address
@@ -48,13 +85,13 @@ export class AccessRule extends Component {
48
85
  * 20-bit private network block in RFC1918.
49
86
  * If not set or empty, it means this rule applies to all IP addresses.
50
87
  */
51
- public ips:any;
88
+ public ips: any;
52
89
 
53
90
 
54
- public allows(action:any, user:WebUser, request:Request) {
91
+ public async allows(action: any, user: WebUser, request: Request) {
55
92
  if (
56
93
  this.matchAction(action)
57
- && this.matchRole(user)
94
+ && await this.matchRole(user)
58
95
  ) {
59
96
  return this.allow
60
97
  }
@@ -75,7 +112,7 @@ export class AccessRule extends Component {
75
112
  * @param Action $action the action
76
113
  * @return bool whether the rule applies to the action
77
114
  */
78
- protected matchAction(action:any) {
115
+ protected matchAction(action: any) {
79
116
  return _.isEmpty(this.actions) || this.actions.includes(action.id);
80
117
  }
81
118
 
@@ -83,7 +120,7 @@ export class AccessRule extends Component {
83
120
  * @param Controller $controller the controller
84
121
  * @return bool whether the rule applies to the controller
85
122
  */
86
- protected matchController(controller:any) {
123
+ protected matchController(controller: any) {
87
124
  // if (empty($this->controllers)) {
88
125
  // return true;
89
126
  // }
@@ -98,22 +135,23 @@ export class AccessRule extends Component {
98
135
  return false;
99
136
  }
100
137
 
101
- protected matchRole(user:any) {
102
- let items = _.isEmpty(this.roles) ? [] : this.roles;
138
+ protected async matchRole(user: WebUser) {
139
+ let items = Utils.isEmpty(this.roles) ? [] : this.roles;
103
140
 
104
- if (!_.isEmpty(this.permissions)) {
105
- items = _.merge(items, this.permissions);
141
+ if (!Utils.isEmpty(this.permissions)) {
142
+ items = Utils.merge(items, this.permissions);
106
143
  }
107
144
 
108
- if (_.isEmpty(items)) {
145
+ if (Utils.isEmpty(items)) {
109
146
  return true;
110
147
  }
111
148
 
112
149
 
113
- if (user === false) {
150
+ if (!user) {
114
151
  throw new InvalidConfigException('The user application component must be available to specify roles in AccessRule.');
115
152
  }
116
153
 
154
+ let roleParams: any = [];
117
155
  for (const itemsKey in items) {
118
156
  let item = items[itemsKey];
119
157
  if (item === '?') {
@@ -126,6 +164,13 @@ export class AccessRule extends Component {
126
164
  }
127
165
  } else {
128
166
  //roleparams
167
+ if (!Utils.isEmpty(this.roleParams)) {
168
+ roleParams = !Utils.isArray(this.roleParams) ? this.roleParams.apply(this) : this.roleParams;
169
+ }
170
+
171
+ if (await user.can(item, this.roleParams)) {
172
+ return true;
173
+ }
129
174
  }
130
175
  }
131
176
 
@@ -5,3 +5,71 @@
5
5
  * Github:https://github.com/cihan53/
6
6
  */
7
7
 
8
+ import {HttpHeaderAuth} from "./HttpHeaderAuth";
9
+ import {Request, Response} from "express";
10
+ import {WebUser} from "../../web/WebUser";
11
+ import Utils from "../../requiments/Utils";
12
+ import {AuthMethod} from "./AuthMethod";
13
+ import {InvalidConfigException} from "../../base";
14
+ import BaseChyz from "../../BaseChyz";
15
+
16
+ export class HttpBasicAuth extends AuthMethod {
17
+
18
+ /**
19
+ * @var string the HTTP header name
20
+ */
21
+ public header = 'Authorization';
22
+
23
+
24
+ /**
25
+ * @var string a pattern to use to extract the HTTP authentication value
26
+ */
27
+
28
+ public pattern = /^Basic\s+(.*?)$/;
29
+
30
+
31
+ /**
32
+ * @throws InvalidConfigException
33
+ */
34
+ public init(): void {
35
+ super.init();
36
+
37
+ if (!this.pattern) {
38
+ throw new InvalidConfigException('You must provide pattern to use to extract the HTTP authentication value!');
39
+ }
40
+
41
+ this.user = BaseChyz.getComponent("user") ?? null;
42
+ }
43
+
44
+
45
+ async authenticate(user: WebUser, request:Request, response:Response) {
46
+
47
+
48
+ let autHeader = this.getHeaderByKey(request.headers, this.header)
49
+ if (autHeader == null || (autHeader = this.patternCheck(autHeader, this.pattern)) == null) {
50
+ return null;
51
+ }
52
+
53
+ let basicauth = autHeader[1].split(":")
54
+
55
+ let identity = await user.loginByAccessToken(basicauth, "HttpBasicAuth");
56
+ if (identity === null) {
57
+ this.challenge(response);
58
+ this.handleFailure(response);
59
+ }
60
+
61
+ return identity;
62
+
63
+
64
+ return null;
65
+ }
66
+
67
+
68
+ /**
69
+ * @throws UnauthorizedHttpException
70
+ */
71
+ public fail(response:Response): void {
72
+ this.challenge(response)
73
+ this.handleFailure(response);
74
+ }
75
+ }
@@ -44,7 +44,7 @@ export class JwtHttpBearerAuth extends HttpBearerAuth {
44
44
  return null;
45
45
  }
46
46
 
47
- BaseChyz.debug("JSON Web Token.",autHeader);
47
+ // BaseChyz.debug("JSON Web Token.",autHeader);
48
48
  let identity = null;
49
49
  let token = null;
50
50
 
@@ -1,3 +1,4 @@
1
1
  export * from "./JwtHttpBearerAuth"
2
2
  export * from "./HttpBearerAuth"
3
3
  export * from "./HttpHeaderAuth"
4
+ export * from "./HttpBasicAuth"
package/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export {Request, Response, NextFunction} from "express";
2
2
  export * from "./base";
3
- export * from "./filters";
4
- export {JwtHttpBearerAuth} from "./filters/auth";
3
+ export * from "./filters";
4
+ export * from "./filters/auth";
5
5
  export * from "./decorator";
6
6
  export * from "./requiments/Utils";
7
7
  export {RouteDefinition} from "./model/RouteDefinition";