@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.
Files changed (54) hide show
  1. package/README.md +2506 -803
  2. package/index.js +61 -9
  3. package/libs/controllers/account.js +221 -37
  4. package/libs/controllers/admin.js +92 -17
  5. package/libs/controllers/adminPermission.js +188 -38
  6. package/libs/controllers/adminTenant.js +44 -257
  7. package/libs/controllers/adminTenantCompany.js +53 -0
  8. package/libs/controllers/adminTenantOrg.js +71 -0
  9. package/libs/controllers/{adminRole.js → adminTenantRole.js} +15 -12
  10. package/libs/controllers/adminTenantUser.js +169 -0
  11. package/libs/controllers/requestLog.js +77 -0
  12. package/libs/controllers/tenant.js +4 -14
  13. package/libs/controllers/tenantCompany.js +54 -0
  14. package/libs/controllers/tenantOrg.js +65 -0
  15. package/libs/controllers/tenantRole.js +219 -0
  16. package/libs/controllers/tenantUser.js +169 -0
  17. package/libs/controllers/user.js +4 -3
  18. package/libs/models/admin-role.js +4 -8
  19. package/libs/models/application.js +16 -10
  20. package/libs/models/company-info.js +25 -0
  21. package/libs/models/login-log.js +11 -9
  22. package/libs/models/permission.js +7 -9
  23. package/libs/models/request-log.js +43 -0
  24. package/libs/models/tenant-application.js +8 -10
  25. package/libs/models/tenant-org.js +5 -9
  26. package/libs/models/tenant-permission.js +7 -9
  27. package/libs/models/tenant-role-application.js +13 -13
  28. package/libs/models/tenant-role-permission.js +9 -14
  29. package/libs/models/tenant-role.js +5 -9
  30. package/libs/models/tenant-share-group-permission.js +5 -9
  31. package/libs/models/tenant-share-group.js +5 -9
  32. package/libs/models/tenant-source-user-share-group.js +5 -9
  33. package/libs/models/tenant-token.js +7 -9
  34. package/libs/models/tenant-user-org.js +11 -10
  35. package/libs/models/tenant-user-role.js +11 -10
  36. package/libs/models/tenant-user-share-group.js +6 -10
  37. package/libs/models/tenant-user.js +35 -16
  38. package/libs/models/tenant.js +17 -9
  39. package/libs/models/user-account.js +17 -9
  40. package/libs/models/user.js +27 -17
  41. package/libs/models/verification-code.js +8 -10
  42. package/libs/services/account.js +95 -71
  43. package/libs/services/admin.js +38 -122
  44. package/libs/services/application.js +170 -0
  45. package/libs/services/permission.js +161 -163
  46. package/libs/services/request-log.js +37 -0
  47. package/libs/services/tenant-company.js +54 -0
  48. package/libs/services/tenant-invite.js +62 -0
  49. package/libs/services/tenant-org.js +118 -0
  50. package/libs/services/tenant-role.js +109 -0
  51. package/libs/services/tenant-user.js +582 -0
  52. package/libs/services/tenant.js +69 -670
  53. package/libs/services/user.js +109 -33
  54. package/package.json +3 -3
