@kne/fastify-account 1.0.0-alpha.10 → 1.0.0-alpha.11

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.9"
25
+ title: "@kne/fastify-account v1.0.0-alpha.10"
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.9</h1>
45
+ <h1 id="-kne-fastify-account">@kne/fastify-account v1.0.0-alpha.10</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
 
@@ -19,14 +19,7 @@ module.exports = fp(async (fastify, options) => {
19
19
  schema: {
20
20
  type: 'object',
21
21
  properties: {
22
- code: { type: 'number' },
23
- data: {
24
- type: 'object',
25
- properties: {
26
- code: { type: 'string', description: '验证码' }
27
- }
28
- },
29
- msg: { type: 'string' }
22
+ code: { type: 'string', description: '验证码' }
30
23
  }
31
24
  }
32
25
  }
@@ -9,7 +9,7 @@ module.exports = fp(async (fastify, options) => {
9
9
  onRequest: [authenticate.user]
10
10
  },
11
11
  async request => {
12
- await services.admin.initSuperAdmin(await services.user.getUser(request.authenticatePayload));
12
+ await services.admin.initSuperAdmin(request.userInfo);
13
13
  return {};
14
14
  }
15
15
  );
@@ -24,6 +24,28 @@ module.exports = fp(async (fastify, options) => {
24
24
  }
25
25
  );
26
26
 
27
+ fastify.post(
28
+ `${options.prefix}/admin/setSuperAdmin`,
29
+ {
30
+ onRequest: [authenticate.user, authenticate.admin],
31
+ schema: {
32
+ body: {
33
+ type: 'object',
34
+ required: ['status', 'userId'],
35
+ properties: {
36
+ status: { type: 'boolean' },
37
+ userId: { type: 'string' }
38
+ }
39
+ }
40
+ }
41
+ },
42
+ async request => {
43
+ const { status, userId } = request.body;
44
+ await services.admin[status ? 'setSuperAdmin' : 'cancelSuperAdmin'](await services.user.getUser({ id: userId }));
45
+ return {};
46
+ }
47
+ );
48
+
27
49
  fastify.post(
28
50
  `${options.prefix}/admin/addUser`,
29
51
  {
@@ -21,6 +21,41 @@ module.exports = fp(async (fastify, options) => {
21
21
  }
22
22
  );
23
23
 
24
+ fastify.get(
25
+ `${options.prefix}/tenant/getTenantUserList`,
26
+ {
27
+ onRequest: [authenticate.user, authenticate.tenant],
28
+ schema: {
29
+ query: {
30
+ type: 'object',
31
+ properties: {
32
+ filter: {
33
+ type: 'object',
34
+ properties: {
35
+ name: { type: 'string' }
36
+ }
37
+ },
38
+ currentPage: { type: 'number' },
39
+ perPage: { type: 'number' }
40
+ }
41
+ }
42
+ }
43
+ },
44
+ async request => {
45
+ const { id: tenantId } = request.tenantInfo.tenant;
46
+ const { filter, currentPage, perPage } = Object.assign(
47
+ {},
48
+ {
49
+ filter: {},
50
+ currentPage: 1,
51
+ perPage: 20
52
+ },
53
+ request.query
54
+ );
55
+ return await services.tenantUser.getTenantUserList({ tenantId, currentPage, perPage, filter });
56
+ }
57
+ );
58
+
24
59
  fastify.get(
25
60
  `${options.prefix}/tenant/orgList`,
26
61
  {
@@ -2,7 +2,7 @@ const fp = require('fastify-plugin');
2
2
 
3
3
  const ROLE = {
4
4
  SuperAdmin: 'SuperAdmin',
5
- TenantAdmin: 'TenantAdmin'
5
+ Common: 'Common'
6
6
  };
7
7
 
8
8
  module.exports = fp(async (fastify, options) => {
@@ -41,7 +41,7 @@ module.exports = fp(async (fastify, options) => {
41
41
  };
42
42
 
43
43
  const setSuperAdmin = async targetUser => {
44
- const user = await services.user.getUserInfo(targetUser);
44
+ const user = await services.user.getUser(targetUser);
45
45
  if (
46
46
  (await models.adminRole.count({
47
47
  where: {
@@ -59,9 +59,30 @@ module.exports = fp(async (fastify, options) => {
59
59
  });
60
60
  };
61
61
 
62
- const generateTenantAdminVerifyCode = async () => {};
62
+ const cancelSuperAdmin = async targetUser => {
63
+ const user = await services.user.getUser(targetUser);
64
+ if (
65
+ !(await models.adminRole.count({
66
+ where: {
67
+ userId: user.id,
68
+ role: ROLE['SuperAdmin']
69
+ }
70
+ })) > 0
71
+ ) {
72
+ throw new Error('当前用户不是超级管理员');
73
+ }
63
74
 
64
- const verifyTenantAdmin = async () => {};
75
+ await models.adminRole.update(
76
+ {
77
+ role: ROLE['Common']
78
+ },
79
+ {
80
+ where: {
81
+ userId: user.id
82
+ }
83
+ }
84
+ );
85
+ };
65
86
 
66
87
  const resetUserPassword = async ({ userId, password }) => {
67
88
  await services.account.resetPassword({ userId, password });
@@ -70,9 +91,8 @@ module.exports = fp(async (fastify, options) => {
70
91
  services.admin = {
71
92
  initSuperAdmin,
72
93
  setSuperAdmin,
94
+ cancelSuperAdmin,
73
95
  checkIsSuperAdmin,
74
- generateTenantAdminVerifyCode,
75
- verifyTenantAdmin,
76
96
  addUser,
77
97
  resetUserPassword
78
98
  };
@@ -456,12 +456,23 @@ module.exports = fp(async (fastify, options) => {
456
456
  await tenantUser.save();
457
457
  };
458
458
 
459
- const getTenantUserList = async ({ tenantId }) => {
459
+ const getTenantUserList = async ({ tenantId, currentPage, perPage, filter }) => {
460
+ const queryFilter = {};
460
461
  await services.tenant.getTenant({ id: tenantId });
461
462
 
463
+ ['name', 'phone', 'email'].forEach(key => {
464
+ if (filter && filter[key]) {
465
+ queryFilter[key] = {
466
+ [Op.like]: `%${filter[key]}%`
467
+ };
468
+ }
469
+ });
470
+
462
471
  const { count, rows } = await fastify.account.models.tenantUser.findAndCountAll({
463
472
  include: [fastify.account.models.tenantRole, fastify.account.models.tenantOrg, fastify.account.models.user],
464
- where: { tenantId }
473
+ where: Object.assign({}, queryFilter, { tenantId }),
474
+ offset: currentPage * (currentPage - 1),
475
+ limit: perPage
465
476
  });
466
477
 
467
478
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-account",
3
- "version": "1.0.0-alpha.10",
3
+ "version": "1.0.0-alpha.11",
4
4
  "description": "fastify的用户管理账号等实现",
5
5
  "main": "index.js",
6
6
  "scripts": {