@kne/fastify-account 1.0.0-alpha.0

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 (37) hide show
  1. package/README.md +1436 -0
  2. package/index.js +54 -0
  3. package/libs/controllers/account.js +189 -0
  4. package/libs/controllers/admin.js +153 -0
  5. package/libs/controllers/adminPermission.js +230 -0
  6. package/libs/controllers/adminRole.js +145 -0
  7. package/libs/controllers/adminTenant.js +461 -0
  8. package/libs/controllers/tenant.js +22 -0
  9. package/libs/controllers/user.js +12 -0
  10. package/libs/models/admin-role.js +19 -0
  11. package/libs/models/application.js +36 -0
  12. package/libs/models/login-log.js +15 -0
  13. package/libs/models/permission.js +53 -0
  14. package/libs/models/tenant-application.js +28 -0
  15. package/libs/models/tenant-org.js +30 -0
  16. package/libs/models/tenant-permission.js +28 -0
  17. package/libs/models/tenant-role-application.js +32 -0
  18. package/libs/models/tenant-role-permission.js +32 -0
  19. package/libs/models/tenant-role.js +27 -0
  20. package/libs/models/tenant-share-group-permission.js +22 -0
  21. package/libs/models/tenant-share-group.js +22 -0
  22. package/libs/models/tenant-source-user-share-group.js +22 -0
  23. package/libs/models/tenant-token.js +32 -0
  24. package/libs/models/tenant-user-org.js +22 -0
  25. package/libs/models/tenant-user-role.js +22 -0
  26. package/libs/models/tenant-user-share-group.js +22 -0
  27. package/libs/models/tenant-user.js +56 -0
  28. package/libs/models/tenant.js +38 -0
  29. package/libs/models/user-account.js +26 -0
  30. package/libs/models/user.js +50 -0
  31. package/libs/models/verification-code.js +26 -0
  32. package/libs/services/account.js +200 -0
  33. package/libs/services/admin.js +183 -0
  34. package/libs/services/permission.js +465 -0
  35. package/libs/services/tenant.js +576 -0
  36. package/libs/services/user.js +108 -0
  37. package/package.json +61 -0