@@ -0,0 +1,169 @@
1
+ const fp = require('fastify-plugin');
2
+
3
+ module.exports = fp(async (fastify, options) => {
4
+ const { authenticate, services } = fastify.account;
5
+ fastify.get(
6
+ `${options.prefix}/admin/getTenantUserList`,
7
+ {
8
+ onRequest: [authenticate.user, authenticate.admin],
9
+ schema: {
10
+ query: {
11
+ type: 'object',
12
+ required: ['tenantId'],
13
+ properties: {
14
+ tenantId: { type: 'string' },
15
+ filter: {
16
+ type: 'object',
17
+ properties: {
18
+ name: { type: 'string' }
19
+ }
20
+ },
21
+ currentPage: { type: 'number' },
22
+ perPage: { type: 'number' }
23
+ }
24
+ }
25
+ }
26
+ },
27
+ async request => {
28
+ const { filter, tenantId, currentPage, perPage } = Object.assign(
29
+ {},
30
+ {
31
+ filter: {},
32
+ currentPage: 1,
33
+ perPage: 20
34
+ },
35
+ request.query
36
+ );
37
+ return await services.tenantUser.getTenantUserList({ tenantId, filter, currentPage, perPage });
38
+ }
39
+ );
40
+
41
+ fastify.post(
42
+ `${options.prefix}/admin/addTenantUser`,
43
+ {
44
+ onRequest: [authenticate.user, authenticate.admin],
45
+ schema: {
46
+ body: {
47
+ type: 'object',
48
+ required: ['tenantId', 'userId', 'name'],
49
+ properties: {
50
+ tenantId: { type: 'string' },
51
+ roleIds: { type: 'array', items: { type: 'number' }, default: [] },
52
+ orgIds: {
53
+ type: 'array',
54
+ items: { type: 'number' },
55
+ default: []
56
+ },
57
+ userId: { type: 'string' },
58
+ name: { type: 'string' },
59
+ avatar: { type: 'string' },
60
+ phone: { type: 'string' },
61
+ email: { type: 'string' },
62
+ description: { type: 'string' }
63
+ }
64
+ }
65
+ }
66
+ },
67
+ async request => {
68
+ await services.tenantUser.addTenantUser(request.body);
69
+ return {};
70
+ }
71
+ );
72
+
73
+ fastify.post(
74
+ `${options.prefix}/admin/saveTenantUser`,
75
+ {
76
+ onRequest: [authenticate.user, authenticate.admin],
77
+ schema: {
78
+ body: {
79
+ type: 'object',
80
+ required: ['tenantId', 'name'],
81
+ properties: {
82
+ tenantId: { type: 'string' },
83
+ roleIds: { type: 'array', items: { type: 'number' }, default: [] },
84
+ orgIds: {
85
+ type: 'array',
86
+ items: { type: 'number' },
87
+ default: []
88
+ },
89
+ name: { type: 'string' },
90
+ avatar: { type: 'string' },
91
+ phone: { type: 'string' },
92
+ email: { type: 'string' },
93
+ description: { type: 'string' }
94
+ }
95
+ }
96
+ }
97
+ },
98
+ async request => {
99
+ await services.tenantUser.saveTenantUser(request.body);
100
+ return {};
101
+ }
102
+ );
103
+
104
+ fastify.post(
105
+ `${options.prefix}/admin/deleteTenantUser`,
106
+ {
107
+ onRequest: [authenticate.user, authenticate.admin],
108
+ schema: {
109
+ body: {
110
+ type: 'object',
111
+ required: ['tenantId', 'tenantUserId'],
112
+ properties: {
113
+ tenantId: { type: 'string' },
114
+ tenantUserId: { type: 'string' }
115
+ }
116
+ }
117
+ }
118
+ },
119
+ async request => {
120
+ const { tenantId, tenantUserId } = request.body;
121
+ await services.tenantUser.deleteTenantUser({ tenantId, tenantUserId });
122
+ return {};
123
+ }
124
+ );
125
+
126
+ fastify.post(
127
+ `${options.prefix}/admin/closeTenantUser`,
128
+ {
129
+ onRequest: [authenticate.user, authenticate.admin],
130
+ schema: {
131
+ body: {
132
+ type: 'object',
133
+ required: ['tenantId', 'tenantUserId'],
134
+ properties: {
135
+ tenantId: { type: 'string' },
136
+ tenantUserId: { type: 'string' }
137
+ }
138
+ }
139
+ }
140
+ },
141
+ async request => {
142
+ const { tenantId, tenantUserId } = request.body;
143
+ await services.tenantUser.closeTenantUser({ tenantId, tenantUserId });
144
+ return {};
145
+ }
146
+ );
147
+
148
+ fastify.post(
149
+ `${options.prefix}/admin/openTenantUser`,
150
+ {
151
+ onRequest: [authenticate.user, authenticate.admin],
152
+ schema: {
153
+ body: {
154
+ type: 'object',
155
+ required: ['tenantId', 'tenantUserId'],
156
+ properties: {
157
+ tenantId: { type: 'string' },
158
+ tenantUserId: { type: 'string' }
159
+ }
160
+ }
161
+ }
162
+ },
163
+ async request => {
164
+ const { tenantId, tenantUserId } = request.body;
165
+ await services.tenantUser.openTenantUser({ tenantId, tenantUserId });
166
+ return {};
167
+ }
168
+ );
169
+ });
@@ -0,0 +1,77 @@
1
+ const fp = require('fastify-plugin');
2
+
3
+ module.exports = fp(async (fastify, options) => {
4
+ const { authenticate, services } = fastify.account;
5
+
6
+ fastify.post(
7
+ `${options.prefix}/admin/getAllOperationLogList`,
8
+ {
9
+ onRequest: [authenticate.user, authenticate.admin],
10
+ schema: {
11
+ tags: ['管理后台'],
12
+ summary: '获取所有操作日志列表',
13
+ body: {
14
+ type: 'object',
15
+ required: [],
16
+ properties: {
17
+ filter: { type: 'object' },
18
+ type: { type: 'string' },
19
+ perPage: { type: 'number' },
20
+ currentPage: { type: 'number' }
21
+ }
22
+ }
23
+ }
24
+ },
25
+ async request => {
26
+ const { filter, type, perPage, currentPage } = Object.assign(
27
+ {
28
+ perPage: 20,
29
+ currentPage: 1
30
+ },
31
+ request.body
32
+ );
33
+ return await services.requestLog.getRequestLogList({
34
+ filter,
35
+ type,
36
+ perPage,
37
+ currentPage
38
+ });
39
+ }
40
+ );
41
+
42
+ fastify.post(
43
+ `${options.prefix}/tenant/getTenantOperationLogList`,
44
+ {
45
+ onRequest: [authenticate.user, authenticate.tenant],
46
+ schema: {
47
+ tags: ['管理后台'],
48
+ summary: '获取租户操作日志列表',
49
+ body: {
50
+ type: 'object',
51
+ required: [],
52
+ properties: {
53
+ filter: { type: 'object' },
54
+ type: { type: 'string' },
55
+ perPage: { type: 'number' },
56
+ currentPage: { type: 'number' }
57
+ }
58
+ }
59
+ }
60
+ },
61
+ async request => {
62
+ const { filter, type, perPage, currentPage } = Object.assign(
63
+ {
64
+ perPage: 20,
65
+ currentPage: 1
66
+ },
67
+ request.body
68
+ );
69
+ return await services.requestLog.getRequestLogList({
70
+ filter,
71
+ type,
72
+ perPage,
73
+ currentPage
74
+ });
75
+ }
76
+ );
77
+ });
@@ -1,33 +1,23 @@
1
1
  const fp = require('fastify-plugin');
