@kne/fastify-account 1.0.0-alpha.2 → 1.0.0-alpha.4
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 +47 -2
- package/index.js +10 -4
- package/libs/controllers/account.js +7 -6
- package/libs/controllers/admin.js +16 -15
- package/libs/controllers/adminPermission.js +42 -35
- package/libs/controllers/adminRole.js +13 -12
- package/libs/controllers/adminTenant.js +39 -36
- package/libs/controllers/tenant.js +7 -6
- package/libs/controllers/user.js +4 -3
- package/libs/models/admin-role.js +4 -8
- package/libs/models/application.js +16 -10
- package/libs/models/login-log.js +4 -8
- package/libs/models/permission.js +7 -9
- package/libs/models/tenant-application.js +8 -10
- package/libs/models/tenant-org.js +5 -9
- package/libs/models/tenant-permission.js +7 -9
- package/libs/models/tenant-role-application.js +13 -13
- package/libs/models/tenant-role-permission.js +9 -14
- package/libs/models/tenant-role.js +5 -9
- package/libs/models/tenant-share-group-permission.js +5 -9
- package/libs/models/tenant-share-group.js +5 -9
- package/libs/models/tenant-source-user-share-group.js +5 -9
- package/libs/models/tenant-token.js +7 -9
- package/libs/models/tenant-user-org.js +11 -10
- package/libs/models/tenant-user-role.js +11 -10
- package/libs/models/tenant-user-share-group.js +6 -10
- package/libs/models/tenant-user.js +35 -16
- package/libs/models/tenant.js +17 -9
- package/libs/models/user-account.js +17 -9
- package/libs/models/user.js +27 -17
- package/libs/models/verification-code.js +4 -8
- package/libs/services/account.js +26 -16
- package/libs/services/admin.js +14 -116
- package/libs/services/application.js +151 -0
- package/libs/services/permission.js +47 -145
- package/libs/services/tenant-invite.js +62 -0
- package/libs/services/tenant-org.js +84 -0
- package/libs/services/tenant-role.js +108 -0
- package/libs/services/tenant-user.js +486 -0
- package/libs/services/tenant.js +68 -669
- package/libs/services/user.js +63 -33
- package/package.json +3 -3
package/libs/services/admin.js
CHANGED
|
@@ -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
|
|
11
|
+
if ((await models.adminRole.count()) > 0) {
|
|
11
12
|
throw new Error('系统已经初始化完成,不能执行该操作');
|
|
12
13
|
}
|
|
13
|
-
await
|
|
14
|
+
await models.adminRole.create({
|
|
14
15
|
userId: user.id,
|
|
15
16
|
role: ROLE['SuperAdmin']
|
|
16
17
|
});
|
|
17
18
|
};
|
|
18
19
|
|
|
19
|
-
const
|
|
20
|
+
const checkSuperAdmin = async user => {
|
|
20
21
|
if (
|
|
21
|
-
(await
|
|
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
|
|
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
|
|
46
|
+
const user = await services.user.getUserInfo(targetUser);
|
|
46
47
|
if (
|
|
47
|
-
(await
|
|
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
|
|
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
|
|
69
|
+
await services.account.resetPassword({ userId, password });
|
|
168
70
|
};
|
|
169
71
|
|
|
170
|
-
|
|
72
|
+
services.admin = {
|
|
171
73
|
initSuperAdmin,
|
|
172
74
|
setSuperAdmin,
|
|
173
|
-
|
|
174
|
-
getAllTenantList,
|
|
175
|
-
addTenant,
|
|
176
|
-
saveTenant,
|
|
177
|
-
superAdminAuthenticate,
|
|
75
|
+
checkSuperAdmin,
|
|
178
76
|
generateTenantAdminVerifyCode,
|
|
179
77
|
verifyTenantAdmin,
|
|
180
|
-
|
|
181
|
-
|
|
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
|
+
});
|