midway-fatcms 0.0.1-beta.24 → 0.0.1-beta.26

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.
@@ -1,275 +1,272 @@
1
- import { Inject, Provide, Config } from '@midwayjs/core';
2
- import { Context } from '@midwayjs/koa';
3
- import { KeysOfSimpleSQL } from '../libs/crud-pro/models/keys';
4
- import { CurdMixService } from './curd/CurdMixService';
5
- import * as md5 from 'md5';
6
- import { UserAccountService } from './UserAccountService';
7
- import { createUniqueId } from '../libs/utils/functions';
8
- import { IConsumerUserInfo, ISessionInfo, SYS_ACCOUNT_TYPE } from '../models/userSession';
9
- import { SystemTables } from '../models/SystemTables';
10
- import { UserSessionService } from './UserSessionService';
11
- import { CommonResult } from '../libs/utils/common-dto';
12
- import { isEnableSuperAdmin } from "../libs/utils/fatcms-request";
13
- import { CommonException } from "../libs/crud-pro/exceptions";
14
- import { GLOBAL_STATIC_CONFIG } from '@/libs/global-config/global-config';
15
-
16
- @Provide()
17
- export class AuthService {
18
- @Inject()
19
- private ctx: Context;
20
-
21
- @Config('superAdminList')
22
- private superAdminList: any[];
23
-
24
- @Inject()
25
- private curdMixService: CurdMixService;
26
-
27
- @Inject()
28
- private userService: UserAccountService;
29
-
30
- @Inject()
31
- private userSessionService: UserSessionService;
32
-
33
- /**
34
- * 明文密码转unsaltedPwd密码
35
- * @param plainPwd
36
- * @param loginName
37
- */
38
- public toUnsaltedPwd(plainPwd: string, loginName: string) {
39
- const pw2 = md5(md5(plainPwd) + '_' + loginName);
40
- return pw2;
41
- }
42
-
43
- /**
44
- * front: pw1 = md5(md5(input_pw) + "_" + loginName)
45
- * db: pw2 = md5(pwd_salt + pw1)
46
- * 检查用户名密码是否正确
47
- * @param loginName
48
- * @param unsaltedPwd
49
- */
50
- public async checkLoginPassword(loginName: string, unsaltedPwd: string): Promise<CommonResult> {
51
- const message = '用户名或密码错误';
52
- const errorCode = 'USERNAME_OR_PASSWORD_IS_INCORRECT';
53
-
54
- if (!loginName) {
55
- return CommonResult.errorRes(message, errorCode, '!loginName');
56
- }
57
-
58
- if (!unsaltedPwd) {
59
- return CommonResult.errorRes(message, errorCode, '!unsaltedPwd');
60
- }
61
-
62
- const userAccount = await this.queryUserAccountByLoginName(loginName);
63
- if (!userAccount) {
64
- return CommonResult.errorRes(message, errorCode, '!userAccount');
65
- }
66
-
67
- if (userAccount.status !== 1) {
68
- return CommonResult.errorRes(message, errorCode, 'userAccount.status !== 1');
69
- }
70
-
71
- if (!userAccount.pwd_md5) {
72
- return CommonResult.errorRes(message, errorCode, '!userAccount.pwd_md5');
73
- }
74
-
75
- const pwd_salt = userAccount.pwd_salt;
76
- const pwd_md5 = md5(unsaltedPwd + pwd_salt);
77
-
78
- if (userAccount['pwd_md5'] !== pwd_md5) {
79
- return CommonResult.errorRes(message, errorCode, "userAccount['pwd_md5'] !== pwd_md5");
80
- }
81
-
82
- return CommonResult.successRes();
83
- }
84
-
85
-
86
- /**
87
- * 创建一个toC端的UserSession
88
- * @param consumerUserInfo
89
- */
90
- public async createUserSessionForConsumer(consumerUserInfo: IConsumerUserInfo) {
91
-
92
- const sessionId = createUniqueId();
93
-
94
- const sessionInfo: ISessionInfo = {
95
- nickName: consumerUserInfo.nickName,
96
- avatar: consumerUserInfo.avatar,
97
- roleCodes: [],
98
- functionCodes: [],
99
- loginName: consumerUserInfo.loginName,
100
- sessionId,
101
- accountId: consumerUserInfo.accountId,
102
- workbenchCode: consumerUserInfo.workbenchCode,
103
- accountType: consumerUserInfo.accountType,
104
- };
105
-
106
- await this.userSessionService.saveUserSession(sessionInfo);
107
-
108
- return {
109
- sessionId,
110
- loginName: sessionInfo.loginName,
111
- accountId: sessionInfo.accountId,
112
- nickName: sessionInfo.nickName,
113
- avatar: sessionInfo.avatar,
114
- workbenchCode: sessionInfo.workbenchCode,
115
- accountType: sessionInfo.accountType,
116
- };
117
- }
118
-
119
- /**
120
- * 创建一个Session
121
- * @param loginName
122
- * @param workbenchCode
123
- */
124
- public async createUserSession(loginName: string, workbenchCode: string) {
125
- const userAccount = await this.queryUserAccountByLoginName(loginName);
126
-
127
- if (!userAccount) {
128
- throw new CommonException('USER_ACCOUNT_NOT_EXIST', '用户账号不存在');
129
- }
130
-
131
- const accountId = userAccount.account_id;
132
- const sessionId = createUniqueId();
133
- const roleCodes = await this.queryUserRoleCodeList(accountId);
134
- const functionCodes = await this.queryFunctionCodeList(roleCodes);
135
-
136
- const sessionInfo: ISessionInfo = {
137
- nickName: userAccount.nick_name,
138
- avatar: userAccount.avatar,
139
- roleCodes,
140
- functionCodes,
141
- loginName,
142
- sessionId,
143
- accountId,
144
- workbenchCode,
145
- accountType: SYS_ACCOUNT_TYPE,
146
- };
147
-
148
- await this.userSessionService.saveUserSession(sessionInfo);
149
-
150
- return {
151
- sessionId,
152
- loginName,
153
- accountId,
154
- nickName: userAccount.nick_name,
155
- avatar: userAccount.avatar,
156
- workbenchCode: sessionInfo.workbenchCode,
157
- accountType: sessionInfo.accountType,
158
- };
159
- }
160
-
161
- /**
162
- * 获取用户
163
- * @param loginName
164
- * @private
165
- */
166
- private async queryUserAccountByLoginName(loginName: string) {
167
- const superAdminList = this.superAdminList;
168
- const findFromSuperAdmin = () => {
169
-
170
- if (!isEnableSuperAdmin(this.ctx)) {
171
- return null;
172
- }
173
-
174
- if (!Array.isArray(superAdminList)) {
175
- return null;
176
- }
177
-
178
- for (let i = 0; i < superAdminList.length; i++) {
179
- const element = superAdminList[i];
180
- if (element && element.login_name === loginName) {
181
- const nick_name = 'SA' + i;
182
- return {
183
- nick_name: element.nick_name || nick_name,
184
- avatar: '',
185
- account_id: nick_name,
186
- status: 1,
187
- login_name: element.login_name,
188
- pwd_salt: element.pwd_salt,
189
- pwd_md5: element.pwd_md5,
190
- };
191
- }
192
- }
193
- };
194
- const superAdmin = findFromSuperAdmin();
195
- if (superAdmin) {
196
- return superAdmin;
197
- }
198
- return this.userService.queryUserAccountByLoginName(loginName);
199
- }
200
-
201
- /**
202
- * 查询此accountId下关联的角色code列表
203
- * @param accountId
204
- * @private
205
- */
206
- private async queryUserRoleCodeList(accountId: string): Promise<string[]> {
207
- const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
208
-
209
- const res = await this.curdMixService.executeCrudByCfg(
210
- {
211
- condition: { account_id: accountId },
212
- limit: 10000,
213
- },
214
- {
215
- sqlTable: SystemTables.sys_perm_user_role,
216
- sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY,
217
- sqlDatabase: SystemDbName,
218
- sqlDbType: SystemDbType,
219
- }
220
- );
221
- const rows = res.getResModel().rows || [];
222
- const roleCodes: string[] = rows.map(({ role_code }) => {
223
- return role_code as string;
224
- });
225
- return [...new Set(roleCodes)];
226
- }
227
-
228
- /**
229
- * 查询此roleCodeList下面关联的所有功能点列表
230
- * @param roleCodeList
231
- * @private
232
- */
233
- private async queryFunctionCodeList(roleCodeList: string[]): Promise<string[]> {
234
- if (!roleCodeList || roleCodeList.length === 0) {
235
- return [];
236
- }
237
-
238
- const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
239
-
240
- const res = await this.curdMixService.executeCrudByCfg(
241
- {
242
- condition: { role_code: { $in: roleCodeList } },
243
- limit: 10000,
244
- },
245
- {
246
- sqlTable: SystemTables.sys_perm_role_func,
247
- sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY,
248
- sqlDatabase: SystemDbName,
249
- sqlDbType: SystemDbType,
250
- }
251
- );
252
- const rows = res.getResModel().rows || [];
253
- const funcCodes: string[] = rows.map(({ func_code }) => {
254
- return func_code;
255
- });
256
- return [...new Set(funcCodes)];
257
- }
258
-
259
- async removeUserSession(sessionId: string) {
260
- return this.userSessionService.removeUserSession(sessionId);
261
- }
262
-
263
- async refreshSession(sessionInfo: ISessionInfo): Promise<ISessionInfo> {
264
- const oldSessionId = sessionInfo.sessionId;
265
-
266
- const sessionId = createUniqueId();
267
- const newSessionInfo: ISessionInfo = { ...sessionInfo, sessionId };
268
-
269
- await this.userSessionService.saveUserSession(newSessionInfo);
270
-
271
- await this.removeUserSession(oldSessionId);
272
-
273
- return newSessionInfo;
274
- }
275
- }
1
+ import { Inject, Provide, Config } from '@midwayjs/core';
2
+ import { Context } from '@midwayjs/koa';
3
+ import { KeysOfSimpleSQL } from '@/libs/crud-pro/models/keys';
4
+ import { CurdMixService } from './curd/CurdMixService';
5
+ import * as md5 from 'md5';
6
+ import { UserAccountService } from './UserAccountService';
7
+ import { createUniqueId } from '@/libs/utils/functions';
8
+ import { IConsumerUserInfo, ISessionInfo, SYS_ACCOUNT_TYPE } from '@/models/userSession';
9
+ import { SystemTables } from '@/models/SystemTables';
10
+ import { UserSessionService } from './UserSessionService';
11
+ import { CommonResult } from '@/libs/utils/common-dto';
12
+ import { isEnableSuperAdmin } from '@/libs/utils/fatcms-request';
13
+ import { CommonException } from '@/libs/crud-pro/exceptions';
14
+ import { GLOBAL_STATIC_CONFIG } from '@/libs/global-config/global-config';
15
+
16
+ @Provide()
17
+ export class AuthService {
18
+ @Inject()
19
+ private ctx: Context;
20
+
21
+ @Config('superAdminList')
22
+ private superAdminList: any[];
23
+
24
+ @Inject()
25
+ private curdMixService: CurdMixService;
26
+
27
+ @Inject()
28
+ private userService: UserAccountService;
29
+
30
+ @Inject()
31
+ private userSessionService: UserSessionService;
32
+
33
+ /**
34
+ * 明文密码转unsaltedPwd密码
35
+ * @param plainPwd
36
+ * @param loginName
37
+ */
38
+ public toUnsaltedPwd(plainPwd: string, loginName: string) {
39
+ const pw2 = md5(md5(plainPwd) + '_' + loginName);
40
+ return pw2;
41
+ }
42
+
43
+ /**
44
+ * front: pw1 = md5(md5(input_pw) + "_" + loginName)
45
+ * db: pw2 = md5(pwd_salt + pw1)
46
+ * 检查用户名密码是否正确
47
+ * @param loginName
48
+ * @param unsaltedPwd
49
+ */
50
+ public async checkLoginPassword(loginName: string, unsaltedPwd: string): Promise<CommonResult> {
51
+ const message = '用户名或密码错误';
52
+ const errorCode = 'USERNAME_OR_PASSWORD_IS_INCORRECT';
53
+
54
+ if (!loginName) {
55
+ return CommonResult.errorRes(message, errorCode, '!loginName');
56
+ }
57
+
58
+ if (!unsaltedPwd) {
59
+ return CommonResult.errorRes(message, errorCode, '!unsaltedPwd');
60
+ }
61
+
62
+ const userAccount = await this.queryUserAccountByLoginName(loginName);
63
+ if (!userAccount) {
64
+ return CommonResult.errorRes(message, errorCode, '!userAccount');
65
+ }
66
+
67
+ if (userAccount.status !== 1) {
68
+ return CommonResult.errorRes(message, errorCode, 'userAccount.status !== 1');
69
+ }
70
+
71
+ if (!userAccount.pwd_md5) {
72
+ return CommonResult.errorRes(message, errorCode, '!userAccount.pwd_md5');
73
+ }
74
+
75
+ const pwd_salt = userAccount.pwd_salt;
76
+ const pwd_md5 = md5(unsaltedPwd + pwd_salt);
77
+
78
+ if (userAccount['pwd_md5'] !== pwd_md5) {
79
+ return CommonResult.errorRes(message, errorCode, "userAccount['pwd_md5'] !== pwd_md5");
80
+ }
81
+
82
+ return CommonResult.successRes();
83
+ }
84
+
85
+ /**
86
+ * 创建一个toC端的UserSession
87
+ * @param consumerUserInfo
88
+ */
89
+ public async createUserSessionForConsumer(consumerUserInfo: IConsumerUserInfo) {
90
+ const sessionId = createUniqueId();
91
+
92
+ const sessionInfo: ISessionInfo = {
93
+ nickName: consumerUserInfo.nickName,
94
+ avatar: consumerUserInfo.avatar,
95
+ roleCodes: [],
96
+ functionCodes: [],
97
+ loginName: consumerUserInfo.loginName,
98
+ sessionId,
99
+ accountId: consumerUserInfo.accountId,
100
+ workbenchCode: consumerUserInfo.workbenchCode,
101
+ accountType: consumerUserInfo.accountType,
102
+ };
103
+
104
+ await this.userSessionService.saveUserSession(sessionInfo);
105
+
106
+ return {
107
+ sessionId,
108
+ loginName: sessionInfo.loginName,
109
+ accountId: sessionInfo.accountId,
110
+ nickName: sessionInfo.nickName,
111
+ avatar: sessionInfo.avatar,
112
+ workbenchCode: sessionInfo.workbenchCode,
113
+ accountType: sessionInfo.accountType,
114
+ };
115
+ }
116
+
117
+ /**
118
+ * 创建一个Session
119
+ * @param loginName
120
+ * @param workbenchCode
121
+ */
122
+ public async createUserSession(loginName: string, workbenchCode: string) {
123
+ const userAccount = await this.queryUserAccountByLoginName(loginName);
124
+
125
+ if (!userAccount) {
126
+ throw new CommonException('USER_ACCOUNT_NOT_EXIST', '用户账号不存在');
127
+ }
128
+
129
+ const accountId = userAccount.account_id;
130
+ const sessionId = createUniqueId();
131
+ const roleCodes = await this.queryUserRoleCodeList(accountId);
132
+ const functionCodes = await this.queryFunctionCodeList(roleCodes);
133
+
134
+ const sessionInfo: ISessionInfo = {
135
+ nickName: userAccount.nick_name,
136
+ avatar: userAccount.avatar,
137
+ roleCodes,
138
+ functionCodes,
139
+ loginName,
140
+ sessionId,
141
+ accountId,
142
+ workbenchCode,
143
+ accountType: SYS_ACCOUNT_TYPE,
144
+ };
145
+
146
+ await this.userSessionService.saveUserSession(sessionInfo);
147
+
148
+ return {
149
+ sessionId,
150
+ loginName,
151
+ accountId,
152
+ nickName: userAccount.nick_name,
153
+ avatar: userAccount.avatar,
154
+ workbenchCode: sessionInfo.workbenchCode,
155
+ accountType: sessionInfo.accountType,
156
+ };
157
+ }
158
+
159
+ /**
160
+ * 获取用户
161
+ * @param loginName
162
+ * @private
163
+ */
164
+ private async queryUserAccountByLoginName(loginName: string) {
165
+ const superAdminList = this.superAdminList;
166
+ const findFromSuperAdmin = () => {
167
+ if (!isEnableSuperAdmin(this.ctx)) {
168
+ return null;
169
+ }
170
+
171
+ if (!Array.isArray(superAdminList)) {
172
+ return null;
173
+ }
174
+
175
+ for (let i = 0; i < superAdminList.length; i++) {
176
+ const element = superAdminList[i];
177
+ if (element && element.login_name === loginName) {
178
+ const nick_name = 'SA' + i;
179
+ return {
180
+ nick_name: element.nick_name || nick_name,
181
+ avatar: '',
182
+ account_id: nick_name,
183
+ status: 1,
184
+ login_name: element.login_name,
185
+ pwd_salt: element.pwd_salt,
186
+ pwd_md5: element.pwd_md5,
187
+ };
188
+ }
189
+ }
190
+ };
191
+ const superAdmin = findFromSuperAdmin();
192
+ if (superAdmin) {
193
+ return superAdmin;
194
+ }
195
+ return this.userService.queryUserAccountByLoginName(loginName);
196
+ }
197
+
198
+ /**
199
+ * 查询此accountId下关联的角色code列表
200
+ * @param accountId
201
+ * @private
202
+ */
203
+ private async queryUserRoleCodeList(accountId: string): Promise<string[]> {
204
+ const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
205
+
206
+ const res = await this.curdMixService.executeCrudByCfg(
207
+ {
208
+ condition: { account_id: accountId },
209
+ limit: 10000,
210
+ },
211
+ {
212
+ sqlTable: SystemTables.sys_perm_user_role,
213
+ sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY,
214
+ sqlDatabase: SystemDbName,
215
+ sqlDbType: SystemDbType,
216
+ }
217
+ );
218
+ const rows = res.getResModel().rows || [];
219
+ const roleCodes: string[] = rows.map(({ role_code }) => {
220
+ return role_code as string;
221
+ });
222
+ return [...new Set(roleCodes)];
223
+ }
224
+
225
+ /**
226
+ * 查询此roleCodeList下面关联的所有功能点列表
227
+ * @param roleCodeList
228
+ * @private
229
+ */
230
+ private async queryFunctionCodeList(roleCodeList: string[]): Promise<string[]> {
231
+ if (!roleCodeList || roleCodeList.length === 0) {
232
+ return [];
233
+ }
234
+
235
+ const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
236
+
237
+ const res = await this.curdMixService.executeCrudByCfg(
238
+ {
239
+ condition: { role_code: { $in: roleCodeList } },
240
+ limit: 10000,
241
+ },
242
+ {
243
+ sqlTable: SystemTables.sys_perm_role_func,
244
+ sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY,
245
+ sqlDatabase: SystemDbName,
246
+ sqlDbType: SystemDbType,
247
+ }
248
+ );
249
+ const rows = res.getResModel().rows || [];
250
+ const funcCodes: string[] = rows.map(({ func_code }) => {
251
+ return func_code;
252
+ });
253
+ return [...new Set(funcCodes)];
254
+ }
255
+
256
+ async removeUserSession(sessionId: string) {
257
+ return this.userSessionService.removeUserSession(sessionId);
258
+ }
259
+
260
+ async refreshSession(sessionInfo: ISessionInfo): Promise<ISessionInfo> {
261
+ const oldSessionId = sessionInfo.sessionId;
262
+
263
+ const sessionId = createUniqueId();
264
+ const newSessionInfo: ISessionInfo = { ...sessionInfo, sessionId };
265
+
266
+ await this.userSessionService.saveUserSession(newSessionInfo);
267
+
268
+ await this.removeUserSession(oldSessionId);
269
+
270
+ return newSessionInfo;
271
+ }
272
+ }
@@ -1,13 +1,14 @@
1
1
  import { Inject, Provide } from '@midwayjs/core';
