@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 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.6"
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.6</h1>
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
- request.adminInfo = await fastify.account.services.admin.checkSuperAdmin(request.userInfo);
50
+ if (!(await fastify.account.services.admin.checkIsSuperAdmin(request.userInfo))) {
51
+ throw Unauthorized('不能执行该操作,需要超级管理员权限');
52
+ }
50
53
  }
51
54
  }
52
55
  ]
@@ -17,17 +17,15 @@ module.exports = fp(async (fastify, options) => {
17
17
  });
18
18
  };
19
19
 
20
- const checkSuperAdmin = async user => {
21
- if (
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
- checkSuperAdmin,
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 }, transaction) => {
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 = transaction || (await fastify.sequelize.instance.transaction());
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
- !transaction && (await t.commit());
324
+ await t.commit();
323
325
  } catch (e) {
324
- !transaction && (await t.rollback());
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
- const needDeleteTenantRole = tenantRoleIds.filter(targetId => roleIds.indexOf(targetId) === -1);
367
- const needAddTenantRole = roleIds.filter(targetId => tenantRoleIds.indexOf(targetId) === -1);
368
- await models.tenantUserRole.destroy({
369
- where: {
370
- tenantId,
371
- tenantUserId: currentTenantUser.uuid,
372
- tenantRoleId: {
373
- [Op.in]: needDeleteTenantRole
374
- }
375
- },
376
- transaction: t
377
- });
378
- await models.tenantUserRole.bulkCreate(
379
- needAddTenantRole.map(tenantRoleId => {
380
- return { tenantId, tenantUserId: currentTenantUser.uuid, tenantRoleId };
381
- }),
382
- { transaction: t }
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
- const needDeleteTenantOrg = tenantOrgIds.filter(targetId => orgIds.indexOf(targetId) === -1);
386
- const needAddTenantOrg = orgIds.filter(targetId => tenantOrgIds.indexOf(targetId) === -1);
387
- await models.tenantUserOrg.destroy({
388
- where: {
389
- tenantId,
390
- tenantUserId: currentTenantUser.uuid,
391
- tenantOrgId: {
392
- [Op.in]: needDeleteTenantOrg
393
- }
394
- },
395
- transaction: t
396
- });
397
- await models.tenantUserOrg.bulkCreate(
398
- needAddTenantOrg.map(tenantOrgId => {
399
- return { tenantId, tenantUserId: currentTenantUser.uuid, tenantOrgId };
400
- }),
401
- { transaction: t }
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
- const user = await services.user.addUser(
504
- {
505
- nickname: current.name,
506
- phone: current.phone,
507
- email: current.email,
508
- password: services.account.md5(current.password || options.defaultPassword),
509
- status: 1
510
- },
511
- { transaction: t }
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
- tenantId,
516
- userId: user.id,
517
- ...current
518
- },
519
- { transaction: t }
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
 
@@ -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 }, transaction) => {
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
- avatar,
68
- nickname,
69
- gender,
70
- birthday,
71
- description,
72
- phone,
73
- email,
74
- status,
75
- userAccountId: account.uuid
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
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-account",
3
- "version": "1.0.0-alpha.7",
3
+ "version": "1.0.0-alpha.9",
4
4
  "description": "fastify的用户管理账号等实现",
5
5
  "main": "index.js",
6
6
  "scripts": {