chyz 1.0.12-rc.6 → 1.0.13-rc.1
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 +66 -13
- package/Chyz.ts +0 -1
- package/Examples/Controllers/ApiController.ts +88 -31
- package/Examples/Controllers/KeyCloakController.ts +100 -0
- package/Examples/Controllers/PublicController.ts +5 -7
- package/Examples/Controllers/SiteController.ts +82 -32
- package/Examples/Models/Categories.ts +36 -0
- package/Examples/Models/Customer.ts +115 -0
- package/Examples/Models/KeycloakUser.ts +66 -0
- package/Examples/Models/Order.ts +64 -0
- package/Examples/Models/OrderItem.ts +27 -0
- package/Examples/Models/ProductModels.ts +50 -0
- package/Examples/Models/ProductToCategories.ts +35 -0
- package/Examples/Models/Products.ts +49 -0
- package/Examples/Models/Stocks.ts +60 -0
- package/Examples/Models/User.ts +66 -35
- package/Examples/Models/UserPermission.ts +37 -0
- package/Examples/index-keycloack.ts +78 -0
- package/Examples/index.ts +16 -15
- package/Examples/keycloak.json +7 -0
- package/Examples/log/app.log +851 -937
- package/Examples/log/errors.log +79 -223
- package/Examples/package.json +46 -44
- package/Examples/tsconfig.json +1 -1
- package/Examples/yarn.lock +2549 -0
- package/README.md +118 -13
- package/base/ActionFilter.ts +2 -2
- package/base/BaseError.ts +2 -2
- package/base/DataErrorDbException.ts +1 -1
- package/base/DbConnection.ts +17 -11
- package/base/ForbiddenHttpException.ts +1 -1
- package/base/InvalidConfigException.ts +1 -1
- package/base/Model.ts +204 -15
- package/base/NotFoundHttpException.ts +1 -1
- package/base/RestClient.ts +28 -0
- package/base/UnauthorizedHttpException.ts +2 -1
- package/base/ValidationHttpException.ts +14 -0
- package/base/db/Exception.ts +14 -0
- package/base/index.ts +2 -0
- package/dist/BaseChyz.js +51 -10
- package/dist/BaseChyz.js.map +1 -1
- package/dist/Chyz.js.map +1 -1
- package/dist/base/ActionFilter.js +2 -2
- package/dist/base/ActionFilter.js.map +1 -1
- package/dist/base/BaseError.js +2 -2
- package/dist/base/BaseError.js.map +1 -1
- package/dist/base/DataErrorDbException.js +1 -1
- package/dist/base/DataErrorDbException.js.map +1 -1
- package/dist/base/DbConnection.js +13 -14
- package/dist/base/DbConnection.js.map +1 -1
- package/dist/base/ForbiddenHttpException.js +1 -1
- package/dist/base/ForbiddenHttpException.js.map +1 -1
- package/dist/base/InvalidConfigException.js +1 -1
- package/dist/base/InvalidConfigException.js.map +1 -1
- package/dist/base/Model.js +181 -13
- package/dist/base/Model.js.map +1 -1
- package/dist/base/NotFoundHttpException.js +1 -1
- package/dist/base/NotFoundHttpException.js.map +1 -1
- package/dist/base/RestClient.js +27 -0
- package/dist/base/RestClient.js.map +1 -0
- package/dist/base/UnauthorizedHttpException.js +1 -1
- package/dist/base/UnauthorizedHttpException.js.map +1 -1
- package/dist/base/ValidationHttpException.js +18 -0
- package/dist/base/ValidationHttpException.js.map +1 -0
- package/dist/base/db/Exception.js +16 -0
- package/dist/base/db/Exception.js.map +1 -0
- package/dist/base/index.js +2 -0
- package/dist/base/index.js.map +1 -1
- package/dist/filters/AccessControl.js +2 -2
- package/dist/filters/AccessControl.js.map +1 -1
- package/dist/filters/AccessRule.js.map +1 -1
- package/dist/filters/auth/AuthMethod.js +2 -2
- package/dist/filters/auth/AuthMethod.js.map +1 -1
- package/dist/filters/auth/HttpHeaderAuth.js.map +1 -1
- package/dist/filters/auth/JwtHttpBearerAuth.js +1 -0
- package/dist/filters/auth/JwtHttpBearerAuth.js.map +1 -1
- package/dist/filters/auth/KeyCloakHttpBearerAuth.js +117 -0
- package/dist/filters/auth/KeyCloakHttpBearerAuth.js.map +1 -0
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -0
- package/dist/web/{User.js → WebUser.js} +12 -7
- package/dist/web/WebUser.js.map +1 -0
- package/filters/AccessControl.ts +3 -3
- package/filters/AccessRule.ts +2 -2
- package/filters/auth/AuthMethod.ts +4 -4
- package/filters/auth/HttpHeaderAuth.ts +2 -2
- package/filters/auth/JwtHttpBearerAuth.ts +4 -5
- package/filters/auth/KeyCloakHttpBearerAuth.ts +115 -0
- package/index.ts +14 -0
- package/package.json +55 -52
- package/tsconfig.json +2 -1
- package/web/{User.ts → WebUser.ts} +10 -6
- package/.idea/Chy-Nodejs-Framework.iml +0 -12
- package/.idea/jsLibraryMappings.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/dist/decorator/decorator.js +0 -16
- package/dist/decorator/decorator.js.map +0 -1
- package/dist/web/User.js.map +0 -1
package/BaseChyz.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
require('dotenv-flow').config();
|
|
2
1
|
import 'reflect-metadata';
|
|
3
2
|
import {RouteDefinition} from "./model/RouteDefinition";
|
|
4
3
|
import {NextFunction, Request, Response} from "express";
|
|
@@ -9,8 +8,8 @@ import Utils from "./requiments/Utils";
|
|
|
9
8
|
const express = require("express");
|
|
10
9
|
const log4js = require("log4js");
|
|
11
10
|
const fs = require('fs');
|
|
12
|
-
const _ = require('lodash');
|
|
13
11
|
|
|
12
|
+
var ip = require('ip');
|
|
14
13
|
var bodyParser = require('body-parser')
|
|
15
14
|
var methodOverride = require('method-override')
|
|
16
15
|
|
|
@@ -26,8 +25,7 @@ export default class BaseChyz {
|
|
|
26
25
|
private _controllerpath: string = "Controllers"
|
|
27
26
|
private static controllers: Array<Controller> = []
|
|
28
27
|
public static components: any = {}
|
|
29
|
-
|
|
30
|
-
// public ac: any = new AccessControl();
|
|
28
|
+
public static middlewares: any = {}
|
|
31
29
|
|
|
32
30
|
|
|
33
31
|
get logConfig(): any {
|
|
@@ -65,7 +63,6 @@ export default class BaseChyz {
|
|
|
65
63
|
writable: true
|
|
66
64
|
})
|
|
67
65
|
|
|
68
|
-
|
|
69
66
|
Object.defineProperty(BaseChyz.express.request, 'identity', {
|
|
70
67
|
configurable: true,
|
|
71
68
|
enumerable: true,
|
|
@@ -117,14 +114,13 @@ export default class BaseChyz {
|
|
|
117
114
|
}
|
|
118
115
|
|
|
119
116
|
|
|
120
|
-
app(config: any = {}) {
|
|
117
|
+
app(config: any = {}): BaseChyz {
|
|
121
118
|
|
|
122
119
|
/**
|
|
123
120
|
* Config set
|
|
124
121
|
*/
|
|
125
122
|
this.config = config;
|
|
126
123
|
|
|
127
|
-
this.init();
|
|
128
124
|
|
|
129
125
|
let components = Utils.findKeyValue(config, "components")
|
|
130
126
|
if (components) {
|
|
@@ -136,6 +132,21 @@ export default class BaseChyz {
|
|
|
136
132
|
}
|
|
137
133
|
}
|
|
138
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
|
+
|
|
139
150
|
return this;
|
|
140
151
|
}
|
|
141
152
|
|
|
@@ -144,23 +155,39 @@ export default class BaseChyz {
|
|
|
144
155
|
return log4js;
|
|
145
156
|
}
|
|
146
157
|
|
|
158
|
+
public getLogger() {
|
|
159
|
+
return this.logProvider().getLogger(this.constructor.name);
|
|
160
|
+
}
|
|
147
161
|
|
|
148
162
|
static logs(...args: any[]) {
|
|
149
163
|
return log4js.getLogger(this.name);
|
|
150
164
|
}
|
|
151
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
|
+
|
|
152
174
|
public static info(...args: any[]) {
|
|
153
175
|
BaseChyz.logs().info(...arguments)
|
|
154
176
|
}
|
|
155
177
|
|
|
178
|
+
public static warn(...args: any[]) {
|
|
179
|
+
BaseChyz.logs().warn(...arguments)
|
|
180
|
+
}
|
|
181
|
+
|
|
156
182
|
public static error(...args: any[]) {
|
|
157
183
|
BaseChyz.logs().error(...arguments)
|
|
158
184
|
}
|
|
159
185
|
|
|
160
|
-
public static
|
|
161
|
-
BaseChyz.logs().
|
|
186
|
+
public static fatal(...args: any[]) {
|
|
187
|
+
BaseChyz.logs().fatal(...arguments)
|
|
162
188
|
}
|
|
163
189
|
|
|
190
|
+
|
|
164
191
|
public static warning(...args: any[]) {
|
|
165
192
|
BaseChyz.logs().warn(...arguments)
|
|
166
193
|
}
|
|
@@ -197,6 +224,12 @@ export default class BaseChyz {
|
|
|
197
224
|
return BaseChyz.components[key] ?? null
|
|
198
225
|
}
|
|
199
226
|
|
|
227
|
+
|
|
228
|
+
public static getMiddlewares(key: any) {
|
|
229
|
+
return BaseChyz.middlewares[key] ?? null
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
|
|
200
233
|
/**
|
|
201
234
|
* load contoller
|
|
202
235
|
*/
|
|
@@ -250,8 +283,8 @@ export default class BaseChyz {
|
|
|
250
283
|
} catch (e) {
|
|
251
284
|
BaseChyz.error(e)
|
|
252
285
|
// next(e)
|
|
253
|
-
res.status(e.statusCode)
|
|
254
|
-
res.json({error: {code: e.statusCode, name: e.name, message: e.message}})
|
|
286
|
+
res.status(e.statusCode || 500)
|
|
287
|
+
res.json({error: {code: e.statusCode || 500, name: e.name, message: e.message}})
|
|
255
288
|
}
|
|
256
289
|
})
|
|
257
290
|
|
|
@@ -266,8 +299,8 @@ export default class BaseChyz {
|
|
|
266
299
|
BaseChyz.express.use(bodyParser.json())
|
|
267
300
|
BaseChyz.express.use(bodyParser.urlencoded({extended: true})); // support encoded bodies
|
|
268
301
|
BaseChyz.express.use(methodOverride());
|
|
269
|
-
BaseChyz.express.use(
|
|
270
|
-
|
|
302
|
+
BaseChyz.express.use(methodOverride());
|
|
303
|
+
|
|
271
304
|
|
|
272
305
|
|
|
273
306
|
// CORS
|
|
@@ -282,7 +315,20 @@ export default class BaseChyz {
|
|
|
282
315
|
next();
|
|
283
316
|
});
|
|
284
317
|
|
|
318
|
+
//Middlewares
|
|
319
|
+
for (const middleware1 of Object.keys(BaseChyz.middlewares)) {
|
|
320
|
+
if (!Utils.isFunction(middleware1)) {
|
|
321
|
+
let keycloak = BaseChyz.middlewares[middleware1].keycloak;
|
|
322
|
+
BaseChyz.express.use(keycloak.middleware(BaseChyz.middlewares[middleware1].config));
|
|
323
|
+
} else {
|
|
324
|
+
BaseChyz.express.use(BaseChyz.middlewares[middleware1]);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
}
|
|
328
|
+
|
|
285
329
|
|
|
330
|
+
BaseChyz.express.use(this.errorResponder)
|
|
331
|
+
BaseChyz.express.use(this.errorHandler)
|
|
286
332
|
}
|
|
287
333
|
|
|
288
334
|
|
|
@@ -292,6 +338,8 @@ export default class BaseChyz {
|
|
|
292
338
|
BaseChyz.express.listen(this._port, () => {
|
|
293
339
|
BaseChyz.info("Express Server Start ")
|
|
294
340
|
BaseChyz.info(`Liten Port ${this._port}`)
|
|
341
|
+
BaseChyz.info(`http://localhost:${this._port}`)
|
|
342
|
+
BaseChyz.info(`http://${ip.address()}:${this._port}`)
|
|
295
343
|
})
|
|
296
344
|
return this;
|
|
297
345
|
}
|
|
@@ -300,3 +348,8 @@ export default class BaseChyz {
|
|
|
300
348
|
}
|
|
301
349
|
|
|
302
350
|
|
|
351
|
+
process.on('uncaughtException', err => {
|
|
352
|
+
BaseChyz.error('There was an uncaught error', err)
|
|
353
|
+
process.exit(1) //mandatory (as per the Node.js docs)
|
|
354
|
+
})
|
|
355
|
+
|
package/Chyz.ts
CHANGED
|
@@ -1,20 +1,27 @@
|
|
|
1
1
|
/*
|
|
2
|
-
*
|
|
2
|
+
*
|
|
3
|
+
* Copyright (c) 2021-2021.. Chy Bilgisayar Bilisim
|
|
3
4
|
* Author: Cihan Ozturk
|
|
4
|
-
*
|
|
5
|
-
*
|
|
5
|
+
* E-mail: cihan@chy.com.tr
|
|
6
|
+
* Github:https://github.com/cihan53/
|
|
7
|
+
*
|
|
6
8
|
*/
|
|
7
9
|
|
|
8
|
-
import {Controller} from "../../base
|
|
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
|
|
13
|
-
import {post} from "../../decorator
|
|
14
|
-
import {controller} from "../../decorator
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
import
|
|
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";
|
|
18
25
|
|
|
19
26
|
@controller("/api")
|
|
20
27
|
class ApiController extends Controller {
|
|
@@ -30,22 +37,22 @@ class ApiController extends Controller {
|
|
|
30
37
|
"class": JwtHttpBearerAuth,
|
|
31
38
|
// "auth": this.myCheck
|
|
32
39
|
},
|
|
33
|
-
'access': {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
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
|
+
// }
|
|
49
56
|
}]
|
|
50
57
|
}
|
|
51
58
|
|
|
@@ -56,19 +63,69 @@ class ApiController extends Controller {
|
|
|
56
63
|
return res.json({message: "index sayfası"})
|
|
57
64
|
}
|
|
58
65
|
|
|
59
|
-
@post("
|
|
60
|
-
Login(req: Request, res: Response) {
|
|
61
|
-
|
|
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
|
+
}
|
|
62
114
|
return res.send("Post Controller")
|
|
63
115
|
}
|
|
64
116
|
|
|
65
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)
|
|
66
122
|
|
|
67
|
-
|
|
123
|
+
}
|
|
68
124
|
|
|
69
125
|
error(req: Request, res: Response) {
|
|
70
126
|
BaseChyz.logs().info("Error Sayfası")
|
|
71
127
|
return res.send("Post Controller")
|
|
72
128
|
}
|
|
73
129
|
}
|
|
74
|
-
|
|
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
|
-
*
|
|
2
|
+
*
|
|
3
|
+
* Copyright (c) 2021-2021.. Chy Bilgisayar Bilisim
|
|
3
4
|
* Author: Cihan Ozturk
|
|
4
|
-
*
|
|
5
|
-
*
|
|
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
|
|
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
|
|
13
|
-
import {post} from "../../decorator
|
|
14
|
-
import {controller} from "../../decorator
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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};
|