@kne/fastify-account 1.0.0-alpha.1 → 1.0.0-alpha.3

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 (42) hide show
  1. package/README.md +101 -5
  2. package/index.js +10 -4
  3. package/libs/controllers/account.js +8 -7
  4. package/libs/controllers/admin.js +16 -15
  5. package/libs/controllers/adminPermission.js +42 -35
  6. package/libs/controllers/adminRole.js +13 -12
  7. package/libs/controllers/adminTenant.js +39 -36
  8. package/libs/controllers/tenant.js +16 -4
  9. package/libs/controllers/user.js +23 -1
  10. package/libs/models/admin-role.js +4 -8
  11. package/libs/models/application.js +16 -10
  12. package/libs/models/login-log.js +4 -8
  13. package/libs/models/permission.js +7 -9
  14. package/libs/models/tenant-application.js +8 -10
  15. package/libs/models/tenant-org.js +5 -9
  16. package/libs/models/tenant-permission.js +7 -9
  17. package/libs/models/tenant-role-application.js +14 -10
  18. package/libs/models/tenant-role-permission.js +10 -9
  19. package/libs/models/tenant-role.js +5 -9
  20. package/libs/models/tenant-share-group-permission.js +5 -9
  21. package/libs/models/tenant-share-group.js +5 -9
  22. package/libs/models/tenant-source-user-share-group.js +5 -9
  23. package/libs/models/tenant-token.js +7 -9
  24. package/libs/models/tenant-user-org.js +11 -10
  25. package/libs/models/tenant-user-role.js +11 -10
  26. package/libs/models/tenant-user-share-group.js +6 -10
  27. package/libs/models/tenant-user.js +35 -16
  28. package/libs/models/tenant.js +17 -9
  29. package/libs/models/user-account.js +17 -9
  30. package/libs/models/user.js +27 -17
  31. package/libs/models/verification-code.js +4 -8
  32. package/libs/services/account.js +26 -16
  33. package/libs/services/admin.js +14 -116
  34. package/libs/services/application.js +151 -0
  35. package/libs/services/permission.js +47 -145
  36. package/libs/services/tenant-invite.js +62 -0
  37. package/libs/services/tenant-org.js +84 -0
  38. package/libs/services/tenant-role.js +108 -0
  39. package/libs/services/tenant-user.js +486 -0
  40. package/libs/services/tenant.js +68 -512
  41. package/libs/services/user.js +69 -30
  42. package/package.json +3 -3
@@ -12,9 +12,10 @@ function userNameIsEmail(username) {
12
12
  }
13
13
 
