chyz 2.0.1-rc.3 → 2.0.1-rc.36

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 (229) hide show
  1. package/BaseChyz.ts +623 -0
  2. package/README.md +518 -518
  3. package/base/ActionFilter.ts +88 -0
  4. package/base/BadRequestHttpException.ts +14 -0
  5. package/base/{BaseError.js → BaseError.ts} +26 -18
  6. package/base/Behavior.ts +6 -0
  7. package/base/CBaseObject.ts +17 -0
  8. package/base/CEvents.ts +24 -0
  9. package/base/{CRequest.js → CRequest.ts} +6 -2
  10. package/base/CWebController.ts +90 -0
  11. package/base/Component.ts +66 -0
  12. package/{model/RouteDefinition.js → base/Configurable.ts} +4 -2
  13. package/base/DataErrorDbException.ts +16 -0
  14. package/base/DbConnection.ts +55 -0
  15. package/base/ForbiddenHttpException.ts +15 -0
  16. package/base/InvalidArgumentException.ts +16 -0
  17. package/base/InvalidConfigException.ts +16 -0
  18. package/base/{Logs.js → Logs.ts} +41 -24
  19. package/base/{Model.js → Model.ts} +225 -158
  20. package/base/{ModelManager.js → ModelManager.ts} +7 -7
  21. package/base/NotFoundHttpException.ts +14 -0
  22. package/base/RestClient.ts +26 -0
  23. package/base/UnauthorizedHttpException.ts +17 -0
  24. package/base/ValidationHttpException.ts +14 -0
  25. package/base/db/Exception.ts +14 -0
  26. package/base/index.ts +19 -0
  27. package/decorator/Middleware.ts +9 -0
  28. package/decorator/{controller.js → controller.ts} +7 -9
  29. package/decorator/{delete.js → delete.ts} +9 -9
  30. package/decorator/enums/ControllerDecoratorParams.ts +5 -0
  31. package/decorator/{get.js → get.ts} +11 -10
  32. package/decorator/index.ts +5 -0
  33. package/decorator/{post.js → post.ts} +10 -10
  34. package/decorator/{put.js → put.ts} +9 -9
  35. package/filters/AccessControl.ts +74 -0
  36. package/filters/AccessRule.ts +182 -0
  37. package/filters/auth/{AuthInterface.d.ts → AuthInterface.ts} +15 -7
  38. package/filters/auth/AuthMethod.ts +100 -0
  39. package/filters/auth/HttpBasicAuth.ts +79 -0
  40. package/filters/auth/HttpBearerAuth.ts +34 -0
  41. package/filters/auth/HttpHeaderAuth.ts +61 -0
  42. package/filters/auth/JwtHttpBearerAuth.ts +83 -0
  43. package/filters/auth/KeyCloakHttpBearerAuth.ts +114 -0
  44. package/filters/auth/index.ts +4 -0
  45. package/filters/index.ts +2 -0
  46. package/index.ts +80 -0
  47. package/log/config/log4js.json +55 -55
  48. package/model/RouteDefinition.ts +18 -0
  49. package/package.json +63 -61
  50. package/rbac/AuthAssignment.ts +50 -0
  51. package/rbac/{AuthItem.js → AuthItem.ts} +22 -16
  52. package/rbac/{AuthItemChild.js → AuthItemChild.ts} +21 -14
  53. package/rbac/AuthManager.ts +399 -0
  54. package/rbac/index.ts +12 -0
  55. package/{web/IdentityInterface.js → requiments/Glob.ts} +3 -2
  56. package/requiments/{ReflectUtil.js → ReflectUtil.ts} +6 -4
  57. package/requiments/{Utils.js → Utils.ts} +40 -34
  58. package/validators/BooleanValidator.ts +0 -0
  59. package/validators/CompareValidator.ts +0 -0
  60. package/validators/DateValidator.ts +0 -0
  61. package/validators/EmailValidator.ts +0 -0
  62. package/validators/Validator.ts +27 -0
  63. package/web/{IdentityInterface.d.ts → IdentityInterface.ts} +15 -2
  64. package/web/WebUser.ts +187 -0
  65. package/BaseChyz.d.ts +0 -74
  66. package/BaseChyz.d.ts.map +0 -1
  67. package/BaseChyz.js +0 -569
  68. package/base/ActionFilter.d.ts +0 -33
  69. package/base/ActionFilter.d.ts.map +0 -1
  70. package/base/ActionFilter.js +0 -90
  71. package/base/BadRequestHttpException.d.ts +0 -6
  72. package/base/BadRequestHttpException.d.ts.map +0 -1
  73. package/base/BadRequestHttpException.js +0 -17
  74. package/base/BaseError.d.ts +0 -44
  75. package/base/BaseError.d.ts.map +0 -1
  76. package/base/Behavior.d.ts +0 -4
  77. package/base/Behavior.d.ts.map +0 -1
  78. package/base/Behavior.js +0 -7
  79. package/base/CBaseObject.d.ts +0 -5
  80. package/base/CBaseObject.d.ts.map +0 -1
  81. package/base/CBaseObject.js +0 -16
  82. package/base/CEvents.d.ts +0 -13
  83. package/base/CEvents.d.ts.map +0 -1
  84. package/base/CEvents.js +0 -24
  85. package/base/CRequest.d.ts +0 -2
  86. package/base/CRequest.d.ts.map +0 -1
  87. package/base/CWebController.d.ts +0 -44
  88. package/base/CWebController.d.ts.map +0 -1
  89. package/base/CWebController.js +0 -81
  90. package/base/Component.d.ts +0 -21
  91. package/base/Component.d.ts.map +0 -1
  92. package/base/Component.js +0 -57
  93. package/base/Configurable.d.ts +0 -3
  94. package/base/Configurable.d.ts.map +0 -1
  95. package/base/Configurable.js +0 -8
  96. package/base/DataErrorDbException.d.ts +0 -6
  97. package/base/DataErrorDbException.d.ts.map +0 -1
  98. package/base/DataErrorDbException.js +0 -17
  99. package/base/DbConnection.d.ts +0 -13
  100. package/base/DbConnection.d.ts.map +0 -1
  101. package/base/DbConnection.js +0 -53
  102. package/base/ForbiddenHttpException.d.ts +0 -5
  103. package/base/ForbiddenHttpException.d.ts.map +0 -1
  104. package/base/ForbiddenHttpException.js +0 -16
  105. package/base/InvalidArgumentException.d.ts +0 -6
  106. package/base/InvalidArgumentException.d.ts.map +0 -1
  107. package/base/InvalidArgumentException.js +0 -17
  108. package/base/InvalidConfigException.d.ts +0 -6
  109. package/base/InvalidConfigException.d.ts.map +0 -1
  110. package/base/InvalidConfigException.js +0 -17
  111. package/base/Logs.d.ts +0 -22
  112. package/base/Logs.d.ts.map +0 -1
  113. package/base/Model.d.ts +0 -302
  114. package/base/Model.d.ts.map +0 -1
  115. package/base/ModelManager.d.ts +0 -2
  116. package/base/ModelManager.d.ts.map +0 -1
  117. package/base/NotFoundHttpException.d.ts +0 -6
  118. package/base/NotFoundHttpException.d.ts.map +0 -1
  119. package/base/NotFoundHttpException.js +0 -17
  120. package/base/RestClient.d.ts +0 -7
  121. package/base/RestClient.d.ts.map +0 -1
  122. package/base/RestClient.js +0 -25
  123. package/base/UnauthorizedHttpException.d.ts +0 -6
  124. package/base/UnauthorizedHttpException.d.ts.map +0 -1
  125. package/base/UnauthorizedHttpException.js +0 -17
  126. package/base/ValidationHttpException.d.ts +0 -6
  127. package/base/ValidationHttpException.d.ts.map +0 -1
  128. package/base/ValidationHttpException.js +0 -17
  129. package/base/db/Exception.d.ts +0 -7
  130. package/base/db/Exception.d.ts.map +0 -1
  131. package/base/db/Exception.js +0 -15
  132. package/base/index.d.ts +0 -20
  133. package/base/index.d.ts.map +0 -1
  134. package/base/index.js +0 -41
  135. package/decorator/Middleware.d.ts +0 -4
  136. package/decorator/Middleware.d.ts.map +0 -1
  137. package/decorator/Middleware.js +0 -11
  138. package/decorator/controller.d.ts +0 -3
  139. package/decorator/controller.d.ts.map +0 -1
  140. package/decorator/delete.d.ts +0 -3
  141. package/decorator/delete.d.ts.map +0 -1
  142. package/decorator/enums/ControllerDecoratorParams.d.ts +0 -6
  143. package/decorator/enums/ControllerDecoratorParams.d.ts.map +0 -1
  144. package/decorator/enums/ControllerDecoratorParams.js +0 -9
  145. package/decorator/get.d.ts +0 -3
  146. package/decorator/get.d.ts.map +0 -1
  147. package/decorator/index.d.ts +0 -6
  148. package/decorator/index.d.ts.map +0 -1
  149. package/decorator/index.js +0 -13
  150. package/decorator/post.d.ts +0 -3
  151. package/decorator/post.d.ts.map +0 -1
  152. package/decorator/put.d.ts +0 -3
  153. package/decorator/put.d.ts.map +0 -1
  154. package/filters/AccessControl.d.ts +0 -13
  155. package/filters/AccessControl.d.ts.map +0 -1
  156. package/filters/AccessControl.js +0 -83
  157. package/filters/AccessRule.d.ts +0 -83
  158. package/filters/AccessRule.d.ts.map +0 -1
  159. package/filters/AccessRule.js +0 -136
  160. package/filters/auth/AuthInterface.d.ts.map +0 -1
  161. package/filters/auth/AuthInterface.js +0 -2
  162. package/filters/auth/AuthMethod.d.ts +0 -38
  163. package/filters/auth/AuthMethod.d.ts.map +0 -1
  164. package/filters/auth/AuthMethod.js +0 -82
  165. package/filters/auth/HttpBasicAuth.d.ts +0 -24
  166. package/filters/auth/HttpBasicAuth.d.ts.map +0 -1
  167. package/filters/auth/HttpBasicAuth.js +0 -78
  168. package/filters/auth/HttpBearerAuth.d.ts +0 -18
  169. package/filters/auth/HttpBearerAuth.d.ts.map +0 -1
  170. package/filters/auth/HttpBearerAuth.js +0 -33
  171. package/filters/auth/HttpHeaderAuth.d.ts +0 -19
  172. package/filters/auth/HttpHeaderAuth.d.ts.map +0 -1
  173. package/filters/auth/HttpHeaderAuth.js +0 -64
  174. package/filters/auth/JwtHttpBearerAuth.d.ts +0 -21
  175. package/filters/auth/JwtHttpBearerAuth.d.ts.map +0 -1
  176. package/filters/auth/JwtHttpBearerAuth.js +0 -87
  177. package/filters/auth/KeyCloakHttpBearerAuth.d.ts +0 -23
  178. package/filters/auth/KeyCloakHttpBearerAuth.d.ts.map +0 -1
  179. package/filters/auth/KeyCloakHttpBearerAuth.js +0 -116
  180. package/filters/auth/index.d.ts +0 -5
  181. package/filters/auth/index.d.ts.map +0 -1
  182. package/filters/auth/index.js +0 -11
  183. package/filters/index.d.ts +0 -3
  184. package/filters/index.d.ts.map +0 -1
  185. package/filters/index.js +0 -7
  186. package/index.d.ts +0 -26
  187. package/index.d.ts.map +0 -1
  188. package/index.js +0 -72
  189. package/model/RouteDefinition.d.ts +0 -7
  190. package/model/RouteDefinition.d.ts.map +0 -1
  191. package/rbac/AuthAssignment.d.ts +0 -19
  192. package/rbac/AuthAssignment.d.ts.map +0 -1
  193. package/rbac/AuthAssignment.js +0 -44
  194. package/rbac/AuthItem.d.ts +0 -27
  195. package/rbac/AuthItem.d.ts.map +0 -1
  196. package/rbac/AuthItemChild.d.ts +0 -19
  197. package/rbac/AuthItemChild.d.ts.map +0 -1
  198. package/rbac/AuthManager.d.ts +0 -112
  199. package/rbac/AuthManager.d.ts.map +0 -1
  200. package/rbac/AuthManager.js +0 -356
  201. package/rbac/index.d.ts +0 -5
  202. package/rbac/index.d.ts.map +0 -1
  203. package/rbac/index.js +0 -28
  204. package/requiments/Glob.d.ts +0 -3
  205. package/requiments/Glob.d.ts.map +0 -1
  206. package/requiments/Glob.js +0 -10
  207. package/requiments/ReflectUtil.d.ts +0 -1
  208. package/requiments/ReflectUtil.d.ts.map +0 -1
  209. package/requiments/Utils.d.ts +0 -2
  210. package/requiments/Utils.d.ts.map +0 -1
  211. package/validators/BooleanValidator.d.ts +0 -1
  212. package/validators/BooleanValidator.d.ts.map +0 -1
  213. package/validators/BooleanValidator.js +0 -1
  214. package/validators/CompareValidator.d.ts +0 -1
  215. package/validators/CompareValidator.d.ts.map +0 -1
  216. package/validators/CompareValidator.js +0 -1
  217. package/validators/DateValidator.d.ts +0 -1
  218. package/validators/DateValidator.d.ts.map +0 -1
  219. package/validators/DateValidator.js +0 -1
  220. package/validators/EmailValidator.d.ts +0 -1
  221. package/validators/EmailValidator.d.ts.map +0 -1
  222. package/validators/EmailValidator.js +0 -1
  223. package/validators/Validator.d.ts +0 -18
  224. package/validators/Validator.d.ts.map +0 -1
  225. package/validators/Validator.js +0 -27
  226. package/web/IdentityInterface.d.ts.map +0 -1
  227. package/web/WebUser.d.ts +0 -72
  228. package/web/WebUser.d.ts.map +0 -1
  229. package/web/WebUser.js +0 -165
