@kne/fastify-account 1.0.0-alpha.0

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 (37) hide show
  1. package/README.md +1436 -0
  2. package/index.js +54 -0
  3. package/libs/controllers/account.js +189 -0
  4. package/libs/controllers/admin.js +153 -0
  5. package/libs/controllers/adminPermission.js +230 -0
  6. package/libs/controllers/adminRole.js +145 -0
  7. package/libs/controllers/adminTenant.js +461 -0
  8. package/libs/controllers/tenant.js +22 -0
  9. package/libs/controllers/user.js +12 -0
  10. package/libs/models/admin-role.js +19 -0
  11. package/libs/models/application.js +36 -0
  12. package/libs/models/login-log.js +15 -0
  13. package/libs/models/permission.js +53 -0
  14. package/libs/models/tenant-application.js +28 -0
  15. package/libs/models/tenant-org.js +30 -0
  16. package/libs/models/tenant-permission.js +28 -0
  17. package/libs/models/tenant-role-application.js +32 -0
  18. package/libs/models/tenant-role-permission.js +32 -0
  19. package/libs/models/tenant-role.js +27 -0
  20. package/libs/models/tenant-share-group-permission.js +22 -0
  21. package/libs/models/tenant-share-group.js +22 -0
  22. package/libs/models/tenant-source-user-share-group.js +22 -0
  23. package/libs/models/tenant-token.js +32 -0
  24. package/libs/models/tenant-user-org.js +22 -0
  25. package/libs/models/tenant-user-role.js +22 -0
  26. package/libs/models/tenant-user-share-group.js +22 -0
  27. package/libs/models/tenant-user.js +56 -0
  28. package/libs/models/tenant.js +38 -0
  29. package/libs/models/user-account.js +26 -0
  30. package/libs/models/user.js +50 -0
  31. package/libs/models/verification-code.js +26 -0
  32. package/libs/services/account.js +200 -0
  33. package/libs/services/admin.js +183 -0
  34. package/libs/services/permission.js +465 -0
  35. package/libs/services/tenant.js +576 -0
  36. package/libs/services/user.js +108 -0
  37. package/package.json +61 -0
