chyz 1.0.13-rc.9 → 1.1.0-rc.2

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 (71) hide show
  1. package/BaseChyz.ts +74 -20
  2. package/Doc/Moel kullanma.md +13 -0
  3. package/Examples/Controllers/ApiController.ts +22 -22
  4. package/Examples/Controllers/BasicApiController.ts +121 -0
  5. package/Examples/Controllers/SiteController.ts +18 -8
  6. package/Examples/Models/AuthAssignment.ts +50 -0
  7. package/Examples/Models/AuthItem.ts +59 -0
  8. package/Examples/Models/AuthItemChild.ts +49 -0
  9. package/Examples/Models/Categories.ts +4 -0
  10. package/Examples/Models/KeycloakUser.ts +4 -0
  11. package/Examples/Models/User.ts +30 -2
  12. package/Examples/index.ts +22 -2
  13. package/Examples/log/app.log +14466 -0
  14. package/Examples/log/errors.log +594 -0
  15. package/Examples/package.json +5 -2
  16. package/README.md +265 -12
  17. package/base/ActionFilter.ts +1 -1
  18. package/base/BaseError.ts +4 -2
  19. package/base/DbConnection.ts +9 -5
  20. package/base/Model.ts +231 -30
  21. package/base/ModelManager.ts +6 -1
  22. package/base/RestClient.ts +4 -4
  23. package/base/ValidationHttpException.ts +1 -1
  24. package/dist/BaseChyz.js +61 -14
  25. package/dist/BaseChyz.js.map +1 -1
  26. package/dist/base/ActionFilter.js +1 -1
  27. package/dist/base/ActionFilter.js.map +1 -1
  28. package/dist/base/BaseError.js +6 -2
  29. package/dist/base/BaseError.js.map +1 -1
  30. package/dist/base/DbConnection.js +1 -0
  31. package/dist/base/DbConnection.js.map +1 -1
  32. package/dist/base/Model.js +192 -4
  33. package/dist/base/Model.js.map +1 -1
  34. package/dist/base/ModelManager.js +0 -8
  35. package/dist/base/ModelManager.js.map +1 -1
  36. package/dist/base/RestClient.js +4 -4
  37. package/dist/base/RestClient.js.map +1 -1
  38. package/dist/base/ValidationHttpException.js +1 -1
  39. package/dist/filters/AccessControl.js +15 -3
  40. package/dist/filters/AccessControl.js.map +1 -1
  41. package/dist/filters/AccessRule.js +99 -38
  42. package/dist/filters/AccessRule.js.map +1 -1
  43. package/dist/filters/auth/HttpBasicAuth.js +65 -0
  44. package/dist/filters/auth/HttpBasicAuth.js.map +1 -1
  45. package/dist/filters/auth/index.js +1 -0
  46. package/dist/filters/auth/index.js.map +1 -1
  47. package/dist/package.json +6 -4
  48. package/dist/rbac/AuthAssignment.js +45 -0
  49. package/dist/rbac/AuthAssignment.js.map +1 -0
  50. package/dist/rbac/AuthItem.js +52 -0
  51. package/dist/rbac/AuthItem.js.map +1 -0
  52. package/dist/rbac/AuthItemChild.js +44 -0
  53. package/dist/rbac/AuthItemChild.js.map +1 -0
  54. package/dist/rbac/AuthManager.js +13 -5
  55. package/dist/rbac/AuthManager.js.map +1 -1
  56. package/dist/requiments/Utils.js +5 -1
  57. package/dist/requiments/Utils.js.map +1 -1
  58. package/dist/web/WebUser.js +78 -0
  59. package/dist/web/WebUser.js.map +1 -1
  60. package/filters/AccessControl.ts +19 -6
  61. package/filters/AccessRule.ts +61 -16
  62. package/filters/auth/HttpBasicAuth.ts +68 -0
  63. package/filters/auth/index.ts +1 -0
  64. package/package.json +6 -4
  65. package/rbac/AuthAssignment.ts +50 -0
  66. package/rbac/AuthItem.ts +57 -0
  67. package/rbac/AuthItemChild.ts +50 -0
  68. package/rbac/AuthManager.ts +19 -9
  69. package/requiments/Utils.ts +6 -0
  70. package/web/IdentityInterface.ts +7 -1
  71. package/web/WebUser.ts +88 -1