2
2
  module.exports = fp(async (fastify, options) => {
3
+ const { authenticate, services } = fastify.account;
3
4
  fastify.get(
4
5
  `${options.prefix}/tenant/getUserTenant`,
5
6
  {
6
- onRequest: [fastify.account.authenticate.user]
7
+ onRequest: [authenticate.user]
7
8
  },
8
9
  async request => {
9
- return await fastify.account.services.tenant.getUserTenant(request.authenticatePayload);
10
+ return await services.tenantUser.getUserTenant(request.authenticatePayload);
10
11
  }
11
12
  );
12
13
 
13
14
  fastify.get(
14
15
  `${options.prefix}/tenant/getTenantUserInfo`,
15
16
  {
16
- onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.tenant]
17
+ onRequest: [authenticate.user, authenticate.tenant]
17
18
  },
18
19
  async request => {
19
20
  return request.tenantInfo;
20
21
  }
21
22
  );
22
-
23
- fastify.get(
24
- `${options.prefix}/tenant/orgList`,
25
- {
26
- onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.tenant]
27
- },
28
- async request => {
29
- const { tenantId } = request.tenantInfo;
30
- return await fastify.account.services.tenant.getTenantOrgList({ tenantId });
31
- }
32
- );
33
23
  });
@@ -0,0 +1,54 @@
1
+ const fp = require('fastify-plugin');
2
+
3
+ module.exports = fp(async (fastify, options) => {
4
+ const { authenticate, services } = fastify.account;
5
+
6
+ fastify.get(
7
+ `${options.prefix}/tenant/getCompanyInfo`,
8
+ {
9
+ onRequest: [authenticate.user, authenticate.tenant],
10
+ schema: {
11
+ tags: ['租户平台'],
12
+ summary: '获取租户的公司信息',
13
+ query: {
14
+ type: 'object',
15
+ properties: {
16
+ currentPage: { type: 'number' },
17
+ perPage: { type: 'number' }
18
+ }
19
+ }
20
+ }
21
+ },
22
+ async request => {
23
+ const { id: tenantId } = request.tenantInfo.tenant;
24
+ return await services.tenantCompany.getTenantCompanyInfo({ tenantId });
25
+ }
26
+ );
27
+
28
+ fastify.post(
29
+ `${options.prefix}/tenant/saveCompanyInfo`,
30
+ {
31
+ onRequest: [authenticate.user, authenticate.tenant],
32
+ schema: {
33
+ tags: ['租户平台'],
34
+ summary: '修改租户的公司信息',
35
+ body: {
36
+ type: 'object',
37
+ required: ['id'],
38
+ properties: {
39
+ id: { type: 'number' },
40
+ name: { type: 'string' },
41
+ shortName: { type: 'string' },
42
+ themeColor: { type: 'string' },
43
+ logo: { type: 'string' },
44
+ description: { type: 'string' }
45
+ }
46
+ }
47
+ }
48
+ },
49
+ async request => {
50
+ const { id: tenantId } = request.tenantInfo.tenant;
51
+ return await services.tenantCompany.saveTenantCompanyInfo(Object.assign({}, request.body, { tenantId }));
52
+ }
53
+ );
54
+ });
@@ -0,0 +1,65 @@
1
+ const fp = require('fastify-plugin');
2
+
3
+ module.exports = fp(async (fastify, options) => {
4
+ const { authenticate, services } = fastify.account;
5
+ fastify.post(
6
+ `${options.prefix}/tenant/addOrg`,
7
+ {
8
+ onRequest: [authenticate.user, authenticate.tenant],
9
+ required: ['name', 'pid'],
10
+ properties: {
11
+ name: { type: 'string' },
12
+ pid: { type: 'number' }
13
+ }
14
+ },
15
+ async request => {
16
+ const { id: tenantId } = request.tenantInfo.tenant;
17
+ return await services.tenantOrg.addTenantOrg(Object.assign({}, request.body, { tenantId }));
18
+ }
19
+ );
20
+
21
+ fastify.get(
22
+ `${options.prefix}/tenant/orgList`,
23
+ {
24
+ onRequest: [authenticate.user, authenticate.tenant],
25
+ schema: {}
26
+ },
27
+ async request => {
28
+ const { id: tenantId } = request.tenantInfo.tenant;
29
+ return await services.tenantOrg.getTenantOrgList({ tenantId });
30
+ }
31
+ );
32
+
33
+ fastify.post(
34
+ `${options.prefix}/tenant/editOrg`,
35
+ {
36
+ onRequest: [authenticate.user, authenticate.tenant],
37
+ required: ['name', 'pid'],
38
+ properties: {
39
+ name: { type: 'string' },
40
+ pid: { type: 'number' }
41
+ }
42
+ },
43
+ async request => {
44
+ const { id: tenantId } = request.tenantInfo.tenant;
45
+ await services.tenantOrg.saveTenantOrg(Object.assign({}, request.body, { tenantId }));
46
+ return {};
47
+ }
48
+ );
49
+
50
+ fastify.post(
51
+ `${options.prefix}/tenant/removeOrg`,
52
+ {
53
+ onRequest: [authenticate.user, authenticate.tenant],
54
+ required: ['id'],
55
+ properties: {
56
+ id: { type: 'number' }
57
+ }
58
+ },
59
+ async request => {
60
+ const { id: tenantId } = request.tenantInfo.tenant;
61
+ await services.tenantOrg.deleteTenantOrg(Object.assign({}, request.body, { tenantId }));
62
+ return {};
63
+ }
64
+ );
65
+ });
@@ -0,0 +1,219 @@
1
+ const fp = require('fastify-plugin');
2
+
3
+ module.exports = fp(async (fastify, options) => {
4
+ const { authenticate, services } = fastify.account;
5
+ fastify.get(
6
+ `${options.prefix}/tenant/getRoleList`,
7
+ {
8
+ onRequest: [authenticate.user, authenticate.tenant],
9
+ schema: {
10
+ query: {
11
+ type: 'object',
12
+ properties: {
13
+ perPage: { type: 'number' },
14
+ currentPage: { type: 'number' },
15
+ filter: {
16
+ type: 'object',
17
+ properties: {
18
+ type: { type: 'number' }
19
+ }
20
+ }
21
+ }
22
+ }
23
+ }
24
+ },
25
+ async request => {
26
+ const { id: tenantId } = request.tenantInfo.tenant;
27
+ const { filter, perPage, currentPage } = Object.assign(
28
+ {
29
+ perPage: 20,
30
+ currentPage: 1,
31
+ filter: {}
32
+ },
33
+ request.query
34
+ );
35
+ return await services.tenantRole.getTenantRoleList({ tenantId, perPage, currentPage, filter });
36
+ }
37
+ );
38
+
39
+ fastify.post(
40
+ `${options.prefix}/tenant/addRole`,
41
+ {
42
+ onRequest: [authenticate.user, authenticate.tenant],
43
+ schema: {
44
+ body: {
45
+ type: 'object',
46
+ required: ['name'],
47
+ properties: {
48
+ name: { type: 'string' },
49
+ description: { type: 'string' }
50
+ }
51
+ }
52
+ }
53
+ },
54
+ async request => {
55
+ const { id: tenantId } = request.tenantInfo.tenant;
56
+ const { name, description } = request.body;
57
+ await services.tenantRole.addTenantRole({ tenantId, name, description });
58
+
59
+ return {};
60
+ }
61
+ );
62
+
63
+ fastify.post(
64
+ `${options.prefix}/tenant/saveRole`,
65
+ {
66
+ onRequest: [authenticate.user, authenticate.tenant],
67
+ schema: {
68
+ body: {
69
+ type: 'object',
70
+ required: ['name', 'id'],
71
+ properties: {
72
+ id: { type: 'number' },
73
+ name: { type: 'string' },
74
+ description: { type: 'string' }
75
+ }
76
+ }
77
+ }
78
+ },
79
+ async request => {
80
+ const { id: tenantId } = request.tenantInfo.tenant;
81
+ await services.tenantRole.saveTenantRole(Object.assign({}, request.body, { tenantId }));
82
+ return {};
83
+ }
84
+ );
85
+
86
+ fastify.post(
87
+ `${options.prefix}/tenant/removeRole`,
88
+ {
89
+ onRequest: [authenticate.user, authenticate.tenant],
90
+ schema: {
91
+ body: {
92
+ type: 'object',
93
+ required: ['id'],
94
+ properties: {
95
+ id: { type: 'number' }
96
+ }
97
+ }
98
+ }
99
+ },
100
+ async request => {
101
+ const { id: tenantId } = request.tenantInfo.tenant;
102
+ const { id } = request.body;
103
+ await services.tenantRole.removeTenantRole({ id, tenantId });
104
+ return {};
105
+ }
106
+ );
107
+
108
+ fastify.get(
109
+ `${options.prefix}/tenant/getRolePermissionList`,
110
+ {
111
+ onRequest: [authenticate.user, authenticate.tenant],
112
+ schema: {
113
+ query: {
114
+ type: 'object',
115
+ required: ['id'],
116
+ properties: {
117
+ id: { type: 'number' }
118
+ }
119
+ }
120
+ }
121
+ },
122
+ async request => {
123
+ const { id: tenantId } = request.tenantInfo.tenant;
124
+ const { id } = request.query;
125
+ return await services.permission.getRolePermissionList({ roleId: id, tenantId });
126
+ }
127
+ );
128
+
129
+ fastify.post(
130
+ `${options.prefix}/tenant/saveRolePermissionList`,
131
+ {
132
+ onRequest: [authenticate.user, authenticate.tenant],
133
+ schema: {
134
+ roleId: { type: 'string' },
135
+ applications: {
136
+ type: 'array',
137
+ items: { type: 'string' }
138
+ },
139
+ permissions: {
140
+ type: 'array',
141
+ items: { type: 'number' }
142
+ }
143
+ }
144
+ },
145
+ async request => {
146
+ const { id: tenantId } = request.tenantInfo.tenant;
147
+ await services.permission.saveRolePermissionList(Object.assign({}, request.body, { tenantId }));
148
+ return {};
149
+ }
150
+ );
151
+
152
+ fastify.get(
153
+ `${options.prefix}/tenant/getApplicationList`,
154
+ {
155
+ onRequest: [authenticate.user, authenticate.tenant],
156
+ schema: {
157
+ tags: ['租户管理-权限'],
158
+ summary: '获取应用列表'
159
+ }
160
+ },
161
+ async request => {
162
+ const { id: tenantId } = request.tenantInfo.tenant;
163
+ const appName = request.headers['x-app-name'];
164
+ return await services.application.getApplicationList({ tenantId, appName });
165
+ }
166
+ );
167
+
168
+ fastify.get(
169
+ `${options.prefix}/tenant/getPermissionList`,
170
+ {
171
+ onRequest: [authenticate.user, authenticate.tenant],
172
+ schema: {
173
+ tags: ['租户管理-权限'],
174
+ summary: '获取应用权限列表',
175
+ query: {
176
+ type: 'object',
177
+ required: ['applicationId'],
178
+ properties: {
179
+ applicationId: { type: 'string' }
180
+ }
181
+ },
182
+ response: {
183
+ 200: {
184
+ content: {
185
+ 'application/json': {
186
+ schema: {
187
+ type: 'array',
188
+ items: {
189
+ type: 'object',
190
+ properties: {
191
+ id: { type: 'number' },
192
+ code: { type: 'string' },
193
+ name: { type: 'string' },
194
+ isModule: { type: 'number' },
195
+ isMust: { type: 'number' },
196
+ type: { type: 'number' },
197
+ pid: { type: 'number' },
198
+ paths: { type: 'array', items: { type: 'number' } },
199
+ description: { type: 'string' },
200
+ status: { type: 'number' },
201
+ createdAt: { type: 'string' },
202
+ updatedAt: { type: 'string' },
203
+ deletedAt: { type: 'string' }
204
+ }
205
+ }
206
+ }
207
+ }
208
+ }
209
+ }
210
+ }
211
+ }
212
+ },
213
+ async request => {
214
+ const { id: tenantId } = request.tenantInfo.tenant;
215
+ const { applicationId } = request.query;
216
+ return await services.permission.getPermissionList({ applicationId, tenantId });
217
+ }
218
+ );
219
+ });