14
14
  module.exports = fp(async (fastify, options) => {
15
+ const { models, services } = fastify.account;
15
16
  const login = async ({ username, password, ip }) => {
16
17
  const isEmail = userNameIsEmail(username);
17
- const user = await fastify.account.models.user.findOne({
18
+ const user = await models.user.findOne({
18
19
  where: Object.assign(
19
20
  {},
20
21
  isEmail
@@ -38,16 +39,23 @@ module.exports = fp(async (fastify, options) => {
38
39
 
39
40
  await passwordAuthentication({ accountId: user.userAccountId, password });
40
41
 
41
- await fastify.account.models.loginLog.create({
42
- userId: user.id,
42
+ await models.loginLog.create({
43
+ userId: user.uuid,
43
44
  ip
44
45
  });
45
46
 
46
- return fastify.jwt.sign({ payload: { id: user.id } });
47
+ return {
48
+ token: fastify.jwt.sign({ payload: { id: user.uuid } }),
49
+ user: Object.assign({}, user.get({ plain: true }), { id: user.uuid })
50
+ };
47
51
  };
48
52
 
49
53
  const passwordAuthentication = async ({ accountId, password }) => {
50
- const userAccount = await fastify.account.models.userAccount.findByPk(accountId);
54
+ const userAccount = await models.userAccount.findOne({
55
+ where: {
56
+ uuid: accountId
57
+ }
58
+ });
51
59
  if (!userAccount) {
52
60
  throw new Error('账号不存在');
53
61
  }
@@ -69,22 +77,24 @@ module.exports = fp(async (fastify, options) => {
69
77
  };
70
78
 
71
79
  const resetPassword = async ({ userId, password }) => {
72
- const userInfo = await fastify.account.models.user.findByPk(userId);
80
+ const userInfo = await models.user.findOne({
81
+ where: { uuid: userId }
82
+ });
73
83
  if (!userInfo) {
74
84
  throw new Error('用户不存在');
75
85
  }
76
- const account = await fastify.account.models.userAccount.create(
86
+ const account = await models.userAccount.create(
77
87
  Object.assign({}, await passwordEncryption(password), {
78
88
  belongToUserId: userId
79
89
  })
80
90
  );
81
91
 
82
- await userInfo.update({ userAccountId: account.id });
92
+ await userInfo.update({ userAccountId: account.uuid });
83
93
  };
84
94
 
85
95
  const register = async ({ avatar, nickname, gender, birthday, description, phone, email, code, password, status, invitationCode }) => {
86
96
  const type = phone ? 0 : 1;
87
- const verificationCode = await fastify.account.models.verificationCode.findOne({
97
+ const verificationCode = await models.verificationCode.findOne({
88
98
  where: {
89
99
  name: type === 0 ? phone : email,
90
100
  type,
@@ -99,7 +109,7 @@ module.exports = fp(async (fastify, options) => {
99
109
  verificationCode.status = 2;
100
110
  await verificationCode.save();
101
111
 
102
- return await fastify.account.services.user.addUser({
112
+ return await services.user.addUser({
103
113
  avatar,
104
114
  nickname,
105
115
  gender,
@@ -117,7 +127,7 @@ module.exports = fp(async (fastify, options) => {
117
127
 
118
128
  // 这里写发送逻辑
119
129
 
120
- await fastify.account.models.verificationCode.update(
130
+ await models.verificationCode.update(
121
131
  {
122
132
  status: 2
123
133
  },
@@ -130,7 +140,7 @@ module.exports = fp(async (fastify, options) => {
130
140
  }
131
141
  );
132
142
 
133
- await fastify.account.models.verificationCode.create({
143
+ await models.verificationCode.create({
134
144
  name: email,
135
145
  type: 1,
136
146
  code
@@ -140,7 +150,7 @@ module.exports = fp(async (fastify, options) => {
140
150
  };
141
151
 
142
152
  const verificationCodeValidate = async ({ name, type, code }) => {
143
- const verificationCode = await fastify.account.models.verificationCode.findOne({
153
+ const verificationCode = await models.verificationCode.findOne({
144
154
  where: {
145
155
  name,
146
156
  type,
@@ -165,7 +175,7 @@ module.exports = fp(async (fastify, options) => {
165
175
 
166
176
  // 这里写发送逻辑
167
177
 
168
- await fastify.account.models.verificationCode.update(
178
+ await models.verificationCode.update(
169
179
  {
170
180
  status: 2
171
181
  },
@@ -178,7 +188,7 @@ module.exports = fp(async (fastify, options) => {
178
188
  }
179
189
  );
180
190
 
181
- await fastify.account.models.verificationCode.create({
191
+ await models.verificationCode.create({
182
192
  name: phone,
183
193
  type: 0,
184
194
  code
@@ -187,7 +197,7 @@ module.exports = fp(async (fastify, options) => {
187
197
  return code;
188
198
  };
189
199
 
190
- fastify.account.services.account = {
200
+ services.account = {
191
201
  login,
192
202
  register,
193
203
  sendEmailCode,
@@ -6,19 +6,20 @@ const ROLE = {
6
6
  };
7
7
 
8
8
  module.exports = fp(async (fastify, options) => {
9
+ const { models, services } = fastify.account;
9
10
  const initSuperAdmin = async user => {
10
- if ((await fastify.account.models.adminRole.count()) > 0) {
11
+ if ((await models.adminRole.count()) > 0) {
11
12
  throw new Error('系统已经初始化完成,不能执行该操作');
12
13
  }
13
- await fastify.account.models.adminRole.create({
14
+ await models.adminRole.create({
14
15
  userId: user.id,
15
16
  role: ROLE['SuperAdmin']
16
17
  });
17
18
  };
18
19
 
19
- const superAdminAuthenticate = async user => {
20
+ const checkSuperAdmin = async user => {
20
21
  if (
21
- (await fastify.account.models.adminRole.count({
22
+ (await models.adminRole.count({
22
23
  where: {
23
24
  userId: user.id,
24
25
  role: ROLE['SuperAdmin']
@@ -30,7 +31,7 @@ module.exports = fp(async (fastify, options) => {
30
31
  };
31
32
 
32
33
  const addUser = async ({ avatar, nickname, phone, email, password, description }) => {
33
- return await fastify.account.services.user.addUser({
34
+ return await services.user.addUser({
34
35
  avatar,
35
36
  nickname,
36
37
  phone,
@@ -42,9 +43,9 @@ module.exports = fp(async (fastify, options) => {
42
43
  };
43
44
 
44
45
  const setSuperAdmin = async targetUser => {
45
- const user = await fastify.account.services.user.getUserInfo(targetUser);
46
+ const user = await services.user.getUserInfo(targetUser);
46
47
  if (
47
- (await fastify.account.models.adminRole.count({
48
+ (await models.adminRole.count({
48
49
  where: {
49
50
  userId: user.id,
50
51
  role: ROLE['SuperAdmin']
@@ -54,130 +55,27 @@ module.exports = fp(async (fastify, options) => {
54
55
  throw new Error('当前用户已经是超级管理员');
55
56
  }
56
57
 
57
- await fastify.account.models.adminRole.create({
58
+ await models.adminRole.create({
58
59
  userId: user.id,
59
60
  role: ROLE['SuperAdmin']
60
61
  });
61
62
  };
62
63
 
63
- const getAllUserList = async ({ filter, perPage, currentPage }) => {
64
- const { count, rows } = await fastify.account.models.user.findAndCountAll({
65
- include: [
66
- {
67
- attributes: ['role'],
68
- model: fastify.account.models.adminRole
69
- },
70
- {
71
- model: fastify.account.models.tenant
72
- }
73
- ]
74
- });
75
- return {
76
- pageData: rows,
77
- totalCount: count
78
- };
79
- };
80
-
81
- const getAllTenantList = async ({ filter, perPage, currentPage }) => {
82
- const queryFilter = {};
83
- if (filter?.name) {
84
- queryFilter.name = {
85
- [fastify.sequelize.Sequelize.Op.like]: `%${filter.name}%`
86
- };
87
- }
88
-
89
- if (filter?.serviceStartTime) {
90
- queryFilter.serviceStartTime = {
91
- [fastify.sequelize.Sequelize.Op.gt]: filter.serviceStartTime
92
- };
93
- }
94
-
95
- if (filter?.serviceEndTime) {
96
- queryFilter.serviceEndTime = {
97
- [fastify.sequelize.Sequelize.Op.lt]: filter.serviceEndTime
98
- };
99
- }
100
-
101
- const { count, rows } = await fastify.account.models.tenant.findAndCountAll({
102
- where: queryFilter,
103
- offset: currentPage * (currentPage - 1),
104
- limit: perPage
105
- });
106
-
107
- const res = await fastify.account.models.tenantUser.findAll({
108
- attributes: ['tenantId', fastify.sequelize.instance.fn('count', fastify.sequelize.instance.col('tenantId'))],
109
- where: {
110
- tenantId: {
111
- [fastify.sequelize.Sequelize.Op.in]: rows.map(({ id }) => id)
112
- }
113
- },
114
- group: 'tenantId'
115
- });
116
-
117
- return {
118
- pageData: rows,
119
- totalCount: count
120
- };
121
- };
122
-
123
- const addTenant = async tenant => {
124
- if (await fastify.account.models.tenant.count({ where: { name: tenant.name } })) {
125
- throw new Error('租户名称不能重复');
126
- }
127
-
128
- const t = await fastify.sequelize.instance.transaction();
129
- try {
130
- const currentTenant = await fastify.account.models.tenant.create(tenant);
131
- await fastify.account.models.tenantRole.create(
132
- {
133
- name: '系统默认角色',
134
- tenantId: currentTenant.id,
135
- description: '创建租户时自动生成,可以设置权限,不可更改删除,所有租户用户默认拥有该角色',
136
- type: 1
137
- },
138
- { transaction: t }
139
- );
140
- await fastify.account.models.tenantOrg.create(
141
- {
142
- name: '根组织',
143
- tenantId: currentTenant.id
144
- },
145
- { transaction: t }
146
- );
147
- await t.commit();
148
- } catch (e) {
149
- await t.rollback();
150
- throw e;
151
- }
152
- };
153
-
154
- const saveTenant = async tenant => {
155
- const currentTenant = await fastify.account.models.tenant.findByPk(tenant.id);
156
- if (!currentTenant) {
157
- throw new Error('租户不存在,请刷新以后重试');
158
- }
159
- await currentTenant.update(tenant);
160
- };
161
-
162
64
  const generateTenantAdminVerifyCode = async () => {};
163
65
 
164
66
  const verifyTenantAdmin = async () => {};
165
67
 
166
68
  const resetUserPassword = async ({ userId, password }) => {
167
- await fastify.account.services.account.resetPassword({ userId, password });
69
+ await services.account.resetPassword({ userId, password });
168
70
  };
169
71
 
170
- fastify.account.services.admin = {
72
+ services.admin = {
171
73
  initSuperAdmin,
172
74
  setSuperAdmin,
173
- getAllUserList,
174
- getAllTenantList,
175
- addTenant,
176
- saveTenant,
177
- superAdminAuthenticate,
75
+ checkSuperAdmin,
178
76
  generateTenantAdminVerifyCode,
179
77
  verifyTenantAdmin,
180
- resetUserPassword,
181
- addUser
78
+ addUser,
79
+ resetUserPassword
182
80
  };
183
81
  });
@@ -0,0 +1,151 @@
1
+ const fp = require('fastify-plugin');
2
+ const isNil = require('lodash/isNil');
3
+ module.exports = fp(async (fastify, options) => {
4
+ const { models, services } = fastify.account;
5
+
6
+ const getApplicationInstance = async ({ id }) => {
7
+ if (!id) {
8
+ throw new Error('应用Id不能为空');
9
+ }
10
+ const application = await models.application.findOne({
11
+ where: {
12
+ uuid: id
13
+ }
14
+ });
15
+ if (!application) {
16
+ throw new Error('应用不存在');
17
+ }
18
+
19
+ return application;
20
+ };
21
+
22
+ const getApplication = async ({ id }) => {
23
+ const application = await getApplicationInstance({ id });
24
+ return Object.assign({}, application.get({ plain: true }), {
25
+ id: application.uuid
26
+ });
27
+ };
28
+
29
+ const addApplication = async application => {
30
+ const currentApplication = await models.application.create(application);
31
+ return Object.assign({}, currentApplication.get({ plain: true }), {
32
+ id: currentApplication.uuid
33
+ });
34
+ };
35
+
36
+ const saveApplication = async ({ id, ...others }) => {
37
+ const application = await getApplicationInstance({ id });
38
+ ['name', 'code', 'avatar', 'url', 'description'].forEach(name => {
39
+ if (!isNil(others[name])) {
40
+ application[name] = others[name];
41
+ }
42
+ });
43
+
44
+ await application.save();
45
+ };
46
+
47
+ const deleteApplication = async ({ id }) => {
48
+ const application = await getApplicationInstance({ id });
49
+ if (
50
+ (await models.tenantApplication.count({
51
+ where: {
52
+ applicationId: application.uuid
53
+ }
54
+ })) > 0
55
+ ) {
56
+ throw new Error('应用已经开放给其他租户使用,不能删除');
57
+ }
58
+
59
+ const permissionIdList = (
60
+ await models.permission.findAll({
61
+ where: { applicationId: application.uuid }
62
+ })
63
+ ).map(({ id }) => id);
64
+
65
+ const t = await fastify.sequelize.instance.transaction();
66
+
67
+ try {
68
+ await models.tenantPermission.destroy(
69
+ {
70
+ where: {
71
+ permissionId: {
72
+ [fastify.sequelize.Sequelize.Op.in]: permissionIdList
73
+ }
74
+ }
75
+ },
76
+ { transaction: t }
77
+ );
78
+
79
+ await models.tenantRolePermission.destroy(
80
+ {
81
+ where: {
82
+ permissionId: {
83
+ [fastify.sequelize.Sequelize.Op.in]: permissionIdList
84
+ }
85
+ }
86
+ },
87
+ { transaction: t }
88
+ );
89
+
90
+ await models.permission.destroy({
91
+ where: {
92
+ applicationId: application.uuid
93
+ },
94
+ transaction: t
95
+ });
96
+ await application.destroy({ transaction: t });
97
+ await t.commit();
98
+ } catch (e) {
99
+ await t.rollback();
100
+ throw e;
101
+ }
102
+ };
103
+
104
+ const getApplicationList = async ({ tenantId }) => {
105
+ const query = {};
106
+ if (tenantId) {
107
+ const tenant = await services.tenant.getTenant({ id: tenantId });
108
+ if (!tenant) {
109
+ throw new Error('租户不存在');
110
+ }
111
+ const tenantApplications = await models.tenantApplication.findAll({
112
+ where: { tenantId }
113
+ });
114
+ query.uuid = {
115
+ [fastify.sequelize.Sequelize.Op.in]: tenantApplications.map(({ applicationId }) => applicationId)
116
+ };
117
+ }
118
+ const list = await models.application.findAll({
119
+ where: query
120
+ });
121
+
122
+ return list.map(item => {
123
+ return Object.assign({}, item.get({ plain: true }), {
124
+ id: item.uuid
125
+ });
126
+ });
127
+ };
128
+
129
+ const getApplicationListByIds = async ({ ids }) => {
130
+ const applications = await models.application.findAll({
131
+ where: {
132
+ uuid: {
133
+ [fastify.sequelize.Sequelize.Op.in]: ids
134
+ }
135
+ }
136
+ });
137
+
138
+ return applications.map(item => {
139
+ return Object.assign({}, item.get({ plain: true }), { id: item.uuid });
140
+ });
141
+ };
142
+
143
+ services.application = {
144
+ addApplication,
145
+ getApplication,
146
+ saveApplication,
147
+ deleteApplication,
148
+ getApplicationList,
149
+ getApplicationListByIds
150
+ };
151
+ });