chyz 1.0.12-rc.8 → 1.0.13-rc.3

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 (96) hide show
  1. package/BaseChyz.ts +72 -12
  2. package/Examples/Controllers/ApiController.ts +88 -30
  3. package/Examples/Controllers/KeyCloakController.ts +100 -0
  4. package/Examples/Controllers/PublicController.ts +5 -7
  5. package/Examples/Controllers/SiteController.ts +82 -32
  6. package/Examples/Models/Categories.ts +36 -0
  7. package/Examples/Models/Customer.ts +115 -0
  8. package/Examples/Models/KeycloakUser.ts +66 -0
  9. package/Examples/Models/Order.ts +64 -0
  10. package/Examples/Models/OrderItem.ts +27 -0
  11. package/Examples/Models/ProductModels.ts +50 -0
  12. package/Examples/Models/ProductToCategories.ts +35 -0
  13. package/Examples/Models/Products.ts +49 -0
  14. package/Examples/Models/Stocks.ts +60 -0
  15. package/Examples/Models/User.ts +66 -35
  16. package/Examples/Models/UserPermission.ts +37 -0
  17. package/Examples/index-keycloack.ts +78 -0
  18. package/Examples/index.ts +16 -15
  19. package/Examples/keycloak.json +7 -0
  20. package/Examples/log/app.log +903 -1034
  21. package/Examples/log/errors.log +79 -256
  22. package/Examples/package.json +46 -44
  23. package/Examples/tsconfig.json +1 -1
  24. package/README.md +118 -16
  25. package/base/ActionFilter.ts +2 -2
  26. package/base/BaseError.ts +2 -2
  27. package/base/DataErrorDbException.ts +1 -1
  28. package/base/DbConnection.ts +6 -0
  29. package/base/ForbiddenHttpException.ts +1 -1
  30. package/base/InvalidConfigException.ts +1 -1
  31. package/base/Model.ts +204 -15
  32. package/base/NotFoundHttpException.ts +1 -1
  33. package/base/RestClient.ts +28 -0
  34. package/base/UnauthorizedHttpException.ts +2 -1
  35. package/base/ValidationHttpException.ts +14 -0
  36. package/base/db/Exception.ts +14 -0
  37. package/base/index.ts +2 -0
  38. package/dist/BaseChyz.js +56 -10
  39. package/dist/BaseChyz.js.map +1 -1
  40. package/dist/README.md +270 -0
  41. package/dist/base/ActionFilter.js +2 -2
  42. package/dist/base/ActionFilter.js.map +1 -1
  43. package/dist/base/BaseError.js +2 -2
  44. package/dist/base/BaseError.js.map +1 -1
  45. package/dist/base/DataErrorDbException.js +1 -1
  46. package/dist/base/DataErrorDbException.js.map +1 -1
  47. package/dist/base/DbConnection.js +3 -0
  48. package/dist/base/DbConnection.js.map +1 -1
  49. package/dist/base/ForbiddenHttpException.js +1 -1
  50. package/dist/base/ForbiddenHttpException.js.map +1 -1
  51. package/dist/base/InvalidConfigException.js +1 -1
  52. package/dist/base/InvalidConfigException.js.map +1 -1
  53. package/dist/base/Model.js +181 -13
  54. package/dist/base/Model.js.map +1 -1
  55. package/dist/base/NotFoundHttpException.js +1 -1
  56. package/dist/base/NotFoundHttpException.js.map +1 -1
  57. package/dist/base/RestClient.js +27 -0
  58. package/dist/base/RestClient.js.map +1 -0
  59. package/dist/base/UnauthorizedHttpException.js +1 -1
  60. package/dist/base/UnauthorizedHttpException.js.map +1 -1
  61. package/dist/base/ValidationHttpException.js +18 -0
  62. package/dist/base/ValidationHttpException.js.map +1 -0
  63. package/dist/base/db/Exception.js +16 -0
  64. package/dist/base/db/Exception.js.map +1 -0
  65. package/dist/base/index.js +2 -0
  66. package/dist/base/index.js.map +1 -1
  67. package/dist/filters/AccessControl.js +2 -2
  68. package/dist/filters/AccessControl.js.map +1 -1
  69. package/dist/filters/AccessRule.js.map +1 -1
  70. package/dist/filters/auth/AuthMethod.js +2 -2
  71. package/dist/filters/auth/AuthMethod.js.map +1 -1
  72. package/dist/filters/auth/HttpHeaderAuth.js.map +1 -1
  73. package/dist/filters/auth/JwtHttpBearerAuth.js +1 -0
  74. package/dist/filters/auth/JwtHttpBearerAuth.js.map +1 -1
  75. package/dist/filters/auth/KeyCloakHttpBearerAuth.js +117 -0
  76. package/dist/filters/auth/KeyCloakHttpBearerAuth.js.map +1 -0
  77. package/dist/index.js +28 -0
  78. package/dist/index.js.map +1 -0
  79. package/dist/package.json +55 -0
  80. package/dist/web/{User.js → WebUser.js} +12 -7
  81. package/dist/web/WebUser.js.map +1 -0
  82. package/filters/AccessControl.ts +3 -3
  83. package/filters/AccessRule.ts +2 -2
  84. package/filters/auth/AuthMethod.ts +4 -4
  85. package/filters/auth/HttpHeaderAuth.ts +2 -2
  86. package/filters/auth/JwtHttpBearerAuth.ts +4 -5
  87. package/filters/auth/KeyCloakHttpBearerAuth.ts +115 -0
  88. package/index.ts +14 -0
  89. package/package-lock.json +5259 -0
  90. package/package.json +56 -52
  91. package/web/{User.ts → WebUser.ts} +10 -6
  92. package/.idea/Chy-Nodejs-Framework.iml +0 -12
  93. package/.idea/jsLibraryMappings.xml +0 -6
  94. package/.idea/modules.xml +0 -8
  95. package/.idea/vcs.xml +0 -6
  96. package/dist/web/User.js.map +0 -1