package/README.md CHANGED
@@ -8,7 +8,6 @@ Klasör Yapısı<br>
8
8
  *---Controllers <br>
9
9
  *---Models<br>
10
10
  *---Log<br>
11
- *---Framework<br>
12
11
  index.ts<br>
13
12
 
14
13
  `##Başlangıç<br>
@@ -17,15 +16,23 @@ yarn start
17
16
  ## index.ts alanlar düzenlenmeli.
18
17
 
19
18
  ```typescript
19
+
20
+ import {BaseChyz} from "../index";
21
+
20
22
  require('dotenv-flow').config();
23
+ import Chyz ,{ DbConnection, BaseChyz} from "chyz/dist/";
24
+ import {AuthManager} from "chyz/dist/rbac/AuthManager"
25
+ import {WebUser} from "chyz/dist/web/WebUser";
26
+ import {User as Identity} from "./Models/User";
21
27
 
22
- import BaseChyz from "chyz/dist/BaseChyz";
23
- import Chyz, {DbConnection} from "chyz/dist";
24
- import {WebUser} from "../web/WebUser";
25
- import {User} from "./Models/User";
26
28
 
27
29
  let config = {
30
+ port: process.env.PORT || 8870,
31
+ controllerpath: "Examples\\Controllers",
28
32
  components: {
33
+ authManager: {
34
+ class: AuthManager,
35
+ },
29
36
  db: {
30
37
  class: DbConnection,
31
38
  database: process.env.DBDATABASE,
@@ -35,7 +42,7 @@ let config = {
35
42
  host: process.env.DBHOST,
36
43
  dialect: 'postgres', /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */
37
44
  // disable logging; default: console.log
38
- logging: false
45
+ logging: (msg: any) => BaseChyz.debug(msg)
39
46
  }
40
47
  },
41
48
  user: {
@@ -43,14 +50,168 @@ let config = {
43
50
  'identityClass': User
44
51
  }
45
52
  }
53
+
46
54
  }
47
55
  Chyz.app(config).Start();
48
56
  ```
49
57
 
58
+ ##Controller
59
+ Basit şekilde kontroller oluşturulabilir.
60
+ ```typescript
61
+ /*
62
+ *
63
+ * Copyright (c) 2021-2021.. Chy Bilgisayar Bilisim
64
+ * Author: Cihan Ozturk
65
+ * E-mail: cihan@chy.com.tr
66
+ * Github:https://github.com/cihan53/
67
+ *
68
+ */
69
+
70
+ import {AccessControl, BaseChyz, JwtHttpBearerAuth, ModelManager, Request, Response,} from "chyz/dist";
71
+ import {ForbiddenHttpException, Model, NotFoundHttpException, ValidationHttpException} from "chyz/dist/base";
72
+
73
+ import Utils from 'chyz/dist/requiments/Utils';
74
+ import {controller, get, post} from "chyz/dist/decorator";
75
+ import {Controller} from "chyz/dist/base/Controller";
76
+ import * as Util from "util";
77
+ import {uid} from "uid";
78
+
79
+ import {User} from "../Models/User";
80
+ import {AuthManager} from "../Lib/AuthManager";
81
+ import {CategoriesClass} from "../Models/Categories";
82
+
83
+
84
+ const keygen = require('ssh-keygen');
85
+ const os = require('os');
86
+ const bcrypt = require('bcrypt');
87
+ const JsonWebToken = require("jsonwebtoken");
88
+ const {Op} = require("sequelize");
89
+
90
+
91
+ @controller("/api")
92
+ class ApiController extends Controller {
93
+
94
+ public myCheck(token) {
95
+ console.log("myyyyyyyyyyyyyyyyyyyyy")
96
+ }
97
+
98
+ public behaviors(): any[] {
99
+
100
+ return [{
101
+ 'authenticator': {
102
+ "class": JwtHttpBearerAuth,
103
+ // "auth": this.myCheck
104
+ },
105
+ 'access': {
106
+ 'class': AccessControl,
107
+ 'only': ['order/list' ],
108
+ 'rules': [
109
+
110
+ {
111
+ 'allow': true,
112
+ 'actions': ['order/list' ],
113
+ 'roles': ['editor'],
114
+ }
115
+ ]
116
+ }
117
+ }]
118
+ }
119
+
120
+ @get("/")
121
+ Index(req: Request, res: Response) {
122
+
123
+ BaseChyz.logs().info("Site Controller Burası", this.id)
124
+ return res.json({message: "index sayfası"})
125
+ }
126
+
127
+ @post("orderCreate")
128
+ async Login(req: Request, res: Response) {
129
+ let data = req.body;
130
+ data.Customer.status = "true";
131
+
132
+ //Customer Model Create
133
+ let customer = ModelManager.Customer.save();
134
+ //Order Model Create
135
+ let order = ModelManager.Order;
136
+
137
+
138
+ let transaction
139
+ try {
140
+ // get transaction
141
+ transaction = await BaseChyz.getComponent("db").transaction();
142
+ customer.load(data, "Customer");//load customer data
143
+ let cus: any = await customer.save({}, {transaction});
144
+
145
+ if (!cus) {
146
+ throw new ValidationHttpException(customer.errors);
147
+ }
148
+
149
+ data.Order.customer_id = cus.id;
150
+
151
+ order.load(data, "Order");
152
+ let res1 = await order.save({}, {transaction});
153
+ if (!res1) {
154
+ throw new ValidationHttpException(order.errors);
155
+ }
156
+
157
+ // commit
158
+ await transaction.commit();
159
+
160
+ } catch (e) {
161
+ if (transaction) {
162
+ await transaction.rollback();
163
+ BaseChyz.warn("Rollback transaction")
164
+ }
165
+
166
+ if (e instanceof ValidationHttpException)
167
+ throw new ValidationHttpException(e.message)
168
+ else
169
+ throw new ForbiddenHttpException(e.message)
170
+ }
171
+ return res.send("Post Controller")
172
+ }
173
+
174
+
175
+ @get("order/list")
176
+ async listOrder(req: Request, res: Response) {
177
+ const {Products}: { Products: ProductsClass } = ModelManager;
178
+ let product = await Products.findAll( );
179
+ return res.json(product)
180
+
181
+ }
182
+
183
+ @get("categories")
184
+ async Categories(req: Request, res: Response) {
185
+ let product = await ModelManager.Categories.findAll({
186
+ include: [
187
+ {
188
+ model: ModelManager.Products.model(),
189
+ }
190
+ ]
191
+ });
192
+ return res.json(product)
193
+
194
+ }
195
+
196
+ error(req: Request, res: Response) {
197
+ BaseChyz.logs().info("Error Sayfası")
198
+ return res.send("Post Controller")
199
+ }
200
+ }
201
+
202
+ module.exports = ApiController
203
+
204
+ ```
205
+
206
+
50
207
  ## Create Model
51
208
 
52
209
  Veritabanı işlemleri için model oluşturma, sequelize desteklidir.
53
210
 
211
+ Model adı "**Class**" şeklinde bitmeli.
212
+
213
+ Örnek = "ModelName**Class**"
214
+
54
215
  ```typescript
55
216
  import {Model, DataTypes} from "chyz/base/Model";
56
217
 
@@ -90,6 +251,7 @@ export class CustomerCLass extends Model {
90
251
 
91
252
  ```
92
253
  ````typescript
254
+
93
255
  export class ProductsClass extends Model {
94
256
  [x: string]: any;
95
257
 
@@ -128,6 +290,48 @@ export class ProductsClass extends Model {
128
290
  }
129
291
  }
130
292
 
293
+ /**
294
+ *
295
+ */
296
+ export class CategoriesClass extends Model {
297
+ [x: string]: any;
298
+
299
+ alias() {
300
+ return "c"
301
+ }
302
+
303
+ tableName() {
304
+ return 'categories';
305
+ }
306
+ attributes() {
307
+ return {
308
+ // Model attributes are defined here
309
+ title: {
310
+ type: DataTypes.STRING,
311
+ allowNull: false
312
+ },
313
+ properties: {
314
+ type: DataTypes.STRING,
315
+ allowNull: false
316
+ }
317
+
318
+ }
319
+ }
320
+ relations(): Relation[] {
321
+ return [
322
+ {
323
+ type: "belongsToMany",
324
+ foreignKey: "category_id",
325
+ sourceKey: "id",
326
+ as: 'product',
327
+ model: ModelManager.Products.model(),
328
+ through: ModelManager.ProductToCategories.model()
329
+ }
330
+ ]
331
+ }
332
+
333
+ }
334
+
131
335
 
132
336
  ````
133
337
 
@@ -146,9 +350,11 @@ export class ProductsClass extends Model {
146
350
  * }
147
351
  * @type {Customer}
148
352
  */
353
+ import { ModelManager} from "chyz/dist";
149
354
  import {Customer} from "./Customer";
355
+
150
356
  //Customer Model Create
151
- let customer: Customer = Customer;
357
+ let customer: Customer = ModelManager.Customer;
152
358
  customer.load(req.body, "Customer");//load customer data
153
359
  let cus: any = await customer.save();
154
360
 
@@ -164,7 +370,7 @@ Transaction oluşturma
164
370
  // get transaction
165
371
  transaction = await BaseChyz.getComponent("db").transaction();
166
372
  //Customer Model Create
167
- let customer: Customer = new Customer();
373
+ let customer: Customer = ModelManager.Customer;
168
374
  customer.load(data, "Customer");//load customer data
169
375
  let cus: any = await customer.save({}, {transaction});
170
376
  if (!cus) {
@@ -173,9 +379,9 @@ Transaction oluşturma
173
379
  } catch (e) {
174
380
  if (transaction) {
175
381
  await transaction.rollback();
176
- BaseChyz.warn("Rollback transaction")
382
+ BaseChyz.warn("Rollback transaction");
177
383
  }
178
- ...
384
+
179
385
  }
180
386
  ```
181
387
 
@@ -217,6 +423,44 @@ export class User extends Model implements IdentityInterface {
217
423
  throw new Error("Method not implemented.");
218
424
  }
219
425
 
426
+ /**
427
+ * Returns auth manager associated with the user component.
428
+ *
429
+ * By default this is the `authManager` application component.
430
+ * You may override this method to return a different auth manager instance if needed.
431
+ */
432
+ protected getAuthManager() {
433
+ return BaseChyz.getComponent("authManager");
434
+ }
435
+
436
+ /**
437
+ * Returns the access checker used for checking access.
438
+ * @return CheckAccessInterface
439
+ */
440
+ protected getAccessChecker() {
441
+ return this.accessChecker !== null ? this.accessChecker : this.getAuthManager();
442
+ }
443
+
444
+ /**
445
+ *
446
+ * @param permissionName
447
+ * @param params
448
+ * @param allowCaching
449
+ */
450
+ public async can(permissionName, params: any[] = [], allowCaching: boolean = true) {
451
+ let accessChecker;
452
+ if ((accessChecker = this.getAccessChecker()) === null) {
453
+ return false;
454
+ }
455
+
456
+ let access = await accessChecker.checkAccess(this.getId(), permissionName, params);
457
+ this._access[permissionName] = access;
458
+ if (allowCaching && Utils.isEmpty(params)) {
459
+
460
+ }
461
+ return access;
462
+ }
463
+
220
464
  public attributes() {
221
465
  return {
222
466
  // Model attributes are defined here
@@ -240,16 +484,25 @@ export class User extends Model implements IdentityInterface {
240
484
  }
241
485
 
242
486
  async findIdentityByAccessToken(token, type) {
487
+
488
+
243
489
  let decoded = JsonWebToken.decode(token, {complete: true})
490
+ if (!decoded.payload.user) {
491
+ return null;
492
+ }
244
493
  let identity = await this.findOne({where: {id: parseInt(decoded.payload.user)}});
245
494
  if(identity){
246
495
  BaseChyz.debug("Find Identity By AccessToken: User Found", decoded.payload)
247
496
  try {
248
497
  JsonWebToken.verify(token, identity.salt_text);
498
+ this.setIdentity(identity);
249
499
  BaseChyz.debug("Find Identity By AccessToken: User Verify Success")
250
- return identity;
500
+ return this;
251
501
  } catch(err) {
252
- BaseChyz.debug("Find Identity By AccessToken: User Verify Failed")
502
+ if (err.name == "TokenExpiredError")
503
+ BaseChyz.debug("Find Identity By AccessToken: Token Expired")
504
+ else
505
+ BaseChyz.debug("Find Identity By AccessToken: User Verify Failed")
253
506
  return null;
254
507
  }
255
508
  }
@@ -41,7 +41,7 @@ export class ActionFilter extends Behavior {
41
41
  } else {
42
42
  onlyMatch = false;
43
43
  for (const onlyKey of this.only) {
44
- if (Utils.matchWildcard(action, onlyKey)) {
44
+ if (Utils.matchWildcard(action.id, onlyKey)) {
45
45
  onlyMatch = true;
46
46
  break;
47
47
  }
package/base/BaseError.ts CHANGED
@@ -5,15 +5,17 @@
5
5
  * Github:https://github.com/cihan53/
6
6
  */
7
7
 
8
+ import Utils from "../requiments/Utils";
9
+
8
10
  export class BaseError extends Error {
9
11
  private statusCode: number;
10
12
 
11
13
  constructor(message: string,statusCode=500) {
12
14
  super(message);
13
- this.message=message;
15
+ this.message= Utils.isString(message)?message: JSON.stringify(message);
14
16
  this.name = this.constructor.name // good practice
15
17
  this.statusCode = statusCode // error code for responding to client
16
- Error.captureStackTrace(this)
18
+ //Error.captureStackTrace(this)
17
19
  }
18
20
 
19
21
  toString(){
@@ -9,13 +9,15 @@ const {Sequelize} = require("sequelize");
9
9
  import {Component} from "./Component";
10
10
  import BaseChyz from "../BaseChyz";
11
11
 
12
+ const sequelizeCache = require('sequelize-transparent-cache')
13
+
12
14
  export class DbConnection extends Component {
13
15
 
14
16
  public database!: string;
15
- public username!: string
16
- public password!: string
17
- public options?: object
18
- public _transaction:any;
17
+ public username!: string;
18
+ public password!: string;
19
+ public options?: object;
20
+
19
21
 
20
22
  private _db: any
21
23
 
@@ -32,10 +34,12 @@ export class DbConnection extends Component {
32
34
 
33
35
  });
34
36
 
37
+
35
38
  // await this.connect();
36
39
  }
37
40
 
38
41
 
42
+
39
43
  get db(): any {
40
44
  return this._db;
41
45
  }
@@ -44,7 +48,7 @@ export class DbConnection extends Component {
44
48
  this._db = value;
45
49
  }
46
50
 
47
- public transaction(){
51
+ public transaction() {
48
52
  return this.db.transaction();
49
53
  }
50
54