@kne/fastify-account 1.0.0-alpha.12 → 1.0.0-alpha.15
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 +1453 -961
- package/index.js +16 -3
- package/libs/controllers/account.js +95 -22
- package/libs/controllers/admin.js +58 -6
- package/libs/controllers/adminPermission.js +139 -20
- package/libs/controllers/adminRole.js +2 -0
- package/libs/services/application.js +16 -0
- package/libs/services/permission.js +76 -15
- package/libs/services/tenant-invite.js +1 -1
- package/libs/services/tenant-role.js +4 -11
- package/libs/services/tenant-user.js +1 -1
- package/libs/services/tenant.js +1 -1
- package/package.json +1 -1
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
2
|
const isNil = require('lodash/isNil');
|
|
3
|
+
const get = require('lodash/get');
|
|
4
|
+
const groupBy = require('lodash/groupBy');
|
|
5
|
+
|
|
6
|
+
const uniq = require('lodash/uniq');
|
|
3
7
|
module.exports = fp(async (fastify, options) => {
|
|
4
8
|
const { models, services } = fastify.account;
|
|
5
|
-
|
|
9
|
+
const { Op } = fastify.sequelize.Sequelize;
|
|
6
10
|
const addPermission = async ({ applicationId, pid, code, name, type, isModule, isMust, description }) => {
|
|
7
11
|
if (!(await services.application.getApplication({ id: applicationId }))) {
|
|
8
12
|
throw new Error('应用不存在');
|
|
@@ -47,10 +51,10 @@ module.exports = fp(async (fastify, options) => {
|
|
|
47
51
|
const tenantPermissions = await models.tenantPermission.findAll({
|
|
48
52
|
where: { tenantId }
|
|
49
53
|
});
|
|
50
|
-
query[
|
|
54
|
+
query[Op.or] = [
|
|
51
55
|
{
|
|
52
56
|
id: {
|
|
53
|
-
[
|
|
57
|
+
[Op.in]: tenantPermissions.map(({ permissionId }) => permissionId)
|
|
54
58
|
}
|
|
55
59
|
},
|
|
56
60
|
{ isMust: 1 }
|
|
@@ -64,6 +68,57 @@ module.exports = fp(async (fastify, options) => {
|
|
|
64
68
|
});
|
|
65
69
|
};
|
|
66
70
|
|
|
71
|
+
const parsePermissionListJSON = async ({ file }) => {
|
|
72
|
+
const data = JSON.parse(await file.toBuffer());
|
|
73
|
+
await Promise.all(
|
|
74
|
+
data.map(async application => {
|
|
75
|
+
const { permissions, ...other } = application;
|
|
76
|
+
const app = await services.application.getApplicationByCode({ code: application.code });
|
|
77
|
+
|
|
78
|
+
if (!app) {
|
|
79
|
+
const newApplication = await services.application.addApplication(other);
|
|
80
|
+
const permissionsPidMapping = groupBy(permissions, 'pid');
|
|
81
|
+
const addPermissions = async (pid, applicationId) =>
|
|
82
|
+
await Promise.all(
|
|
83
|
+
(get(permissionsPidMapping, pid) || []).map(async ({ id, ...permissionProps }) => {
|
|
84
|
+
const permission = await services.permission.addPermission(Object.assign({}, permissionProps, { applicationId, pid }));
|
|
85
|
+
await addPermissions(permission.id, applicationId);
|
|
86
|
+
})
|
|
87
|
+
);
|
|
88
|
+
await addPermissions(0, newApplication.uuid);
|
|
89
|
+
} else {
|
|
90
|
+
const permissionsPidMapping = groupBy(permissions, 'pid');
|
|
91
|
+
const addPermissions = async (pid, applicationId, importPid) => {
|
|
92
|
+
await Promise.all(
|
|
93
|
+
(get(permissionsPidMapping, importPid || pid) || []).map(async ({ id, ...permissionProps }) => {
|
|
94
|
+
const current = await models.permission.findOne({ where: { code: permissionProps.code, pid } });
|
|
95
|
+
if (current) {
|
|
96
|
+
await addPermissions(current.id, applicationId, id);
|
|
97
|
+
} else {
|
|
98
|
+
const permission = await services.permission.addPermission(Object.assign({}, permissionProps, { applicationId, pid }));
|
|
99
|
+
await addPermissions(permission.id, applicationId);
|
|
100
|
+
}
|
|
101
|
+
})
|
|
102
|
+
);
|
|
103
|
+
};
|
|
104
|
+
await addPermissions(0, app.uuid);
|
|
105
|
+
}
|
|
106
|
+
return app;
|
|
107
|
+
})
|
|
108
|
+
);
|
|
109
|
+
return data;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const exportPermissionList = async ({ applicationIds, tenantId }) => {
|
|
113
|
+
return await Promise.all(
|
|
114
|
+
(applicationIds || []).map(async applicationId => {
|
|
115
|
+
let application = await services.application.getApplication({ id: applicationId });
|
|
116
|
+
application.permissions = await services.permission.getPermissionList({ applicationId, tenantId });
|
|
117
|
+
return application;
|
|
118
|
+
})
|
|
119
|
+
);
|
|
120
|
+
};
|
|
121
|
+
|
|
67
122
|
const deletePermission = async ({ id }) => {
|
|
68
123
|
const currentPermission = await models.permission.findByPk(id);
|
|
69
124
|
|
|
@@ -90,7 +145,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
90
145
|
await models.tenantPermission.destroy({
|
|
91
146
|
where: {
|
|
92
147
|
permissionId: {
|
|
93
|
-
[
|
|
148
|
+
[Op.in]: permissionIdList
|
|
94
149
|
}
|
|
95
150
|
},
|
|
96
151
|
transaction: t
|
|
@@ -98,7 +153,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
98
153
|
await models.tenantRolePermission.destroy({
|
|
99
154
|
where: {
|
|
100
155
|
permissionId: {
|
|
101
|
-
[
|
|
156
|
+
[Op.in]: permissionIdList
|
|
102
157
|
}
|
|
103
158
|
},
|
|
104
159
|
transaction: t
|
|
@@ -106,7 +161,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
106
161
|
await models.permission.destroy({
|
|
107
162
|
where: {
|
|
108
163
|
id: {
|
|
109
|
-
[
|
|
164
|
+
[Op.in]: permissionIdList
|
|
110
165
|
}
|
|
111
166
|
},
|
|
112
167
|
transaction: t
|
|
@@ -159,7 +214,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
159
214
|
await models.tenantRoleApplication.destroy({
|
|
160
215
|
where: {
|
|
161
216
|
applicationId: {
|
|
162
|
-
[
|
|
217
|
+
[Op.in]: needDeleteApplications
|
|
163
218
|
},
|
|
164
219
|
tenantId
|
|
165
220
|
},
|
|
@@ -169,7 +224,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
169
224
|
await models.tenantRolePermission.destroy({
|
|
170
225
|
where: {
|
|
171
226
|
permissionId: {
|
|
172
|
-
[
|
|
227
|
+
[Op.in]: needDeletePermissions
|
|
173
228
|
},
|
|
174
229
|
tenantId
|
|
175
230
|
},
|
|
@@ -179,7 +234,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
179
234
|
await models.tenantApplication.destroy({
|
|
180
235
|
where: {
|
|
181
236
|
applicationId: {
|
|
182
|
-
[
|
|
237
|
+
[Op.in]: needDeleteApplications
|
|
183
238
|
},
|
|
184
239
|
tenantId
|
|
185
240
|
},
|
|
@@ -189,7 +244,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
189
244
|
await models.tenantPermission.destroy({
|
|
190
245
|
where: {
|
|
191
246
|
permissionId: {
|
|
192
|
-
[
|
|
247
|
+
[Op.in]: needDeletePermissions
|
|
193
248
|
},
|
|
194
249
|
tenantId
|
|
195
250
|
},
|
|
@@ -239,15 +294,20 @@ module.exports = fp(async (fastify, options) => {
|
|
|
239
294
|
where: { tenantId }
|
|
240
295
|
});
|
|
241
296
|
|
|
297
|
+
const mustPermissions = await models.permission.findAll({
|
|
298
|
+
attributes: ['id'],
|
|
299
|
+
where: { isMust: 1 }
|
|
300
|
+
});
|
|
301
|
+
|
|
242
302
|
const tenantApplicationIds = tenantApplications.map(({ applicationId }) => applicationId);
|
|
243
|
-
const tenantPermissionIds = tenantPermissions.map(({ permissionId }) => permissionId);
|
|
303
|
+
const tenantPermissionIds = uniq([...mustPermissions.map(({ id }) => id), ...tenantPermissions.map(({ permissionId }) => permissionId)]);
|
|
244
304
|
|
|
245
305
|
const currentApplications = await models.tenantRoleApplication.findAll({
|
|
246
306
|
where: {
|
|
247
307
|
roleId: role.id,
|
|
248
308
|
tenantId,
|
|
249
309
|
applicationId: {
|
|
250
|
-
[
|
|
310
|
+
[Op.in]: tenantApplicationIds
|
|
251
311
|
}
|
|
252
312
|
}
|
|
253
313
|
});
|
|
@@ -256,7 +316,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
256
316
|
where: {
|
|
257
317
|
roleId: role.id,
|
|
258
318
|
tenantId,
|
|
259
|
-
permissionId: { [
|
|
319
|
+
permissionId: { [Op.in]: tenantPermissionIds }
|
|
260
320
|
}
|
|
261
321
|
});
|
|
262
322
|
|
|
@@ -276,7 +336,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
276
336
|
(await models.tenantRoleApplication.destroy({
|
|
277
337
|
where: {
|
|
278
338
|
applicationId: {
|
|
279
|
-
[
|
|
339
|
+
[Op.in]: needDeleteApplications
|
|
280
340
|
},
|
|
281
341
|
tenantId
|
|
282
342
|
},
|
|
@@ -287,7 +347,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
287
347
|
(await models.tenantRolePermission.destroy({
|
|
288
348
|
where: {
|
|
289
349
|
permissionId: {
|
|
290
|
-
[
|
|
350
|
+
[Op.in]: needDeletePermissions
|
|
291
351
|
},
|
|
292
352
|
tenantId
|
|
293
353
|
},
|
|
@@ -357,6 +417,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
357
417
|
services.permission = {
|
|
358
418
|
addPermission,
|
|
359
419
|
getPermissionList,
|
|
420
|
+
exportPermissionList,
|
|
360
421
|
deletePermission,
|
|
361
422
|
savePermission,
|
|
362
423
|
saveTenantPermissionList,
|
|
@@ -5,7 +5,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
5
5
|
const queryFilter = {};
|
|
6
6
|
const { count, rows } = await models.tenantToken.findAndCountAll({
|
|
7
7
|
where: Object.assign({}, queryFilter, { tenantId, type: 10 }),
|
|
8
|
-
offset:
|
|
8
|
+
offset: perPage * (currentPage - 1),
|
|
9
9
|
limit: perPage
|
|
10
10
|
});
|
|
11
11
|
return { pageData: rows, totalCount: count };
|
|
@@ -11,7 +11,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
11
11
|
}
|
|
12
12
|
const { count, rows } = await models.tenantRole.findAndCountAll({
|
|
13
13
|
where: Object.assign({}, queryFilter, { tenantId }),
|
|
14
|
-
offset:
|
|
14
|
+
offset: perPage * (currentPage - 1),
|
|
15
15
|
limit: perPage
|
|
16
16
|
});
|
|
17
17
|
|
|
@@ -83,16 +83,9 @@ module.exports = fp(async (fastify, options) => {
|
|
|
83
83
|
return await models.permission.findAll({
|
|
84
84
|
attributes: ['id', 'code', 'name', 'isModule', 'pid', 'applicationId', 'paths'],
|
|
85
85
|
where: {
|
|
86
|
-
|
|
87
|
-
{
|
|
88
|
-
|
|
89
|
-
[Op.in]: tenantRolePermission.map(({ permissionId }) => permissionId)
|
|
90
|
-
}
|
|
91
|
-
},
|
|
92
|
-
{
|
|
93
|
-
isMust: true
|
|
94
|
-
}
|
|
95
|
-
]
|
|
86
|
+
id: {
|
|
87
|
+
[Op.in]: tenantRolePermission.map(({ permissionId }) => permissionId)
|
|
88
|
+
}
|
|
96
89
|
}
|
|
97
90
|
});
|
|
98
91
|
};
|
|
@@ -471,7 +471,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
471
471
|
const { count, rows } = await fastify.account.models.tenantUser.findAndCountAll({
|
|
472
472
|
include: [fastify.account.models.tenantRole, fastify.account.models.tenantOrg, fastify.account.models.user],
|
|
473
473
|
where: Object.assign({}, queryFilter, { tenantId }),
|
|
474
|
-
offset:
|
|
474
|
+
offset: perPage * (currentPage - 1),
|
|
475
475
|
limit: perPage
|
|
476
476
|
});
|
|
477
477
|
|
package/libs/services/tenant.js
CHANGED
|
@@ -98,7 +98,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
98
98
|
|
|
99
99
|
const { count, rows } = await models.tenant.findAndCountAll({
|
|
100
100
|
where: queryFilter,
|
|
101
|
-
offset:
|
|
101
|
+
offset: perPage * (currentPage - 1),
|
|
102
102
|
limit: perPage
|
|
103
103
|
});
|
|
104
104
|
|