@kne/fastify-account 1.0.0-alpha.9 → 2.0.0-alpha.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/README.md +6 -2046
- package/index.js +34 -21
- package/libs/controllers/account.js +246 -173
- package/libs/controllers/admin-user.js +110 -0
- package/libs/controllers/admin.js +55 -146
- package/libs/controllers/user.js +7 -24
- package/libs/models/example._js +13 -0
- package/libs/models/user-account.js +13 -32
- package/libs/models/user.js +37 -36
- package/libs/models/verification-code.js +16 -21
- package/libs/services/account.js +124 -149
- package/libs/services/admin.js +21 -73
- package/libs/services/user.js +52 -58
- package/package.json +24 -28
- package/libs/controllers/adminPermission.js +0 -237
- package/libs/controllers/adminRole.js +0 -146
- package/libs/controllers/adminTenant.js +0 -464
- package/libs/controllers/tenant.js +0 -34
- package/libs/models/admin-role.js +0 -15
- package/libs/models/application.js +0 -42
- package/libs/models/login-log.js +0 -11
- package/libs/models/permission.js +0 -51
- package/libs/models/tenant-application.js +0 -26
- package/libs/models/tenant-org.js +0 -26
- package/libs/models/tenant-permission.js +0 -26
- package/libs/models/tenant-role-application.js +0 -37
- package/libs/models/tenant-role-permission.js +0 -34
- package/libs/models/tenant-role.js +0 -23
- package/libs/models/tenant-share-group-permission.js +0 -18
- package/libs/models/tenant-share-group.js +0 -18
- package/libs/models/tenant-source-user-share-group.js +0 -18
- package/libs/models/tenant-token.js +0 -30
- package/libs/models/tenant-user-org.js +0 -23
- package/libs/models/tenant-user-role.js +0 -23
- package/libs/models/tenant-user-share-group.js +0 -18
- package/libs/models/tenant-user.js +0 -75
- package/libs/models/tenant.js +0 -46
- package/libs/services/application.js +0 -151
- package/libs/services/permission.js +0 -367
- package/libs/services/tenant-invite.js +0 -62
- package/libs/services/tenant-org.js +0 -97
- package/libs/services/tenant-role.js +0 -108
- package/libs/services/tenant-user.js +0 -555
- package/libs/services/tenant.js +0 -132
|
@@ -1,367 +0,0 @@
|
|
|
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
|
-
|
|
6
|
-
const addPermission = async ({ applicationId, pid, code, name, type, isModule, isMust, description }) => {
|
|
7
|
-
if (!(await services.application.getApplication({ id: applicationId }))) {
|
|
8
|
-
throw new Error('应用不存在');
|
|
9
|
-
}
|
|
10
|
-
const paths = [];
|
|
11
|
-
if (pid > 0) {
|
|
12
|
-
const parentNode = await models.permission.findByPk(pid);
|
|
13
|
-
if (!parentNode) {
|
|
14
|
-
throw new Error('未找到父级');
|
|
15
|
-
}
|
|
16
|
-
paths.push(...parentNode.paths, parentNode.id);
|
|
17
|
-
}
|
|
18
|
-
if (
|
|
19
|
-
(await models.permission.count({
|
|
20
|
-
where: {
|
|
21
|
-
pid,
|
|
22
|
-
code,
|
|
23
|
-
applicationId
|
|
24
|
-
}
|
|
25
|
-
})) > 0
|
|
26
|
-
) {
|
|
27
|
-
throw new Error('同一级权限code不能重复');
|
|
28
|
-
}
|
|
29
|
-
return await models.permission.create({
|
|
30
|
-
applicationId,
|
|
31
|
-
code,
|
|
32
|
-
description,
|
|
33
|
-
name,
|
|
34
|
-
type,
|
|
35
|
-
pid,
|
|
36
|
-
isModule,
|
|
37
|
-
isMust,
|
|
38
|
-
paths
|
|
39
|
-
});
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const getPermissionList = async ({ applicationId, tenantId }) => {
|
|
43
|
-
const query = {};
|
|
44
|
-
|
|
45
|
-
if (tenantId) {
|
|
46
|
-
await services.tenant.getTenant({ id: tenantId });
|
|
47
|
-
const tenantPermissions = await models.tenantPermission.findAll({
|
|
48
|
-
where: { tenantId }
|
|
49
|
-
});
|
|
50
|
-
query[fastify.sequelize.Sequelize.Op.or] = [
|
|
51
|
-
{
|
|
52
|
-
id: {
|
|
53
|
-
[fastify.sequelize.Sequelize.Op.in]: tenantPermissions.map(({ permissionId }) => permissionId)
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
{ isMust: 1 }
|
|
57
|
-
];
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
await services.application.getApplication({ id: applicationId });
|
|
61
|
-
|
|
62
|
-
return await models.permission.findAll({
|
|
63
|
-
where: Object.assign({}, { applicationId }, query)
|
|
64
|
-
});
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
const deletePermission = async ({ id }) => {
|
|
68
|
-
const currentPermission = await models.permission.findByPk(id);
|
|
69
|
-
|
|
70
|
-
if (!currentPermission) {
|
|
71
|
-
throw new Error('权限不存在');
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
await services.application.getApplication({ id: currentPermission.applicationId });
|
|
75
|
-
|
|
76
|
-
const permissionList = await models.permission.findAll({
|
|
77
|
-
where: {
|
|
78
|
-
applicationId: currentPermission.applicationId
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
const childrenNode = permissionList.filter(({ paths }) => {
|
|
83
|
-
return paths.indexOf(currentPermission.id) > -1;
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
const permissionIdList = [currentPermission.id, ...childrenNode.map(({ id }) => id)];
|
|
87
|
-
|
|
88
|
-
const t = await fastify.sequelize.instance.transaction();
|
|
89
|
-
try {
|
|
90
|
-
await models.tenantPermission.destroy({
|
|
91
|
-
where: {
|
|
92
|
-
permissionId: {
|
|
93
|
-
[fastify.sequelize.Sequelize.Op.in]: permissionIdList
|
|
94
|
-
}
|
|
95
|
-
},
|
|
96
|
-
transaction: t
|
|
97
|
-
});
|
|
98
|
-
await models.tenantRolePermission.destroy({
|
|
99
|
-
where: {
|
|
100
|
-
permissionId: {
|
|
101
|
-
[fastify.sequelize.Sequelize.Op.in]: permissionIdList
|
|
102
|
-
}
|
|
103
|
-
},
|
|
104
|
-
transaction: t
|
|
105
|
-
});
|
|
106
|
-
await models.permission.destroy({
|
|
107
|
-
where: {
|
|
108
|
-
id: {
|
|
109
|
-
[fastify.sequelize.Sequelize.Op.in]: permissionIdList
|
|
110
|
-
}
|
|
111
|
-
},
|
|
112
|
-
transaction: t
|
|
113
|
-
});
|
|
114
|
-
await t.commit();
|
|
115
|
-
} catch (e) {
|
|
116
|
-
await t.rollback();
|
|
117
|
-
throw e;
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
const savePermission = async permission => {
|
|
122
|
-
const currentPermission = await models.permission.findByPk(permission.id);
|
|
123
|
-
|
|
124
|
-
if (!permission) {
|
|
125
|
-
throw new Error('权限不存在');
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
['name', 'type', 'isMust', 'description'].forEach(name => {
|
|
129
|
-
if (!isNil(permission[name])) {
|
|
130
|
-
currentPermission[name] = permission[name];
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
await currentPermission.save();
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
const saveTenantPermissionList = async ({ tenantId, applications, permissions }) => {
|
|
138
|
-
await services.tenant.getTenant({ id: tenantId });
|
|
139
|
-
const currentApplications = await models.tenantApplication.findAll({
|
|
140
|
-
where: { tenantId }
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
const currentApplicationIds = currentApplications.map(({ applicationId }) => applicationId);
|
|
144
|
-
|
|
145
|
-
const currentPermissions = await models.tenantPermission.findAll({
|
|
146
|
-
where: { tenantId }
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
const currentPermissionIds = currentPermissions.map(({ permissionId }) => permissionId);
|
|
150
|
-
|
|
151
|
-
const t = await fastify.sequelize.instance.transaction();
|
|
152
|
-
try {
|
|
153
|
-
//先删除,后添加
|
|
154
|
-
const needDeleteApplications = currentApplications.filter(item => applications.indexOf(item.applicationId) === -1).map(({ applicationId }) => applicationId);
|
|
155
|
-
const needAddApplications = applications.filter(applicationId => currentApplicationIds.indexOf(applicationId) === -1);
|
|
156
|
-
const needDeletePermissions = currentPermissions.filter(item => permissions.indexOf(item.permissionId) === -1).map(({ permissionId }) => permissionId);
|
|
157
|
-
const needAddPermissions = permissions.filter(permissionId => currentPermissionIds.indexOf(permissionId) === -1);
|
|
158
|
-
|
|
159
|
-
await models.tenantRoleApplication.destroy({
|
|
160
|
-
where: {
|
|
161
|
-
applicationId: {
|
|
162
|
-
[fastify.sequelize.Sequelize.Op.in]: needDeleteApplications
|
|
163
|
-
},
|
|
164
|
-
tenantId
|
|
165
|
-
},
|
|
166
|
-
transaction: t
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
await models.tenantRolePermission.destroy({
|
|
170
|
-
where: {
|
|
171
|
-
permissionId: {
|
|
172
|
-
[fastify.sequelize.Sequelize.Op.in]: needDeletePermissions
|
|
173
|
-
},
|
|
174
|
-
tenantId
|
|
175
|
-
},
|
|
176
|
-
transaction: t
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
await models.tenantApplication.destroy({
|
|
180
|
-
where: {
|
|
181
|
-
applicationId: {
|
|
182
|
-
[fastify.sequelize.Sequelize.Op.in]: needDeleteApplications
|
|
183
|
-
},
|
|
184
|
-
tenantId
|
|
185
|
-
},
|
|
186
|
-
transaction: t
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
await models.tenantPermission.destroy({
|
|
190
|
-
where: {
|
|
191
|
-
permissionId: {
|
|
192
|
-
[fastify.sequelize.Sequelize.Op.in]: needDeletePermissions
|
|
193
|
-
},
|
|
194
|
-
tenantId
|
|
195
|
-
},
|
|
196
|
-
transaction: t
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
needAddApplications.length > 0 &&
|
|
200
|
-
(await models.tenantApplication.bulkCreate(
|
|
201
|
-
needAddApplications.map(applicationId => {
|
|
202
|
-
return { tenantId, applicationId };
|
|
203
|
-
}),
|
|
204
|
-
{ transaction: t }
|
|
205
|
-
));
|
|
206
|
-
|
|
207
|
-
needAddPermissions.length > 0 &&
|
|
208
|
-
(await models.tenantPermission.bulkCreate(
|
|
209
|
-
needAddPermissions.map(permissionId => {
|
|
210
|
-
return { tenantId, permissionId };
|
|
211
|
-
}),
|
|
212
|
-
{ transaction: t }
|
|
213
|
-
));
|
|
214
|
-
|
|
215
|
-
await t.commit();
|
|
216
|
-
} catch (e) {
|
|
217
|
-
await t.rollback();
|
|
218
|
-
throw e;
|
|
219
|
-
}
|
|
220
|
-
};
|
|
221
|
-
|
|
222
|
-
const saveRolePermissionList = async ({ roleId, applications, permissions }) => {
|
|
223
|
-
const role = await models.tenantRole.findByPk(roleId);
|
|
224
|
-
if (!role) {
|
|
225
|
-
throw new Error('角色不存在');
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
const tenantId = role.tenantId;
|
|
229
|
-
|
|
230
|
-
await services.tenant.getTenant({ id: tenantId });
|
|
231
|
-
|
|
232
|
-
const tenantApplications = await models.tenantApplication.findAll({
|
|
233
|
-
attributes: ['applicationId'],
|
|
234
|
-
where: { tenantId }
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
const tenantPermissions = await models.tenantPermission.findAll({
|
|
238
|
-
attributes: ['permissionId'],
|
|
239
|
-
where: { tenantId }
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
const tenantApplicationIds = tenantApplications.map(({ applicationId }) => applicationId);
|
|
243
|
-
const tenantPermissionIds = tenantPermissions.map(({ permissionId }) => permissionId);
|
|
244
|
-
|
|
245
|
-
const currentApplications = await models.tenantRoleApplication.findAll({
|
|
246
|
-
where: {
|
|
247
|
-
roleId: role.id,
|
|
248
|
-
tenantId,
|
|
249
|
-
applicationId: {
|
|
250
|
-
[fastify.sequelize.Sequelize.Op.in]: tenantApplicationIds
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
const currentPermissions = await models.tenantRolePermission.findAll({
|
|
256
|
-
where: {
|
|
257
|
-
roleId: role.id,
|
|
258
|
-
tenantId,
|
|
259
|
-
permissionId: { [fastify.sequelize.Sequelize.Op.in]: tenantPermissionIds }
|
|
260
|
-
}
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
const currentApplicationIds = currentApplications.map(({ applicationId }) => applicationId);
|
|
264
|
-
const currentPermissionIds = currentPermissions.map(({ permissionId }) => permissionId);
|
|
265
|
-
|
|
266
|
-
const t = await fastify.sequelize.instance.transaction();
|
|
267
|
-
|
|
268
|
-
try {
|
|
269
|
-
//先删除,后添加
|
|
270
|
-
const needDeleteApplications = currentApplicationIds.filter(applicationId => applications.indexOf(applicationId) === -1);
|
|
271
|
-
const needAddApplications = applications.filter(applicationId => currentApplicationIds.indexOf(applicationId) === -1 && tenantApplicationIds.indexOf(applicationId) > -1);
|
|
272
|
-
const needDeletePermissions = currentPermissionIds.filter(permissionId => permissions.indexOf(permissionId) === -1);
|
|
273
|
-
const needAddPermissions = permissions.filter(permissionId => currentPermissionIds.indexOf(permissionId) === -1 && tenantPermissionIds.indexOf(permissionId) > -1);
|
|
274
|
-
|
|
275
|
-
needDeleteApplications.length > 0 &&
|
|
276
|
-
(await models.tenantRoleApplication.destroy({
|
|
277
|
-
where: {
|
|
278
|
-
applicationId: {
|
|
279
|
-
[fastify.sequelize.Sequelize.Op.in]: needDeleteApplications
|
|
280
|
-
},
|
|
281
|
-
tenantId
|
|
282
|
-
},
|
|
283
|
-
transaction: t
|
|
284
|
-
}));
|
|
285
|
-
|
|
286
|
-
needDeletePermissions.length > 0 &&
|
|
287
|
-
(await models.tenantRolePermission.destroy({
|
|
288
|
-
where: {
|
|
289
|
-
permissionId: {
|
|
290
|
-
[fastify.sequelize.Sequelize.Op.in]: needDeletePermissions
|
|
291
|
-
},
|
|
292
|
-
tenantId
|
|
293
|
-
},
|
|
294
|
-
transaction: t
|
|
295
|
-
}));
|
|
296
|
-
|
|
297
|
-
needAddApplications.length > 0 &&
|
|
298
|
-
(await models.tenantRoleApplication.bulkCreate(
|
|
299
|
-
needAddApplications.map(applicationId => {
|
|
300
|
-
return {
|
|
301
|
-
tenantId,
|
|
302
|
-
roleId,
|
|
303
|
-
applicationId
|
|
304
|
-
};
|
|
305
|
-
}),
|
|
306
|
-
{ transaction: t }
|
|
307
|
-
));
|
|
308
|
-
|
|
309
|
-
needAddPermissions.length > 0 &&
|
|
310
|
-
(await models.tenantRolePermission.bulkCreate(
|
|
311
|
-
needAddPermissions.map(permissionId => {
|
|
312
|
-
return {
|
|
313
|
-
tenantId,
|
|
314
|
-
roleId,
|
|
315
|
-
permissionId
|
|
316
|
-
};
|
|
317
|
-
}),
|
|
318
|
-
{ transaction: t }
|
|
319
|
-
));
|
|
320
|
-
|
|
321
|
-
await t.commit();
|
|
322
|
-
} catch (e) {
|
|
323
|
-
await t.rollback();
|
|
324
|
-
throw e;
|
|
325
|
-
}
|
|
326
|
-
};
|
|
327
|
-
|
|
328
|
-
const getTenantPermissionList = async ({ tenantId }) => {
|
|
329
|
-
await services.tenant.getTenant({ id: tenantId });
|
|
330
|
-
|
|
331
|
-
const applications = await models.tenantApplication.findAll({
|
|
332
|
-
where: { tenantId, status: 0 }
|
|
333
|
-
});
|
|
334
|
-
|
|
335
|
-
const permissions = await models.tenantPermission.findAll({
|
|
336
|
-
where: { tenantId, status: 0 }
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
return { applications, permissions };
|
|
340
|
-
};
|
|
341
|
-
|
|
342
|
-
const getRolePermissionList = async ({ roleId }) => {
|
|
343
|
-
const role = await models.tenantRole.findByPk(roleId);
|
|
344
|
-
if (!role) {
|
|
345
|
-
throw new Error('角色不存在');
|
|
346
|
-
}
|
|
347
|
-
const applications = await models.tenantRoleApplication.findAll({
|
|
348
|
-
where: { roleId: role.id, tenantId: role.tenantId }
|
|
349
|
-
});
|
|
350
|
-
const permissions = await models.tenantRolePermission.findAll({
|
|
351
|
-
where: { roleId: role.id, tenantId: role.tenantId }
|
|
352
|
-
});
|
|
353
|
-
|
|
354
|
-
return { applications, permissions };
|
|
355
|
-
};
|
|
356
|
-
|
|
357
|
-
services.permission = {
|
|
358
|
-
addPermission,
|
|
359
|
-
getPermissionList,
|
|
360
|
-
deletePermission,
|
|
361
|
-
savePermission,
|
|
362
|
-
saveTenantPermissionList,
|
|
363
|
-
saveRolePermissionList,
|
|
364
|
-
getTenantPermissionList,
|
|
365
|
-
getRolePermissionList
|
|
366
|
-
};
|
|
367
|
-
});
|
|
@@ -1,62 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,97 +0,0 @@
|
|
|
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
|
-
const getTenantOrgRoot = async ({ tenantId }) => {
|
|
78
|
-
const data = await models.tenantOrg.findOne({
|
|
79
|
-
where: { tenantId, pid: 0 }
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
if (!data) {
|
|
83
|
-
throw new Error('该租户不存在根节点');
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return data.get({ plain: true });
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
services.tenantOrg = {
|
|
90
|
-
getTenantOrgInstance,
|
|
91
|
-
addTenantOrg,
|
|
92
|
-
saveTenantOrg,
|
|
93
|
-
deleteTenantOrg,
|
|
94
|
-
getTenantOrgList,
|
|
95
|
-
getTenantOrgRoot
|
|
96
|
-
};
|
|
97
|
-
});
|
|
@@ -1,108 +0,0 @@
|
|
|
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
|
-
});
|