package/BaseChyz.ts CHANGED
@@ -8,8 +8,8 @@ import Utils from "./requiments/Utils";
8
8
  const express = require("express");
9
9
  const log4js = require("log4js");
10
10
  const fs = require('fs');
11
- const _ = require('lodash');
12
11
 
12
+ var ip = require('ip');
13
13
  var bodyParser = require('body-parser')
14
14
  var methodOverride = require('method-override')
15
15
 
@@ -25,6 +25,8 @@ export default class BaseChyz {
25
25
  private _controllerpath: string = "Controllers"
26
26
  private static controllers: Array<Controller> = []
27
27
  public static components: any = {}
28
+ public static middlewares: any = {}
29
+
28
30
 
29
31
  get logConfig(): any {
30
32
  return this._logConfig;
@@ -61,7 +63,6 @@ export default class BaseChyz {
61
63
  writable: true
62
64
  })
63
65
 
64
-
65
66
  Object.defineProperty(BaseChyz.express.request, 'identity', {
66
67
  configurable: true,
67
68
  enumerable: true,
@@ -120,7 +121,6 @@ export default class BaseChyz {
120
121
  */
121
122
  this.config = config;
122
123
 
123
- this.init();
124
124
 
125
125
  let components = Utils.findKeyValue(config, "components")
126
126
  if (components) {
@@ -132,6 +132,21 @@ export default class BaseChyz {
132
132
  }
133
133
  }
134
134
 
135
+
136
+ let middlewares = Utils.findKeyValue(config, "middlewares")
137
+ if (middlewares) {
138
+ for (const middlewareKey in middlewares) {
139
+ let middleware1 = middlewares[middlewareKey];
140
+ BaseChyz.debug("Create middlewares ", middlewareKey)
141
+ BaseChyz.middlewares[middlewareKey] = middleware1;
142
+ // BaseChyz.middlewares[middlewareKey] = Utils.createObject(new middleware1.class, middleware1);
143
+ }
144
+ }
145
+
146
+
147
+
148
+ this.init();
149
+
135
150
  return this;
136
151
  }
137
152
 
@@ -140,7 +155,7 @@ export default class BaseChyz {
140
155
  return log4js;
141
156
  }
142
157
 
143
- public getLogger(){
158
+ public getLogger() {
144
159
  return this.logProvider().getLogger(this.constructor.name);
145
160
  }
146
161
 
@@ -148,18 +163,31 @@ export default class BaseChyz {
148
163
  return log4js.getLogger(this.name);
149
164
  }
150
165
 
166
+ public static trace(...args: any[]) {
167
+ BaseChyz.logs().fatal(...arguments)
168
+ }
169
+
170
+ public static debug(...args: any[]) {
171
+ BaseChyz.logs().debug(...arguments)
172
+ }
173
+
151
174
  public static info(...args: any[]) {
152
175
  BaseChyz.logs().info(...arguments)
153
176
  }
154
177
 
178
+ public static warn(...args: any[]) {
179
+ BaseChyz.logs().warn(...arguments)
180
+ }
181
+
155
182
  public static error(...args: any[]) {
156
183
  BaseChyz.logs().error(...arguments)
157
184
  }
158
185
 
159
- public static debug(...args: any[]) {
160
- BaseChyz.logs().debug(...arguments)
186
+ public static fatal(...args: any[]) {
187
+ BaseChyz.logs().fatal(...arguments)
161
188
  }
162
189
 
190
+
163
191
  public static warning(...args: any[]) {
164
192
  BaseChyz.logs().warn(...arguments)
165
193
  }
@@ -196,6 +224,12 @@ export default class BaseChyz {
196
224
  return BaseChyz.components[key] ?? null
197
225
  }
198
226
 
227
+
228
+ public static getMiddlewares(key: any) {
229
+ return BaseChyz.middlewares[key] ?? null
230
+ }
231
+
232
+
199
233
  /**
200
234
  * load contoller
201
235
  */
@@ -247,10 +281,16 @@ export default class BaseChyz {
247
281
  await instance[route.methodName](req, res, next);
248
282
  instance.afterAction(route, req, res);
249
283
  } catch (e) {
250
- BaseChyz.error(e)
251
- // next(e)
252
- res.status(e.statusCode)
253
- res.json({error: {code: e.statusCode, name: e.name, message: e.message}})
284
+ if (e instanceof Error) {
285
+ BaseChyz.error(e)
286
+
287
+ // @ts-ignore
288
+ res.status(e.statusCode || 500)
289
+ // @ts-ignore
290
+ res.json({error: {code: e.statusCode || 500, name: e.name, message: e.message}})
291
+ }else{
292
+ res.json(e )
293
+ }
254
294
  }
255
295
  })
256
296
 
@@ -265,8 +305,8 @@ export default class BaseChyz {
265
305
  BaseChyz.express.use(bodyParser.json())
266
306
  BaseChyz.express.use(bodyParser.urlencoded({extended: true})); // support encoded bodies
267
307
  BaseChyz.express.use(methodOverride());
268
- BaseChyz.express.use(this.errorResponder)
269
- BaseChyz.express.use(this.errorHandler)
308
+ BaseChyz.express.use(methodOverride());
309
+
270
310
 
271
311
 
272
312
  // CORS
@@ -281,7 +321,20 @@ export default class BaseChyz {
281
321
  next();
282
322
  });
283
323
 
324
+ //Middlewares
325
+ for (const middleware1 of Object.keys(BaseChyz.middlewares)) {
326
+ if (!Utils.isFunction(middleware1)) {
327
+ let keycloak = BaseChyz.middlewares[middleware1].keycloak;
328
+ BaseChyz.express.use(keycloak.middleware(BaseChyz.middlewares[middleware1].config));
329
+ } else {
330
+ BaseChyz.express.use(BaseChyz.middlewares[middleware1]);
331
+ }
332
+
333
+ }
284
334
 
335
+
336
+ BaseChyz.express.use(this.errorResponder)
337
+ BaseChyz.express.use(this.errorHandler)
285
338
  }
286
339
 
287
340
 
@@ -291,6 +344,8 @@ export default class BaseChyz {
291
344
  BaseChyz.express.listen(this._port, () => {
292
345
  BaseChyz.info("Express Server Start ")
293
346
  BaseChyz.info(`Liten Port ${this._port}`)
347
+ BaseChyz.info(`http://localhost:${this._port}`)
348
+ BaseChyz.info(`http://${ip.address()}:${this._port}`)
294
349
  })
295
350
  return this;
296
351
  }
@@ -299,3 +354,8 @@ export default class BaseChyz {
299
354
  }
300
355
 
301
356
 
357
+ process.on('uncaughtException', err => {
358
+ BaseChyz.error('There was an uncaught error', err)
359
+ process.exit(1) //mandatory (as per the Node.js docs)
360
+ })
361
+
@@ -1,19 +1,27 @@
1
1
  /*
2
- * Copyright (c) 2021. Chy Bilgisayar Bilisim
2
+ *
3
+ * Copyright (c) 2021-2021.. Chy Bilgisayar Bilisim
3
4
  * Author: Cihan Ozturk
4
- * E-mail: cihan@chy.com.tr
5
- * Github:https://github.com/cihan53/
5
+ * E-mail: cihan@chy.com.tr
6
+ * Github:https://github.com/cihan53/
7
+ *
6
8
  */
7
9
 
8
- import {Controller} from "../../base/Controller";
10
+ import {Controller} from "../../base";
9
11
  import BaseChyz from "../../BaseChyz";
10
12
  // @ts-ignore
11
13
  import {Request, Response} from "express";
12
- import {get} from "../../decorator/get";
13
- import {post} from "../../decorator/post";
14
- import {controller} from "../../decorator/controller";
15
- import {AccessControl} from "../../filters/AccessControl";
16
- import {JwtHttpBearerAuth} from "../../filters/auth/JwtHttpBearerAuth";
14
+ import {get} from "../../decorator";
15
+ import {post} from "../../decorator";
16
+ import {controller} from "../../decorator";
17
+ import {JwtHttpBearerAuth} from "../../filters/auth";
18
+
19
+ import {ValidationHttpException} from "../../base";
20
+ import {ForbiddenHttpException} from "../../base";
21
+ import {OrderClass, Order} from "../Models/Order";
22
+ import {CustomerClass, Customer} from "../Models/Customer";
23
+ import {Products} from "../Models/Products";
24
+ import {ProductModels} from "../Models/ProductModels";
17
25
 
18
26
  @controller("/api")
19
27
  class ApiController extends Controller {
@@ -29,22 +37,22 @@ class ApiController extends Controller {
29
37
  "class": JwtHttpBearerAuth,
30
38
  // "auth": this.myCheck
31
39
  },
32
- 'access': {
33
- 'class': AccessControl,
34
- 'only': ['login', 'logout', 'signup'],
35
- 'rules': [
36
- {
37
- 'allow': true,
38
- 'actions': ['login', 'index'],
39
- 'roles': ['?'],
40
- },
41
- {
42
- 'allow': true,
43
- 'actions': ['logout', "logout2"],
44
- 'roles': ['@'],
45
- }
46
- ]
47
- }
40
+ // 'access': {
41
+ // 'class': AccessControl,
42
+ // 'only': ['login', 'logout', 'signup'],
43
+ // 'rules': [
44
+ // {
45
+ // 'allow': true,
46
+ // 'actions': ['login', 'index'],
47
+ // 'roles': ['?'],
48
+ // },
49
+ // {
50
+ // 'allow': true,
51
+ // 'actions': ['logout', "logout2"],
52
+ // 'roles': ['@'],
53
+ // }
54
+ // ]
55
+ // }
48
56
  }]
49
57
  }