package/base/index.ts ADDED
@@ -0,0 +1,19 @@
1
+ export {Logs} from "./Logs"
2
+ export {BaseError} from "./BaseError"
3
+ export {ActionFilter} from "./ActionFilter"
4
+ export {Behavior} from "./Behavior"
5
+ export {Component} from "./Component"
6
+ export {Configurable} from "./Configurable"
7
+ export {CWebController} from "./CWebController"
8
+ export {DbConnection} from "./DbConnection"
9
+ export {ForbiddenHttpException} from "./ForbiddenHttpException"
10
+ export {InvalidConfigException} from "./InvalidConfigException"
11
+ export {InvalidArgumentException} from "./InvalidArgumentException"
12
+ export {NotFoundHttpException} from "./NotFoundHttpException"
13
+ export {UnauthorizedHttpException} from "./UnauthorizedHttpException"
14
+ export {DataErrorDbException} from "./DataErrorDbException"
15
+ export {ValidationHttpException} from "./ValidationHttpException"
16
+ export {Model, Relation, DataTypes, NOW} from "./Model"
17
+ export {RestClient} from "./RestClient"
18
+ export {ModelManager} from "./ModelManager"
19
+ export {CEvents} from "./CEvents"
@@ -0,0 +1,9 @@
1
+ import "reflect-metadata";
2
+ import { ControllerDecoratorParams } from "./enums/ControllerDecoratorParams";
3
+ import { RequestHandler } from "express";
4
+
5
+ export function Middleware(middlewares: RequestHandler[]): Function {
6
+ return function(target: any, propertyKey: string): void {
7
+ Reflect.defineMetadata(ControllerDecoratorParams.Middleware, middlewares, target, propertyKey);
8
+ }
9
+ }
@@ -1,24 +1,22 @@
1
- "use strict";
2
1
  /*
3
2
  * Copyright (c) 2021. Chy Bilgisayar Bilisim
4
3
  * Author: Cihan Ozturk
5
4
  * E-mail: cihan@chy.com.tr
6
5
  * Github:https://github.com/cihan53/
7
6
  */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.controller = void 0;