2
2
  import { Context } from '@midwayjs/koa';
3
- import { KeysOfSimpleSQL } from '../libs/crud-pro/models/keys';
3
+ import { KeysOfSimpleSQL } from '@/libs/crud-pro/models/keys';
4
4
  import { CurdProService } from './curd/CurdProService';
5
5
  import { RelatedType } from './curd/CurdMixUtils';
6
- import { SystemTables } from '../models/SystemTables';
7
- import { parseConfigContentToEnumInfo, IEnumInfo } from '../libs/utils/parseConfig';
6
+ import { SystemTables } from '@/models/SystemTables';
7
+ import { parseConfigContentToEnumInfo, IEnumInfo } from '@/libs/utils/parseConfig';
8
8
  import { RedisCacheService } from './base/RedisCacheService';
9
9
  import { CurdMixByLinkToCustomService } from './curd/CurdMixByLinkToCustomService';
10
10
  import { GLOBAL_STATIC_CONFIG } from '@/libs/global-config/global-config';
11
+ import { RedisKeys } from '@/models/RedisKeys';
11
12
 
12
13
  interface IQueryEnumInfo {
13
14
  type: RelatedType;
@@ -42,7 +43,7 @@ export class EnumInfoService {
42
43
  const result = [];
43
44
  for (let i = 0; i < codeList.length; i++) {
44
45
  const { code, type } = codeList[i];
45
- const cacheKey = `enums@@${type}@@${code}`;
46
+ const cacheKey = `${RedisKeys.QUERY_ENUM_INFO_PREFIX}${type}@@${code}`;
46
47
  const emums = refreshCache ? null : await this.redisCacheService.getJsonObject(cacheKey);
47
48
  if (!Array.isArray(emums) || emums.length === 0) {
48
49
  let values;
@@ -4,11 +4,11 @@ import * as mime from 'mime-types';
4
4
  import * as md5 from 'md5';
5
5
  import * as fs from 'fs';
6
6
  import * as util from 'util';
7
- import { BizException } from '../models/devops';
7
+ import { BizException } from '@/models/devops';
8
8
  import { CurdMixService } from './curd/CurdMixService';
9
- import { KeysOfSimpleSQL, KeysOfValidators } from '../libs/crud-pro/models/keys';
10
- import { AccessType, IEntityCommonInfo, UploadCategoryType } from '../models/bizmodels';
11
- import { IRequestCfgModel } from '../libs/crud-pro/interfaces';
9
+ import { KeysOfSimpleSQL, KeysOfValidators } from '@/libs/crud-pro/models/keys';
10
+ import { AccessType, IEntityCommonInfo, UploadCategoryType } from '@/models/bizmodels';
11
+ import { IRequestCfgModel } from '@/libs/crud-pro/interfaces';
12
12
  import { BaseService } from './base/BaseService';
13
13
  import { GLOBAL_STATIC_CONFIG } from '@/libs/global-config/global-config';
14
14