@@ -0,0 +1,145 @@
1
+ const fp = require('fastify-plugin');
2
+
3
+ module.exports = fp(async (fastify, options) => {
4
+ fastify.get(
5
+ `${options.prefix}/admin/getRoleList`,
6
+ {
7
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
8
+ schema: {
9
+ query: {
10
+ type: 'object',
11
+ required: ['tenantId'],
12
+ properties: {
13
+ tenantId: { type: 'string' },
14
+ filter: {
15
+ type: 'object',
16
+ properties: {
17
+ type: { type: 'number' }
18
+ }
19
+ }
20
+ }
21
+ }
22
+ }
23
+ },
24
+ async request => {
25
+ const { filter, tenantId, perPage, currentPage } = Object.assign(
26
+ {
27
+ perPage: 20,
28
+ currentPage: 1,
29
+ filter: {}
30
+ },
31
+ request.query
32
+ );
33
+ return await fastify.account.services.tenant.getRoleList({ tenantId, perPage, currentPage, filter });
34
+ }
35
+ );
36
+
37
+ fastify.post(
38
+ `${options.prefix}/admin/addRole`,
39
+ {
40
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
41
+ schema: {
42
+ body: {
43
+ type: 'object',
44
+ required: ['tenantId', 'name'],
45
+ properties: {
46
+ tenantId: { type: 'string' },
47
+ name: { type: 'string' },
48
+ description: { type: 'string' }
49
+ }
50
+ }
51
+ }
52
+ },
53
+ async request => {
54
+ const { tenantId, name, description } = request.body;
55
+ await fastify.account.services.tenant.addRole({ tenantId, name, description });
56
+
57
+ return {};
58
+ }
59
+ );
60
+
61
+ fastify.post(
62
+ `${options.prefix}/admin/saveRole`,
63
+ {
64
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
65
+ schema: {
66
+ body: {
67
+ type: 'object',
68
+ required: ['name', 'id'],
69
+ properties: {
70
+ id: { type: 'number' },
71
+ name: { type: 'string' },
72
+ description: { type: 'string' }
73
+ }
74
+ }
75
+ }
76
+ },
77
+ async request => {
78
+ await fastify.account.services.tenant.saveRole(request.body);
79
+ return {};
80
+ }
81
+ );
82
+
83
+ fastify.post(
84
+ `${options.prefix}/admin/removeRole`,
85
+ {
86
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
87
+ schema: {
88
+ body: {
89
+ type: 'object',
90
+ required: ['id'],
91
+ properties: {
92
+ id: { type: 'number' }
93
+ }
94
+ }
95
+ }
96
+ },
97
+ async request => {
98
+ const { id } = request.body;
99
+ await fastify.account.services.tenant.removeRole({ id });
100
+ return {};
101
+ }
102
+ );
103
+
104
+ fastify.get(
105
+ `${options.prefix}/admin/getRolePermissionList`,
106
+ {
107
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
108
+ schema: {
109
+ query: {
110
+ type: 'object',
111
+ required: ['id'],
112
+ properties: {
113
+ id: { type: 'number' }
114
+ }
115
+ }
116
+ }
117
+ },
118
+ async request => {
119
+ const { id } = request.query;
120
+ return await fastify.account.services.permission.getRolePermissionList({ roleId: id });
121
+ }
122
+ );
123
+
124
+ fastify.post(
125
+ `${options.prefix}/admin/saveRolePermissionList`,
126
+ {
127
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
128
+ schema: {
129
+ roleId: { type: 'string' },
130
+ applications: {
131
+ type: 'array',
132
+ items: { type: 'string' }
133
+ },
134
+ permissions: {
135
+ type: 'array',
136
+ items: { type: 'number' }
137
+ }
138
+ }
139
+ },
140
+ async request => {
141
+ await fastify.account.services.permission.saveRolePermissionList(request.body);
142
+ return {};
143
+ }
144
+ );
145
+ });
@@ -0,0 +1,461 @@
1
+ const fp = require('fastify-plugin');
2
+
3
+ module.exports = fp(async (fastify, options) => {
4
+ fastify.get(
5
+ `${options.prefix}/admin/getAllTenantList`,
6
+ {
7
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
8
+ schema: {
9
+ query: {
10
+ type: 'object',
11
+ properties: {
12
+ name: {
13
+ type: 'string'
14
+ },
15
+ serviceStartTime: {
16
+ type: 'string',
17
+ format: 'date-time'
18
+ },
19
+ serviceEndTime: {
20
+ type: 'string',
21
+ format: 'date-time'
22
+ },
23
+ perPage: {
24
+ type: 'number'
25
+ },
26
+ currentPage: {
27
+ type: 'number'
28
+ }
29
+ }
30
+ }
31
+ }
32
+ },
33
+ async request => {
34
+ const { name, perPage, currentPage } = Object.assign(
35
+ {
36
+ perPage: 20,
37
+ currentPage: 1
38
+ },
39
+ request.query
40
+ );
41
+ return await fastify.account.services.admin.getAllTenantList({
42
+ filter: { name },
43
+ perPage,
44
+ currentPage
45
+ });
46
+ }
47
+ );
48
+
49
+ fastify.get(
50
+ `${options.prefix}/admin/getTenantInfo`,
51
+ {
52
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
53
+ schema: {
54
+ query: {
55
+ type: 'object',
56
+ properties: {
57
+ id: { type: 'string' }
58
+ }
59
+ }
60
+ }
61
+ },
62
+ async request => {
63
+ const { id } = request.query;
64
+ return await fastify.account.services.tenant.getTenantInfo({ id });
65
+ }
66
+ );
67
+
68
+ fastify.post(
69
+ `${options.prefix}/admin/addTenant`,
70
+ {
71
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
72
+ schema: {
73
+ body: {
74
+ type: 'object',
75
+ required: ['name', 'accountNumber', 'serviceStartTime', 'serviceEndTime'],
76
+ properties: {
77
+ name: { type: 'string' },
78
+ accountNumber: { type: 'number' },
79
+ serviceStartTime: {
80
+ type: 'string',
81
+ format: 'date-time'
82
+ },
83
+ serviceEndTime: {
84
+ type: 'string',
85
+ format: 'date-time'
86
+ }
87
+ }
88
+ }
89
+ }
90
+ },
91
+ async request => {
92
+ await fastify.account.services.admin.addTenant(request.body);
93
+ return {};
94
+ }
95
+ );
96
+
97
+ fastify.post(
98
+ `${options.prefix}/admin/saveTenant`,
99
+ {
100
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
101
+ schema: {
102
+ body: {
103
+ type: 'object',
104
+ required: ['id', 'name', 'accountNumber', 'serviceStartTime', 'serviceEndTime'],
105
+ properties: {
106
+ id: { type: 'string' },
107
+ name: { type: 'string' },
108
+ accountNumber: { type: 'number' },
109
+ serviceStartTime: {
110
+ type: 'string',
111
+ format: 'date-time'
112
+ },
113
+ serviceEndTime: {
114
+ type: 'string',
115
+ format: 'date-time'
116
+ }
117
+ }
118
+ }
119
+ }
120
+ },
121
+ async request => {
122
+ await fastify.account.services.admin.saveTenant(request.body);
123
+ return {};
124
+ }
125
+ );
126
+
127
+ fastify.post(
128
+ `${options.prefix}/admin/tenant/addOrg`,
129
+ {
130
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
131
+ required: ['name', 'tenantId', 'pid'],
132
+ properties: {
133
+ name: { type: 'string' },
134
+ tenantId: { type: 'string' },
135
+ pid: { type: 'number' }
136
+ }
137
+ },
138
+ async request => {
139
+ return await fastify.account.services.tenant.addTenantOrg(request.body);
140
+ }
141
+ );
142
+
143
+ fastify.get(
144
+ `${options.prefix}/admin/tenant/orgList`,
145
+ {
146
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
147
+ schema: {
148
+ query: {
149
+ type: 'object',
150
+ properties: {
151
+ id: { type: 'string' }
152
+ }
153
+ }
154
+ }
155
+ },
156
+ async request => {
157
+ const { tenantId } = request.query;
158
+ return await fastify.account.services.tenant.getTenantOrgList({ tenantId });
159
+ }
160
+ );
161
+
162
+ fastify.post(
163
+ `${options.prefix}/admin/tenant/editOrg`,
164
+ {
165
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
166
+ required: ['name', 'tenantId', 'pid'],
167
+ properties: {
168
+ name: { type: 'string' },
169
+ tenantId: { type: 'string' },
170
+ pid: { type: 'number' }
171
+ }
172
+ },
173
+ async request => {
174
+ await fastify.account.services.tenant.saveTenantOrg(request.body);
175
+ return {};
176
+ }
177
+ );
178
+
179
+ fastify.post(
180
+ `${options.prefix}/admin/tenant/removeOrg`,
181
+ {
182
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
183
+ required: ['tenantId', 'id'],
184
+ properties: {
185
+ tenantId: { type: 'string' },
186
+ id: { type: 'number' }
187
+ }
188
+ },
189
+ async request => {
190
+ await fastify.account.services.tenant.deleteTenantOrg(request.body);
191
+ return {};
192
+ }
193
+ );
194
+
195
+ fastify.get(
196
+ `${options.prefix}/admin/getTenantUserList`,
197
+ {
198
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
199
+ schema: {
200
+ query: {
201
+ type: 'object',
202
+ properties: {
203
+ tenantId: { type: 'string' }
204
+ }
205
+ }
206
+ }
207
+ },
208
+ async request => {
209
+ const { tenantId } = request.query;
210
+ return await fastify.account.services.tenant.getTenantUserList({ tenantId });
211
+ }
212
+ );
213
+
214
+ fastify.post(
215
+ `${options.prefix}/admin/addTenantUser`,
216
+ {
217
+ schema: {
218
+ body: {
219
+ type: 'object',
220
+ required: ['tenantId', 'userId', 'name'],
221
+ properties: {
222
+ tenantId: { type: 'string' },
223
+ roleIds: { type: 'array', items: { type: 'number' }, default: [] },
224
+ orgIds: {
225
+ type: 'array',
226
+ items: { type: 'number' },
227
+ default: []
228
+ },
229
+ userId: { type: 'string' },
230
+ name: { type: 'string' },
231
+ avatar: { type: 'string' },
232
+ phone: { type: 'string' },
233
+ email: { type: 'string' },
234
+ description: { type: 'string' }
235
+ }
236
+ }
237
+ }
238
+ },
239
+ async request => {
240
+ await fastify.account.services.tenant.addTenantUser(request.body);
241
+ return {};
242
+ }
243
+ );
244
+
245
+ fastify.post(
246
+ `${options.prefix}/admin/saveTenantUser`,
247
+ {
248
+ schema: {
249
+ body: {
250
+ type: 'object',
251
+ required: ['tenantId', 'name'],
252
+ properties: {
253
+ tenantId: { type: 'string' },
254
+ roleIds: { type: 'array', items: { type: 'number' }, default: [] },
255
+ orgIds: {
256
+ type: 'array',
257
+ items: { type: 'number' },
258
+ default: []
259
+ },
260
+ name: { type: 'string' },
261
+ avatar: { type: 'string' },
262
+ phone: { type: 'string' },
263
+ email: { type: 'string' },
264
+ description: { type: 'string' }
265
+ }
266
+ }
267
+ }
268
+ },
269
+ async request => {
270
+ await fastify.account.services.tenant.saveTenantUser(request.body);
271
+ return {};
272
+ }
273
+ );
274
+
275
+ fastify.post(
276
+ `${options.prefix}/admin/deleteTenantUser`,
277
+ {
278
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
279
+ schema: {
280
+ body: {
281
+ type: 'object',
282
+ required: ['tenantId', 'tenantUserId'],
283
+ properties: {
284
+ tenantId: { type: 'string' },
285
+ tenantUserId: { type: 'string' }
286
+ }
287
+ }
288
+ }
289
+ },
290
+ async request => {
291
+ const { tenantId, tenantUserId } = request.body;
292
+ await fastify.account.services.tenant.deleteTenantUser({ tenantId, tenantUserId });
293
+ return {};
294
+ }
295
+ );
296
+
297
+ fastify.post(
298
+ `${options.prefix}/admin/closeTenant`,
299
+ {
300
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
301
+ schema: {
302
+ body: {
303
+ type: 'object',
304
+ required: ['tenantId'],
305
+ properties: {
306
+ tenantId: { type: 'string' }
307
+ }
308
+ }
309
+ }
310
+ },
311
+ async request => {
312
+ const { tenantId } = request.body;
313
+ await fastify.account.services.tenant.closeTenant({ tenantId });
314
+ return {};
315
+ }
316
+ );
317
+
318
+ fastify.post(
319
+ `${options.prefix}/admin/openTenant`,
320
+ {
321
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
322
+ schema: {
323
+ body: {
324
+ type: 'object',
325
+ required: ['tenantId'],
326
+ properties: {
327
+ tenantId: { type: 'string' }
328
+ }
329
+ }
330
+ }
331
+ },
332
+ async request => {
333
+ const { tenantId } = request.body;
334
+ await fastify.account.services.tenant.openTenant({ tenantId });
335
+ return {};
336
+ }
337
+ );
338
+
339
+ fastify.post(
340
+ `${options.prefix}/admin/closeTenantUser`,
341
+ {
342
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
343
+ schema: {
344
+ body: {
345
+ type: 'object',
346
+ required: ['tenantId', 'tenantUserId'],
347
+ properties: {
348
+ tenantId: { type: 'string' },
349
+ tenantUserId: { type: 'string' }
350
+ }
351
+ }
352
+ }
353
+ },
354
+ async request => {
355
+ const { tenantId, tenantUserId } = request.body;
356
+ await fastify.account.services.tenant.closeTenantUser({ tenantId, tenantUserId });
357
+ return {};
358
+ }
359
+ );
360
+
361
+ fastify.post(
362
+ `${options.prefix}/admin/openTenantUser`,
363
+ {
364
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
365
+ schema: {
366
+ body: {
367
+ type: 'object',
368
+ required: ['tenantId', 'tenantUserId'],
369
+ properties: {
370
+ tenantId: { type: 'string' },
371
+ tenantUserId: { type: 'string' }
372
+ }
373
+ }
374
+ }
375
+ },
376
+ async request => {
377
+ const { tenantId, tenantUserId } = request.body;
378
+ await fastify.account.services.tenant.openTenantUser({ tenantId, tenantUserId });
379
+ return {};
380
+ }
381
+ );
382
+
383
+ fastify.get(
384
+ `${options.prefix}/admin/getInviteList`,
385
+ {
386
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
387
+ schema: {
388
+ query: {
389
+ type: 'object',
390
+ required: ['tenantId'],
391
+ properties: {
392
+ tenantId: { type: 'string' }
393
+ }
394
+ }
395
+ }
396
+ },
397
+ async request => {
398
+ const { filter, perPage, currentPage, tenantId } = Object.assign(
399
+ {
400
+ perPage: 20,
401
+ currentPage: 1
402
+ },
403
+ request.query
404
+ );
405
+
406
+ return await fastify.account.services.tenant.getInviteList({ filter, perPage, currentPage, tenantId });
407
+ }
408
+ );
409
+
410
+ fastify.post(
411
+ `${options.prefix}/admin/addInviteToken`,
412
+ {
413
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
414
+ schema: {
415
+ body: {
416
+ type: 'object',
417
+ required: ['tenantId'],
418
+ properties: {
419
+ tenantId: { type: 'string' },
420
+ info: {
421
+ type: 'object',
422
+ properties: {
423
+ roleIds: { type: 'array', items: { type: 'number' }, default: [] },
424
+ orgIds: {
425
+ type: 'array',
426
+ items: { type: 'number' },
427
+ default: []
428
+ }
429
+ }
430
+ }
431
+ }
432
+ }
433
+ }
434
+ },
435
+ async request => {
436
+ const { tenantId, info } = request.body;
437
+ await fastify.account.services.tenant.addInviteToken({ tenantId, info });
438
+ return {};
439
+ }
440
+ );
441
+
442
+ fastify.post(
443
+ `${options.prefix}/admin/deleteInviteToken`,
444
+ {
445
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
446
+ schema: {
447
+ body: {
448
+ type: 'object',
449
+ required: ['id'],
450
+ properties: {
451
+ id: { type: 'number' }
452
+ }
453
+ }
454
+ }
455
+ },
456
+ async request => {
457
+ await fastify.account.services.tenant.deleteInviteToken({ id: request.body.id });
458
+ return {};
459
+ }
460
+ );
461
+ });
@@ -0,0 +1,22 @@
1
+ const fp = require('fastify-plugin');
2
+ module.exports = fp(async (fastify, options) => {
3
+ fastify.get(
4
+ `${options.prefix}/tenant/getUserTenant`,
5
+ {
6
+ onRequest: [fastify.account.authenticate.user]
7
+ },
8
+ async request => {
9
+ return await fastify.account.services.tenant.getUserTenant(request.authenticatePayload);
10
+ }
11
+ );
12
+
13
+ fastify.get(
14
+ `${options.prefix}/tenant/getUserCurrentTenant`,
15
+ {
16
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.tenant]
17
+ },
18
+ async request => {
19
+ return request.tenantInfo;
20
+ }
21
+ );
22
+ });
@@ -0,0 +1,12 @@
1
+ const fp = require('fastify-plugin');
2
+ module.exports = fp(async (fastify, options) => {
3
+ fastify.get(
4
+ `${options.prefix}/getUserInfo`,
5
+ {
6
+ onRequest: [fastify.account.authenticate.user]
7
+ },
8
+ async request => {
9
+ return { userInfo: request.userInfo };
10
+ }
11
+ );
12
+ });
@@ -0,0 +1,19 @@
1
+ module.exports = (sequelize, DataTypes) => {
2
+ return sequelize.define(
3
+ 'adminRole',
4
+ {
5
+ userId: {
6
+ type: DataTypes.UUID,
7
+ allowNull: false
8
+ },
9
+ role: {
10
+ type: DataTypes.STRING,
11
+ allowNull: false
12
+ },
13
+ target: DataTypes.STRING
14
+ },
15
+ {
16
+ paranoid: true
17
+ }
18
+ );
19
+ };
@@ -0,0 +1,36 @@
1
+ module.exports = (sequelize, DataTypes) => {
2
+ return sequelize.define(
3
+ 'application',
4
+ {
5
+ id: {
6
+ type: DataTypes.UUID,
7
+ defaultValue: DataTypes.UUIDV4,
8
+ primaryKey: true
9
+ },
10
+ avatar: DataTypes.STRING,
11
+ name: {
12
+ type: DataTypes.STRING,
13
+ allowNull: false
14
+ },
15
+ url: DataTypes.STRING,
16
+ code: {
17
+ type: DataTypes.STRING,
18
+ allowNull: false
19
+ },
20
+ description: DataTypes.TEXT,
21
+ status: {
22
+ type: DataTypes.INTEGER,
23
+ defaultValue: 0
24
+ }
25
+ },
26
+ {
27
+ paranoid: true,
28
+ indexes: [
29
+ {
30
+ unique: true,
31
+ fields: ['code', 'deletedAt']
32
+ }
33
+ ]
34
+ }
35
+ );
36
+ };
@@ -0,0 +1,15 @@
1
+ module.exports = (sequelize, DataTypes) => {
2
+ return sequelize.define(
3
+ 'loginLog',
4
+ {
5
+ userId: {
6
+ type: DataTypes.UUID,
7
+ allowNull: false
8
+ },
9
+ ip: DataTypes.STRING
10
+ },
11
+ {
12
+ paranoid: true
13
+ }
14
+ );
15
+ };