package/index.js ADDED
@@ -0,0 +1,54 @@
1
+ const fp = require('fastify-plugin');
2
+ const packageJson = require('./package.json');
3
+ const path = require('path');
4
+ const merge = require('lodash/merge');
5
+
6
+ module.exports = fp(
7
+ async function (fastify, options) {
8
+ await fastify.sequelize.addModels(path.resolve(__dirname, './models'));
9
+ options = merge(
10
+ {
11
+ prefix: `/api/v${packageJson.version.split('.')[0]}/account`, //如果为true,发送邮件和短信将不调用,验证码随response返回
12
+ isTest: false,
13
+ jwt: {
14
+ secret: 'super-secret'
15
+ },
16
+ defaultPassword: 'Aa000000!'
17
+ },
18
+ options
19
+ );
20
+ fastify.register(require('fastify-ip'));
21
+ fastify.register(require('@fastify/jwt'), options.jwt);
22
+ fastify.register(require('@kne/fastify-namespace'), {
23
+ options,
24
+ name: 'account',
25
+ modules: [
26
+ ['models', await fastify.sequelize.addModels(path.resolve(__dirname, './libs/models'))],
27
+ ['services', path.resolve(__dirname, './libs/services')],
28
+ ['controllers', path.resolve(__dirname, './libs/controllers')],
29
+ [
30
+ 'authenticate',
31
+ {
32
+ user: async request => {
33
+ const info = await request.jwtVerify();
34
+ //这里判断失效时间
35
+ //info.iat
36
+ request.authenticatePayload = info.payload;
37
+ request.userInfo = await fastify.account.services.user.getUserInfo(request.authenticatePayload);
38
+ },
39
+ tenant: async request => {
40
+ request.tenantInfo = await fastify.account.services.tenant.tenantUserAuthenticate(request.userInfo);
41
+ },
42
+ admin: async request => {
43
+ request.adminInfo = await fastify.account.services.admin.superAdminAuthenticate(request.userInfo);
44
+ }
45
+ }
46
+ ]
47
+ ]
48
+ });
49
+ },
50
+ {
51
+ name: 'fastify-account',
52
+ dependencies: ['fastify-sequelize']
53
+ }
54
+ );
@@ -0,0 +1,189 @@
1
+ const fp = require('fastify-plugin');
2
+ module.exports = fp(async (fastify, options) => {
3
+ fastify.post(
4
+ `${options.prefix}/sendEmailCode`,
5
+ {
6
+ schema: {
7
+ body: {
8
+ type: 'object',
9
+ required: ['email'],
10
+ properties: {
11
+ email: { type: 'string', description: '邮箱' }
12
+ }
13
+ },
14
+ response: {
15
+ 200: {
16
+ content: {
17
+ 'application/json': {
18
+ schema: {
19
+ type: 'object',
20
+ properties: {
21
+ code: { type: 'number' },
22
+ data: {
23
+ type: 'object',
24
+ properties: {
25
+ code: { type: 'string', description: '验证码' }
26
+ }
27
+ },
28
+ msg: { type: 'string' }
29
+ }
30
+ }
31
+ }
32
+ }
33
+ }
34
+ }
35
+ }
36
+ },
37
+ async request => {
38
+ const { email } = request.body;
39
+ const code = await fastify.account.services.account.sendEmailCode({ email });
40
+ return options.isTest ? { code } : {};
41
+ }
42
+ );
43
+
44
+ fastify.post(
45
+ `${options.prefix}/sendSMSCode`,
46
+ {
47
+ schema: {
48
+ body: {
49
+ type: 'object',
50
+ required: ['phone'],
51
+ properties: {
52
+ phone: { type: 'string', description: '电话' }
53
+ }
54
+ }
55
+ }
56
+ },
57
+ async request => {
58
+ const { phone } = request.body;
59
+ const code = await fastify.account.services.account.sendSMSCode({ phone });
60
+ return options.isTest ? { code } : {};
61
+ }
62
+ );
63
+
64
+ fastify.post(
65
+ `${options.prefix}/validateCode`,
66
+ {
67
+ schema: {
68
+ body: {
69
+ type: 'object',
70
+ required: ['name', 'type', 'code'],
71
+ properties: {
72
+ name: { type: 'string', description: '被验证的账号,手机或邮箱' },
73
+ type: { type: 'number', description: '0:手机注册,1:邮箱注册,2:手机登录,3:邮箱登录,4:验证租户管理员' },
74
+ code: { type: 'string', description: '接受到的验证码' }
75
+ }
76
+ }
77
+ }
78
+ },
79
+ async request => {
80
+ const { name, type, code } = request.body;
81
+ const isPass = await fastify.account.services.account.verificationCodeValidate({
82
+ name,
83
+ type,
84
+ code
85
+ });
86
+ if (!isPass) {
87
+ throw new Error('验证码错误');
88
+ }
89
+ return {};
90
+ }
91
+ );
92
+
93
+ fastify.post(
94
+ `${options.prefix}/accountIsExists`,
95
+ {
96
+ schema: {
97
+ body: {
98
+ oneOf: [
99
+ {
100
+ type: 'object',
101
+ required: ['phone'],
102
+ properties: {
103
+ phone: { type: 'string' }
104
+ }
105
+ },
106
+ {
107
+ type: 'object',
108
+ required: ['email'],
109
+ properties: {
110
+ email: { type: 'string' }
111
+ }
112
+ }
113
+ ]
114
+ }
115
+ }
116
+ },
117
+ async request => {
118
+ const { phone, email } = request.body;
119
+ return { isExists: await fastify.account.services.user.accountIsExists({ phone, email }) };
120
+ }
121
+ );
122
+
123
+ fastify.post(
124
+ `${options.prefix}/register`,
125
+ {
126
+ schema: {
127
+ body: {
128
+ oneOf: [
129
+ {
130
+ type: 'object',
131
+ required: ['phone', 'password', 'code'],
132
+ properties: {
133
+ avatar: { type: 'string' },
134
+ phone: { type: 'string' },
135
+ code: { type: 'string' },
136
+ password: { type: 'string' },
137
+ invitationCode: { type: 'string' },
138
+ nickname: { type: 'string' },
139
+ gender: { type: 'string' },
140
+ birthday: { type: 'string', format: 'date' },
141
+ description: { type: 'string' }
142
+ }
143
+ },
144
+ {
145
+ type: 'object',
146
+ required: ['email', 'password', 'code'],
147
+ properties: {
148
+ avatar: { type: 'string' },
149
+ email: { type: 'string' },
150
+ code: { type: 'string' },
151
+ password: { type: 'string' },
152
+ invitationCode: { type: 'string' },
153
+ nickname: { type: 'string' },
154
+ gender: { type: 'string' },
155
+ birthday: { type: 'string', format: 'date' },
156
+ description: { type: 'string' }
157
+ }
158
+ }
159
+ ]
160
+ }
161
+ }
162
+ },
163
+ async request => {
164
+ const account = request.body;
165
+ return await fastify.account.services.account.register(account);
166
+ }
167
+ );
168
+
169
+ fastify.post(
170
+ `${options.prefix}/login`,
171
+ {
172
+ schema: {
173
+ body: {
174
+ type: 'object',
175
+ required: ['username', 'password'],
176
+ properties: {
177
+ username: { type: 'string' },
178
+ password: { type: 'string' }
179
+ }
180
+ }
181
+ }
182
+ },
183
+ async request => {
184
+ const { username, password } = request.body;
185
+ const token = await fastify.account.services.account.login({ username, password, ip: request.ip });
186
+ return { token };
187
+ }
188
+ );
189
+ });
@@ -0,0 +1,153 @@
1
+ const fp = require('fastify-plugin');
2
+
3
+ module.exports = fp(async (fastify, options) => {
4
+ // 用于系统初始化时,设置第一个用户,只能使用一次,其他用户由该用户创建
5
+ fastify.post(
6
+ `${options.prefix}/initSuperAdmin`,
7
+ {
8
+ onRequest: [fastify.account.authenticate.user]
9
+ },
10
+ async request => {
11
+ await fastify.account.services.admin.initSuperAdmin(await fastify.account.services.user.getUserInfo(request.authenticatePayload));
12
+ return {};
13
+ }
14
+ );
15
+
16
+ fastify.get(
17
+ `${options.prefix}/admin/getSuperAdminInfo`,
18
+ {
19
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin]
20
+ },
21
+ async request => {
22
+ return { userInfo: request.userInfo };
23
+ }
24
+ );
25
+
26
+ fastify.post(
27
+ `${options.prefix}/admin/addUser`,
28
+ {
29
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
30
+ schema: {
31
+ body: {}
32
+ }
33
+ },
34
+ async request => {
35
+ const userInfo = request.body;
36
+ await fastify.account.services.admin.addUser(Object.assign({}, userInfo, { password: options.defaultPassword }));
37
+ return {};
38
+ }
39
+ );
40
+
41
+ fastify.get(
42
+ `${options.prefix}/admin/getAllUserList`,
43
+ {
44
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
45
+ schema: {
46
+ query: {}
47
+ }
48
+ },
49
+ async request => {
50
+ const { filter, perPage, currentPage } = Object.assign(
51
+ {
52
+ perPage: 20,
53
+ currentPage: 1
54
+ },
55
+ request.query
56
+ );
57
+ return await fastify.account.services.admin.getAllUserList({
58
+ filter,
59
+ perPage,
60
+ currentPage
61
+ });
62
+ }
63
+ );
64
+
65
+ fastify.post(
66
+ `${options.prefix}/admin/resetUserPassword`,
67
+ {
68
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
69
+ schema: {
70
+ body: {
71
+ type: 'object',
72
+ required: ['userId', 'password'],
73
+ properties: {
74
+ password: { type: 'string' },
75
+ userId: { type: 'string' }
76
+ }
77
+ }
78
+ }
79
+ },
80
+ async request => {
81
+ await fastify.account.services.admin.resetUserPassword(request.body);
82
+ return {};
83
+ }
84
+ );
85
+
86
+ fastify.post(
87
+ `${options.prefix}/admin/saveUser`,
88
+ {
89
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
90
+ schema: {
91
+ body: {
92
+ type: 'object',
93
+ required: ['id'],
94
+ properties: {
95
+ id: { type: 'string' },
96
+ avatar: { type: 'string' },
97
+ nickname: { type: 'string' },
98
+ phone: { type: 'string' },
99
+ email: { type: 'string' },
100
+ description: { type: 'string' }
101
+ }
102
+ }
103
+ }
104
+ },
105
+ async request => {
106
+ const user = request.body;
107
+ await fastify.account.services.user.saveUser(user);
108
+ return {};
109
+ }
110
+ );
111
+
112
+ fastify.post(
113
+ `${options.prefix}/admin/closeUser`,
114
+ {
115
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
116
+ schema: {
117
+ body: {
118
+ type: 'object',
119
+ required: ['id'],
120
+ properties: {
121
+ id: { type: 'string' }
122
+ }
123
+ }
124
+ }
125
+ },
126
+ async request => {
127
+ const { id } = request.body;
128
+ await fastify.account.services.user.closeUser({ id });
129
+ return {};
130
+ }
131
+ );
132
+
133
+ fastify.post(
134
+ `${options.prefix}/admin/openUser`,
135
+ {
136
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
137
+ schema: {
138
+ body: {
139
+ type: 'object',
140
+ required: ['id'],
141
+ properties: {
142
+ id: { type: 'string' }
143
+ }
144
+ }
145
+ }
146
+ },
147
+ async request => {
148
+ const { id } = request.body;
149
+ await fastify.account.services.user.openUser({ id });
150
+ return {};
151
+ }
152
+ );
153
+ });
@@ -0,0 +1,230 @@
1
+ const fp = require('fastify-plugin');
2
+
3
+ module.exports = fp(async (fastify, options) => {
4
+ fastify.post(
5
+ `${options.prefix}/admin/addApplication`,
6
+ {
7
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
8
+ schema: {
9
+ body: {
10
+ type: 'object',
11
+ required: ['name', 'code'],
12
+ properties: {
13
+ name: { type: 'string' },
14
+ url: { type: 'string' },
15
+ avatar: { type: 'string' },
16
+ code: { type: 'string' },
17
+ description: { type: 'string' }
18
+ }
19
+ }
20
+ }
21
+ },
22
+ async request => {
23
+ await fastify.account.services.permission.addApplication(request.body);
24
+ return {};
25
+ }
26
+ );
27
+
28
+ fastify.post(
29
+ `${options.prefix}/admin/saveApplication`,
30
+ {
31
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
32
+ schema: {
33
+ body: {
34
+ type: 'object',
35
+ required: ['id', 'name', 'code'],
36
+ properties: {
37
+ id: { type: 'string' },
38
+ url: { type: 'string' },
39
+ name: { type: 'string' },
40
+ avatar: { type: 'string' },
41
+ code: { type: 'string' },
42
+ description: { type: 'string' }
43
+ }
44
+ }
45
+ }
46
+ },
47
+ async request => {
48
+ await fastify.account.services.permission.saveApplication(request.body);
49
+ return {};
50
+ }
51
+ );
52
+
53
+ fastify.post(
54
+ `${options.prefix}/admin/deleteApplication`,
55
+ {
56
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
57
+ schema: {
58
+ body: {
59
+ type: 'object',
60
+ required: ['id'],
61
+ properties: {
62
+ id: { type: 'string' }
63
+ }
64
+ }
65
+ }
66
+ },
67
+ async request => {
68
+ const { id } = request.body;
69
+ await fastify.account.services.permission.deleteApplication({ id });
70
+ return {};
71
+ }
72
+ );
73
+
74
+ fastify.get(
75
+ `${options.prefix}/admin/getApplicationList`,
76
+ {
77
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
78
+ schema: {
79
+ query: {
80
+ type: 'object',
81
+ properties: {
82
+ tenantId: { type: 'string' }
83
+ }
84
+ }
85
+ }
86
+ },
87
+ async request => {
88
+ const { tenantId } = request.query;
89
+ return await fastify.account.services.permission.getApplicationList({ tenantId });
90
+ }
91
+ );
92
+
93
+ fastify.post(
94
+ `${options.prefix}/admin/addPermission`,
95
+ {
96
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
97
+ schema: {
98
+ body: {
99
+ type: 'object',
100
+ required: ['applicationId', 'name', 'code'],
101
+ properties: {
102
+ applicationId: { type: 'string' },
103
+ name: { type: 'string' },
104
+ code: { type: 'string' },
105
+ type: { type: 'number' },
106
+ isModule: { type: 'number' },
107
+ isMust: { type: 'number' },
108
+ pid: { type: 'number' },
109
+ description: { type: 'string' }
110
+ }
111
+ }
112
+ }
113
+ },
114
+ async request => {
115
+ await fastify.account.services.permission.addPermission(request.body);
116
+ return {};
117
+ }
118
+ );
119
+
120
+ fastify.get(
121
+ `${options.prefix}/admin/getPermissionList`,
122
+ {
123
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
124
+ schema: {
125
+ query: {
126
+ type: 'object',
127
+ required: ['applicationId'],
128
+ properties: {
129
+ applicationId: { type: 'string' },
130
+ tenantId: { type: 'string' }
131
+ }
132
+ }
133
+ }
134
+ },
135
+ async request => {
136
+ const { applicationId, tenantId } = request.query;
137
+ return await fastify.account.services.permission.getPermissionList({ applicationId, tenantId });
138
+ }
139
+ );
140
+
141
+ fastify.post(
142
+ `${options.prefix}/admin/deletePermission`,
143
+ {
144
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
145
+ schema: {
146
+ body: {
147
+ type: 'object',
148
+ required: ['id'],
149
+ properties: {
150
+ id: { type: 'string' }
151
+ }
152
+ }
153
+ }
154
+ },
155
+ async request => {
156
+ const { id } = request.body;
157
+
158
+ await fastify.account.services.permission.deletePermission({ id });
159
+
160
+ return {};
161
+ }
162
+ );
163
+
164
+ fastify.post(
165
+ `${options.prefix}/admin/savePermission`,
166
+ {
167
+ onRequest: [fastify.account.authenticate.user, fastify.account.authenticate.admin],
168
+ schema: {
169
+ body: {
170
+ type: 'object',
171
+ required: ['id'],
172
+ properties: {
173
+ id: { type: 'string' },
174
+ name: { type: 'string' },
175
+ type: { type: 'number' },
176
+ isMust: { type: 'number' },
177
+ description: { type: 'string' }
178
+ }
179
+ }
180
+ }
181
+ },
182
+ async request => {
183
+ await fastify.account.services.permission.savePermission(request.body);
184
+ return {};
185
+ }
186
+ );
187
+
188
+ fastify.post(
189
+ `${options.prefix}/admin/saveTenantPermissionList`,
190
+ {
191
+ body: {
192
+ type: 'object',
193
+ required: ['tenantId', 'applications', 'permissions'],
194
+ properties: {
195
+ tenantId: { type: 'string' },
196
+ applications: {
197
+ type: 'array',
198
+ items: { type: 'string' }
199
+ },
200
+ permissions: {
201
+ type: 'array',
202
+ items: { type: 'number' }
203
+ }
204
+ }
205
+ }
206
+ },
207
+ async request => {
208
+ await fastify.account.services.permission.saveTenantPermissionList(request.body);
209
+
210
+ return {};
211
+ }
212
+ );
213
+
214
+ fastify.get(
215
+ `${options.prefix}/admin/getTenantPermissionList`,
216
+ {
217
+ query: {
218
+ type: 'object',
219
+ required: ['tenantId'],
220
+ properties: {
221
+ tenantId: { type: 'string' }
222
+ }
223
+ }
224
+ },
225
+ async request => {
226
+ const { tenantId } = request.query;
227
+ return await fastify.account.services.permission.getTenantPermissionList({ tenantId });
228
+ }
229
+ );
230
+ });