50
58
 
@@ -55,19 +63,69 @@ class ApiController extends Controller {
55
63
  return res.json({message: "index sayfası"})
56
64
  }
57
65
 
58
- @post("login")
59
- Login(req: Request, res: Response) {
60
- BaseChyz.logs().info("Post Controller")
66
+ @post("orderCreate")
67
+ async Login(req: Request, res: Response) {
68
+ let data = req.body;
69
+ data.Customer.status = "true";
70
+ data.Customer["2fa"] = "true";
71
+
72
+ //Customer Model Create
73
+ let customer: CustomerClass = Customer;
74
+ //Order Model Create
75
+ let order: OrderClass = Order;
76
+
77
+
78
+ let transaction
79
+ try {
80
+ // get transaction
81
+ transaction = await BaseChyz.getComponent("db").transaction();
82
+
83
+
84
+ customer.load(data, "Customer");//load customer data
85
+ let cus: any = await customer.save({}, {transaction});
86
+
87
+ if (!cus) {
88
+ throw new ValidationHttpException(customer.errors);
89
+ }
90
+
91
+ data.Order.customer_id = cus.id;
92
+ // data.Order.total = 0;
93
+ // data.Order.status = true;
94
+ order.load(data, "Order");
95
+ let res1 = await order.save({}, {transaction});
96
+ if (!res1) {
97
+ throw new ValidationHttpException(order.errors);
98
+ }
99
+
100
+ // commit
101
+ await transaction.commit();
102
+
103
+ } catch (e) {
104
+ if (transaction) {
105
+ await transaction.rollback();
106
+ BaseChyz.warn("Rollback transaction")
107
+ }
108
+
109
+ if (e instanceof ValidationHttpException)
110
+ throw new ValidationHttpException(e.message)
111
+ else
112
+ throw new ForbiddenHttpException(e.message)
113
+ }
61
114
  return res.send("Post Controller")
62
115
  }
63
116
 
64
117
 
118
+ @get("order/list")
119
+ async listOrder(req: Request, res: Response) {
120
+ let product = await Products.findAll({include: [ProductModels.model()]});
121
+ return res.json(product)
65
122
 
66
-
123
+ }
67
124
 
68
125
  error(req: Request, res: Response) {
69
126
  BaseChyz.logs().info("Error Sayfası")
70
127
  return res.send("Post Controller")
71
128
  }
72
129
  }
