@kne/fastify-account 1.0.0-alpha.8 → 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.7"
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.7</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,
@@ -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({
@@ -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();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-account",
3
- "version": "1.0.0-alpha.8",
3
+ "version": "1.0.0-alpha.9",
4
4
  "description": "fastify的用户管理账号等实现",
5
5
  "main": "index.js",
6
6
  "scripts": {