chyz 1.0.13-rc.2 → 1.0.13-rc.23
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/BaseChyz.ts +60 -18
- package/Doc/Moel kullanma.md +13 -0
- package/Examples/Controllers/ApiController.ts +35 -26
- package/Examples/Controllers/BasicApiController.ts +121 -0
- package/Examples/Controllers/KeyCloakController.ts +4 -4
- package/Examples/Controllers/SiteController.ts +18 -8
- package/Examples/Models/AuthAssignment.ts +50 -0
- package/Examples/Models/AuthItem.ts +59 -0
- package/Examples/Models/AuthItemChild.ts +49 -0
- package/Examples/Models/Categories.ts +14 -3
- package/Examples/Models/Customer.ts +2 -2
- package/Examples/Models/KeycloakUser.ts +4 -0
- package/Examples/Models/Order.ts +5 -5
- package/Examples/Models/OrderItem.ts +2 -2
- package/Examples/Models/ProductModels.ts +4 -5
- package/Examples/Models/ProductToCategories.ts +15 -4
- package/Examples/Models/Products.ts +9 -8
- package/Examples/Models/Stocks.ts +2 -2
- package/Examples/Models/User.ts +8 -1
- package/Examples/Models/UserPermission.ts +2 -2
- package/Examples/Models/index.ts +19 -0
- package/Examples/index.ts +20 -1
- package/Examples/log/app.log +9456 -0
- package/Examples/log/errors.log +1904 -0
- package/Examples/tsconfig.json +2 -1
- package/README.md +267 -16
- package/base/ActionFilter.ts +1 -1
- package/base/BaseError.ts +4 -2
- package/base/InvalidArgumentException.ts +16 -0
- package/base/Model.ts +81 -55
- package/base/ModelManager.ts +19 -0
- package/base/RestClient.ts +4 -4
- package/base/ValidationHttpException.ts +1 -1
- package/base/index.ts +2 -0
- package/dist/BaseChyz.js +48 -9
- package/dist/BaseChyz.js.map +1 -1
- package/dist/base/ActionFilter.js +1 -1
- package/dist/base/ActionFilter.js.map +1 -1
- package/dist/base/BaseError.js +6 -2
- package/dist/base/BaseError.js.map +1 -1
- package/dist/base/InvalidArgumentException.js +18 -0
- package/dist/base/InvalidArgumentException.js.map +1 -0
- package/dist/base/Model.js +47 -32
- package/dist/base/Model.js.map +1 -1
- package/dist/base/ModelManager.js +9 -0
- package/dist/base/ModelManager.js.map +1 -0
- package/dist/base/RestClient.js +4 -4
- package/dist/base/RestClient.js.map +1 -1
- package/dist/base/ValidationHttpException.js +1 -1
- package/dist/base/index.js +2 -0
- package/dist/base/index.js.map +1 -1
- package/dist/filters/AccessControl.js +15 -3
- package/dist/filters/AccessControl.js.map +1 -1
- package/dist/filters/AccessRule.js +99 -38
- package/dist/filters/AccessRule.js.map +1 -1
- package/dist/filters/auth/HttpBasicAuth.js +65 -0
- package/dist/filters/auth/HttpBasicAuth.js.map +1 -1
- package/dist/filters/auth/JwtHttpBearerAuth.js +1 -1
- package/dist/filters/auth/JwtHttpBearerAuth.js.map +1 -1
- package/dist/filters/auth/index.js +1 -0
- package/dist/filters/auth/index.js.map +1 -1
- package/dist/index.js +2 -3
- package/dist/index.js.map +1 -1
- package/dist/package.json +55 -0
- package/dist/rbac/AuthAssignment.js +45 -0
- package/dist/rbac/AuthAssignment.js.map +1 -0
- package/dist/rbac/AuthItem.js +52 -0
- package/dist/rbac/AuthItem.js.map +1 -0
- package/dist/rbac/AuthItemChild.js +44 -0
- package/dist/rbac/AuthItemChild.js.map +1 -0
- package/dist/rbac/AuthManager.js +359 -0
- package/dist/rbac/AuthManager.js.map +1 -0
- package/dist/web/WebUser.js +78 -0
- package/dist/web/WebUser.js.map +1 -1
- package/filters/AccessControl.ts +19 -6
- package/filters/AccessRule.ts +61 -16
- package/filters/auth/HttpBasicAuth.ts +68 -0
- package/filters/auth/JwtHttpBearerAuth.ts +1 -1
- package/filters/auth/index.ts +1 -0
- package/index.ts +2 -2
- package/package-lock.json +5259 -0
- package/package.json +6 -6
- package/rbac/AuthAssignment.ts +50 -0
- package/rbac/AuthItem.ts +57 -0
- package/rbac/AuthItemChild.ts +50 -0
- package/rbac/AuthManager.ts +398 -0
- package/web/IdentityInterface.ts +6 -0
- package/web/WebUser.ts +88 -1
- package/Examples/yarn.lock +0 -2549
package/filters/AccessRule.ts
CHANGED
|
@@ -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:
|
|
102
|
-
let items =
|
|
138
|
+
protected async matchRole(user: WebUser) {
|
|
139
|
+
let items = Utils.isEmpty(this.roles) ? [] : this.roles;
|
|
103
140
|
|
|
104
|
-
if (!
|
|
105
|
-
items =
|
|
141
|
+
if (!Utils.isEmpty(this.permissions)) {
|
|
142
|
+
items = Utils.merge(items, this.permissions);
|
|
106
143
|
}
|
|
107
144
|
|
|
108
|
-
if (
|
|
145
|
+
if (Utils.isEmpty(items)) {
|
|
109
146
|
return true;
|
|
110
147
|
}
|
|
111
148
|
|
|
112
149
|
|
|
113
|
-
if (user
|
|
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
|
+
}
|
package/filters/auth/index.ts
CHANGED
package/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export {Request, Response, NextFunction} from "express";
|
|
2
2
|
export * from "./base";
|
|
3
|
-
export *
|
|
4
|
-
export
|
|
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";
|