@kne/fastify-account 1.0.0-alpha.8 → 2.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 (44) hide show
  1. package/README.md +6 -2046
  2. package/index.js +33 -20
  3. package/libs/controllers/account.js +246 -173
  4. package/libs/controllers/admin-user.js +110 -0
  5. package/libs/controllers/admin.js +55 -146
  6. package/libs/controllers/user.js +7 -24
  7. package/libs/models/example._js +13 -0
  8. package/libs/models/user-account.js +13 -32
  9. package/libs/models/user.js +37 -36
  10. package/libs/models/verification-code.js +16 -21
  11. package/libs/services/account.js +124 -149
  12. package/libs/services/admin.js +21 -75
  13. package/libs/services/user.js +52 -58
  14. package/package.json +24 -28
  15. package/libs/controllers/adminPermission.js +0 -237
  16. package/libs/controllers/adminRole.js +0 -146
  17. package/libs/controllers/adminTenant.js +0 -464
  18. package/libs/controllers/tenant.js +0 -34
  19. package/libs/models/admin-role.js +0 -15
  20. package/libs/models/application.js +0 -42
  21. package/libs/models/login-log.js +0 -11
  22. package/libs/models/permission.js +0 -51
  23. package/libs/models/tenant-application.js +0 -26
  24. package/libs/models/tenant-org.js +0 -26
  25. package/libs/models/tenant-permission.js +0 -26
  26. package/libs/models/tenant-role-application.js +0 -37
  27. package/libs/models/tenant-role-permission.js +0 -34
  28. package/libs/models/tenant-role.js +0 -23
  29. package/libs/models/tenant-share-group-permission.js +0 -18
  30. package/libs/models/tenant-share-group.js +0 -18
  31. package/libs/models/tenant-source-user-share-group.js +0 -18
  32. package/libs/models/tenant-token.js +0 -30
  33. package/libs/models/tenant-user-org.js +0 -23
  34. package/libs/models/tenant-user-role.js +0 -23
  35. package/libs/models/tenant-user-share-group.js +0 -18
  36. package/libs/models/tenant-user.js +0 -75
  37. package/libs/models/tenant.js +0 -46
  38. package/libs/services/application.js +0 -151
  39. package/libs/services/permission.js +0 -367
  40. package/libs/services/tenant-invite.js +0 -62
  41. package/libs/services/tenant-org.js +0 -97
  42. package/libs/services/tenant-role.js +0 -108
  43. package/libs/services/tenant-user.js +0 -549
  44. package/libs/services/tenant.js +0 -132