73
- module.exports=ApiController
130
+
131
+ module.exports = ApiController
@@ -0,0 +1,100 @@
1
+ /*
2
+ *
3
+ * Copyright (c) 2021-2021.. Chy Bilgisayar Bilisim
4
+ * Author: Cihan Ozturk
5
+ * E-mail: cihan@chy.com.tr
6
+ * Github:https://github.com/cihan53/
7
+ *
8
+ */
9
+
10
+ import {Controller} from "../../base/Controller";
11
+ import BaseChyz from "../../BaseChyz";
12
+ // @ts-ignore
13
+ import {Request, Response} from "express";
14
+ import {get,post,controller} from "../../decorator";
15
+
16
+ import {ValidationHttpException,ForbiddenHttpException} from "../../base";
17
+ import {KeyCloakHttpBearerAuth} from "../../filters/auth/KeyCloakHttpBearerAuth";
18
+
19
+ import {OrderClass, Order} from "../Models/Order";
20
+ import {CustomerClass, Customer} from "../Models/Customer";
21
+
22
+ @controller("/oauth2.0")
23
+ class ApiController extends Controller {
24
+
25
+ public behaviors(): any[] {
26
+
27
+ return [{
28
+ 'authenticator': {
29
+ "class": KeyCloakHttpBearerAuth,
30
+ // "auth": this.myCheck
31
+ }
32
+ }]
33
+ }
34
+
35
+ @get("/")
36
+ Index(req: Request, res: Response) {
37
+
38
+ BaseChyz.logs().info("Site Controller Burası", this.id)
39
+ return res.json({message: "index sayfası"})
40
+ }
41
+
42
+ @post("orderCreate")
43
+ async Login(req: Request, res: Response) {
44
+ let data = req.body;
45
+ data.Customer.status = "true";
46
+ data.Customer["2fa"] = "true";
47
+
48
+ //Customer Model Create
49
+ let customer: CustomerClass = Customer;
50
+ //Order Model Create
51
+ let order: OrderClass = Order ;
52
+
53
+
54
+ let transaction
55
+ try {
56
+ // get transaction
57
+ transaction = await BaseChyz.getComponent("db").transaction();
58
+
59
+
60
+ customer.load(data, "Customer");//load customer data
61
+ let cus: any = await customer.save({}, {transaction});
62
+
63
+ if (!cus) {
64
+ throw new ValidationHttpException(customer.errors);
65
+ }
66
+
67
+ data.Order.customer_id = cus.id;
68
+ // data.Order.total = 0;
69
+ // data.Order.status = true;
70
+ order.load(data, "Order");
71
+ let res1 = await order.save({}, {transaction});
72
+ if (!res1) {
73
+ throw new ValidationHttpException(order.errors);
74
+ }
75
+
76
+ // commit
77
+ await transaction.commit();
78
+
79
+ } catch (e) {
80
+ if (transaction) {
81
+ await transaction.rollback();
82
+ BaseChyz.warn("Rollback transaction")
83
+ }
84
+
85
+ if (e instanceof ValidationHttpException)
86
+ throw new ValidationHttpException(e.message)
87
+ else
88
+ throw new ForbiddenHttpException(e.message)
89
+ }
90
+ return res.send("Post Controller")
91
+ }
92
+
93
+
94
+ error(req: Request, res: Response) {
95
+ BaseChyz.logs().info("Error Sayfası")
96
+ return res.send("Post Controller")
97
+ }
98
+ }
99
+
100
+ module.exports = ApiController
@@ -1,8 +1,10 @@
1
1
  /*
2
- * Copyright (c) 2021. Chy Bilgisayar Bilisim
2
+ *
3
+ * Copyright (c) 2021-2021.. Chy Bilgisayar Bilisim
3
4
  * Author: Cihan Ozturk
4
- * E-mail: cihan@chy.com.tr
5
- * Github:https://github.com/cihan53/
5
+ * E-mail: cihan@chy.com.tr
6
+ * Github:https://github.com/cihan53/
7
+ *
6
8
  */
