@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.
- package/README.md +2506 -803
- package/index.js +61 -9
- package/libs/controllers/account.js +221 -37
- package/libs/controllers/admin.js +92 -17
- package/libs/controllers/adminPermission.js +188 -38
- package/libs/controllers/adminTenant.js +44 -257
- package/libs/controllers/adminTenantCompany.js +53 -0
- package/libs/controllers/adminTenantOrg.js +71 -0
- package/libs/controllers/{adminRole.js → adminTenantRole.js} +15 -12
- package/libs/controllers/adminTenantUser.js +169 -0
- package/libs/controllers/requestLog.js +77 -0
- package/libs/controllers/tenant.js +4 -14
- package/libs/controllers/tenantCompany.js +54 -0
- package/libs/controllers/tenantOrg.js +65 -0
- package/libs/controllers/tenantRole.js +219 -0
- package/libs/controllers/tenantUser.js +169 -0
- 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/company-info.js +25 -0
- package/libs/models/login-log.js +11 -9
- package/libs/models/permission.js +7 -9
- package/libs/models/request-log.js +43 -0
- 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 +8 -10
- package/libs/services/account.js +95 -71
- package/libs/services/admin.js +38 -122
- package/libs/services/application.js +170 -0
- package/libs/services/permission.js +161 -163
- package/libs/services/request-log.js +37 -0
- package/libs/services/tenant-company.js +54 -0
- package/libs/services/tenant-invite.js +62 -0
- package/libs/services/tenant-org.js +118 -0
- package/libs/services/tenant-role.js +109 -0
- package/libs/services/tenant-user.js +582 -0
- package/libs/services/tenant.js +69 -670
- package/libs/services/user.js +109 -33
- package/package.json +3 -3
package/libs/services/admin.js
CHANGED
|
@@ -2,35 +2,34 @@ const fp = require('fastify-plugin');
|
|
|
2
2
|
|
|
3
3
|
const ROLE = {
|
|
4
4
|
SuperAdmin: 'SuperAdmin',
|
|
5
|
-
|
|
5
|
+
Common: 'Common'
|
|
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
|
-
|
|
21
|
-
(await
|
|
20
|
+
const checkIsSuperAdmin = async user => {
|
|
21
|
+
return (
|
|
22
|
+
(await models.adminRole.count({
|
|
22
23
|
where: {
|
|
23
24
|
userId: user.id,
|
|
24
25
|
role: ROLE['SuperAdmin']
|
|
25
26
|
}
|
|
26
|
-
}))
|
|
27
|
-
)
|
|
28
|
-
throw new Error('不能执行该操作,需要超级管理员权限');
|
|
29
|
-
}
|
|
27
|
+
})) > 0
|
|
28
|
+
);
|
|
30
29
|
};
|
|
31
30
|
|
|
32
31
|
const addUser = async ({ avatar, nickname, phone, email, password, description }) => {
|
|
33
|
-
return await
|
|
32
|
+
return await services.user.addUser({
|
|
34
33
|
avatar,
|
|
35
34
|
nickname,
|
|
36
35
|
phone,
|
|
@@ -42,9 +41,9 @@ module.exports = fp(async (fastify, options) => {
|
|
|
42
41
|
};
|
|
43
42
|
|
|
44
43
|
const setSuperAdmin = async targetUser => {
|
|
45
|
-
const user = await
|
|
44
|
+
const user = await services.user.getUser(targetUser);
|
|
46
45
|
if (
|
|
47
|
-
(await
|
|
46
|
+
(await models.adminRole.count({
|
|
48
47
|
where: {
|
|
49
48
|
userId: user.id,
|
|
50
49
|
role: ROLE['SuperAdmin']
|
|
@@ -54,130 +53,47 @@ module.exports = fp(async (fastify, options) => {
|
|
|
54
53
|
throw new Error('当前用户已经是超级管理员');
|
|
55
54
|
}
|
|
56
55
|
|
|
57
|
-
await
|
|
56
|
+
await models.adminRole.create({
|
|
58
57
|
userId: user.id,
|
|
59
58
|
role: ROLE['SuperAdmin']
|
|
60
59
|
});
|
|
61
60
|
};
|
|
62
61
|
|
|
63
|
-
const
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
{
|
|
71
|
-
model: fastify.account.models.tenant
|
|
62
|
+
const cancelSuperAdmin = async targetUser => {
|
|
63
|
+
const user = await services.user.getUser(targetUser);
|
|
64
|
+
if (
|
|
65
|
+
!(await models.adminRole.count({
|
|
66
|
+
where: {
|
|
67
|
+
userId: user.id,
|
|
68
|
+
role: ROLE['SuperAdmin']
|
|
72
69
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
};
|
|
70
|
+
})) > 0
|
|
71
|
+
) {
|
|
72
|
+
throw new Error('当前用户不是超级管理员');
|
|
99
73
|
}
|
|
100
74
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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
|
-
}
|
|
75
|
+
await models.adminRole.update(
|
|
76
|
+
{
|
|
77
|
+
role: ROLE['Common']
|
|
113
78
|
},
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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);
|
|
79
|
+
{
|
|
80
|
+
where: {
|
|
81
|
+
userId: user.id
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
);
|
|
160
85
|
};
|
|
161
86
|
|
|
162
|
-
const generateTenantAdminVerifyCode = async () => {};
|
|
163
|
-
|
|
164
|
-
const verifyTenantAdmin = async () => {};
|
|
165
|
-
|
|
166
87
|
const resetUserPassword = async ({ userId, password }) => {
|
|
167
|
-
await
|
|
88
|
+
await services.account.resetPassword({ userId, password });
|
|
168
89
|
};
|
|
169
90
|
|
|
170
|
-
|
|
91
|
+
services.admin = {
|
|
171
92
|
initSuperAdmin,
|
|
172
93
|
setSuperAdmin,
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
superAdminAuthenticate,
|
|
178
|
-
generateTenantAdminVerifyCode,
|
|
179
|
-
verifyTenantAdmin,
|
|
180
|
-
resetUserPassword,
|
|
181
|
-
addUser
|
|
94
|
+
cancelSuperAdmin,
|
|
95
|
+
checkIsSuperAdmin,
|
|
96
|
+
addUser,
|
|
97
|
+
resetUserPassword
|
|
182
98
|
};
|
|
183
99
|
});
|
|
@@ -0,0 +1,170 @@
|
|
|
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 getApplicationByCode = async ({ code }) => {
|
|
30
|
+
const application = await models.application.findOne({
|
|
31
|
+
where: {
|
|
32
|
+
code
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
if (application) {
|
|
36
|
+
await getApplicationInstance({ id: application.uuid });
|
|
37
|
+
return Object.assign({}, application.get({ plain: true }), {
|
|
38
|
+
id: application.uuid
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const addApplication = async application => {
|
|
45
|
+
const currentApplication = await models.application.create(application);
|
|
46
|
+
return Object.assign({}, currentApplication.get({ plain: true }), {
|
|
47
|
+
id: currentApplication.uuid
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const saveApplication = async ({ id, ...others }) => {
|
|
52
|
+
const application = await getApplicationInstance({ id });
|
|
53
|
+
['name', 'code', 'avatar', 'url', 'description'].forEach(name => {
|
|
54
|
+
if (!isNil(others[name])) {
|
|
55
|
+
application[name] = others[name];
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
await application.save();
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const deleteApplication = async ({ id }) => {
|
|
63
|
+
const application = await getApplicationInstance({ id });
|
|
64
|
+
if (
|
|
65
|
+
(await models.tenantApplication.count({
|
|
66
|
+
where: {
|
|
67
|
+
applicationId: application.uuid
|
|
68
|
+
}
|
|
69
|
+
})) > 0
|
|
70
|
+
) {
|
|
71
|
+
throw new Error('应用已经开放给其他租户使用,不能删除');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const permissionIdList = (
|
|
75
|
+
await models.permission.findAll({
|
|
76
|
+
where: { applicationId: application.uuid }
|
|
77
|
+
})
|
|
78
|
+
).map(({ id }) => id);
|
|
79
|
+
|
|
80
|
+
const t = await fastify.sequelize.instance.transaction();
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
await models.tenantPermission.destroy(
|
|
84
|
+
{
|
|
85
|
+
where: {
|
|
86
|
+
permissionId: {
|
|
87
|
+
[fastify.sequelize.Sequelize.Op.in]: permissionIdList
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
{ transaction: t }
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
await models.tenantRolePermission.destroy(
|
|
95
|
+
{
|
|
96
|
+
where: {
|
|
97
|
+
permissionId: {
|
|
98
|
+
[fastify.sequelize.Sequelize.Op.in]: permissionIdList
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
{ transaction: t }
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
await models.permission.destroy({
|
|
106
|
+
where: {
|
|
107
|
+
applicationId: application.uuid
|
|
108
|
+
},
|
|
109
|
+
transaction: t
|
|
110
|
+
});
|
|
111
|
+
await application.destroy({ transaction: t });
|
|
112
|
+
await t.commit();
|
|
113
|
+
} catch (e) {
|
|
114
|
+
await t.rollback();
|
|
115
|
+
throw e;
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const getApplicationList = async ({ tenantId, appName }) => {
|
|
120
|
+
const query = {};
|
|
121
|
+
if (tenantId) {
|
|
122
|
+
const tenant = await services.tenant.getTenant({ id: tenantId });
|
|
123
|
+
if (!tenant) {
|
|
124
|
+
throw new Error('租户不存在');
|
|
125
|
+
}
|
|
126
|
+
const tenantApplications = await models.tenantApplication.findAll({
|
|
127
|
+
where: { tenantId }
|
|
128
|
+
});
|
|
129
|
+
query.uuid = {
|
|
130
|
+
[fastify.sequelize.Sequelize.Op.in]: tenantApplications.map(({ applicationId }) => applicationId)
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
if (appName) {
|
|
134
|
+
query['code'] = appName;
|
|
135
|
+
}
|
|
136
|
+
const list = await models.application.findAll({
|
|
137
|
+
where: query
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
return list.map(item => {
|
|
141
|
+
return Object.assign({}, item.get({ plain: true }), {
|
|
142
|
+
id: item.uuid
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const getApplicationListByIds = async ({ ids }) => {
|
|
148
|
+
const applications = await models.application.findAll({
|
|
149
|
+
where: {
|
|
150
|
+
uuid: {
|
|
151
|
+
[fastify.sequelize.Sequelize.Op.in]: ids
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
return applications.map(item => {
|
|
157
|
+
return Object.assign({}, item.get({ plain: true }), { id: item.uuid });
|
|
158
|
+
});
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
services.application = {
|
|
162
|
+
addApplication,
|
|
163
|
+
getApplication,
|
|
164
|
+
getApplicationByCode,
|
|
165
|
+
saveApplication,
|
|
166
|
+
deleteApplication,
|
|
167
|
+
getApplicationList,
|
|
168
|
+
getApplicationListByIds
|
|
169
|
+
};
|
|
170
|
+
});
|