@kne/fastify-account 1.0.0-alpha.2 → 1.0.0-alpha.21
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 +2506 -803
- package/index.js +61 -9
- package/libs/controllers/account.js +221 -37
- package/libs/controllers/admin.js +92 -17
- package/libs/controllers/adminPermission.js +188 -38
- package/libs/controllers/adminTenant.js +44 -257
- package/libs/controllers/adminTenantCompany.js +53 -0
- package/libs/controllers/adminTenantOrg.js +71 -0
- package/libs/controllers/{adminRole.js → adminTenantRole.js} +15 -12
- package/libs/controllers/adminTenantUser.js +169 -0
- package/libs/controllers/requestLog.js +77 -0
- package/libs/controllers/tenant.js +4 -14
- package/libs/controllers/tenantCompany.js +54 -0
- package/libs/controllers/tenantOrg.js +65 -0
- package/libs/controllers/tenantRole.js +219 -0
- package/libs/controllers/tenantUser.js +169 -0
- package/libs/controllers/user.js +4 -3
- package/libs/models/admin-role.js +4 -8
- package/libs/models/application.js +16 -10
- package/libs/models/company-info.js +25 -0
- package/libs/models/login-log.js +11 -9
- package/libs/models/permission.js +7 -9
- package/libs/models/request-log.js +43 -0
- package/libs/models/tenant-application.js +8 -10
- package/libs/models/tenant-org.js +5 -9
- package/libs/models/tenant-permission.js +7 -9
- package/libs/models/tenant-role-application.js +13 -13
- package/libs/models/tenant-role-permission.js +9 -14
- package/libs/models/tenant-role.js +5 -9
- package/libs/models/tenant-share-group-permission.js +5 -9
- package/libs/models/tenant-share-group.js +5 -9
- package/libs/models/tenant-source-user-share-group.js +5 -9
- package/libs/models/tenant-token.js +7 -9
- package/libs/models/tenant-user-org.js +11 -10
- package/libs/models/tenant-user-role.js +11 -10
- package/libs/models/tenant-user-share-group.js +6 -10
- package/libs/models/tenant-user.js +35 -16
- package/libs/models/tenant.js +17 -9
- package/libs/models/user-account.js +17 -9
- package/libs/models/user.js +27 -17
- package/libs/models/verification-code.js +8 -10
- package/libs/services/account.js +95 -71
- package/libs/services/admin.js +38 -122
- package/libs/services/application.js +170 -0
- package/libs/services/permission.js +161 -163
- package/libs/services/request-log.js +37 -0
- package/libs/services/tenant-company.js +54 -0
- package/libs/services/tenant-invite.js +62 -0
- package/libs/services/tenant-org.js +118 -0
- package/libs/services/tenant-role.js +109 -0
- package/libs/services/tenant-user.js +582 -0
- package/libs/services/tenant.js +69 -670
- package/libs/services/user.js +109 -33
- package/package.json +3 -3
|
@@ -1,116 +1,26 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
2
|
const isNil = require('lodash/isNil');
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
};
|
|
3
|
+
const get = require('lodash/get');
|
|
4
|
+
const groupBy = require('lodash/groupBy');
|
|
99
5
|
|
|
6
|
+
const uniq = require('lodash/uniq');
|
|
7
|
+
module.exports = fp(async (fastify, options) => {
|
|
8
|
+
const { models, services } = fastify.account;
|
|
9
|
+
const { Op } = fastify.sequelize.Sequelize;
|
|
100
10
|
const addPermission = async ({ applicationId, pid, code, name, type, isModule, isMust, description }) => {
|
|
101
|
-
if (!(await
|
|
11
|
+
if (!(await services.application.getApplication({ id: applicationId }))) {
|
|
102
12
|
throw new Error('应用不存在');
|
|
103
13
|
}
|
|
104
14
|
const paths = [];
|
|
105
15
|
if (pid > 0) {
|
|
106
|
-
const parentNode = await
|
|
16
|
+
const parentNode = await models.permission.findByPk(pid);
|
|
107
17
|
if (!parentNode) {
|
|
108
18
|
throw new Error('未找到父级');
|
|
109
19
|
}
|
|
110
20
|
paths.push(...parentNode.paths, parentNode.id);
|
|
111
21
|
}
|
|
112
22
|
if (
|
|
113
|
-
(await
|
|
23
|
+
(await models.permission.count({
|
|
114
24
|
where: {
|
|
115
25
|
pid,
|
|
116
26
|
code,
|
|
@@ -120,7 +30,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
120
30
|
) {
|
|
121
31
|
throw new Error('同一级权限code不能重复');
|
|
122
32
|
}
|
|
123
|
-
return await
|
|
33
|
+
return await models.permission.create({
|
|
124
34
|
applicationId,
|
|
125
35
|
code,
|
|
126
36
|
description,
|
|
@@ -135,36 +45,113 @@ module.exports = fp(async (fastify, options) => {
|
|
|
135
45
|
|
|
136
46
|
const getPermissionList = async ({ applicationId, tenantId }) => {
|
|
137
47
|
const query = {};
|
|
48
|
+
|
|
138
49
|
if (tenantId) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
throw new Error('租户不存在');
|
|
142
|
-
}
|
|
143
|
-
const tenantPermissions = await fastify.account.models.tenantPermission.findAll({
|
|
50
|
+
await services.tenant.getTenant({ id: tenantId });
|
|
51
|
+
const tenantPermissions = await models.tenantPermission.findAll({
|
|
144
52
|
where: { tenantId }
|
|
145
53
|
});
|
|
146
|
-
query[
|
|
54
|
+
query[Op.or] = [
|
|
147
55
|
{
|
|
148
56
|
id: {
|
|
149
|
-
[
|
|
57
|
+
[Op.in]: tenantPermissions.map(({ permissionId }) => permissionId)
|
|
150
58
|
}
|
|
151
59
|
},
|
|
152
60
|
{ isMust: 1 }
|
|
153
61
|
];
|
|
154
62
|
}
|
|
155
|
-
|
|
63
|
+
|
|
64
|
+
await services.application.getApplication({ id: applicationId });
|
|
65
|
+
|
|
66
|
+
return await models.permission.findAll({
|
|
156
67
|
where: Object.assign({}, { applicationId }, query)
|
|
157
68
|
});
|
|
158
69
|
};
|
|
159
70
|
|
|
71
|
+
const importPermissionsToApplication = async ({ applicationId, permissions }) => {
|
|
72
|
+
const permissionsPidMapping = groupBy(permissions, 'pid');
|
|
73
|
+
const pidMapping = {};
|
|
74
|
+
for (let pid of await Promise.all(Object.keys(permissionsPidMapping).sort())) {
|
|
75
|
+
const targetPid = pid === '0' ? 0 : pidMapping[pid];
|
|
76
|
+
await Promise.all(
|
|
77
|
+
permissionsPidMapping[pid].map(async item => {
|
|
78
|
+
const originPermission = await models.permission.findOne({
|
|
79
|
+
where: {
|
|
80
|
+
pid: targetPid,
|
|
81
|
+
code: item.code,
|
|
82
|
+
applicationId
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
if (originPermission) {
|
|
86
|
+
pidMapping[item.id] = originPermission.id;
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const newPermission = await services.permission.addPermission({
|
|
90
|
+
applicationId,
|
|
91
|
+
pid: targetPid,
|
|
92
|
+
code: item.code,
|
|
93
|
+
name: item.name,
|
|
94
|
+
type: item.type,
|
|
95
|
+
isModule: item.isModule,
|
|
96
|
+
isMust: item.isMust,
|
|
97
|
+
description: item.description
|
|
98
|
+
});
|
|
99
|
+
pidMapping[item.id] = newPermission.id;
|
|
100
|
+
})
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const parsePermissionListJSON = async ({ file }) => {
|
|
106
|
+
const data = JSON.parse(await file.toBuffer());
|
|
107
|
+
await Promise.all(
|
|
108
|
+
data.map(async application => {
|
|
109
|
+
const { permissions, ...other } = application;
|
|
110
|
+
let app = await services.application.getApplicationByCode({ code: application.code });
|
|
111
|
+
if (!app) {
|
|
112
|
+
app = await services.application.addApplication(other);
|
|
113
|
+
}
|
|
114
|
+
await services.permission.importPermissionsToApplication({ applicationId: app.uuid, permissions });
|
|
115
|
+
return app;
|
|
116
|
+
})
|
|
117
|
+
);
|
|
118
|
+
return data;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const copyPermissions = async ({ applicationId, originApplicationId }) => {
|
|
122
|
+
if (applicationId === originApplicationId) {
|
|
123
|
+
throw new Error('复制对象不能和自己相同');
|
|
124
|
+
}
|
|
125
|
+
await services.application.getApplication({ id: originApplicationId });
|
|
126
|
+
await services.application.getApplication({ id: applicationId });
|
|
127
|
+
const permissions = await models.permission.findAll({
|
|
128
|
+
where: { applicationId: originApplicationId }
|
|
129
|
+
});
|
|
130
|
+
await services.permission.importPermissionsToApplication({ applicationId, permissions });
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const exportPermissionList = async ({ applicationIds }) => {
|
|
134
|
+
return await Promise.all(
|
|
135
|
+
(applicationIds || []).map(async applicationId => {
|
|
136
|
+
let application = await services.application.getApplication({ id: applicationId });
|
|
137
|
+
application.permissions = await models.permission.findAll({
|
|
138
|
+
where: { applicationId }
|
|
139
|
+
});
|
|
140
|
+
return application;
|
|
141
|
+
})
|
|
142
|
+
);
|
|
143
|
+
};
|
|
144
|
+
|
|
160
145
|
const deletePermission = async ({ id }) => {
|
|
161
|
-
const currentPermission = await
|
|
146
|
+
const currentPermission = await models.permission.findByPk(id);
|
|
162
147
|
|
|
163
148
|
if (!currentPermission) {
|
|
164
149
|
throw new Error('权限不存在');
|
|
165
150
|
}
|
|
166
151
|
|
|
167
|
-
|
|
152
|
+
await services.application.getApplication({ id: currentPermission.applicationId });
|
|
153
|
+
|
|
154
|
+
const permissionList = await models.permission.findAll({
|
|
168
155
|
where: {
|
|
169
156
|
applicationId: currentPermission.applicationId
|
|
170
157
|
}
|
|
@@ -178,26 +165,26 @@ module.exports = fp(async (fastify, options) => {
|
|
|
178
165
|
|
|
179
166
|
const t = await fastify.sequelize.instance.transaction();
|
|
180
167
|
try {
|
|
181
|
-
await
|
|
168
|
+
await models.tenantPermission.destroy({
|
|
182
169
|
where: {
|
|
183
170
|
permissionId: {
|
|
184
|
-
[
|
|
171
|
+
[Op.in]: permissionIdList
|
|
185
172
|
}
|
|
186
173
|
},
|
|
187
174
|
transaction: t
|
|
188
175
|
});
|
|
189
|
-
await
|
|
176
|
+
await models.tenantRolePermission.destroy({
|
|
190
177
|
where: {
|
|
191
178
|
permissionId: {
|
|
192
|
-
[
|
|
179
|
+
[Op.in]: permissionIdList
|
|
193
180
|
}
|
|
194
181
|
},
|
|
195
182
|
transaction: t
|
|
196
183
|
});
|
|
197
|
-
await
|
|
184
|
+
await models.permission.destroy({
|
|
198
185
|
where: {
|
|
199
186
|
id: {
|
|
200
|
-
[
|
|
187
|
+
[Op.in]: permissionIdList
|
|
201
188
|
}
|
|
202
189
|
},
|
|
203
190
|
transaction: t
|
|
@@ -210,7 +197,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
210
197
|
};
|
|
211
198
|
|
|
212
199
|
const savePermission = async permission => {
|
|
213
|
-
const currentPermission = await
|
|
200
|
+
const currentPermission = await models.permission.findByPk(permission.id);
|
|
214
201
|
|
|
215
202
|
if (!permission) {
|
|
216
203
|
throw new Error('权限不存在');
|
|
@@ -226,16 +213,14 @@ module.exports = fp(async (fastify, options) => {
|
|
|
226
213
|
};
|
|
227
214
|
|
|
228
215
|
const saveTenantPermissionList = async ({ tenantId, applications, permissions }) => {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
}
|
|
232
|
-
const currentApplications = await fastify.account.models.tenantApplication.findAll({
|
|
216
|
+
await services.tenant.getTenant({ id: tenantId });
|
|
217
|
+
const currentApplications = await models.tenantApplication.findAll({
|
|
233
218
|
where: { tenantId }
|
|
234
219
|
});
|
|
235
220
|
|
|
236
221
|
const currentApplicationIds = currentApplications.map(({ applicationId }) => applicationId);
|
|
237
222
|
|
|
238
|
-
const currentPermissions = await
|
|
223
|
+
const currentPermissions = await models.tenantPermission.findAll({
|
|
239
224
|
where: { tenantId }
|
|
240
225
|
});
|
|
241
226
|
|
|
@@ -249,40 +234,40 @@ module.exports = fp(async (fastify, options) => {
|
|
|
249
234
|
const needDeletePermissions = currentPermissions.filter(item => permissions.indexOf(item.permissionId) === -1).map(({ permissionId }) => permissionId);
|
|
250
235
|
const needAddPermissions = permissions.filter(permissionId => currentPermissionIds.indexOf(permissionId) === -1);
|
|
251
236
|
|
|
252
|
-
await
|
|
237
|
+
await models.tenantRoleApplication.destroy({
|
|
253
238
|
where: {
|
|
254
239
|
applicationId: {
|
|
255
|
-
[
|
|
240
|
+
[Op.in]: needDeleteApplications
|
|
256
241
|
},
|
|
257
242
|
tenantId
|
|
258
243
|
},
|
|
259
244
|
transaction: t
|
|
260
245
|
});
|
|
261
246
|
|
|
262
|
-
await
|
|
247
|
+
await models.tenantRolePermission.destroy({
|
|
263
248
|
where: {
|
|
264
249
|
permissionId: {
|
|
265
|
-
[
|
|
250
|
+
[Op.in]: needDeletePermissions
|
|
266
251
|
},
|
|
267
252
|
tenantId
|
|
268
253
|
},
|
|
269
254
|
transaction: t
|
|
270
255
|
});
|
|
271
256
|
|
|
272
|
-
await
|
|
257
|
+
await models.tenantApplication.destroy({
|
|
273
258
|
where: {
|
|
274
259
|
applicationId: {
|
|
275
|
-
[
|
|
260
|
+
[Op.in]: needDeleteApplications
|
|
276
261
|
},
|
|
277
262
|
tenantId
|
|
278
263
|
},
|
|
279
264
|
transaction: t
|
|
280
265
|
});
|
|
281
266
|
|
|
282
|
-
await
|
|
267
|
+
await models.tenantPermission.destroy({
|
|
283
268
|
where: {
|
|
284
269
|
permissionId: {
|
|
285
|
-
[
|
|
270
|
+
[Op.in]: needDeletePermissions
|
|
286
271
|
},
|
|
287
272
|
tenantId
|
|
288
273
|
},
|
|
@@ -290,7 +275,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
290
275
|
});
|
|
291
276
|
|
|
292
277
|
needAddApplications.length > 0 &&
|
|
293
|
-
(await
|
|
278
|
+
(await models.tenantApplication.bulkCreate(
|
|
294
279
|
needAddApplications.map(applicationId => {
|
|
295
280
|
return { tenantId, applicationId };
|
|
296
281
|
}),
|
|
@@ -298,7 +283,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
298
283
|
));
|
|
299
284
|
|
|
300
285
|
needAddPermissions.length > 0 &&
|
|
301
|
-
(await
|
|
286
|
+
(await models.tenantPermission.bulkCreate(
|
|
302
287
|
needAddPermissions.map(permissionId => {
|
|
303
288
|
return { tenantId, permissionId };
|
|
304
289
|
}),
|
|
@@ -312,45 +297,53 @@ module.exports = fp(async (fastify, options) => {
|
|
|
312
297
|
}
|
|
313
298
|
};
|
|
314
299
|
|
|
315
|
-
const saveRolePermissionList = async ({ roleId, applications, permissions }) => {
|
|
316
|
-
const role = await
|
|
300
|
+
const saveRolePermissionList = async ({ roleId, tenantId, applications, permissions }) => {
|
|
301
|
+
const role = await models.tenantRole.findByPk(roleId);
|
|
317
302
|
if (!role) {
|
|
318
303
|
throw new Error('角色不存在');
|
|
319
304
|
}
|
|
320
|
-
|
|
321
|
-
|
|
305
|
+
|
|
306
|
+
if (tenantId && role.tenantId !== tenantId) {
|
|
307
|
+
throw new Error('数据已过期,请刷新页面后重试');
|
|
322
308
|
}
|
|
323
309
|
|
|
324
|
-
|
|
310
|
+
tenantId = role.tenantId;
|
|
311
|
+
|
|
312
|
+
await services.tenant.getTenant({ id: tenantId });
|
|
325
313
|
|
|
326
|
-
const tenantApplications = await
|
|
314
|
+
const tenantApplications = await models.tenantApplication.findAll({
|
|
327
315
|
attributes: ['applicationId'],
|
|
328
316
|
where: { tenantId }
|
|
329
317
|
});
|
|
330
318
|
|
|
331
|
-
const tenantPermissions = await
|
|
319
|
+
const tenantPermissions = await models.tenantPermission.findAll({
|
|
332
320
|
attributes: ['permissionId'],
|
|
333
321
|
where: { tenantId }
|
|
334
322
|
});
|
|
335
323
|
|
|
324
|
+
const mustPermissions = await models.permission.findAll({
|
|
325
|
+
attributes: ['id'],
|
|
326
|
+
where: { isMust: 1 }
|
|
327
|
+
});
|
|
328
|
+
|
|
336
329
|
const tenantApplicationIds = tenantApplications.map(({ applicationId }) => applicationId);
|
|
337
|
-
const tenantPermissionIds = tenantPermissions.map(({ permissionId }) => permissionId);
|
|
330
|
+
const tenantPermissionIds = uniq([...mustPermissions.map(({ id }) => id), ...tenantPermissions.map(({ permissionId }) => permissionId)]);
|
|
338
331
|
|
|
339
|
-
const currentApplications = await
|
|
332
|
+
const currentApplications = await models.tenantRoleApplication.findAll({
|
|
340
333
|
where: {
|
|
341
334
|
roleId: role.id,
|
|
342
335
|
tenantId,
|
|
343
336
|
applicationId: {
|
|
344
|
-
[
|
|
337
|
+
[Op.in]: tenantApplicationIds
|
|
345
338
|
}
|
|
346
339
|
}
|
|
347
340
|
});
|
|
348
341
|
|
|
349
|
-
const currentPermissions = await
|
|
342
|
+
const currentPermissions = await models.tenantRolePermission.findAll({
|
|
350
343
|
where: {
|
|
351
344
|
roleId: role.id,
|
|
352
345
|
tenantId,
|
|
353
|
-
permissionId: { [
|
|
346
|
+
permissionId: { [Op.in]: tenantPermissionIds }
|
|
354
347
|
}
|
|
355
348
|
});
|
|
356
349
|
|
|
@@ -367,10 +360,10 @@ module.exports = fp(async (fastify, options) => {
|
|
|
367
360
|
const needAddPermissions = permissions.filter(permissionId => currentPermissionIds.indexOf(permissionId) === -1 && tenantPermissionIds.indexOf(permissionId) > -1);
|
|
368
361
|
|
|
369
362
|
needDeleteApplications.length > 0 &&
|
|
370
|
-
(await
|
|
363
|
+
(await models.tenantRoleApplication.destroy({
|
|
371
364
|
where: {
|
|
372
365
|
applicationId: {
|
|
373
|
-
[
|
|
366
|
+
[Op.in]: needDeleteApplications
|
|
374
367
|
},
|
|
375
368
|
tenantId
|
|
376
369
|
},
|
|
@@ -378,10 +371,10 @@ module.exports = fp(async (fastify, options) => {
|
|
|
378
371
|
}));
|
|
379
372
|
|
|
380
373
|
needDeletePermissions.length > 0 &&
|
|
381
|
-
(await
|
|
374
|
+
(await models.tenantRolePermission.destroy({
|
|
382
375
|
where: {
|
|
383
376
|
permissionId: {
|
|
384
|
-
[
|
|
377
|
+
[Op.in]: needDeletePermissions
|
|
385
378
|
},
|
|
386
379
|
tenantId
|
|
387
380
|
},
|
|
@@ -389,7 +382,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
389
382
|
}));
|
|
390
383
|
|
|
391
384
|
needAddApplications.length > 0 &&
|
|
392
|
-
(await
|
|
385
|
+
(await models.tenantRoleApplication.bulkCreate(
|
|
393
386
|
needAddApplications.map(applicationId => {
|
|
394
387
|
return {
|
|
395
388
|
tenantId,
|
|
@@ -401,7 +394,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
401
394
|
));
|
|
402
395
|
|
|
403
396
|
needAddPermissions.length > 0 &&
|
|
404
|
-
(await
|
|
397
|
+
(await models.tenantRolePermission.bulkCreate(
|
|
405
398
|
needAddPermissions.map(permissionId => {
|
|
406
399
|
return {
|
|
407
400
|
tenantId,
|
|
@@ -420,46 +413,51 @@ module.exports = fp(async (fastify, options) => {
|
|
|
420
413
|
};
|
|
421
414
|
|
|
422
415
|
const getTenantPermissionList = async ({ tenantId }) => {
|
|
423
|
-
await
|
|
416
|
+
await services.tenant.getTenant({ id: tenantId });
|
|
424
417
|
|
|
425
|
-
const applications = await
|
|
418
|
+
const applications = await models.tenantApplication.findAll({
|
|
426
419
|
where: { tenantId, status: 0 }
|
|
427
420
|
});
|
|
428
421
|
|
|
429
|
-
const permissions = await
|
|
422
|
+
const permissions = await models.tenantPermission.findAll({
|
|
430
423
|
where: { tenantId, status: 0 }
|
|
431
424
|
});
|
|
432
425
|
|
|
433
426
|
return { applications, permissions };
|
|
434
427
|
};
|
|
435
428
|
|
|
436
|
-
const getRolePermissionList = async ({ roleId }) => {
|
|
437
|
-
const role = await
|
|
429
|
+
const getRolePermissionList = async ({ roleId, tenantId }) => {
|
|
430
|
+
const role = await models.tenantRole.findByPk(roleId);
|
|
438
431
|
if (!role) {
|
|
439
432
|
throw new Error('角色不存在');
|
|
440
433
|
}
|
|
441
|
-
|
|
434
|
+
|
|
435
|
+
if (tenantId && role.tenantId !== tenantId) {
|
|
436
|
+
throw new Error('数据已过期,请刷新页面后重试');
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
const applications = await models.tenantRoleApplication.findAll({
|
|
442
440
|
where: { roleId: role.id, tenantId: role.tenantId }
|
|
443
441
|
});
|
|
444
|
-
const permissions = await
|
|
442
|
+
const permissions = await models.tenantRolePermission.findAll({
|
|
445
443
|
where: { roleId: role.id, tenantId: role.tenantId }
|
|
446
444
|
});
|
|
447
445
|
|
|
448
446
|
return { applications, permissions };
|
|
449
447
|
};
|
|
450
448
|
|
|
451
|
-
|
|
452
|
-
addApplication,
|
|
453
|
-
saveApplication,
|
|
454
|
-
deleteApplication,
|
|
455
|
-
getApplicationList,
|
|
449
|
+
services.permission = {
|
|
456
450
|
addPermission,
|
|
457
451
|
getPermissionList,
|
|
452
|
+
exportPermissionList,
|
|
458
453
|
deletePermission,
|
|
459
454
|
savePermission,
|
|
460
455
|
saveTenantPermissionList,
|
|
461
456
|
saveRolePermissionList,
|
|
462
457
|
getTenantPermissionList,
|
|
463
|
-
getRolePermissionList
|
|
458
|
+
getRolePermissionList,
|
|
459
|
+
parsePermissionListJSON,
|
|
460
|
+
copyPermissions,
|
|
461
|
+
importPermissionsToApplication
|
|
464
462
|
};
|
|
465
463
|
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const fp = require('fastify-plugin');
|
|
2
|
+
|
|
3
|
+
module.exports = fp(async (fastify, options) => {
|
|
4
|
+
const { models, services } = fastify.account;
|
|
5
|
+
|
|
6
|
+
const addRequestLog = async ({ userInfo, type, tenantId, appName, action, summary }) => {
|
|
7
|
+
const application = appName && (await services.application.getApplicationByCode({ code: appName }));
|
|
8
|
+
await models.requestLog.create({
|
|
9
|
+
userId: userInfo.id,
|
|
10
|
+
tenantId,
|
|
11
|
+
applicationId: application?.id,
|
|
12
|
+
type,
|
|
13
|
+
action,
|
|
14
|
+
summary
|
|
15
|
+
});
|
|
16
|
+
return {};
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const getRequestLogList = async ({ filter, type, perPage, currentPage }) => {
|
|
20
|
+
const { count, rows } = await models.requestLog.findAndCountAll({
|
|
21
|
+
include: [models.application, models.user],
|
|
22
|
+
order: [['createdAt', 'DESC']],
|
|
23
|
+
where: Object.assign({}, filter, { type }),
|
|
24
|
+
offset: perPage * (currentPage - 1),
|
|
25
|
+
limit: perPage
|
|
26
|
+
});
|
|
27
|
+
return {
|
|
28
|
+
pageData: rows,
|
|
29
|
+
totalCount: count
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
services.requestLog = {
|
|
34
|
+
addRequestLog,
|
|
35
|
+
getRequestLogList
|
|
36
|
+
};
|
|
37
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
const fp = require('fastify-plugin');
|
|
2
|
+
const isNil = require('lodash/isNil');
|
|
3
|
+
|
|
4
|
+
module.exports = fp(async (fastify, options) => {
|
|
5
|
+
const { models, services } = fastify.account;
|
|
6
|
+
|
|
7
|
+
const addCompanyInfo = async companyInfo => {
|
|
8
|
+
const currentCompanyInfo = await models.companyInfo.create(companyInfo);
|
|
9
|
+
return Object.assign({}, currentCompanyInfo.get({ plain: true }), {
|
|
10
|
+
id: currentCompanyInfo.uuid
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const getTenantCompanyInfoInstance = async ({ tenantId }) => {
|
|
15
|
+
const currentTenantCompanyInfo = await models.companyInfo.findOne({
|
|
16
|
+
where: {
|
|
17
|
+
tenantId
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
if (!currentTenantCompanyInfo) {
|
|
21
|
+
return await addCompanyInfo({ tenantId });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return currentTenantCompanyInfo;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const getTenantCompanyInfo = async ({ tenantId }) => {
|
|
28
|
+
return await getTenantCompanyInfoInstance({ tenantId });
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const saveTenantCompanyInfo = async ({ tenantId, id, ...others }) => {
|
|
32
|
+
const tenantCompanyInfo = await models.companyInfo.findOne({
|
|
33
|
+
where: {
|
|
34
|
+
id
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
if (tenantId !== tenantCompanyInfo.tenantId) {
|
|
38
|
+
throw new Error('租户Id和当前租户用户的租户Id不一致');
|
|
39
|
+
}
|
|
40
|
+
['name', 'shortName', 'themeColor', 'logo', 'description'].forEach(name => {
|
|
41
|
+
if (!isNil(others[name])) {
|
|
42
|
+
tenantCompanyInfo[name] = others[name];
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
await tenantCompanyInfo.save();
|
|
47
|
+
return tenantCompanyInfo;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
services.tenantCompany = {
|
|
51
|
+
getTenantCompanyInfo,
|
|
52
|
+
saveTenantCompanyInfo
|
|
53
|
+
};
|
|
54
|
+
});
|