7
9
 
8
10
  import {Controller} from "../../base/Controller";
@@ -10,11 +12,7 @@ import BaseChyz from "../../BaseChyz";
10
12
  // @ts-ignore
11
13
  import {Request, Response} from "express";
12
14
  import {get} from "../../decorator/get";
13
- import {post} from "../../decorator/post";
14
15
  import {controller} from "../../decorator/controller";
15
- import {AccessControl} from "../../filters/AccessControl";
16
- import {JwtHttpBearerAuth} from "../../filters/auth/JwtHttpBearerAuth";
17
- import Utils from "../../requiments/Utils";
18
16
 
19
17
  @controller("/public")
20
18
  class PublicController extends Controller {
@@ -5,16 +5,20 @@
5
5
  * Github:https://github.com/cihan53/
6
6
  */
7
7
 
8
- import {Controller} from "../../base/Controller";
8
+ import {Controller} from "../../base";
9
9
  import BaseChyz from "../../BaseChyz";
10
10
  // @ts-ignore
11
11
  import {Request, Response} from "express";
12
- import {get} from "../../decorator/get";
13
- import {post} from "../../decorator/post";
14
- import {controller} from "../../decorator/controller";
15
- import {AccessControl} from "../../filters/AccessControl";
16
- import {JwtHttpBearerAuth} from "../../filters/auth/JwtHttpBearerAuth";
17
- import Utils from "../../requiments/Utils";
12
+ import {get} from "../../decorator";
13
+ import {post} from "../../decorator";
14
+ import {controller} from "../../decorator";
15
+ import {User} from "../Models/User";
16
+ import {ForbiddenHttpException} from "../../base";
17
+ import {JwtHttpBearerAuth} from "../../filters/auth";
18
+
19
+ const bcrypt = require('bcrypt');
20
+ const JsonWebToken = require("jsonwebtoken");
21
+
18
22
 
19
23
  @controller("/site")
20
24
  class SiteController extends Controller {
@@ -22,33 +26,43 @@ class SiteController extends Controller {
22
26
  public myCheck(token) {
23
27
  console.log("myyyyyyyyyyyyyyyyyyyyy")
24
28
  }
25
-
26
29
  public behaviors(): any[] {
27
-
28
30
  return [{
29
- // 'authenticator': {
30
- // "class": JwtHttpBearerAuth,
31
- // // "auth": this.myCheck
32
- // },
33
- 'access': {
34
- 'class': AccessControl,
35
- 'only': ['login', 'logout','index' ],
36
- 'rules': [
37
- {
38
- 'allow': false,
39
- 'actions': ['login', 'index' ],
40
- 'roles': ['?'],
41
- },
42
- {
43
- 'allow': true,
44
- 'actions': ['logout', "logout2"],
45
- 'roles': ['@'],
46
- }
47
- ]
31
+ 'authenticator': {
32
+ "class": JwtHttpBearerAuth,
33
+ "except":["index","login"]
34
+ // "auth": this.myCheck
48
35
  }
49
36
  }]
50
37
  }
51
38
 
39
+
40
+ // public behaviors(): any[] {
41
+ //
42
+ // return [{
43
+ // // 'authenticator': {
44
+ // // "class": JwtHttpBearerAuth,
45
+ // // // "auth": this.myCheck
46
+ // // },
47
+ // 'access': {
48
+ // 'class': AccessControl,
49
+ // 'only': ['login', 'logout','index' ],
50
+ // 'rules': [
51
+ // {
52
+ // 'allow': false,
53
+ // 'actions': ['login', 'index' ],
54
+ // 'roles': ['?'],
55
+ // },
56
+ // {
57
+ // 'allow': true,
58
+ // 'actions': ['logout', "logout2"],
59
+ // 'roles': ['@'],
60
+ // }
61
+ // ]
62
+ // }
63
+ // }]
64
+ // }
65
+
52
66
  @get("index")
53
67
  Index(req: Request, res: Response) {
54
68
 
@@ -57,9 +71,45 @@ class SiteController extends Controller {
57
71
  }
58
72
 
59
73
  @post("login")
60
- Login(req: Request, res: Response) {
61
- BaseChyz.logs().info("Post Controller")
62
- return res.send("Post Controller")
74
+ async login(req: Request, res: Response) {
75
+ let UserModel: User = new User();
76
+ let token
77
+ let username = req.body.username;
78
+ let password = req.body.password;
79
+
80
+
81
+ let user = await UserModel.findOne({where: {username: username}})
82
+ if (user) {
83
+ BaseChyz.debug("Db found user", username)
84
+ const match = await bcrypt.compare(password, user.password);
85
+ if (match) {
86
+ BaseChyz.debug("Db user verify", username)
87
+ //login
88
+ // @ts-ignore
89
+ let xForwardedFor = (req.headers['x-forwarded-for'] || '').replace(/:\d+$/, '');
90
+ let ip = xForwardedFor || req.socket.remoteAddress;
91
+ var source: string = req.headers['user-agent'] || '';
92
+ if (req.headers['x-ucbrowser-ua']) { //special case of UC Browser
93
+ source = req.headers['x-ucbrowser-ua']+"";
94
+ }
95
+ token = await JsonWebToken.sign({
96
+ user: user.id,
97
+ ip: ip,
98
+ agent: source,
99
+ }, user.authkey, {expiresIn: '1h'});
100
+
101
+ BaseChyz.debug("Db user create access token", username,"expiresIn","1h")
102
+ return res.json({token: token})
103
+ } else {
104
+ let error: any = new ForbiddenHttpException(BaseChyz.t('You are not allowed to perform this action.'))
105
+ res.status(500).json( error.toJSON())
106
+ }
107
+ } else {
108
+ let error: any = new ForbiddenHttpException(BaseChyz.t('You are not allowed to perform this action.'))
109
+ res.status(500).json( error.toJSON())
110
+ }
111
+
112
+
63
113
  }
64
114
 
65
115
  @get("logout")
@@ -68,7 +118,7 @@ class SiteController extends Controller {
68
118
  // @ts-ignore
69
119
  let identity = req.user ?? BaseChyz.getComponent("user").getIdentity();
70
120
  // console.log("logout2", identity.id)
71
- console.log(identity)
121
+ //console.log(identity)
72
122
 
73
123
  BaseChyz.logs().info("Logout Controller")
74
124
  return res.send("Logout Controller")
@@ -0,0 +1,36 @@
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 {DataTypes, Model} from "../../base";
8
+
9
+ export class CategoriesClass extends Model {
10
+ [x: string]: any;
11
+
12
+ tableName() {
13
+ return 'categories';
14
+ }
15
+
16
+
17
+ attributes() {
18
+ return {
19
+ // Model attributes are defined here
20
+ title: {
21
+ type: DataTypes.STRING,
22
+ allowNull: false
23
+ },
24
+ properties: {
25
+ type: DataTypes.STRING,
26
+ allowNull: false
27
+ }
28
+
29
+ }
30
+ }
31
+
32
+
33
+ }
34
+
35
+ const Categories = new CategoriesClass();
36
+ export {Categories};