@kne/fastify-account 1.0.0-alpha.2 → 1.0.0-alpha.4

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 (42) hide show
  1. package/README.md +47 -2
  2. package/index.js +10 -4
  3. package/libs/controllers/account.js +7 -6
  4. package/libs/controllers/admin.js +16 -15
  5. package/libs/controllers/adminPermission.js +42 -35
  6. package/libs/controllers/adminRole.js +13 -12
  7. package/libs/controllers/adminTenant.js +39 -36
  8. package/libs/controllers/tenant.js +7 -6
  9. package/libs/controllers/user.js +4 -3
  10. package/libs/models/admin-role.js +4 -8
  11. package/libs/models/application.js +16 -10
  12. package/libs/models/login-log.js +4 -8
  13. package/libs/models/permission.js +7 -9
  14. package/libs/models/tenant-application.js +8 -10
  15. package/libs/models/tenant-org.js +5 -9
  16. package/libs/models/tenant-permission.js +7 -9
  17. package/libs/models/tenant-role-application.js +13 -13
  18. package/libs/models/tenant-role-permission.js +9 -14
  19. package/libs/models/tenant-role.js +5 -9
  20. package/libs/models/tenant-share-group-permission.js +5 -9
  21. package/libs/models/tenant-share-group.js +5 -9
  22. package/libs/models/tenant-source-user-share-group.js +5 -9
  23. package/libs/models/tenant-token.js +7 -9
  24. package/libs/models/tenant-user-org.js +11 -10
  25. package/libs/models/tenant-user-role.js +11 -10
  26. package/libs/models/tenant-user-share-group.js +6 -10
  27. package/libs/models/tenant-user.js +35 -16
  28. package/libs/models/tenant.js +17 -9
  29. package/libs/models/user-account.js +17 -9
  30. package/libs/models/user.js +27 -17
  31. package/libs/models/verification-code.js +4 -8
  32. package/libs/services/account.js +26 -16
  33. package/libs/services/admin.js +14 -116
  34. package/libs/services/application.js +151 -0
  35. package/libs/services/permission.js +47 -145
  36. package/libs/services/tenant-invite.js +62 -0
  37. package/libs/services/tenant-org.js +84 -0
  38. package/libs/services/tenant-role.js +108 -0
  39. package/libs/services/tenant-user.js +486 -0
  40. package/libs/services/tenant.js +68 -669
  41. package/libs/services/user.js +63 -33
  42. package/package.json +3 -3
@@ -1,116 +1,22 @@
1
1
  const fp = require('fastify-plugin');
2
2
  const isNil = require('lodash/isNil');