@@ -1,37 +0,0 @@
1
- module.exports = ({ DataTypes }) => {
2
- return {
3
- model: {
4
- tenantId: {
5
- type: DataTypes.UUID,
6
- allowNull: false
7
- },
8
- applicationId: {
9
- type: DataTypes.UUID,
10
- allowNull: false
11
- },
12
- roleId: {
13
- type: DataTypes.INTEGER,
14
- allowNull: false
15
- },
16
- status: {
17
- type: DataTypes.INTEGER,
18
- defaultValue: 0 //0:开启 11:关闭
19
- }
20
- },
21
- options: {
22
- indexes: [
23
- {
24
- name: 'application_key',
25
- unique: true,
26
- fields: ['tenant_id', 'application_id', 'role_id', 'deleted_at']
27
- }
28
- ]
29
- },
30
- associate: ({ tenantRoleApplication, application }) => {
31
- tenantRoleApplication.belongsTo(application, {
32
- targetKey: 'uuid',
33
- constraints: false
34
- });
35
- }
36
- };
37
- };
@@ -1,34 +0,0 @@
1
- module.exports = ({ DataTypes }) => {
2
- return {
3
- model: {
4
- tenantId: {
5
- type: DataTypes.UUID,
6
- allowNull: false
7
- },
8
- permissionId: {
9
- type: DataTypes.INTEGER,
10
- allowNull: false
11
- },
12
- roleId: {
13
- type: DataTypes.INTEGER,
14
- allowNull: false
15
- },
16
- status: {
17
- type: DataTypes.INTEGER,
18
- defaultValue: 0 //0:开启 11:关闭
19
- }
20
- },
21
- options: {
22
- indexes: [
23
- {
24
- name: 'permission_key',
25
- unique: true,
26
- fields: ['tenant_id', 'permission_id', 'role_id', 'deleted_at']
27
- }
28
- ]
29
- },
30
- associate: ({ tenantRolePermission, permission }) => {
31
- tenantRolePermission.belongsTo(permission, { constraints: false });
32
- }
33
- };
34
- };
@@ -1,23 +0,0 @@
1
- module.exports = ({ DataTypes }) => {
2
- return {
3
- model: {
4
- tenantId: {
5
- type: DataTypes.UUID,
6
- allowNull: false
7
- },
8
- name: {
9
- type: DataTypes.STRING,
10
- allowNull: false
11
- },
12
- status: {
13
- type: DataTypes.INTEGER,
14
- defaultValue: 0
15
- },
16
- type: {
17
- type: DataTypes.INTEGER,
18
- defaultValue: 0 //0:用户自定义,1:系统默认
19
- },
20
- description: DataTypes.TEXT
21
- }
22
- };
23
- };
@@ -1,18 +0,0 @@
1
- module.exports = ({ DataTypes }) => {
2
- return {
3
- model: {
4
- tenantId: {
5
- type: DataTypes.UUID,
6
- allowNull: false
7
- },
8
- tenantShareGroupId: {
9
- type: DataTypes.INTEGER,
10
- allowNull: false
11
- },
12
- tenantPermissionId: {
13
- type: DataTypes.INTEGER,
14
- allowNull: false
15
- }
16
- }
17
- };
18
- };
@@ -1,18 +0,0 @@
1
- module.exports = ({ DataTypes }) => {
2
- return {
3
- model: {
4
- tenantId: {
5
- type: DataTypes.UUID,
6
- allowNull: false
7
- },
8
- name: {
9
- type: DataTypes.STRING,
10
- allowNull: false
11
- },
12
- status: {
13
- type: DataTypes.INTEGER,
14
- defaultValue: 0
15
- }
16
- }
17
- };
18
- };
@@ -1,18 +0,0 @@
1
- module.exports = ({ DataTypes }) => {
2
- return {
3
- model: {
4
- tenantId: {
5
- type: DataTypes.UUID,
6
- allowNull: false
7
- },
8
- tenantUserId: {
9
- type: DataTypes.STRING,
10
- allowNull: false
11
- },
12
- tenantShareGroupId: {
13
- type: DataTypes.INTEGER,
14
- allowNull: false
15
- }
16
- }
17
- };
18
- };
@@ -1,30 +0,0 @@
1
- module.exports = ({ DataTypes }) => {
2
- return {
3
- model: {
4
- tenantId: {
5
- type: DataTypes.UUID,
6
- allowNull: false
7
- },
8
- token: {
9
- type: DataTypes.STRING,
10
- allowNull: false
11
- },
12
- info: DataTypes.JSON,
13
- type: {
14
- type: DataTypes.INTEGER,
15
- allowNull: false //10:邀请用户加入租户
16
- },
17
- createdTenantUserId: {
18
- type: DataTypes.UUID
19
- }
20
- },
21
- options: {
22
- indexes: [
23
- {
24
- unique: true,
25
- fields: ['tenant_id', 'token', 'type', 'deleted_at']
26
- }
27
- ]
28
- }
29
- };
30
- };
@@ -1,23 +0,0 @@
1
- module.exports = ({ DataTypes }) => {
2
- return {
3
- model: {
4
- id: {
5
- type: DataTypes.INTEGER,
6
- autoIncrement: true,
7
- primaryKey: true
8
- },
9
- tenantId: {
10
- type: DataTypes.UUID,
11
- allowNull: false
12
- },
13
- tenantUserId: {
14
- type: DataTypes.UUID,
15
- allowNull: false
16
- },
17
- tenantOrgId: {
18
- type: DataTypes.INTEGER,
19
- allowNull: false
20
- }
21
- }
22
- };
23
- };
@@ -1,23 +0,0 @@
1
- module.exports = ({ DataTypes }) => {
2
- return {
3
- model: {
4
- id: {
5
- type: DataTypes.INTEGER,
6
- autoIncrement: true,
7
- primaryKey: true
8
- },
9
- tenantId: {
10
- type: DataTypes.UUID,
11
- allowNull: false
12
- },
13
- tenantUserId: {
14
- type: DataTypes.UUID,
15
- allowNull: false
16
- },
17
- tenantRoleId: {
18
- type: DataTypes.INTEGER,
19
- allowNull: false
20
- }
21
- }
22
- };
23
- };
@@ -1,18 +0,0 @@
1
- module.exports = ({ DataTypes }) => {
2
- return {
3
- model: {
4
- tenantId: {
5
- type: DataTypes.UUID,
6
- allowNull: false
7
- },
8
- tenantUserId: {
9
- type: DataTypes.UUID,
10
- allowNull: false
11
- },
12
- tenantShareGroupId: {
13
- type: DataTypes.INTEGER,
14
- allowNull: false
15
- }
16
- }
17
- };
18
- };
@@ -1,75 +0,0 @@
1
- module.exports = ({ DataTypes }) => {
2
- return {
3
- model: {
4
- id: {
5
- type: DataTypes.INTEGER,
6
- autoIncrement: true,
7
- primaryKey: true
8
- },
9
- uuid: {
10
- type: DataTypes.UUID,
11
- defaultValue: DataTypes.UUIDV4
12
- },
13
- userId: {
14
- type: DataTypes.UUID,
15
- allowNull: false
16
- },
17
- tenantId: {
18
- type: DataTypes.UUID,
19
- allowNull: false
20
- },
21
- name: {
22
- type: DataTypes.STRING,
23
- allowNull: false
24
- },
25
- phone: {
26
- type: DataTypes.STRING
27
- },
28
- email: {
29
- type: DataTypes.STRING
30
- },
31
- avatar: {
32
- type: DataTypes.STRING
33
- },
34
- description: {
35
- type: DataTypes.TEXT
36
- },
37
- status: {
38
- type: DataTypes.INTEGER,
39
- defaultValue: 0 //0:正常,11:已禁用,12:已关闭,
40
- }
41
- },
42
- options: {
43
- indexes: [
44
- {
45
- unique: true,
46
- fields: ['uuid', 'deleted_at']
47
- },
48
- {
49
- unique: true,
50
- fields: ['tenant_id', 'user_id', 'deleted_at']
51
- }
52
- ]
53
- },
54
- associate: ({ user, tenantRole, tenantUser, tenantUserRole, tenantOrg, tenantUserOrg }) => {
55
- tenantUser.belongsToMany(tenantRole, {
56
- through: tenantUserRole,
57
- foreignKey: 'tenantUserId',
58
- otherKey: 'tenantRoleId',
59
- sourceKey: 'uuid',
60
- constraints: false
61
- });
62
- tenantUser.belongsToMany(tenantOrg, {
63
- through: tenantUserOrg,
64
- foreignKey: 'tenantUserId',
65
- otherKey: 'tenantOrgId',
66
- sourceKey: 'uuid',
67
- constraints: false
68
- });
69
- tenantUser.belongsTo(user, {
70
- targetKey: 'uuid',
71
- constraints: false
72
- });
73
- }
74
- };
75
- };
@@ -1,46 +0,0 @@
1
- module.exports = ({ DataTypes }) => {
2
- return {
3
- model: {
4
- id: {
5
- type: DataTypes.INTEGER,
6
- autoIncrement: true,
7
- primaryKey: true
8
- },
9
- uuid: {
10
- type: DataTypes.UUID,
11
- defaultValue: DataTypes.UUIDV4
12
- },
13
- name: {
14
- type: DataTypes.STRING,
15
- allowNull: false
16
- },
17
- accountNumber: {
18
- type: DataTypes.INTEGER,
19
- defaultValue: 0,
20
- allowNull: false
21
- },
22
- serviceStartTime: {
23
- type: DataTypes.DATE,
24
- defaultValue: new Date(),
25
- allowNull: false
26
- },
27
- serviceEndTime: {
28
- type: DataTypes.DATE,
29
- allowNull: false
30
- },
31
- status: {
32
- type: DataTypes.INTEGER,
33
- defaultValue: 0 // 0:正常,10:过期被关闭,11:已禁用,12:已关闭,
34
- },
35
- description: DataTypes.TEXT
36
- },
37
- options: {
38
- indexes: [
39
- {
40
- unique: true,
41
- fields: ['uuid', 'deleted_at']
42
- }
43
- ]
44
- }
45
- };
46
- };
@@ -1,151 +0,0 @@
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
- });