@kne/fastify-account 1.0.0-alpha.7 → 1.0.0-alpha.9
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 +2 -2
- package/index.js +4 -1
- package/libs/services/admin.js +4 -6
- package/libs/services/tenant-org.js +14 -1
- package/libs/services/tenant-user.js +75 -66
- package/libs/services/user.js +13 -16
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -22,7 +22,7 @@ npm i --save @kne/fastify-account
|
|
|
22
22
|
### API
|
|
23
23
|
|
|
24
24
|
---
|
|
25
|
-
title: "@kne/fastify-account v1.0.0-alpha.
|
|
25
|
+
title: "@kne/fastify-account v1.0.0-alpha.8"
|
|
26
26
|
language_tabs:
|
|
27
27
|
- shell: Shell
|
|
28
28
|
- http: HTTP
|
|
@@ -42,7 +42,7 @@ headingLevel: 2
|
|
|
42
42
|
|
|
43
43
|
<!-- Generator: Widdershins v4.0.1 -->
|
|
44
44
|
|
|
45
|
-
<h1 id="-kne-fastify-account">@kne/fastify-account v1.0.0-alpha.
|
|
45
|
+
<h1 id="-kne-fastify-account">@kne/fastify-account v1.0.0-alpha.8</h1>
|
|
46
46
|
|
|
47
47
|
> Scroll down for code samples, example requests and responses. Select a language for code samples from the tabs above or the mobile navigation menu.
|
|
48
48
|
|
package/index.js
CHANGED
|
@@ -2,6 +2,7 @@ const fp = require('fastify-plugin');
|
|
|
2
2
|
const packageJson = require('./package.json');
|
|
3
3
|
const path = require('path');
|
|
4
4
|
const merge = require('lodash/merge');
|
|
5
|
+
const { Unauthorized } = require('http-errors');
|
|
5
6
|
|
|
6
7
|
module.exports = fp(
|
|
7
8
|
async function (fastify, options) {
|
|
@@ -46,7 +47,9 @@ module.exports = fp(
|
|
|
46
47
|
request.tenantInfo = await fastify.account.services.tenantUser.getTenantUserByUserId(request.userInfo);
|
|
47
48
|
},
|
|
48
49
|
admin: async request => {
|
|
49
|
-
|
|
50
|
+
if (!(await fastify.account.services.admin.checkIsSuperAdmin(request.userInfo))) {
|
|
51
|
+
throw Unauthorized('不能执行该操作,需要超级管理员权限');
|
|
52
|
+
}
|
|
50
53
|
}
|
|
51
54
|
}
|
|
52
55
|
]
|
package/libs/services/admin.js
CHANGED
|
@@ -17,17 +17,15 @@ module.exports = fp(async (fastify, options) => {
|
|
|
17
17
|
});
|
|
18
18
|
};
|
|
19
19
|
|
|
20
|
-
const
|
|
21
|
-
|
|
20
|
+
const checkIsSuperAdmin = async user => {
|
|
21
|
+
return (
|
|
22
22
|
(await models.adminRole.count({
|
|
23
23
|
where: {
|
|
24
24
|
userId: user.id,
|
|
25
25
|
role: ROLE['SuperAdmin']
|
|
26
26
|
}
|
|
27
27
|
})) === 0
|
|
28
|
-
)
|
|
29
|
-
throw new Error('不能执行该操作,需要超级管理员权限');
|
|
30
|
-
}
|
|
28
|
+
);
|
|
31
29
|
};
|
|
32
30
|
|
|
33
31
|
const addUser = async ({ avatar, nickname, phone, email, password, description }) => {
|
|
@@ -72,7 +70,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
72
70
|
services.admin = {
|
|
73
71
|
initSuperAdmin,
|
|
74
72
|
setSuperAdmin,
|
|
75
|
-
|
|
73
|
+
checkIsSuperAdmin,
|
|
76
74
|
generateTenantAdminVerifyCode,
|
|
77
75
|
verifyTenantAdmin,
|
|
78
76
|
addUser,
|
|
@@ -74,11 +74,24 @@ module.exports = fp(async (fastify, options) => {
|
|
|
74
74
|
return data.map(item => item.get({ plain: true }));
|
|
75
75
|
};
|
|
76
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
|
+
|
|
77
89
|
services.tenantOrg = {
|
|
78
90
|
getTenantOrgInstance,
|
|
79
91
|
addTenantOrg,
|
|
80
92
|
saveTenantOrg,
|
|
81
93
|
deleteTenantOrg,
|
|
82
|
-
getTenantOrgList
|
|
94
|
+
getTenantOrgList,
|
|
95
|
+
getTenantOrgRoot
|
|
83
96
|
};
|
|
84
97
|
});
|
|
@@ -213,6 +213,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
213
213
|
const checkTenantUserInfoValidate = async ({ tenantId, roleIds, orgIds, userId }) => {
|
|
214
214
|
await services.tenant.getTenant({ id: tenantId });
|
|
215
215
|
if (
|
|
216
|
+
roleIds &&
|
|
216
217
|
roleIds.length > 0 &&
|
|
217
218
|
(await models.tenantRole.count({
|
|
218
219
|
where: {
|
|
@@ -224,7 +225,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
224
225
|
) {
|
|
225
226
|
throw new Error('包含租户不存在的角色');
|
|
226
227
|
}
|
|
227
|
-
if (orgIds.length === 0) {
|
|
228
|
+
if (orgIds && orgIds.length === 0) {
|
|
228
229
|
const tenantOrg = await models.tenantOrg.findOne({
|
|
229
230
|
where: {
|
|
230
231
|
pid: 0,
|
|
@@ -234,10 +235,11 @@ module.exports = fp(async (fastify, options) => {
|
|
|
234
235
|
if (!tenantOrg) {
|
|
235
236
|
throw new Error('租户根节点不存在');
|
|
236
237
|
}
|
|
237
|
-
orgIds = [tenantOrg.id];
|
|
238
238
|
}
|
|
239
239
|
|
|
240
240
|
if (
|
|
241
|
+
orgIds &&
|
|
242
|
+
orgIds.length > 0 &&
|
|
241
243
|
(await models.tenantOrg.count({
|
|
242
244
|
where: {
|
|
243
245
|
tenantId,
|
|
@@ -257,7 +259,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
257
259
|
}
|
|
258
260
|
};
|
|
259
261
|
|
|
260
|
-
const addTenantUser = async ({ tenantId, roleIds, orgIds, userId, ...tenantUser }
|
|
262
|
+
const addTenantUser = async ({ tenantId, roleIds, orgIds, userId, ...tenantUser }) => {
|
|
261
263
|
const tenant = await services.tenant.getTenant({ id: tenantId });
|
|
262
264
|
|
|
263
265
|
const currentAccountNumber = await models.tenantUser.count({
|
|
@@ -270,7 +272,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
270
272
|
|
|
271
273
|
await checkTenantUserInfoValidate({ tenantId, roleIds, orgIds, userId });
|
|
272
274
|
|
|
273
|
-
const t =
|
|
275
|
+
const t = await fastify.sequelize.instance.transaction();
|
|
274
276
|
|
|
275
277
|
if (
|
|
276
278
|
(await models.tenantUser.count({
|
|
@@ -319,9 +321,9 @@ module.exports = fp(async (fastify, options) => {
|
|
|
319
321
|
{ transaction: t }
|
|
320
322
|
);
|
|
321
323
|
|
|
322
|
-
|
|
324
|
+
await t.commit();
|
|
323
325
|
} catch (e) {
|
|
324
|
-
|
|
326
|
+
await t.rollback();
|
|
325
327
|
throw e;
|
|
326
328
|
}
|
|
327
329
|
};
|
|
@@ -363,43 +365,47 @@ module.exports = fp(async (fastify, options) => {
|
|
|
363
365
|
});
|
|
364
366
|
await currentTenantUser.save({ transaction: t });
|
|
365
367
|
// 修改角色
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
368
|
+
if (roleIds) {
|
|
369
|
+
const needDeleteTenantRole = tenantRoleIds.filter(targetId => roleIds.indexOf(targetId) === -1);
|
|
370
|
+
const needAddTenantRole = roleIds.filter(targetId => tenantRoleIds.indexOf(targetId) === -1);
|
|
371
|
+
await models.tenantUserRole.destroy({
|
|
372
|
+
where: {
|
|
373
|
+
tenantId,
|
|
374
|
+
tenantUserId: currentTenantUser.uuid,
|
|
375
|
+
tenantRoleId: {
|
|
376
|
+
[Op.in]: needDeleteTenantRole
|
|
377
|
+
}
|
|
378
|
+
},
|
|
379
|
+
transaction: t
|
|
380
|
+
});
|
|
381
|
+
await models.tenantUserRole.bulkCreate(
|
|
382
|
+
needAddTenantRole.map(tenantRoleId => {
|
|
383
|
+
return { tenantId, tenantUserId: currentTenantUser.uuid, tenantRoleId };
|
|
384
|
+
}),
|
|
385
|
+
{ transaction: t }
|
|
386
|
+
);
|
|
387
|
+
}
|
|
384
388
|
//修改组织
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
389
|
+
if (orgIds) {
|
|
390
|
+
const needDeleteTenantOrg = tenantOrgIds.filter(targetId => orgIds.indexOf(targetId) === -1);
|
|
391
|
+
const needAddTenantOrg = orgIds.filter(targetId => tenantOrgIds.indexOf(targetId) === -1);
|
|
392
|
+
await models.tenantUserOrg.destroy({
|
|
393
|
+
where: {
|
|
394
|
+
tenantId,
|
|
395
|
+
tenantUserId: currentTenantUser.uuid,
|
|
396
|
+
tenantOrgId: {
|
|
397
|
+
[Op.in]: needDeleteTenantOrg
|
|
398
|
+
}
|
|
399
|
+
},
|
|
400
|
+
transaction: t
|
|
401
|
+
});
|
|
402
|
+
await models.tenantUserOrg.bulkCreate(
|
|
403
|
+
needAddTenantOrg.map(tenantOrgId => {
|
|
404
|
+
return { tenantId, tenantUserId: currentTenantUser.uuid, tenantOrgId };
|
|
405
|
+
}),
|
|
406
|
+
{ transaction: t }
|
|
407
|
+
);
|
|
408
|
+
}
|
|
403
409
|
await t.commit();
|
|
404
410
|
} catch (e) {
|
|
405
411
|
await t.rollback();
|
|
@@ -493,36 +499,39 @@ module.exports = fp(async (fastify, options) => {
|
|
|
493
499
|
errors.push({ item: current, msg: '租户用户已经存在,或手机邮箱和已有租户用户重复' });
|
|
494
500
|
continue;
|
|
495
501
|
}
|
|
496
|
-
const t = await fastify.sequelize.instance.transaction();
|
|
497
|
-
try {
|
|
498
|
-
if (await services.user.accountIsExists(current, {})) {
|
|
499
|
-
errors.push({ item: current, msg: '用户已经存在,已发送加入租户邀请等待对方同意' });
|
|
500
|
-
continue;
|
|
501
|
-
}
|
|
502
502
|
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
503
|
+
if (await services.user.accountIsExists(current, {})) {
|
|
504
|
+
errors.push({ item: current, msg: '用户已经存在,已发送加入租户邀请等待对方同意' });
|
|
505
|
+
continue;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
try {
|
|
509
|
+
const user = await services.user.addUser({
|
|
510
|
+
nickname: current.name,
|
|
511
|
+
phone: current.phone,
|
|
512
|
+
email: current.email,
|
|
513
|
+
password: services.account.md5(current.password || options.defaultPassword),
|
|
514
|
+
status: 1
|
|
515
|
+
});
|
|
516
|
+
const rootOrg = await services.tenantOrg.getTenantOrgRoot({ tenantId });
|
|
513
517
|
await services.tenantUser.addTenantUser(
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
518
|
+
Object.assign(
|
|
519
|
+
{},
|
|
520
|
+
{
|
|
521
|
+
orgIds: [rootOrg.id],
|
|
522
|
+
roleIds: []
|
|
523
|
+
},
|
|
524
|
+
{
|
|
525
|
+
tenantId,
|
|
526
|
+
userId: user.id,
|
|
527
|
+
...current
|
|
528
|
+
}
|
|
529
|
+
)
|
|
520
530
|
);
|
|
521
531
|
successes.push({ item: current });
|
|
522
|
-
await t.commit();
|
|
523
532
|
} catch (e) {
|
|
524
|
-
await t.rollback();
|
|
525
533
|
errors.push({ item: current, msg: e.message });
|
|
534
|
+
throw e;
|
|
526
535
|
}
|
|
527
536
|
}
|
|
528
537
|
|
package/libs/services/user.js
CHANGED
|
@@ -54,7 +54,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
54
54
|
);
|
|
55
55
|
};
|
|
56
56
|
|
|
57
|
-
const addUser = async ({ avatar, nickname, gender, birthday, description, phone, email, password, status }
|
|
57
|
+
const addUser = async ({ avatar, nickname, gender, birthday, description, phone, email, password, status }) => {
|
|
58
58
|
if ((await accountIsExists({ phone, email })) > 0) {
|
|
59
59
|
throw new Error('手机号或者邮箱都不能重复');
|
|
60
60
|
}
|
|
@@ -62,21 +62,18 @@ module.exports = fp(async (fastify, options) => {
|
|
|
62
62
|
throw new Error('密码不能为空');
|
|
63
63
|
}
|
|
64
64
|
const account = await models.userAccount.create(await services.account.passwordEncryption(password));
|
|
65
|
-
const user = await models.user.create(
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
{ transaction }
|
|
78
|
-
);
|
|
79
|
-
await account.update({ belongToUserId: user.uuid }, { transaction });
|
|
65
|
+
const user = await models.user.create({
|
|
66
|
+
avatar,
|
|
67
|
+
nickname,
|
|
68
|
+
gender,
|
|
69
|
+
birthday,
|
|
70
|
+
description,
|
|
71
|
+
phone,
|
|
72
|
+
email,
|
|
73
|
+
status,
|
|
74
|
+
userAccountId: account.uuid
|
|
75
|
+
});
|
|
76
|
+
await account.update({ belongToUserId: user.uuid });
|
|
80
77
|
|
|
81
78
|
return Object.assign({}, user.get({ pain: true }), { id: user.uuid });
|
|
82
79
|
};
|