3
3
  module.exports = fp(async (fastify, options) => {
4
- const addApplication = async application => {
5
- return fastify.account.models.application.create(application);
6
- };
7
- const saveApplication = async ({ id, ...others }) => {
8
- const application = await fastify.account.models.application.findByPk(id);
9
- if (!application) {
10
- throw new Error('应用不存在');
11
- }
12
- ['name', 'code', 'avatar', 'url', 'description'].forEach(name => {
13
- if (!isNil(others[name])) {
14
- application[name] = others[name];
15
- }
16
- });
17
-
18
- await application.save();
19
- };
20
-
21
- const deleteApplication = async ({ id }) => {
22
- const application = await fastify.account.models.application.findByPk(id);
23
- if (!application) {
24
- throw new Error('应用不存在');
25
- }
26
- if (
27
- (await fastify.account.models.tenantApplication.count({
28
- where: {
29
- applicationId: application.id
30
- }
31
- })) > 0
32
- ) {
33
- throw new Error('应用已经开放给其他租户使用,不能删除');
34
- }
35
-
36
- const permissionIdList = (
37
- await fastify.account.models.permission.findAll({
38
- where: { applicationId: application.id }
39
- })
40
- ).map(({ id }) => id);
41
-
42
- const t = await fastify.sequelize.instance.transaction();
43
-
44
- try {
45
- await fastify.account.models.tenantPermission.destroy(
46
- {
47
- where: {
48
- permissionId: {
49
- [fastify.sequelize.Sequelize.Op.in]: permissionIdList
50
- }
51
- }
52
- },
53
- { transaction: t }
54
- );
55
-
56
- await fastify.account.models.tenantRolePermission.destroy(
57
- {
58
- where: {
59
- permissionId: {
60
- [fastify.sequelize.Sequelize.Op.in]: permissionIdList
61
- }
62
- }
63
- },
64
- { transaction: t }
65
- );
66
-
67
- await fastify.account.models.permission.destroy({
68
- where: {
69
- applicationId: application.id
70
- },
71
- transaction: t
72
- });
73
- await application.destroy({ transaction: t });
74
- await t.commit();
75
- } catch (e) {
76
- await t.rollback();
77
- throw e;
78
- }
79
- };
80
-
81
- const getApplicationList = async ({ tenantId }) => {
82
- const query = {};
83
- if (tenantId) {
84
- const tenant = await fastify.account.models.tenant.findByPk(tenantId);
85
- if (!tenant) {
86
- throw new Error('租户不存在');
87
- }
88
- const tenantApplications = await fastify.account.models.tenantApplication.findAll({
89
- where: { tenantId }
90
- });
91
- query.id = {
92
- [fastify.sequelize.Sequelize.Op.in]: tenantApplications.map(({ applicationId }) => applicationId)
93
- };
94
- }
95
- return await fastify.account.models.application.findAll({
96
- where: query
97
- });
98
- };
4
+ const { models, services } = fastify.account;
99
5
 
100
6
  const addPermission = async ({ applicationId, pid, code, name, type, isModule, isMust, description }) => {
101
- if (!(await fastify.account.models.application.findByPk(applicationId))) {
7
+ if (!(await services.application.getApplication({ id: applicationId }))) {
102
8
  throw new Error('应用不存在');
103
9
  }
104
10
  const paths = [];
105
11
  if (pid > 0) {
106
- const parentNode = await fastify.account.models.permission.findByPk(pid);
12
+ const parentNode = await models.permission.findByPk(pid);
107
13
  if (!parentNode) {
108
14
  throw new Error('未找到父级');
109
15
  }
110
16
  paths.push(...parentNode.paths, parentNode.id);
111
17
  }
112
18
  if (
113
- (await fastify.account.models.permission.count({
19
+ (await models.permission.count({
114
20
  where: {
115
21
  pid,
116
22
  code,
@@ -120,7 +26,7 @@ module.exports = fp(async (fastify, options) => {
120
26
  ) {
121
27
  throw new Error('同一级权限code不能重复');
122
28
  }
123
- return await fastify.account.models.permission.create({
29
+ return await models.permission.create({
124
30
  applicationId,
125
31
  code,
126
32
  description,
@@ -135,12 +41,10 @@ module.exports = fp(async (fastify, options) => {
135
41
 
136
42
  const getPermissionList = async ({ applicationId, tenantId }) => {
137
43
  const query = {};
44
+
138
45
  if (tenantId) {
139
- const tenant = await fastify.account.models.tenant.findByPk(tenantId);
140
- if (!tenant) {
141
- throw new Error('租户不存在');
142
- }
143
- const tenantPermissions = await fastify.account.models.tenantPermission.findAll({
46
+ await services.tenant.getTenant({ id: tenantId });
47
+ const tenantPermissions = await models.tenantPermission.findAll({
144
48
  where: { tenantId }
145
49
  });
146
50
  query[fastify.sequelize.Sequelize.Op.or] = [
@@ -152,19 +56,24 @@ module.exports = fp(async (fastify, options) => {
152
56
  { isMust: 1 }
153
57
  ];
154
58
  }
155
- return await fastify.account.models.permission.findAll({
59
+
60
+ await services.application.getApplication({ id: applicationId });
61
+
62
+ return await models.permission.findAll({
156
63
  where: Object.assign({}, { applicationId }, query)
157
64
  });
158
65
  };
159
66
 
160
67
  const deletePermission = async ({ id }) => {
161
- const currentPermission = await fastify.account.models.permission.findByPk(id);
68
+ const currentPermission = await models.permission.findByPk(id);
162
69
 
163
70
  if (!currentPermission) {
164
71
  throw new Error('权限不存在');
165
72
  }
166
73
 
167
- const permissionList = await fastify.account.models.permission.findAll({
74
+ await services.application.getApplication({ id: currentPermission.applicationId });
75
+
76
+ const permissionList = await models.permission.findAll({
168
77
  where: {
169
78
  applicationId: currentPermission.applicationId
170
79
  }
@@ -178,7 +87,7 @@ module.exports = fp(async (fastify, options) => {
178
87
 
179
88
  const t = await fastify.sequelize.instance.transaction();
180
89
  try {
181
- await fastify.account.models.tenantPermission.destroy({
90
+ await models.tenantPermission.destroy({
182
91
  where: {
183
92
  permissionId: {
184
93
  [fastify.sequelize.Sequelize.Op.in]: permissionIdList
@@ -186,7 +95,7 @@ module.exports = fp(async (fastify, options) => {
186
95
  },
187
96
  transaction: t
188
97
  });
189
- await fastify.account.models.tenantRolePermission.destroy({
98
+ await models.tenantRolePermission.destroy({
190
99
  where: {
191
100
  permissionId: {
192
101
  [fastify.sequelize.Sequelize.Op.in]: permissionIdList
@@ -194,7 +103,7 @@ module.exports = fp(async (fastify, options) => {
194
103
  },
195
104
  transaction: t
196
105
  });
197
- await fastify.account.models.permission.destroy({
106
+ await models.permission.destroy({
198
107
  where: {
199
108
  id: {
200
109
  [fastify.sequelize.Sequelize.Op.in]: permissionIdList
@@ -210,7 +119,7 @@ module.exports = fp(async (fastify, options) => {
210
119
  };
211
120
 
212
121
  const savePermission = async permission => {
213
- const currentPermission = await fastify.account.models.permission.findByPk(permission.id);
122
+ const currentPermission = await models.permission.findByPk(permission.id);
214
123
 
215
124
  if (!permission) {
216
125
  throw new Error('权限不存在');
@@ -226,16 +135,14 @@ module.exports = fp(async (fastify, options) => {
226
135
  };
227
136
 
228
137
  const saveTenantPermissionList = async ({ tenantId, applications, permissions }) => {
229
- if (!(await fastify.account.models.tenant.findByPk(tenantId))) {
230
- throw new Error('租户不存在');
231
- }
232
- const currentApplications = await fastify.account.models.tenantApplication.findAll({
138
+ await services.tenant.getTenant({ id: tenantId });
139
+ const currentApplications = await models.tenantApplication.findAll({
233
140
  where: { tenantId }
234
141
  });
235
142
 
236
143
  const currentApplicationIds = currentApplications.map(({ applicationId }) => applicationId);
237
144
 
238
- const currentPermissions = await fastify.account.models.tenantPermission.findAll({
145
+ const currentPermissions = await models.tenantPermission.findAll({
239
146
  where: { tenantId }
240
147
  });
241
148
 
@@ -249,7 +156,7 @@ module.exports = fp(async (fastify, options) => {
249
156
  const needDeletePermissions = currentPermissions.filter(item => permissions.indexOf(item.permissionId) === -1).map(({ permissionId }) => permissionId);
250
157
  const needAddPermissions = permissions.filter(permissionId => currentPermissionIds.indexOf(permissionId) === -1);
251
158
 
252
- await fastify.account.models.tenantRoleApplication.destroy({
159
+ await models.tenantRoleApplication.destroy({
253
160
  where: {
254
161
  applicationId: {
255
162
  [fastify.sequelize.Sequelize.Op.in]: needDeleteApplications
@@ -259,7 +166,7 @@ module.exports = fp(async (fastify, options) => {
259
166
  transaction: t
260
167
  });
261
168
 
262
- await fastify.account.models.tenantRolePermission.destroy({
169
+ await models.tenantRolePermission.destroy({
263
170
  where: {
264
171
  permissionId: {
265
172
  [fastify.sequelize.Sequelize.Op.in]: needDeletePermissions
@@ -269,7 +176,7 @@ module.exports = fp(async (fastify, options) => {
269
176
  transaction: t
270
177
  });
271
178
 
272
- await fastify.account.models.tenantApplication.destroy({
179
+ await models.tenantApplication.destroy({
273
180
  where: {
274
181
  applicationId: {
275
182
  [fastify.sequelize.Sequelize.Op.in]: needDeleteApplications
@@ -279,7 +186,7 @@ module.exports = fp(async (fastify, options) => {
279
186
  transaction: t
280
187
  });
281
188
 
282
- await fastify.account.models.tenantPermission.destroy({
189
+ await models.tenantPermission.destroy({
283
190
  where: {
284
191
  permissionId: {
285
192
  [fastify.sequelize.Sequelize.Op.in]: needDeletePermissions
@@ -290,7 +197,7 @@ module.exports = fp(async (fastify, options) => {
290
197
  });
291
198
 
292
199
  needAddApplications.length > 0 &&
293
- (await fastify.account.models.tenantApplication.bulkCreate(
200
+ (await models.tenantApplication.bulkCreate(
294
201
  needAddApplications.map(applicationId => {
295
202
  return { tenantId, applicationId };
296
203
  }),
@@ -298,7 +205,7 @@ module.exports = fp(async (fastify, options) => {
298
205
  ));
299
206
 
300
207
  needAddPermissions.length > 0 &&
301
- (await fastify.account.models.tenantPermission.bulkCreate(
208
+ (await models.tenantPermission.bulkCreate(
302
209
  needAddPermissions.map(permissionId => {
303
210
  return { tenantId, permissionId };
304
211
  }),
@@ -313,22 +220,21 @@ module.exports = fp(async (fastify, options) => {
313
220
  };
314
221
 
315
222
  const saveRolePermissionList = async ({ roleId, applications, permissions }) => {
316
- const role = await fastify.account.models.tenantRole.findByPk(roleId);
223
+ const role = await models.tenantRole.findByPk(roleId);
317
224
  if (!role) {
318
225
  throw new Error('角色不存在');
319
226
  }
320
- if (!(await fastify.account.models.tenant.findByPk(role.tenantId))) {
321
- throw new Error('租户不存在');
322
- }
323
227
 
324
228
  const tenantId = role.tenantId;
325
229
 
326
- const tenantApplications = await fastify.account.models.tenantApplication.findAll({
230
+ await services.tenant.getTenant({ id: tenantId });
231
+
232
+ const tenantApplications = await models.tenantApplication.findAll({
327
233
  attributes: ['applicationId'],
328
234
  where: { tenantId }
329
235
  });
330
236
 
331
- const tenantPermissions = await fastify.account.models.tenantPermission.findAll({
237
+ const tenantPermissions = await models.tenantPermission.findAll({
332
238
  attributes: ['permissionId'],
333
239
  where: { tenantId }
334
240
  });
@@ -336,7 +242,7 @@ module.exports = fp(async (fastify, options) => {
336
242
  const tenantApplicationIds = tenantApplications.map(({ applicationId }) => applicationId);
337
243
  const tenantPermissionIds = tenantPermissions.map(({ permissionId }) => permissionId);
338
244
 
339
- const currentApplications = await fastify.account.models.tenantRoleApplication.findAll({
245
+ const currentApplications = await models.tenantRoleApplication.findAll({
340
246
  where: {
341
247
  roleId: role.id,
342
248
  tenantId,
@@ -346,7 +252,7 @@ module.exports = fp(async (fastify, options) => {
346
252
  }
347
253
  });
348
254
 
349
- const currentPermissions = await fastify.account.models.tenantRolePermission.findAll({
255
+ const currentPermissions = await models.tenantRolePermission.findAll({
350
256
  where: {
351
257
  roleId: role.id,
352
258
  tenantId,
@@ -367,7 +273,7 @@ module.exports = fp(async (fastify, options) => {
367
273
  const needAddPermissions = permissions.filter(permissionId => currentPermissionIds.indexOf(permissionId) === -1 && tenantPermissionIds.indexOf(permissionId) > -1);
368
274
 
369
275
  needDeleteApplications.length > 0 &&
370
- (await fastify.account.models.tenantRoleApplication.destroy({
276
+ (await models.tenantRoleApplication.destroy({
371
277
  where: {
372
278
  applicationId: {
373
279
  [fastify.sequelize.Sequelize.Op.in]: needDeleteApplications
@@ -378,7 +284,7 @@ module.exports = fp(async (fastify, options) => {
378
284
  }));
379
285
 
380
286
  needDeletePermissions.length > 0 &&
381
- (await fastify.account.models.tenantRolePermission.destroy({
287
+ (await models.tenantRolePermission.destroy({
382
288
  where: {
383
289
  permissionId: {
384
290
  [fastify.sequelize.Sequelize.Op.in]: needDeletePermissions
@@ -389,7 +295,7 @@ module.exports = fp(async (fastify, options) => {
389
295
  }));
390
296
 
391
297
  needAddApplications.length > 0 &&
392
- (await fastify.account.models.tenantRoleApplication.bulkCreate(
298
+ (await models.tenantRoleApplication.bulkCreate(
393
299
  needAddApplications.map(applicationId => {
394
300
  return {
395
301
  tenantId,
@@ -401,7 +307,7 @@ module.exports = fp(async (fastify, options) => {
401
307
  ));
402
308
 
403
309
  needAddPermissions.length > 0 &&
404
- (await fastify.account.models.tenantRolePermission.bulkCreate(
310
+ (await models.tenantRolePermission.bulkCreate(
405
311
  needAddPermissions.map(permissionId => {
406
312
  return {
407
313
  tenantId,
@@ -420,13 +326,13 @@ module.exports = fp(async (fastify, options) => {
420
326
  };
421
327
 
422
328
  const getTenantPermissionList = async ({ tenantId }) => {
423
- await fastify.account.services.tenant.getTenantInfo({ id: tenantId });
329
+ await services.tenant.getTenant({ id: tenantId });
424
330
 
425
- const applications = await fastify.account.models.tenantApplication.findAll({
331
+ const applications = await models.tenantApplication.findAll({
426
332
  where: { tenantId, status: 0 }
427
333
  });
428
334
 
429
- const permissions = await fastify.account.models.tenantPermission.findAll({
335
+ const permissions = await models.tenantPermission.findAll({
430
336
  where: { tenantId, status: 0 }
431
337
  });
432
338
 
@@ -434,25 +340,21 @@ module.exports = fp(async (fastify, options) => {
434
340
  };
435
341
 
436
342
  const getRolePermissionList = async ({ roleId }) => {
437
- const role = await fastify.account.models.tenantRole.findByPk(roleId);
343
+ const role = await models.tenantRole.findByPk(roleId);
438
344
  if (!role) {
439
345
  throw new Error('角色不存在');
440
346
  }
441
- const applications = await fastify.account.models.tenantRoleApplication.findAll({
347
+ const applications = await models.tenantRoleApplication.findAll({
442
348
  where: { roleId: role.id, tenantId: role.tenantId }
443
349
  });
444
- const permissions = await fastify.account.models.tenantRolePermission.findAll({
350
+ const permissions = await models.tenantRolePermission.findAll({
445
351
  where: { roleId: role.id, tenantId: role.tenantId }
446
352
  });
447
353
 
448
354
  return { applications, permissions };
449
355
  };
450
356
 
451
- fastify.account.services.permission = {
452
- addApplication,
453
- saveApplication,
454
- deleteApplication,
455
- getApplicationList,
357
+ services.permission = {
456
358
  addPermission,
457
359
  getPermissionList,
458
360
  deletePermission,
@@ -0,0 +1,62 @@
1
+ const fp = require('fastify-plugin');
2
+ module.exports = fp(async (fastify, options) => {
3
+ const { models, services } = fastify.account;
4
+ const getInviteList = async ({ tenantId, filter, currentPage, perPage }) => {
5
+ const queryFilter = {};
6
+ const { count, rows } = await models.tenantToken.findAndCountAll({
7
+ where: Object.assign({}, queryFilter, { tenantId, type: 10 }),
8
+ offset: currentPage * (currentPage - 1),
9
+ limit: perPage
10
+ });
11
+ return { pageData: rows, totalCount: count };
12
+ };
13
+
14
+ const generateTenantToken = async ({ type, tenantId, info, tenantUserId }) => {
15
+ await services.tenant.getTenant({ id: tenantId });
16
+ const token = fastify.jwt.sign({ tenantId });
17
+ return await models.tenantToken.create({
18
+ token,
19
+ tenantId,
20
+ info,
21
+ tenantUserId,
22
+ type
23
+ });
24
+ };
25
+
26
+ const decodeTenantToken = async ({ type, tenantId, token }) => {
27
+ if (
28
+ (await models.tenantToken.count({
29
+ where: {
30
+ type,
31
+ tenantId,
32
+ token
33
+ }
34
+ })) === 0
35
+ ) {
36
+ throw new Error('token已过期');
37
+ }
38
+
39
+ return fastify.jwt.decode(token);
40
+ };
41
+
42
+ const addInviteToken = async ({ info, tenantId, tenantUserId }) => {
43
+ return await generateTenantToken({ info, tenantId, tenantUserId, type: 10 });
44
+ };
45
+
46
+ const deleteInviteToken = async ({ id }) => {
47
+ const token = await models.tenantToken.findByPk(id);
48
+ if (!token) {
49
+ throw new Error('数据不存在');
50
+ }
51
+
52
+ await token.destroy();
53
+ };
54
+
55
+ services.tenantInvite = {
56
+ getInviteList,
57
+ generateTenantToken,
58
+ decodeTenantToken,
59
+ addInviteToken,
60
+ deleteInviteToken
61
+ };
62
+ });
@@ -0,0 +1,84 @@
1
+ const fp = require('fastify-plugin');
2
+ module.exports = fp(async (fastify, options) => {
3
+ const { models, services } = fastify.account;
4
+
5
+ const getTenantOrgInstance = async ({ id }) => {
6
+ const tenantOrg = await models.tenantOrg.findByPk(id, {
7
+ where: {
8
+ type: 0
9
+ }
10
+ });
11
+
12
+ if (!tenantOrg) {
13
+ throw new Error('该组织不存在');
14
+ }
15
+
16
+ return tenantOrg;
17
+ };
18
+
19
+ const addTenantOrg = async org => {
20
+ if (await models.tenantOrg.count({ where: { name: org.name } })) {
21
+ throw new Error('组织名称不能重复');
22
+ }
23
+
24
+ return await models.tenantOrg.create({
25
+ name: org.name,
26
+ enName: org.enName,
27
+ tenantId: org.tenantId,
28
+ pid: org.pid
29
+ });
30
+ };
31
+
32
+ const saveTenantOrg = async ({ id, ...otherInfo }) => {
33
+ const tenantOrg = await getTenantOrgInstance({ id });
34
+ if (
35
+ await models.tenantOrg.count({
36
+ where: {
37
+ name: otherInfo.name,
38
+ pid: otherInfo.pid,
39
+ tenantId: otherInfo.tenantId
40
+ }
41
+ })
42
+ ) {
43
+ throw new Error('组织名称在同一父组织下有重复');
44
+ }
45
+
46
+ ['name', 'enName', 'tenantId', 'pid'].forEach(name => {
47
+ if (otherInfo[name]) {
48
+ tenantOrg[name] = otherInfo[name];
49
+ }
50
+ });
51
+
52
+ await tenantOrg.save();
53
+ };
54
+
55
+ const deleteTenantOrg = async ({ id, tenantId }) => {
56
+ const tenantOrg = await getTenantOrgInstance({ id });
57
+
58
+ const { rows } = await models.tenantOrg.findAndCountAll({
59
+ where: { tenantId, pid: id }
60
+ });
61
+
62
+ if (rows?.length) {
63
+ throw new Error('组织下有用户或子组织无法删除');
64
+ }
65
+
66
+ await tenantOrg.destroy();
67
+ };
68
+
69
+ const getTenantOrgList = async ({ tenantId }) => {
70
+ const data = await models.tenantOrg.findAll({
71
+ where: { tenantId }
72
+ });
73
+
74
+ return data.map(item => item.get({ plain: true }));
75
+ };
76
+
77
+ services.tenantOrg = {
78
+ getTenantOrgInstance,
79
+ addTenantOrg,
80
+ saveTenantOrg,
81
+ deleteTenantOrg,
82
+ getTenantOrgList
83
+ };
84
+ });
@@ -0,0 +1,108 @@
1
+ const fp = require('fastify-plugin');
2
+ const isNil = require('lodash/isNil');
3
+ module.exports = fp(async (fastify, options) => {
4
+ const { models, services } = fastify.account;
5
+ const { Op } = fastify.sequelize.Sequelize;
6
+
7
+ const getTenantRoleList = async ({ tenantId, currentPage, perPage, filter }) => {
8
+ const queryFilter = {};
9
+ if (!isNil(filter?.type)) {
10
+ queryFilter.type = filter.type;
11
+ }
12
+ const { count, rows } = await models.tenantRole.findAndCountAll({
13
+ where: Object.assign({}, queryFilter, { tenantId }),
14
+ offset: currentPage * (currentPage - 1),
15
+ limit: perPage
16
+ });
17
+
18
+ return { pageData: rows, totalCount: count };
19
+ };
20
+
21
+ const getTenantRoleInstance = async ({ id }) => {
22
+ const tenantRole = await models.tenantRole.findByPk(id, {
23
+ where: {
24
+ type: 0
25
+ }
26
+ });
27
+
28
+ if (!tenantRole) {
29
+ throw new Error('角色不存在');
30
+ }
31
+
32
+ return tenantRole;
33
+ };
34
+
35
+ const addTenantRole = async ({ tenantId, name, description }) => {
36
+ await services.tenant.getTenant({ id: tenantId });
37
+
38
+ return await models.tenantRole.create({
39
+ tenantId,
40
+ name,
41
+ description
42
+ });
43
+ };
44
+
45
+ const saveTenantRole = async ({ id, ...otherInfo }) => {
46
+ const tenantRole = await getTenantRoleInstance({ id });
47
+
48
+ ['name', 'description'].forEach(name => {
49
+ if (otherInfo[name]) {
50
+ tenantRole[name] = otherInfo[name];
51
+ }
52
+ });
53
+
54
+ await tenantRole.save();
55
+ };
56
+
57
+ const removeTenantRole = async ({ id }) => {
58
+ const tenantRole = await getTenantRoleInstance({ id });
59
+
60
+ await services.tenantUser.checkTenantRoleUsed({ tenantRoleId: tenantRole.id });
61
+
62
+ if (tenantRole.type === 1) {
63
+ throw new Error('该角色为系统默认角色,不能删除');
64
+ }
65
+
66
+ await tenantRole.destroy();
67
+ };
68
+
69
+ const getPermissionByTenantRoles = async ({ tenantRoleIds }) => {
70
+ const tenantRolePermission = await models.tenantRolePermission.findAll({
71
+ attributes: ['permissionId'],
72
+ include: {
73
+ attributes: ['code', 'name', 'isModule', 'paths'],
74
+ model: models.permission
75
+ },
76
+ where: {
77
+ roleId: {
78
+ [Op.in]: tenantRoleIds
79
+ }
80
+ }
81
+ });
82
+
83
+ return await models.permission.findAll({
84
+ attributes: ['id', 'code', 'name', 'isModule', 'pid', 'applicationId', 'paths'],
85
+ where: {
86
+ [Op.or]: [
87
+ {
88
+ id: {
89
+ [Op.in]: tenantRolePermission.map(({ permissionId }) => permissionId)
90
+ }
91
+ },
92
+ {
93
+ isMust: true
94
+ }
95
+ ]
96
+ }
97
+ });
98
+ };
99
+
100
+ services.tenantRole = {
101
+ getPermissionByTenantRoles,
102
+ getTenantRoleList,
103
+ getTenantRoleInstance,
104
+ addTenantRole,
105
+ saveTenantRole,
106
+ removeTenantRole
107
+ };
108
+ });