@kne/fastify-account 1.0.0-alpha.9 → 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 +31 -21
  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 -73
  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 -555
  44. package/libs/services/tenant.js +0 -132
@@ -1,154 +1,63 @@
1
1
  const fp = require('fastify-plugin');
2
2
 
3
- module.exports = fp(async (fastify, options) => {
4
- const { authenticate, services } = fastify.account;
5
- // 用于系统初始化时,设置第一个用户,只能使用一次,其他用户由该用户创建
6
- fastify.post(
7
- `${options.prefix}/initSuperAdmin`,
8
- {
9
- onRequest: [authenticate.user]
10
- },
11
- async request => {
12
- await services.admin.initSuperAdmin(await services.user.getUser(request.authenticatePayload));
13
- return {};
14
- }
15
- );
16
-
17
- fastify.get(
18
- `${options.prefix}/admin/getSuperAdminInfo`,
19
- {
20
- onRequest: [authenticate.user, authenticate.admin]
21
- },
22
- async request => {
23
- return { userInfo: request.userInfo };
24
- }
25
- );
26
-
27
- fastify.post(
28
- `${options.prefix}/admin/addUser`,
29
- {
30
- onRequest: [authenticate.user, authenticate.admin],
31
- schema: {
32
- body: {}
33
- }
34
- },
35
- async request => {
36
- const userInfo = request.body;
37
- await services.admin.addUser(Object.assign({}, userInfo, { password: services.account.md5(options.defaultPassword) }));
38
- return {};
39
- }
40
- );
41
-
42
- fastify.get(
43
- `${options.prefix}/admin/getAllUserList`,
44
- {
45
- onRequest: [authenticate.user, authenticate.admin],
46
- schema: {
47
- query: {}
48
- }
49
- },
50
- async request => {
51
- const { filter, perPage, currentPage } = Object.assign(
3
+ const adminController = fp(async (fastify, options) => {
4
+ const {services, authenticate} = fastify[options.name];
5
+ fastify.post(
6
+ `${options.prefix}/admin/initSuperAdmin`,
52
7
  {
53
- perPage: 20,
54
- currentPage: 1
8
+ onRequest: [authenticate.user],
9
+ schema: {
10
+ tags: ['管理后台'],
11
+ summary: '初始化用户为管理员',
12
+ description: '用于系统初始化时,设置第一个用户,只能使用一次,其他用户由该用户创建'
13
+ }
55
14
  },
56
- request.query
57
- );
58
- return await services.user.getAllUserList({
59
- filter,
60
- perPage,
61
- currentPage
62
- });
63
- }
64
- );
65
-
66
- fastify.post(
67
- `${options.prefix}/admin/resetUserPassword`,
68
- {
69
- onRequest: [authenticate.user, authenticate.admin],
70
- schema: {
71
- body: {
72
- type: 'object',
73
- required: ['userId', 'password'],
74
- properties: {
75
- password: { type: 'string' },
76
- userId: { type: 'string' }
77
- }
78
- }
79
- }
80
- },
81
- async request => {
82
- await services.admin.resetUserPassword(request.body);
83
- return {};
84
- }
85
- );
86
-
87
- fastify.post(
88
- `${options.prefix}/admin/saveUser`,
89
- {
90
- onRequest: [authenticate.user, authenticate.admin],
91
- schema: {
92
- body: {
93
- type: 'object',
94
- required: ['id'],
95
- properties: {
96
- id: { type: 'string' },
97
- avatar: { type: 'string' },
98
- nickname: { type: 'string' },
99
- phone: { type: 'string' },
100
- email: { type: 'string' },
101
- description: { type: 'string' }
102
- }
15
+ async request => {
16
+ await services.admin.initSuperAdmin(request.userInfo);
17
+ return {};
103
18
  }
104
- }
105
- },
106
- async request => {
107
- const user = request.body;
108
- await services.user.saveUser(user);
109
- return {};
110
- }
111
- );
19
+ );
112
20
 
113
- fastify.post(
114
- `${options.prefix}/admin/closeUser`,
115
- {
116
- onRequest: [authenticate.user, authenticate.admin],
117
- schema: {
118
- body: {
119
- type: 'object',
120
- required: ['id'],
121
- properties: {
122
- id: { type: 'string' }
123
- }
124
- }
125
- }
126
- },
127
- async request => {
128
- const { id } = request.body;
129
- await services.user.closeUser({ id });
130
- return {};
131
- }
132
- );
133
-
134
- fastify.post(
135
- `${options.prefix}/admin/openUser`,
136
- {
137
- onRequest: [authenticate.user, authenticate.admin],
138
- schema: {
139
- body: {
140
- type: 'object',
141
- required: ['id'],
142
- properties: {
143
- id: { type: 'string' }
144
- }
21
+ fastify.get(
22
+ `${options.prefix}/admin/getSuperAdminInfo`,
23
+ {
24
+ onRequest: [authenticate.user, authenticate.admin],
25
+ schema: {
26
+ tags: ['管理后台'],
27
+ summary: '获取管理员信息',
28
+ response: {
29
+ 200: {
30
+ content: {
31
+ 'application/json': {
32
+ schema: {
33
+ type: 'object',
34
+ properties: {
35
+ userInfo: {
36
+ type: 'object',
37
+ properties: {
38
+ id: {type: 'string', description: '用户id'},
39
+ nickname: {type: 'string', description: '用户昵称'},
40
+ email: {type: 'string', description: '邮箱'},
41
+ phone: {type: 'string', description: '电话'},
42
+ gender: {type: 'string', description: '性别'},
43
+ birthday: {type: 'string', format: 'date', description: '出生日期'},
44
+ description: {type: 'string', description: '个人简介'},
45
+ currentTenantId: {type: 'string', description: '当前租户ID'},
46
+ status: {type: 'number', description: '状态'}
47
+ }
48
+ }
49
+ }
50
+ }
51
+ }
52
+ }
53
+ }
54
+ }
55
+ }
56
+ },
57
+ async request => {
58
+ return {userInfo: request.userInfo};
145
59
  }
146
- }
147
- },
148
- async request => {
149
- const { id } = request.body;
150
- await services.user.openUser({ id });
151
- return {};
152
- }
153
- );
60
+ );
154
61
  });
62
+
63
+ module.exports = adminController;
@@ -1,8 +1,10 @@
1
1
  const fp = require('fastify-plugin');
2
- module.exports = fp(async (fastify, options) => {
3
- const { authenticate, services } = fastify.account;
2
+
3
+ const userController = fp(async (fastify, options) => {
4
+ const { authenticate } = fastify[options.name];
5
+
4
6
  fastify.get(
5
- `${options.prefix}/getUserInfo`,
7
+ `${options.prefix}/user/getUserInfo`,
6
8
  {
7
9
  onRequest: [authenticate.user]
8
10
  },
@@ -10,25 +12,6 @@ module.exports = fp(async (fastify, options) => {
10
12
  return { userInfo: request.userInfo };
11
13
  }
12
14
  );
13
-
14
- fastify.post(
15
- `${options.prefix}/setCurrentTenantId`,
16
- {
17
- onRequest: [authenticate.user],
18
- schema: {
19
- body: {
20
- type: 'object',
21
- required: ['tenantId'],
22
- properties: {
23
- tenantId: { type: 'string' }
24
- }
25
- }
26
- }
27
- },
28
- async request => {
29
- const { tenantId } = request.body;
30
- await services.user.setCurrentTenantId({ id: request.userInfo.id, tenantId });
31
- return {};
32
- }
33
- );
34
15
  });
16
+
17
+ module.exports = userController;
@@ -0,0 +1,13 @@
1
+ module.exports = ({DataTypes}) => {
2
+ return {
3
+ name: 'modelName',//此处定义modelName,默认根据文件名转驼峰命名,可以缺省
4
+ model: {
5
+ name: DataTypes.STRING
6
+ }, associate: ({/*这里可以获取models*/}, fastify) => {
7
+ //可以这样获取某个namespace的models fastify.account.models
8
+ }, options: {
9
+ indexed: []
10
+ }
11
+ };
12
+ };
13
+
@@ -1,34 +1,15 @@
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
- password: {
14
- type: DataTypes.STRING,
15
- allowNull: false
16
- },
17
- salt: {
18
- type: DataTypes.STRING,
19
- allowNull: false
20
- },
21
- belongToUserId: {
22
- type: DataTypes.UUID //用户修改密码时重新生成一条数据,该字段用于记录这个账号之前是属于哪个账号的
23
- }
24
- },
25
- options: {
26
- indexes: [
27
- {
28
- unique: true,
29
- fields: ['uuid', 'deleted_at']
1
+ const userAccount = ({DataTypes, definePrimaryType}) => {
2
+ return {
3
+ model: {
4
+ password: {
5
+ type: DataTypes.STRING, allowNull: false, comment: '加密后的密钥'
6
+ }, salt: {
7
+ type: DataTypes.STRING, allowNull: false, comment: '用来加密的盐'
8
+ }, belongToUserId: definePrimaryType('belongToUserId', {
9
+ comment: '账号所属的userId,用来追踪用户之前设置的密码记录'
10
+ })
30
11
  }
31
- ]
32
- }
33
- };
12
+ };
34
13
  };
14
+
15
+ module.exports = userAccount;
@@ -1,40 +1,50 @@
1
- module.exports = ({ DataTypes }) => {
1
+ const user = ({ DataTypes, definePrimaryType }) => {
2
2
  return {
3
3
  model: {
4
- id: {
5
- type: DataTypes.INTEGER,
6
- autoIncrement: true,
7
- primaryKey: true
4
+ nickname: {
5
+ type: DataTypes.STRING,
6
+ comment: '用户昵称'
8
7
  },
9
- uuid: {
10
- type: DataTypes.UUID,
11
- defaultValue: DataTypes.UUIDV4
8
+ email: {
9
+ type: DataTypes.STRING,
10
+ comment: '用户邮箱'
12
11
  },
13
- nickname: DataTypes.STRING,
14
- email: DataTypes.STRING,
15
- phone: DataTypes.STRING,
16
- userAccountId: {
17
- type: DataTypes.UUID,
18
- allowNull: false
12
+ phone: {
13
+ type: DataTypes.STRING,
14
+ comment: '用户手机号'
19
15
  },
16
+ userAccountId: definePrimaryType('userAccountId', {
17
+ allowNull: false,
18
+ comment: '当前账号id'
19
+ }),
20
20
  status: {
21
21
  type: DataTypes.INTEGER,
22
- defaultValue: 0 //0:正常,10:初始化未激活,需要用户设置密码后使用,11:已禁用,12:已关闭,
22
+ defaultValue: 0,
23
+ comment: '0:正常,10:初始化未激活,需要用户设置密码后使用,11:已禁用,12:已关闭'
24
+ },
25
+ avatar: {
26
+ type: DataTypes.STRING,
27
+ comment: '头像fileId'
28
+ },
29
+ gender: {
30
+ type: DataTypes.STRING,
31
+ comment: 'F:女,M:男'
32
+ },
33
+ birthday: {
34
+ type: DataTypes.DATE,
35
+ comment: '出生日期'
23
36
  },
24
- currentTenantId: {
25
- type: DataTypes.UUID
37
+ description: {
38
+ type: DataTypes.TEXT,
39
+ comment: '个人描述'
26
40
  },
27
- avatar: DataTypes.STRING,
28
- gender: DataTypes.STRING, //F:女,M男
29
- birthday: DataTypes.DATE,
30
- description: DataTypes.TEXT
41
+ isSuperAdmin: {
42
+ type: DataTypes.BOOLEAN,
43
+ comment: '是否是平台超级管理员'
44
+ }
31
45
  },
32
46
  options: {
33
47
  indexes: [
34
- {
35
- unique: true,
36
- fields: ['uuid', 'deleted_at']
37
- },
38
48
  {
39
49
  unique: true,
40
50
  fields: ['email', 'deleted_at']
@@ -44,17 +54,8 @@ module.exports = ({ DataTypes }) => {
44
54
  fields: ['phone', 'deleted_at']
45
55
  }
46
56
  ]
47
- },
48
- associate: ({ adminRole, user, tenant, tenantUser }) => {
49
- user.hasOne(adminRole, { foreignKey: 'userId', sourceKey: 'uuid', constraints: false });
50
- user.belongsToMany(tenant, {
51
- through: { model: tenantUser, unique: false },
52
- otherKey: 'tenantId',
53
- foreignKey: 'userId',
54
- targetKey: 'uuid',
55
- sourceKey: 'uuid',
56
- constraints: false
57
- });
58
57
  }
59
58
  };
60
59
  };
60
+
61
+ module.exports = user;
@@ -1,22 +1,17 @@
1
- module.exports = ({ DataTypes }) => {
2
- return {
3
- model: {
4
- name: {
5
- type: DataTypes.STRING,
6
- allowNull: false
7
- },
8
- type: {
9
- type: DataTypes.INTEGER,
10
- allowNull: false //0:手机注册,1:邮箱注册,2:手机登录,3:邮箱登录,4:验证租户管理员
11
- },
12
- code: {
13
- type: DataTypes.STRING,
14
- allowNull: false
15
- },
16
- status: {
17
- type: DataTypes.INTEGER,
18
- defaultValue: 0 //0:未验证,1:已验证,2:已过期
19
- }
20
- }
21
- };
1
+ const verificationCode = ({DataTypes}) => {
2
+ return {
3
+ model: {
4
+ name: {
5
+ type: DataTypes.STRING, allowNull: false
6
+ }, type: {
7
+ type: DataTypes.INTEGER.UNSIGNED, allowNull: false, comment: '0:注册,2:登录,4:验证租户管理员,5:忘记密码'
8
+ }, code: {
9
+ type: DataTypes.STRING, allowNull: false
10
+ }, status: {
11
+ type: DataTypes.INTEGER.UNSIGNED, defaultValue: 0, comment: '0:未验证,1:已验证,2:已过期'
12
+ }
13
+ }
14
+ };
22
15
  };
16
+
17
+ module.exports = verificationCode;