7
+
10
8
  // @ts-ignore
11
- require("reflect-metadata");
12
- const controller = (prefix = '') => {
13
- return (target) => {
9
+ import "reflect-metadata";
10
+ export const controller = (prefix: string = ''): ClassDecorator => {
11
+ return (target: any) => {
14
12
  // @ts-ignore
15
13
  Reflect.defineMetadata('prefix', prefix, target);
14
+
16
15
  // Since routes are set by our methods this should almost never be true (except the controller has no methods)
17
16
  // @ts-ignore
18
- if (!Reflect.hasMetadata('routes', target)) {
17
+ if (! Reflect.hasMetadata('routes', target)) {
19
18
  // @ts-ignore
20
19
  Reflect.defineMetadata('routes', [], target);
21
20
  }
22
21
  };
23
- };
24
- exports.controller = controller;
22
+ };
@@ -1,6 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.del = void 0;
4
1
  /*
5
2
  * Copyright (c) 2021. Chy Bilgisayar Bilisim
6
3
  * Author: Cihan Ozturk
@@ -8,11 +5,13 @@ exports.del = void 0;
8
5
  * Github:https://github.com/cihan53/
9
6
  */
10
7
  // @ts-ignore
11
- require("reflect-metadata");
12
- const del = (path) => {
8
+ import "reflect-metadata";
9
+ import {RouteDefinition} from '../model/RouteDefinition';
10
+
11
+ export const del = (path: string): MethodDecorator => {
13
12
  // `target` equals our class, `propertyKey` equals our decorated method name
14
13
  // @ts-ignore
15
- return (target, propertyKey) => {
14
+ return (target, propertyKey: string): void => {
16
15
  // In case this is the first route to be registered the `routes` metadata is likely to be undefined at this point.
17
16
  // To prevent any further validation simply set it to an empty array here.
18
17
  // @ts-ignore
@@ -20,11 +19,13 @@ const del = (path) => {
20
19
  // @ts-ignore
21
20
  Reflect.defineMetadata('routes', [], target.constructor);
22
21
  }
22
+
23
23
  // Get the routes stored so far, extend it by the new route and re-set the metadata.
24
24
  // @ts-ignore
25
- const routes = Reflect.getMetadata('routes', target.constructor);
25
+ const routes = Reflect.getMetadata('routes', target.constructor) as Array<RouteDefinition>;
26
+
26
27
  routes.push({
27
- id: "",
28
+ id:"",
28
29
  requestMethod: 'delete',
29
30
  path,
30
31
  methodName: propertyKey
@@ -33,4 +34,3 @@ const del = (path) => {
33
34
  Reflect.defineMetadata('routes', routes, target.constructor);
34
35
  };
35
36
  };
36
- exports.del = del;
@@ -0,0 +1,5 @@
1
+ export enum ControllerDecoratorParams {
2
+ Path = 'path',
3
+ Method = 'method',
4
+ Middleware = 'middleware'
5
+ }
@@ -1,6 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.get = void 0;
4
1
  /*
5
2
  * Copyright (c) 2021. Chy Bilgisayar Bilisim
6
3
  * Author: Cihan Ozturk
@@ -8,11 +5,13 @@ exports.get = void 0;
8
5
  * Github:https://github.com/cihan53/
9
6
  */
10
7
  // @ts-ignore
11
- require("reflect-metadata");
12
- const get = (path) => {
8
+ import "reflect-metadata";
9
+ import {RouteDefinition} from '../model/RouteDefinition';
10
+
11
+ export const get = (path: string): MethodDecorator => {
13
12
  // `target` equals our class, `propertyKey` equals our decorated method name
14
13
  // @ts-ignore
15
- return (target, propertyKey) => {
14
+ return (target, propertyKey: string): void => {
16
15
  // In case this is the first route to be registered the `routes` metadata is likely to be undefined at this point.
17
16
  // To prevent any further validation simply set it to an empty array here.
18
17
  // @ts-ignore
@@ -20,17 +19,19 @@ const get = (path) => {
20
19
  // @ts-ignore
21
20
  Reflect.defineMetadata('routes', [], target.constructor);
22
21
  }
22
+
23
23
  // Get the routes stored so far, extend it by the new route and re-set the metadata.
24
24
  // @ts-ignore
25
- const routes = Reflect.getMetadata('routes', target.constructor);
25
+ const routes = Reflect.getMetadata('routes', target.constructor) as Array<RouteDefinition>;
26
+
26
27
  routes.push({
27
- id: "",
28
+ id:"",
28
29
  requestMethod: 'get',
29
30
  path,
30
31
  methodName: propertyKey
31
32
  });
33
+
32
34
  // @ts-ignore
33
35
  Reflect.defineMetadata('routes', routes, target.constructor);
34
36
  };
35
- };
36
- exports.get = get;
37
+ };
@@ -0,0 +1,5 @@
1
+ export {controller} from "./controller"
2
+ export {get} from "./get"
3
+ export {post} from "./post"
4
+ export {put} from "./put"
5
+ export {del} from "./delete"
@@ -1,6 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.post = void 0;
4
1
  /*
5
2
  * Copyright (c) 2021. Chy Bilgisayar Bilisim
6
3
  * Author: Cihan Ozturk
@@ -8,11 +5,13 @@ exports.post = void 0;
8
5
  * Github:https://github.com/cihan53/
9
6
  */
10
7
  // @ts-ignore
11
- require("reflect-metadata");
12
- const post = (path) => {
8
+ import "reflect-metadata";
9
+ import {RouteDefinition} from '../model/RouteDefinition';
10
+
11
+ export const post = (path: string): MethodDecorator => {
13
12
  // `target` equals our class, `propertyKey` equals our decorated method name
14
13
  // @ts-ignore
15
- return (target, propertyKey) => {
14
+ return (target, propertyKey: string): void => {
16
15
  // In case this is the first route to be registered the `routes` metadata is likely to be undefined at this point.
17
16
  // To prevent any further validation simply set it to an empty array here.
18
17
  // @ts-ignore
@@ -20,11 +19,13 @@ const post = (path) => {
20
19
  // @ts-ignore
21
20
  Reflect.defineMetadata('routes', [], target.constructor);
22
21
  }
22
+
23
23
  // Get the routes stored so far, extend it by the new route and re-set the metadata.
24
24
  // @ts-ignore
25
- const routes = Reflect.getMetadata('routes', target.constructor);
25
+ const routes = Reflect.getMetadata('routes', target.constructor) as Array<RouteDefinition>;
26
+
26
27
  routes.push({
27
- id: "",
28
+ id:"",
28
29
  requestMethod: 'post',
29
30
  path,
30
31
  methodName: propertyKey
@@ -32,5 +33,4 @@ const post = (path) => {
32
33
  // @ts-ignore
33
34
  Reflect.defineMetadata('routes', routes, target.constructor);
34
35
  };
35
- };
36
- exports.post = post;
36
+ };
@@ -1,6 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.put = void 0;
4
1
  /*
5
2
  * Copyright (c) 2021. Chy Bilgisayar Bilisim
6
3
  * Author: Cihan Ozturk
@@ -8,11 +5,13 @@ exports.put = void 0;
8
5
  * Github:https://github.com/cihan53/
9
6
  */
10
7
  // @ts-ignore
11
- require("reflect-metadata");
12
- const put = (path) => {
8
+ import "reflect-metadata";
9
+ import {RouteDefinition} from '../model/RouteDefinition';
10
+
11
+ export const put = (path: string): MethodDecorator => {
13
12
  // `target` equals our class, `propertyKey` equals our decorated method name
14
13
  // @ts-ignore
15
- return (target, propertyKey) => {
14
+ return (target, propertyKey: string): void => {
16
15
  // In case this is the first route to be registered the `routes` metadata is likely to be undefined at this point.
17
16
  // To prevent any further validation simply set it to an empty array here.
18
17
  // @ts-ignore
@@ -20,11 +19,13 @@ const put = (path) => {
20
19
  // @ts-ignore
21
20
  Reflect.defineMetadata('routes', [], target.constructor);
22
21
  }
22
+
23
23
  // Get the routes stored so far, extend it by the new route and re-set the metadata.
24
24
  // @ts-ignore
25
- const routes = Reflect.getMetadata('routes', target.constructor);
25
+ const routes = Reflect.getMetadata('routes', target.constructor) as Array<RouteDefinition>;
26
+
26
27
  routes.push({
27
- id: "",
28
+ id:"",
28
29
  requestMethod: 'put',
29
30
  path,
30
31
  methodName: propertyKey
@@ -33,4 +34,3 @@ const put = (path) => {
33
34
  Reflect.defineMetadata('routes', routes, target.constructor);
34
35
  };
35
36
  };
36
- exports.put = put;
@@ -0,0 +1,74 @@
1
+ /*
2
+ * Copyright (c) 2021. Chy Bilgisayar Bilisim
3
+ * Author: Cihan Ozturk
4
+ * E-mail: cihan@chy.com.tr
5
+ * Github:https://github.com/cihan53/
6
+ */
7
+ import {Request,Response,BaseChyz} from "../index";
8
+ import {ForbiddenHttpException} from "../base";
9
+ import {ActionFilter} from "../base";
10
+ import {AccessRule} from "./AccessRule";
11
+ import {WebUser} from "../web/WebUser";
12
+ import {Utils} from "../requiments/Utils";
13
+
14
+
15
+ export class AccessControl extends ActionFilter {
16
+
17
+ public user: any = null;
18
+ public rules: any;
19
+ public denyCallback: any = null;
20
+
21
+ public init() {
22
+ super.init()
23
+
24
+ if (this.user == undefined) {
25
+ this.user = Utils.cloneDeep(BaseChyz.getComponent("user")) ?? new WebUser();
26
+ }
27
+
28
+
29
+ this.rules.forEach((rule: any, index: number) => {
30
+ if (rule === Object(rule)) {
31
+ this.rules[index] = Utils.createObject(new AccessRule(), rule);
32
+ }
33
+ })
34
+ }
35
+
36
+
37
+ public async beforeAction(action: any, request: Request,res:Response) {
38
+ let allow;
39
+ // @ts-ignore
40
+ let user = request.user ?? this.user;
41
+ // @ts-ignore
42
+ user.identity = request.identity ?? null;
43
+
44
+ for (const rulesKey in this.rules) {
45
+ let rule = this.rules[rulesKey];
46
+ if ((allow = await rule.allows(action, user, request))) {
47
+ return true;
48
+ } else if (allow === false) {
49
+ if (this.denyCallback != null) {
50
+ rule.denyCallback.apply(rule, action);
51
+ } else {
52
+ this.denyAccess(user);
53
+ }
54
+ return false;
55
+ }
56
+ }
57
+
58
+
59
+ if (this.denyCallback != null) {
60
+ this.denyCallback.apply(null, action);
61
+ } else {
62
+ this.denyAccess(user);
63
+ }
64
+
65
+ return false;
66
+ }
67
+
68
+ public denyAccess(user: WebUser) {
69
+ if (user != undefined && user.getIsGuest()) {
70
+ user.loginRequired();
71
+ } else throw new ForbiddenHttpException(BaseChyz.t('You are not allowed to perform this action.'));
72
+ }
73
+
74
+ }
@@ -0,0 +1,182 @@
1
+ /*
2
+ * Copyright (c) 2021. Chy Bilgisayar Bilisim
3
+ * Author: Cihan Ozturk
4
+ * E-mail: cihan@chy.com.tr
5
+ * Github:https://github.com/cihan53/
6
+ */
7
+ import {WebUser} from "../web/WebUser";
8
+ import {Component} from "../base";
9
+ import {InvalidConfigException} from "../base";
10
+ import {Request} from "express";
11
+ import {Utils} from "../requiments/Utils";
12
+
13
+
14
+ export class AccessRule extends Component {
15
+
16
+ /**
17
+ * @var allow whether this is an 'allow' rule or 'deny' rule.
18
+ */
19
+ public allow: any;
20
+ /**
21
+ * @var array list of action IDs that this rule applies to. The comparison is case-sensitive.
22
+ * If not set or empty, it means this rule applies to all actions.
23
+ */
24
+ public actions: any;
25
+
26
+ /**
27
+ * @var array list of the controller IDs that this rule applies to.
28
+ */
29
+ public controllers: any;
30
+
31
+ /**
32
+ * - `?`: matches a guest user (not authenticated yet)
33
+ * - `@`: matches an authenticated user
34
+ */
35
+
36
+ public roles: any;
37
+
38
+ /**
39
+ * @var array list of RBAC (Role-Based Access Control) permissions that this rules applies to.
40
+ */
41
+ public permissions: any;
42
+
43
+ /**
44
+ * @var array|Closure parameters to pass to the [[User::can()]] function for evaluating
45
+ * user permissions in [[$roles]].
46
+ *
47
+ * If this is an array, it will be passed directly to [[User::can()]]. For example for passing an
48
+ * ID from the current request, you may use the following:
49
+ *
50
+ * ```php
51
+ * ['postId' => Yii::$app->request->get('id')]
52
+ * ```
53
+ *
54
+ * You may also specify a closure that returns an array. This can be used to
55
+ * evaluate the array values only if they are needed, for example when a model needs to be
56
+ * loaded like in the following code:
57
+ *
58
+ * ```php
59
+ * 'rules' => [
60
+ * [
61
+ * 'allow' => true,
62
+ * 'actions' => ['update'],
63
+ * 'roles' => ['updatePost'],
64
+ * 'roleParams' => function($rule) {
65
+ * return ['post' => Post::findOne(Yii::$app->request->get('id'))];
66
+ * },
67
+ * ],
68
+ * ],
69
+ * ```
70
+ *
71
+ * A reference to the [[AccessRule]] instance will be passed to the closure as the first parameter.
72
+ *
73
+ * @see roles
74
+ * @since 2.0.12
75
+ */
76
+ public roleParams: any = [];
77
+
78
+
79
+ /**
80
+ * @var array list of user IP addresses that this rule applies to. An IP address
81
+ * can contain the wildcard `*` at the end so that it matches IP addresses with the same prefix.
82
+ * For example, '192.168.*' matches all IP addresses in the segment '192.168.'.
83
+ * It may also contain a pattern/mask like '172.16.0.0/12' which would match all IPs from the
84
+ * 20-bit private network block in RFC1918.
85
+ * If not set or empty, it means this rule applies to all IP addresses.
86
+ */
87
+ public ips: any;
88
+
89
+
90
+ public async allows(action: any, user: WebUser, request: Request) {
91
+ if (
92
+ this.matchAction(action)
93
+ && await this.matchRole(user)
94
+ ) {
95
+ return this.allow
96
+ }
97
+ // if (this.matchAction($action)
98
+ // && this.matchRole($user)
99
+ // && this.matchIP($request->getUserIP())
100
+ // && this.matchVerb($request->getMethod())
101
+ // && this.matchController($action->controller)
102
+ // && this.matchCustom($action)
103
+ // ) {
104
+ // return $this->allow ? true : false;
105
+ // }
106
+
107
+ return null;
108
+ }
109
+
110
+ /**
111
+ * @param action $action the action
112
+ * @return bool whether the rule applies to the action
113
+ */
114
+ protected matchAction(action: any) {
115
+ return Utils.isEmpty(this.actions) || this.actions.includes(action.id);
116
+ }
117
+
118
+ /**
119
+ * @param controller $controller the controller
120
+ * @return bool whether the rule applies to the controller
121
+ */
122
+ protected matchController(controller: any) {
123
+ // if (empty($this->controllers)) {
124
+ // return true;
125
+ // }
126
+ //
127
+ // $id = $controller->getUniqueId();
128
+ // foreach ($this->controllers as $pattern) {
129
+ // if (StringHelper::matchWildcard($pattern, $id)) {
130
+ // return true;
131
+ // }
132
+ // }
133
+
134
+ return false;
135
+ }
136
+
137
+ protected async matchRole(user: WebUser) {
138
+ let items = Utils.isEmpty(this.roles) ? [] : this.roles;
139
+
140
+ if (!Utils.isEmpty(this.permissions)) {
141
+ items = Utils.merge(items, this.permissions);
142
+ }
143
+
144
+ if (Utils.isEmpty(items)) {
145
+ return true;
146
+ }
147
+
148
+
149
+ if (!user) {
150
+ throw new InvalidConfigException('The user application component must be available to specify roles in AccessRule.');
151
+ }
152
+
153
+ // @ts-ignore
154
+ let roleParams: any = [];
155
+ for (const itemsKey in items) {
156
+ let item = items[itemsKey];
157
+ if (item === '?') {
158
+ if (user.getIsGuest()) {
159
+ return true;
160
+ }
161
+ } else if (item === '@') {
162
+ if (!user.getIsGuest()) {
163
+ return true;
164
+ }
165
+ } else {
166
+ //role-params
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
+ }
174
+ }
175
+ }
176
+
177
+
178
+ return false;
179
+ }
180
+
181
+
182
+ }
@@ -1,5 +1,12 @@
1
- import { Request, Response } from "express";
2
- export interface AuthInterface {
1
+ /*
2
+ * Copyright (c) 2021. Chy Bilgisayar Bilisim
3
+ * Author: Cihan Ozturk
4
+ * E-mail: cihan@chy.com.tr
5
+ * Github:https://github.com/cihan53/
6
+ */
7
+ import {Request, Response} from "express";
8
+
9
+ export interface AuthInterface{
3
10
  /**
4
11
  * Authenticates the current user.
5
12
  * @param User $user
@@ -8,19 +15,20 @@ export interface AuthInterface {
8
15
  * @return IdentityInterface the authenticated user identity. If authentication information is not provided, null will be returned.
9
16
  * @throws UnauthorizedHttpException if authentication information is provided but is invalid.
10
17
  */
11
- authenticate(user: any, request: Request, response: Response): any;
18
+ authenticate(user:any, request:Request, response:Response):any;
19
+
12
20
  /**
13
21
  * Generates challenges upon authentication failure.
14
22
  * For example, some appropriate HTTP headers may be generated.
15
23
  * @param Response $response
16
24
  */
17
- challenge(response: Response): Response;
25
+ challenge(response:Response):Response;
26
+
18
27
  /**
19
28
  * Handles authentication failure.
20
29
  * The implementation should normally throw UnauthorizedHttpException to indicate authentication failure.
21
30
  * @param Response $response
22
31
  * @throws UnauthorizedHttpException
23
32
  */
24
- handleFailure(response: Response): Response;
25
- }
26
- //# sourceMappingURL=AuthInterface.d.ts.map
33
+ handleFailure(response:Response):Response;
34
+ }
@@ -0,0 +1,100 @@
1
+ /*
2
+ * Copyright (c) 2021. Chy Bilgisayar Bilisim
3
+ * Author: Cihan Ozturk
4
+ * E-mail: cihan@chy.com.tr
5
+ * Github:https://github.com/cihan53/
6
+ */
7
+
8
+ import {ActionFilter} from "../../base";
9
+ import {AuthInterface} from "./AuthInterface";
10
+ import {UnauthorizedHttpException} from "../../base";
11
+ import {WebUser} from "../../web/WebUser";
12
+ import {Request, Response} from "express";
13
+
14
+ export abstract class AuthMethod extends ActionFilter implements AuthInterface {
15
+
16
+ /**
17
+ * @var user the user object representing the user authentication status. If not set, the `user` application component will be used.
18
+ */
19
+ public user: WebUser | undefined;
20
+
21
+ /**
22
+ * @var Request the current request. If not set, the `request` application component will be used.
23
+ */
24
+ public request: Request | undefined;
25
+
26
+ /**
27
+ * @var Response the response to be sent. If not set, the `response` application component will be used.
28
+ */
29
+ public response: Response | undefined;
30
+
31
+
32
+ public optional = [];
33
+
34
+ /**
35
+ *
36
+ * @param action
37
+ * @param request
38
+ * @param response
39
+ */
40
+ public async beforeAction(action: any, request: Request, response: Response) {
41
+ let identity =await this.authenticate(
42
+ this.user ?? new WebUser(),
43
+ request,
44
+ response
45
+ )
46
+
47
+ // @ts-ignore
48
+ request.identity = identity;
49
+
50
+ if (identity !== null) {
51
+ return true;
52
+ }
53
+
54
+ this.challenge(response);
55
+ this.handleFailure(response);
56
+ return false;
57
+ }
58
+
59
+ /**
60
+ *
61
+ * @param user
62
+ * @param request
63
+ * @param response
64
+ */
65
+ authenticate(user: WebUser, request: Request, response: Response) {
66
+
67
+ }
68
+
69
+ // @ts-ignore
70
+ challenge(response: Response): Response {
71
+
72
+ }
73
+
74
+ // @ts-ignore
75
+ handleFailure(response: Response): Response {
76
+ throw new UnauthorizedHttpException('Your request was made with invalid credentials.');
77
+ }
78
+
79
+ getHeaderByKey(headers: any, findKey: any) {
80
+ let key = Object.keys(headers).find(key => key.toLowerCase() === findKey.toLowerCase())
81
+ if (key) {
82
+ return headers[key];
83
+ }
84
+
85
+ return null
86
+ }
87
+
88
+ patternCheck(headerText: any, pattern: RegExp) {
89
+ if (pattern) {
90
+ let matches = headerText.match(pattern)
91
+ if (matches && matches.length > 0) {
92
+ return matches;
93
+ } else {
94
+ return null
95
+ }
96
+ }
97
+
98
+ return null
99
+ }
100
+ }