@kne/fastify-account 1.0.0-alpha.17 → 1.0.0-alpha.18

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/index.js CHANGED
@@ -17,11 +17,10 @@ module.exports = fp(
17
17
  secret: 'super-secret',
18
18
  expires: null
19
19
  },
20
- defaultPassword: 'Aa000000!'
20
+ sendMessage: async () => {}
21
21
  },
22
22
  options
23
23
  );
24
- fastify.register(require('fastify-ip'));
25
24
  fastify.register(require('@fastify/jwt'), options.jwt);
26
25
  fastify.register(require('@kne/fastify-namespace'), {
27
26
  options,
@@ -46,14 +45,41 @@ module.exports = fp(
46
45
  }
47
46
  request.authenticatePayload = info.payload;
48
47
  request.userInfo = await fastify.account.services.user.getUser(request.authenticatePayload);
48
+ request.appName = request.headers['x-app-name'];
49
+ await fastify.account.services.requestLog.addRequestLog({
50
+ userInfo: request.userInfo,
51
+ tenantId: request.userInfo.currentTenantId,
52
+ appName: request.appName,
53
+ type: 'user',
54
+ action: `${request.routerPath}:${request.method}`,
55
+ summary: request.routeSchema?.summary
56
+ });
49
57
  },
50
58
  tenant: async request => {
51
- request.tenantInfo = await fastify.account.services.tenantUser.getTenantUserByUserId(request.userInfo);
59
+ request.tenantInfo = await fastify.account.services.tenantUser.getTenantUserByUserId({
60
+ userInfo: request.userInfo,
61
+ appName: request.appName
62
+ });
63
+ await fastify.account.services.requestLog.addRequestLog({
64
+ userInfo: request.userInfo,
65
+ tenantId: request.tenantInfo.tenant.id,
66
+ appName: request.appName,
67
+ type: 'tenant',
68
+ action: `${request.routerPath}:${request.method}`,
69
+ summary: request.routeSchema?.summary
70
+ });
52
71
  },
53
72
  admin: async request => {
54
73
  if (!(await fastify.account.services.admin.checkIsSuperAdmin(request.userInfo))) {
55
74
  throw Unauthorized('不能执行该操作,需要超级管理员权限');
56
75
  }
76
+ await fastify.account.services.requestLog.addRequestLog({
77
+ userInfo: request.userInfo,
78
+ appName: request.appName,
79
+ type: 'admin',
80
+ action: `${request.routerPath}:${request.method}`,
81
+ summary: request.routeSchema?.summary
82
+ });
57
83
  },
58
84
  createPermission: permission => async request => {
59
85
  const permissions = get(request.tenantInfo, 'tenantUser.permissions');
@@ -32,7 +32,7 @@ module.exports = fp(async (fastify, options) => {
32
32
  },
33
33
  async request => {
34
34
  const { email } = request.body;
35
- const code = await services.account.sendVerificationCode({ name: email, type: 0, messageType: 1 });
35
+ const code = await services.account.sendVerificationCode({ name: email, type: 0 });
36
36
  return options.isTest ? { code } : {};
37
37
  }
38
38
  );
@@ -68,7 +68,7 @@ module.exports = fp(async (fastify, options) => {
68
68
  },
69
69
  async request => {
70
70
  const { phone } = request.body;
71
- const code = await services.account.sendVerificationCode({ name: phone, type: 0, messageType: 0 });
71
+ const code = await services.account.sendVerificationCode({ name: phone, type: 0 });
72
72
  return options.isTest ? { code } : {};
73
73
  }
74
74
  );
@@ -249,7 +249,8 @@ module.exports = fp(async (fastify, options) => {
249
249
  },
250
250
  async request => {
251
251
  const { username, password } = request.body;
252
- const { token, user } = await services.account.login({ username, password, ip: request.ip });
252
+ const appName = request.headers['x-app-name'];
253
+ const { token, user } = await services.account.login({ username, password, ip: request.ip, appName });
253
254
  return { token, currentTenantId: user.currentTenantId };
254
255
  }
255
256
  );
@@ -297,32 +298,17 @@ module.exports = fp(async (fastify, options) => {
297
298
  tags: ['账号'],
298
299
  summary: '用户重置密码',
299
300
  body: {
300
- oneOf: [
301
- {
302
- type: 'object',
303
- required: ['email', 'newPwd', 'token'],
304
- properties: {
305
- email: { type: 'string', description: '邮箱' },
306
- newPwd: { type: 'string', description: '新密码' },
307
- token: { type: 'string', description: '验证token' }
308
- }
309
- },
310
- {
311
- type: 'object',
312
- required: ['phone', 'newPwd'],
313
- properties: {
314
- phone: { type: 'string', description: '手机号' },
315
- newPwd: { type: 'string', description: '新密码' },
316
- token: { type: 'string', description: '验证token' }
317
- }
318
- }
319
- ]
301
+ type: 'object',
302
+ required: ['newPwd', 'token'],
303
+ properties: {
304
+ newPwd: { type: 'string', description: '新密码' },
305
+ token: { type: 'string', description: '验证token' }
306
+ }
320
307
  }
321
308
  }
322
309
  },
323
310
  async request => {
324
- await services.account.resetPassword({
325
- email: request.body.email,
311
+ await services.account.resetPasswordByToken({
326
312
  password: request.body.newPwd,
327
313
  token: request.body.token
328
314
  });
@@ -359,7 +345,7 @@ module.exports = fp(async (fastify, options) => {
359
345
  },
360
346
  async request => {
361
347
  const name = request.body.email || request.body.phone;
362
- const token = await services.account.sendJWTVerificationCode({ name, type: 5, messageType: 3 });
348
+ const token = await services.account.sendJWTVerificationCode({ name, type: 5 });
363
349
  return options.isTest ? { token } : {};
364
350
  }
365
351
  );
@@ -66,253 +66,6 @@ module.exports = fp(async (fastify, options) => {
66
66
  }
67
67
  );
68
68
 
69
- fastify.post(
70
- `${options.prefix}/admin/addTenant`,
71
- {
72
- onRequest: [authenticate.user, authenticate.admin],
73
- schema: {
74
- body: {
75
- type: 'object',
76
- required: ['name', 'accountNumber', 'serviceStartTime', 'serviceEndTime'],
77
- properties: {
78
- name: { type: 'string' },
79
- accountNumber: { type: 'number' },
80
- serviceStartTime: {
81
- type: 'string',
82
- format: 'date-time'
83
- },
84
- serviceEndTime: {
85
- type: 'string',
86
- format: 'date-time'
87
- }
88
- }
89
- }
90
- }
91
- },
92
- async request => {
93
- await services.tenant.addTenant(request.body);
94
- return {};
95
- }
96
- );
97
-
98
- fastify.post(
99
- `${options.prefix}/admin/saveTenant`,
100
- {
101
- onRequest: [authenticate.user, authenticate.admin],
102
- schema: {
103
- body: {
104
- type: 'object',
105
- required: ['id', 'name', 'accountNumber', 'serviceStartTime', 'serviceEndTime'],
106
- properties: {
107
- id: { type: 'string' },
108
- name: { type: 'string' },
109
- accountNumber: { type: 'number' },
110
- serviceStartTime: {
111
- type: 'string',
112
- format: 'date-time'
113
- },
114
- serviceEndTime: {
115
- type: 'string',
116
- format: 'date-time'
117
- }
118
- }
119
- }
120
- }
121
- },
122
- async request => {
123
- await services.tenant.saveTenant(request.body);
124
- return {};
125
- }
126
- );
127
-
128
- fastify.post(
129
- `${options.prefix}/admin/tenant/addOrg`,
130
- {
131
- onRequest: [authenticate.user, authenticate.admin],
132
- required: ['name', 'tenantId', 'pid'],
133
- properties: {
134
- name: { type: 'string' },
135
- tenantId: { type: 'string' },
136
- pid: { type: 'number' }
137
- }
138
- },
139
- async request => {
140
- return await services.tenantOrg.addTenantOrg(request.body);
141
- }
142
- );
143
-
144
- fastify.get(
145
- `${options.prefix}/admin/tenant/orgList`,
146
- {
147
- onRequest: [authenticate.user, authenticate.admin],
148
- schema: {
149
- query: {
150
- type: 'object',
151
- properties: {
152
- id: { type: 'string' }
153
- }
154
- }
155
- }
156
- },
157
- async request => {
158
- const { tenantId } = request.query;
159
- return await services.tenantOrg.getTenantOrgList({ tenantId });
160
- }
161
- );
162
-
163
- fastify.post(
164
- `${options.prefix}/admin/tenant/editOrg`,
165
- {
166
- onRequest: [authenticate.user, authenticate.admin],
167
- required: ['name', 'tenantId', 'pid'],
168
- properties: {
169
- name: { type: 'string' },
170
- tenantId: { type: 'string' },
171
- pid: { type: 'number' }
172
- }
173
- },
174
- async request => {
175
- await services.tenantOrg.saveTenantOrg(request.body);
176
- return {};
177
- }
178
- );
179
-
180
- fastify.post(
181
- `${options.prefix}/admin/tenant/removeOrg`,
182
- {
183
- onRequest: [authenticate.user, authenticate.admin],
184
- required: ['tenantId', 'id'],
185
- properties: {
186
- tenantId: { type: 'string' },
187
- id: { type: 'number' }
188
- }
189
- },
190
- async request => {
191
- await services.tenantOrg.deleteTenantOrg(request.body);
192
- return {};
193
- }
194
- );
195
-
196
- fastify.get(
197
- `${options.prefix}/admin/getTenantUserList`,
198
- {
199
- onRequest: [authenticate.user, authenticate.admin],
200
- schema: {
201
- query: {
202
- type: 'object',
203
- properties: {
204
- tenantId: { type: 'string' },
205
- filter: {
206
- type: 'object',
207
- properties: {
208
- name: { type: 'string' }
209
- }
210
- },
211
- currentPage: { type: 'number' },
212
- perPage: { type: 'number' }
213
- }
214
- }
215
- }
216
- },
217
- async request => {
218
- const { filter, tenantId, currentPage, perPage } = Object.assign(
219
- {},
220
- {
221
- filter: {},
222
- currentPage: 1,
223
- perPage: 20
224
- },
225
- request.query
226
- );
227
- return await services.tenantUser.getTenantUserList({ tenantId, filter, currentPage, perPage });
228
- }
229
- );
230
-
231
- fastify.post(
232
- `${options.prefix}/admin/addTenantUser`,
233
- {
234
- onRequest: [authenticate.user, authenticate.admin],
235
- schema: {
236
- body: {
237
- type: 'object',
238
- required: ['tenantId', 'userId', 'name'],
239
- properties: {
240
- tenantId: { type: 'string' },
241
- roleIds: { type: 'array', items: { type: 'number' }, default: [] },
242
- orgIds: {
243
- type: 'array',
244
- items: { type: 'number' },
245
- default: []
246
- },
247
- userId: { type: 'string' },
248
- name: { type: 'string' },
249
- avatar: { type: 'string' },
250
- phone: { type: 'string' },
251
- email: { type: 'string' },
252
- description: { type: 'string' }
253
- }
254
- }
255
- }
256
- },
257
- async request => {
258
- await services.tenantUser.addTenantUser(request.body);
259
- return {};
260
- }
261
- );
262
-
263
- fastify.post(
264
- `${options.prefix}/admin/saveTenantUser`,
265
- {
266
- onRequest: [authenticate.user, authenticate.admin],
267
- schema: {
268
- body: {
269
- type: 'object',
270
- required: ['tenantId', 'name'],
271
- properties: {
272
- tenantId: { type: 'string' },
273
- roleIds: { type: 'array', items: { type: 'number' }, default: [] },
274
- orgIds: {
275
- type: 'array',
276
- items: { type: 'number' },
277
- default: []
278
- },
279
- name: { type: 'string' },
280
- avatar: { type: 'string' },
281
- phone: { type: 'string' },
282
- email: { type: 'string' },
283
- description: { type: 'string' }
284
- }
285
- }
286
- }
287
- },
288
- async request => {
289
- await services.tenantUser.saveTenantUser(request.body);
290
- return {};
291
- }
292
- );
293
-
294
- fastify.post(
295
- `${options.prefix}/admin/deleteTenantUser`,
296
- {
297
- onRequest: [authenticate.user, authenticate.admin],
298
- schema: {
299
- body: {
300
- type: 'object',
301
- required: ['tenantId', 'tenantUserId'],
302
- properties: {
303
- tenantId: { type: 'string' },
304
- tenantUserId: { type: 'string' }
305
- }
306
- }
307
- }
308
- },
309
- async request => {
310
- const { tenantId, tenantUserId } = request.body;
311
- await services.tenantUser.deleteTenantUser({ tenantId, tenantUserId });
312
- return {};
313
- }
314
- );
315
-
316
69
  fastify.post(
317
70
  `${options.prefix}/admin/closeTenant`,
318
71
  {
@@ -356,45 +109,60 @@ module.exports = fp(async (fastify, options) => {
356
109
  );
357
110
 
358
111
  fastify.post(
359
- `${options.prefix}/admin/closeTenantUser`,
112
+ `${options.prefix}/admin/addTenant`,
360
113
  {
361
114
  onRequest: [authenticate.user, authenticate.admin],
362
115
  schema: {
363
116
  body: {
364
117
  type: 'object',
365
- required: ['tenantId', 'tenantUserId'],
118
+ required: ['name', 'accountNumber', 'serviceStartTime', 'serviceEndTime'],
366
119
  properties: {
367
- tenantId: { type: 'string' },
368
- tenantUserId: { type: 'string' }
120
+ name: { type: 'string' },
121
+ accountNumber: { type: 'number' },
122
+ serviceStartTime: {
123
+ type: 'string',
124
+ format: 'date-time'
125
+ },
126
+ serviceEndTime: {
127
+ type: 'string',
128
+ format: 'date-time'
129
+ }
369
130
  }
370
131
  }
371
132
  }
372
133
  },
373
134
  async request => {
374
- const { tenantId, tenantUserId } = request.body;
375
- await services.tenantUser.closeTenantUser({ tenantId, tenantUserId });
135
+ await services.tenant.addTenant(request.body);
376
136
  return {};
377
137
  }
378
138
  );
379
139
 
380
140
  fastify.post(
381
- `${options.prefix}/admin/openTenantUser`,
141
+ `${options.prefix}/admin/saveTenant`,
382
142
  {
383
143
  onRequest: [authenticate.user, authenticate.admin],
384
144
  schema: {
385
145
  body: {
386
146
  type: 'object',
387
- required: ['tenantId', 'tenantUserId'],
147
+ required: ['id', 'name', 'accountNumber', 'serviceStartTime', 'serviceEndTime'],
388
148
  properties: {
389
- tenantId: { type: 'string' },
390
- tenantUserId: { type: 'string' }
149
+ id: { type: 'string' },
150
+ name: { type: 'string' },
151
+ accountNumber: { type: 'number' },
152
+ serviceStartTime: {
153
+ type: 'string',
154
+ format: 'date-time'
155
+ },
156
+ serviceEndTime: {
157
+ type: 'string',
158
+ format: 'date-time'
159
+ }
391
160
  }
392
161
  }
393
162
  }
394
163
  },
395
164
  async request => {
396
- const { tenantId, tenantUserId } = request.body;
397
- await services.tenantUser.openTenantUser({ tenantId, tenantUserId });
165
+ await services.tenant.saveTenant(request.body);
398
166
  return {};
399
167
  }
400
168
  );
@@ -0,0 +1,71 @@
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}/admin/tenant/addOrg`,
7
+ {
8
+ onRequest: [authenticate.user, authenticate.admin],
9
+ required: ['name', 'tenantId', 'pid'],
10
+ properties: {
11
+ name: { type: 'string' },
12
+ tenantId: { type: 'string' },
13
+ pid: { type: 'number' }
14
+ }
15
+ },
16
+ async request => {
17
+ return await services.tenantOrg.addTenantOrg(request.body);
18
+ }
19
+ );
20
+
21
+ fastify.get(
22
+ `${options.prefix}/admin/tenant/orgList`,
23
+ {
24
+ onRequest: [authenticate.user, authenticate.admin],
25
+ schema: {
26
+ query: {
27
+ type: 'object',
28
+ properties: {
29
+ id: { type: 'string' }
30
+ }
31
+ }
32
+ }
33
+ },
34
+ async request => {
35
+ const { tenantId } = request.query;
36
+ return await services.tenantOrg.getTenantOrgList({ tenantId });
37
+ }
38
+ );
39
+
40
+ fastify.post(
41
+ `${options.prefix}/admin/tenant/editOrg`,
42
+ {
43
+ onRequest: [authenticate.user, authenticate.admin],
44
+ required: ['name', 'tenantId', 'pid'],
45
+ properties: {
46
+ name: { type: 'string' },
47
+ tenantId: { type: 'string' },
48
+ pid: { type: 'number' }
49
+ }
50
+ },
51
+ async request => {
52
+ await services.tenantOrg.saveTenantOrg(request.body);
53
+ return {};
54
+ }
55
+ );
56
+
57
+ fastify.post(
58
+ `${options.prefix}/admin/tenant/removeOrg`,
59
+ {
60
+ onRequest: [authenticate.user, authenticate.admin],
61
+ required: ['id'],
62
+ properties: {
63
+ id: { type: 'number' }
64
+ }
65
+ },
66
+ async request => {
67
+ await services.tenantOrg.deleteTenantOrg(request.body);
68
+ return {};
69
+ }
70
+ );
71
+ });
@@ -0,0 +1,168 @@
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
+ properties: {
13
+ tenantId: { type: 'string' },
14
+ filter: {
15
+ type: 'object',
16
+ properties: {
17
+ name: { type: 'string' }
18
+ }
19
+ },
20
+ currentPage: { type: 'number' },
21
+ perPage: { type: 'number' }
22
+ }
23
+ }
24
+ }
25
+ },
26
+ async request => {
27
+ const { filter, tenantId, currentPage, perPage } = Object.assign(
28
+ {},
29
+ {
30
+ filter: {},
31
+ currentPage: 1,
32
+ perPage: 20
33
+ },
34
+ request.query
35
+ );
36
+ return await services.tenantUser.getTenantUserList({ tenantId, filter, currentPage, perPage });
37
+ }
38
+ );
39
+
40
+ fastify.post(
41
+ `${options.prefix}/admin/addTenantUser`,
42
+ {
43
+ onRequest: [authenticate.user, authenticate.admin],
44
+ schema: {
45
+ body: {
46
+ type: 'object',
47
+ required: ['tenantId', 'userId', 'name'],
48
+ properties: {
49
+ tenantId: { type: 'string' },
50
+ roleIds: { type: 'array', items: { type: 'number' }, default: [] },
51
+ orgIds: {
52
+ type: 'array',
53
+ items: { type: 'number' },
54
+ default: []
55
+ },
56
+ userId: { type: 'string' },
57
+ name: { type: 'string' },
58
+ avatar: { type: 'string' },
59
+ phone: { type: 'string' },
60
+ email: { type: 'string' },
61
+ description: { type: 'string' }
62
+ }
63
+ }
64
+ }
65
+ },
66
+ async request => {
67
+ await services.tenantUser.addTenantUser(request.body);
68
+ return {};
69
+ }
70
+ );
71
+
72
+ fastify.post(
73
+ `${options.prefix}/admin/saveTenantUser`,
74
+ {
75
+ onRequest: [authenticate.user, authenticate.admin],
76
+ schema: {
77
+ body: {
78
+ type: 'object',
79
+ required: ['tenantId', 'name'],
80
+ properties: {
81
+ tenantId: { type: 'string' },
82
+ roleIds: { type: 'array', items: { type: 'number' }, default: [] },
83
+ orgIds: {
84
+ type: 'array',
85
+ items: { type: 'number' },
86
+ default: []
87
+ },
88
+ name: { type: 'string' },
89
+ avatar: { type: 'string' },
90
+ phone: { type: 'string' },
91
+ email: { type: 'string' },
92
+ description: { type: 'string' }
93
+ }
94
+ }
95
+ }
96
+ },
97
+ async request => {
98
+ await services.tenantUser.saveTenantUser(request.body);
99
+ return {};
100
+ }
101
+ );
102
+
103
+ fastify.post(
104
+ `${options.prefix}/admin/deleteTenantUser`,
105
+ {
106
+ onRequest: [authenticate.user, authenticate.admin],
107
+ schema: {
108
+ body: {
109
+ type: 'object',
110
+ required: ['tenantId', 'tenantUserId'],
111
+ properties: {
112
+ tenantId: { type: 'string' },
113
+ tenantUserId: { type: 'string' }
114
+ }
115
+ }
116
+ }
117
+ },
118
+ async request => {
119
+ const { tenantId, tenantUserId } = request.body;
120
+ await services.tenantUser.deleteTenantUser({ tenantId, tenantUserId });
121
+ return {};
122
+ }
123
+ );
124
+
125
+ fastify.post(
126
+ `${options.prefix}/admin/closeTenantUser`,
127
+ {
128
+ onRequest: [authenticate.user, authenticate.admin],
129
+ schema: {
130
+ body: {
131
+ type: 'object',
132
+ required: ['tenantId', 'tenantUserId'],
133
+ properties: {
134
+ tenantId: { type: 'string' },
135
+ tenantUserId: { type: 'string' }
136
+ }
137
+ }
138
+ }
139
+ },
140
+ async request => {
141
+ const { tenantId, tenantUserId } = request.body;
142
+ await services.tenantUser.closeTenantUser({ tenantId, tenantUserId });
143
+ return {};
144
+ }
145
+ );
146
+
147
+ fastify.post(
148
+ `${options.prefix}/admin/openTenantUser`,
149
+ {
150
+ onRequest: [authenticate.user, authenticate.admin],
151
+ schema: {
152
+ body: {
153
+ type: 'object',
154
+ required: ['tenantId', 'tenantUserId'],
155
+ properties: {
156
+ tenantId: { type: 'string' },
157
+ tenantUserId: { type: 'string' }
158
+ }
159
+ }
160
+ }
161
+ },
162
+ async request => {
163
+ const { tenantId, tenantUserId } = request.body;
164
+ await services.tenantUser.openTenantUser({ tenantId, tenantUserId });
165
+ return {};
166
+ }
167
+ );
168
+ });