You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

413 lines
22 KiB

  1. namespace CounsellorBL
  2. {
  3. using CounsellorBL.BLStructure;
  4. using CounsellorBL.Common;
  5. using CounsellorBL.ConstDefinition;
  6. using CounsellorBL.Helper;
  7. using Microsoft.Extensions.Caching.Memory;
  8. using MonumentDefine;
  9. using OT.COM.ArsenalDB;
  10. using OT.COM.SignalerMessage;
  11. using SoldierData.EnterprizeV4;
  12. using System;
  13. using System.Collections.Concurrent;
  14. using System.Collections.Generic;
  15. using System.Globalization;
  16. using System.Linq;
  17. public partial class AuthorityService : DBService
  18. {
  19. public override string MainTable => null;
  20. [Auth(false)]
  21. public CResponseMessage HandShake(CRequestMessage i_crmInput)
  22. {
  23. CResponseMessage crmRes = new CSuccessResponseMessage(null, i_crmInput);
  24. crmRes.param[BLWording.TOKEN] = Guid.NewGuid().ToString();
  25. return crmRes;
  26. }
  27. [Auth(false)]
  28. public CResponseMessage Login(CRequestMessage i_crmInput)
  29. {
  30. int nExpMinute = Convert.ToInt32(CustomizeDBMgr.SettingData[BLWording.TOKEN_EXP_MINUTE], CultureInfo.CurrentCulture);
  31. string sMsg = null;
  32. CResponseMessage crmRes = null;
  33. do
  34. {
  35. List<Dictionary<string, object>> lDic = getQryParameterList(i_crmInput);
  36. IMemoryCache imc = GetMemoryCache();
  37. if (imc != null)
  38. {
  39. lock (imc)
  40. {
  41. if (imc.TryGetValue(BLWording.TOKENMAP, out ConcurrentDictionary<string, tb_sys_session> dicMap))
  42. {
  43. List<string> lKeys = dicMap.Keys.ToList();
  44. DateTime dtLast = DateTime.Now.AddMinutes(-1 * nExpMinute);
  45. foreach (string sKey in lKeys)
  46. {
  47. tb_sys_session s = dicMap[sKey];
  48. if (s.update_date < dtLast)
  49. {
  50. dicMap.TryRemove(sKey, out _);
  51. }
  52. }
  53. }
  54. }
  55. }
  56. if (lDic != null && lDic.Count == 1)
  57. {
  58. Dictionary<string, object> dicLoginInfo = lDic[0];
  59. bool bIsAD = false;
  60. if (dicLoginInfo.ContainsKey(BLWording.ENTERCODE) && dicLoginInfo.ContainsKey(BLWording.AUTOENTER))
  61. {
  62. string sEnterCode = dicLoginInfo[BLWording.ENTERCODE].ToString();
  63. string sAutoCode = dicLoginInfo[BLWording.AUTOENTER].ToString();
  64. int nLen = sEnterCode.Length;
  65. int nIdex = 0;
  66. if (nLen > 0 && nLen == sAutoCode.Length)
  67. {
  68. for (nIdex = 0; nIdex < nLen; nIdex++)
  69. {
  70. if (sEnterCode[nIdex] != sAutoCode[nLen - nIdex - 1])
  71. {
  72. break;
  73. }
  74. }
  75. }
  76. bIsAD = nIdex == nLen;
  77. }
  78. QueryJsonElementCollection lBlocks = new QueryJsonElementCollection();
  79. QueryJsonElement qjeA = lBlocks.GetInst();
  80. qjeA.table = tb_sys_user.TABLENAME;
  81. qjeA.wherecols = new WhereNode(WhereNode.ENodeOperation.ENO_AND,
  82. new WhereNode(tb_sys_user.CN_ACCOUNT, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_user), dicLoginInfo[tb_sys_user.CN_ACCOUNT]),
  83. new WhereNode(tb_sys_user.CN_STATUS_FLAG, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_user), BLWording.STATUS_FLAG_ON)
  84. );
  85. qjeA.displaycols = new List<string>() { tb_sys_user.CN_UID };
  86. lBlocks.Add(qjeA);
  87. QueryJsonElement qjeEmp = lBlocks.GetInst();
  88. qjeEmp.table = tb_hr_employee.TABLENAME;
  89. qjeEmp.jointype = QueryJsonElement.LEFT_JOIN;
  90. qjeEmp.joincols = new Dictionary<string, string>()
  91. { {tb_hr_employee.CN_UID, tb_sys_user.CN_UID } };
  92. qjeEmp.jointable = qjeA;
  93. lBlocks.Add(qjeEmp);
  94. QueryJsonElement qjeB = lBlocks.GetInst();
  95. qjeB.table = tb_sys_user2entercode.TABLENAME;
  96. qjeB.jointype = QueryJsonElement.LEFT_JOIN;
  97. qjeB.joincols = new Dictionary<string, string>()
  98. { {tb_sys_user2entercode.CN_USER_UID, tb_sys_user.CN_UID } };
  99. qjeB.jointable = qjeA;
  100. sMsg = EncryptHelper.Encrypt(dicLoginInfo[BLWording.ENTERCODE].ToString(), out string sEnc);
  101. if (sMsg != null)
  102. {
  103. break;
  104. }
  105. if (!bIsAD)
  106. {
  107. qjeB.wherecols = new WhereNode(tb_sys_user2entercode.CN_USER_ENTERCODE, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_user2entercode), sEnc);
  108. }
  109. lBlocks.Add(qjeB);
  110. QueryJsonElement qjeC = lBlocks.GetInst();
  111. qjeC.table = tb_sys_user2role.TABLENAME;
  112. qjeC.jointype = QueryJsonElement.LEFT_JOIN;
  113. qjeC.displaycols = new List<string>() { tb_sys_user2role.CN_ROLE_UID, tb_sys_user2role.CN_STATUS_FLAG };
  114. qjeC.joincols = new Dictionary<string, string>()
  115. { {tb_sys_user2role.CN_USER_UID, tb_sys_user.CN_UID } };
  116. qjeC.jointable = qjeA;
  117. //qjeC.wherecols = new WhereNode(tb_sys_user2role.CN_STATUS_FLAG, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_user2role), BLWording.STATUS_FLAG_ON);
  118. lBlocks.Add(qjeC);
  119. // QueryJsonElement qjeD = lBlocks.GetInst();
  120. // qjeD.table = tb_sys_role2org.TABLENAME;
  121. // qjeD.jointype = QueryJsonElement.LEFT_JOIN;
  122. // qjeD.joincols = new Dictionary<string, string>()
  123. //{ {tb_sys_role2org.CN_ROLE_UID, tb_sys_user2role.CN_ROLE_UID } };
  124. // qjeD.jointable = qjeC;
  125. // qjeD.wherecols = new WhereNode(WhereNode.ENodeOperation.ENO_AND,
  126. // new WhereNode(tb_sys_role2org.CN_ORG_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_role2org), dicLoginInfo[BLWording.ORGANIZATION]),
  127. // new WhereNode(tb_sys_role2org.CN_STATUS_FLAG, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_role2org), BLWording.STATUS_FLAG_ON));
  128. // lBlocks.Add(qjeD);
  129. sMsg = MakeSelectJoinByBlocks(lBlocks, out Command cSelect);
  130. ArsenalInterface ai = ArsenalDBMgr.GetInst(cSelect);
  131. List<tb_sys_user2role> lRes = ai.RunQueryList<tb_sys_user2role>(cSelect, null);
  132. //QueryDataSet lRes = ai.RunQueryDataSet(cSelect, null);
  133. if (!cSelect.IsSuccess)
  134. {
  135. break;
  136. }
  137. if (!lRes.Any())
  138. {
  139. sMsg = BseMessageWording.NO_MATCHED_ACCOUNT;
  140. break;
  141. }
  142. string sRoleName = "NoRole";
  143. if (!string.IsNullOrEmpty(lRes[0].role_uid) && lRes[0].status_flag == BLWording.STATUS_FLAG_ON)
  144. {
  145. tb_sys_role rData = new tb_sys_role();
  146. rData.SetDirty(tb_sys_role.CN_NAME);
  147. tb_sys_role rCond = new tb_sys_role() { uid = lRes[0].role_uid };
  148. Command cSelectRoleName = Command.SetupSelectCmd(rData, rCond);
  149. List<tb_sys_role> lRole = ai.RunQueryList<tb_sys_role>(cSelectRoleName, null);
  150. sRoleName = lRole[0].name;
  151. }
  152. List<Command> lCmds = new List<Command>();
  153. DateTime dtNow = DateTime.Now.AddMinutes(-1 * Convert.ToInt32(CustomizeDBMgr.SettingData[BLWording.TOKEN_EXP_MINUTE], CultureInfo.CurrentCulture));
  154. WhereNode wn = new WhereNode(tb_sys_session.CN_UPDATE_DATE, WhereNode.EColumnOperation.EOT_LT, typeof(tb_sys_session), dtNow);
  155. Command cDelete = Command.SetupDeleteCmd(wn);
  156. lCmds.Add(cDelete);
  157. tb_sys_session s = new tb_sys_session();
  158. s.create_user_uid = s.update_user_uid = lRes[0].uid;
  159. s.create_org_uid = s.update_org_uid = dicLoginInfo[BLWording.ORGANIZATION].ToString();
  160. s.role_name = sRoleName;
  161. s.uid = Guid.NewGuid().ToString();
  162. Command cInsert = Command.SetupInsertCmd(s);
  163. lCmds.Add(cInsert);
  164. ai.RunEditCmds(lCmds);
  165. if (!cInsert.IsSuccess)
  166. {
  167. sMsg = cInsert.LastErrorCode;
  168. break;
  169. }
  170. // Privilidges
  171. List<string> lRoles = new List<string>();
  172. foreach (tb_sys_user2role r in lRes)
  173. {
  174. lRoles.Add(r.role_uid);
  175. }
  176. lBlocks.Clear();
  177. QueryJsonElement qjeAp = lBlocks.GetInst();
  178. qjeAp.table = tb_sys_program2action_grant.TABLENAME;
  179. qjeAp.displaycols = new List<string>() { tb_sys_program2action_grant.CN_PROGRAM2ACTION_UID, tb_sys_program2action_grant.CN_UID, tb_sys_program2action_grant.CN_GRANT_UID };
  180. qjeAp.wherecols = new WhereNode(WhereNode.ENodeOperation.ENO_AND,
  181. new WhereNode(tb_sys_program2action_grant.CN_GRANT_TYPE, WhereNode.EColumnOperation.EOT_IN, typeof(tb_sys_program2action_grant), BLWording.ROLE_ID),
  182. new WhereNode(tb_sys_program2action_grant.CN_GRANT_UID, WhereNode.EColumnOperation.EOT_IN, typeof(tb_sys_program2action_grant), lRoles.ToArray()),
  183. new WhereNode(tb_sys_program2action_grant.CN_STATUS_FLAG, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_program2action_grant), BLWording.STATUS_FLAG_ON)
  184. );
  185. lBlocks.Add(qjeAp);
  186. QueryJsonElement qjeBp = lBlocks.GetInst();
  187. qjeBp.table = tb_sys_program2action.TABLENAME;
  188. qjeBp.aliascols = new Dictionary<string, List<string>>() { { tb_sys_program2action.CN_ACTION_NAME, new List<string>() { "action_name" } } };
  189. qjeBp.jointype = QueryJsonElement.LEFT_JOIN;
  190. qjeBp.jointable = qjeAp;
  191. qjeBp.joincols = new Dictionary<string, string>() {
  192. { tb_sys_program2action.CN_UID,tb_sys_program2action_grant.CN_PROGRAM2ACTION_UID }};
  193. lBlocks.Add(qjeBp);
  194. QueryJsonElement qjeCp = lBlocks.GetInst();
  195. qjeCp.table = tb_sys_program.TABLENAME;
  196. qjeCp.displaycols = new List<string>() { tb_sys_program.CN_ROUT_PATH, tb_sys_program.CN_MODULE_UID, tb_sys_program.CN_SEQ, tb_sys_program.CN_ICON, tb_sys_program.CN_MENUDISPLAY };
  197. qjeCp.aliascols = new Dictionary<string, List<string>>() { { tb_sys_program.CN_NAME, new List<string>() { "program_name" } }, { tb_sys_program.CN_UID, new List<string>() { "program_id" } } };
  198. qjeCp.jointype = QueryJsonElement.LEFT_JOIN;
  199. qjeCp.joincols = new Dictionary<string, string>()
  200. { {tb_sys_program.CN_UID, tb_sys_program2action.CN_PROGRAM_UID } };
  201. qjeCp.jointable = qjeBp;
  202. qjeCp.wherecols = new WhereNode(tb_sys_program.CN_STATUS_FLAG, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_program), BLWording.STATUS_FLAG_ON);
  203. lBlocks.Add(qjeCp);
  204. QueryJsonElement qjeDp = lBlocks.GetInst();
  205. qjeDp.table = tb_sys_role.TABLENAME;
  206. qjeDp.aliascols = new Dictionary<string, List<string>>() { { tb_sys_role.CN_NAME, new List<string>() { "role_name" } } };
  207. qjeDp.jointype = QueryJsonElement.LEFT_JOIN;
  208. qjeDp.joincols = new Dictionary<string, string>()
  209. { {tb_sys_role.CN_UID, tb_sys_program2action_grant.CN_GRANT_UID } };
  210. qjeDp.jointable = qjeAp;
  211. lBlocks.Add(qjeDp);
  212. qjeAp.ordercols = new List<Tuple<QueryJsonElement, string, string>>() {
  213. Tuple.Create<QueryJsonElement, string, string>(qjeCp, tb_sys_program.CN_SEQ, BLWording.ORDER_ASC),
  214. Tuple.Create<QueryJsonElement, string, string>(qjeBp, tb_sys_program2action.CN_SEQUENCE, BLWording.ORDER_ASC)
  215. };
  216. sMsg = MakeSelectJoinByBlocks(lBlocks, out cSelect);
  217. QueryDataSet qds = ai.RunQueryDataSet(cSelect, null);
  218. lBlocks.Clear();
  219. QueryJsonElement qjeUser = lBlocks.GetInst();
  220. qjeUser.table = tb_sys_program2action_grant.TABLENAME;
  221. qjeUser.displaycols = new List<string>() { tb_sys_program2action_grant.CN_PROGRAM2ACTION_UID, tb_sys_program2action_grant.CN_UID, tb_sys_program2action_grant.CN_GRANT_UID };
  222. qjeUser.wherecols = new WhereNode(WhereNode.ENodeOperation.ENO_AND,
  223. new WhereNode(tb_sys_program2action_grant.CN_GRANT_TYPE, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_program2action_grant), BLWording.USER_ID),
  224. new WhereNode(tb_sys_program2action_grant.CN_GRANT_UID, WhereNode.EColumnOperation.EOT_IN, typeof(tb_sys_program2action_grant), lRes.Select(x => x.user_uid).FirstOrDefault()),
  225. new WhereNode(tb_sys_program2action_grant.CN_STATUS_FLAG, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_program2action_grant), BLWording.STATUS_FLAG_ON)
  226. );
  227. lBlocks.Add(qjeUser);
  228. QueryJsonElement qjeUserA = lBlocks.GetInst();
  229. qjeUserA.table = tb_sys_program2action.TABLENAME;
  230. qjeUserA.aliascols = new Dictionary<string, List<string>>() { { tb_sys_program2action.CN_ACTION_NAME, new List<string>() { "action_name" } } };
  231. qjeUserA.jointype = QueryJsonElement.LEFT_JOIN;
  232. qjeUserA.jointable = qjeUser;
  233. qjeUserA.joincols = new Dictionary<string, string>() {
  234. { tb_sys_program2action.CN_UID,tb_sys_program2action_grant.CN_PROGRAM2ACTION_UID }};
  235. lBlocks.Add(qjeUserA);
  236. QueryJsonElement qjeUserP = lBlocks.GetInst();
  237. qjeUserP.table = tb_sys_program.TABLENAME;
  238. qjeUserP.displaycols = new List<string>() { tb_sys_program.CN_ROUT_PATH, tb_sys_program.CN_MODULE_UID, tb_sys_program.CN_SEQ, tb_sys_program.CN_ICON, tb_sys_program.CN_MENUDISPLAY };
  239. qjeUserP.aliascols = new Dictionary<string, List<string>>() { { tb_sys_program.CN_NAME, new List<string>() { "program_name" } }, { tb_sys_program.CN_UID, new List<string>() { "program_id" } } };
  240. qjeUserP.jointype = QueryJsonElement.LEFT_JOIN;
  241. qjeUserP.joincols = new Dictionary<string, string>()
  242. { {tb_sys_program.CN_UID, tb_sys_program2action.CN_PROGRAM_UID } };
  243. qjeUserP.jointable = qjeUserA;
  244. qjeUserP.wherecols = new WhereNode(tb_sys_program.CN_STATUS_FLAG, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_program), "1");
  245. lBlocks.Add(qjeUserP);
  246. qjeUser.ordercols = new List<Tuple<QueryJsonElement, string, string>>() {
  247. Tuple.Create(qjeUserP, tb_sys_program.CN_SEQ, BLWording.ORDER_ASC),
  248. Tuple.Create(qjeUserA, tb_sys_program2action.CN_SEQUENCE, BLWording.ORDER_ASC)
  249. };
  250. cSelect = null;
  251. sMsg = MakeSelectJoinByBlocks(lBlocks, out cSelect);
  252. QueryDataSet qdsUser = ai.RunQueryDataSet(cSelect, null);
  253. var roleData = new QueryResponse(qds).Records;
  254. var userData = new QueryResponse(qdsUser).Records;
  255. var qrsPrivilidge = roleData.Union(userData).ToList();
  256. crmRes = new CSuccessResponseMessage(null, i_crmInput);
  257. crmRes.param[BLWording.TOKEN] = s.uid;
  258. crmRes.param[BLWording.PRIVILEGES] = qrsPrivilidge;
  259. crmRes.param["rolename"] = sRoleName;
  260. }
  261. }
  262. while (false);
  263. if (sMsg != null)
  264. {
  265. crmRes = new CErrorResponseMessage(sMsg, i_crmInput);
  266. }
  267. return crmRes;
  268. }
  269. private class VarificationModel
  270. {
  271. public string ip_address { get; set; }
  272. public string create_user_uid { get; set; }
  273. public string branch_name { get; set; }
  274. }
  275. public CResponseMessage VerifyIPAddress(CRequestMessage i_crmInput)
  276. {
  277. string sMsg = null;
  278. CResponseMessage crmRes = null;
  279. do
  280. {
  281. List<Dictionary<string, object>> lDic = getQryParameterList(i_crmInput);
  282. var receive_branch_uid = ProjectHelper.GetLoginUser(i_crmInput).receive_branch_uid;
  283. if (lDic != null && lDic.Count == 1)
  284. {
  285. if (i_crmInput == null)
  286. {
  287. throw new ArgumentNullException(nameof(i_crmInput));
  288. }
  289. var token = i_crmInput.token;
  290. if (!string.IsNullOrEmpty(token))
  291. {
  292. QueryJsonElementCollection lBlocks = new QueryJsonElementCollection();
  293. QueryJsonElement qjeSession = lBlocks.GetInst();
  294. qjeSession.table = tb_sys_session.TABLENAME;
  295. qjeSession.wherecols = new WhereNode(tb_sys_session.CN_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_session), token);
  296. qjeSession.displaycols = new List<string> { tb_sys_session.CN_CREATE_USER_UID };
  297. // 人員資訊
  298. QueryJsonElement qjeEmployee = lBlocks.GetInst();
  299. qjeEmployee.table = tb_hr_employee.TABLENAME;
  300. qjeEmployee.jointype = QueryJsonElement.JOIN;
  301. qjeEmployee.joincols = new Dictionary<string, string>() { { tb_hr_employee.CN_UID, tb_sys_session.CN_CREATE_USER_UID } };
  302. qjeEmployee.jointable = qjeSession;
  303. // 所屬社團
  304. QueryJsonElement qjeEmp2Branch = lBlocks.GetInst();
  305. qjeEmp2Branch.table = tb_hr_employee2branch.TABLENAME;
  306. qjeEmp2Branch.jointype = QueryJsonElement.JOIN;
  307. qjeEmp2Branch.joincols = new Dictionary<string, string>() { { tb_hr_employee2branch.CN_UID, tb_hr_employee.CN_UID } };
  308. qjeEmp2Branch.jointable = qjeEmployee;
  309. // 社團資訊
  310. QueryJsonElement qjeBranch = lBlocks.GetInst();
  311. qjeBranch.table = tb_grp_branch.TABLENAME;
  312. qjeBranch.jointype = QueryJsonElement.JOIN;
  313. qjeBranch.joincols = new Dictionary<string, string>() { { tb_grp_branch.CN_UID, tb_hr_employee2branch.CN_BRANCH_UID } };
  314. qjeBranch.wherecols = new WhereNode(tb_grp_branch.CN_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_grp_branch), receive_branch_uid);
  315. qjeBranch.jointable = qjeEmp2Branch;
  316. qjeBranch.displaycols = new List<string>() {
  317. tb_grp_branch.CN_BRANCH_NAME,
  318. tb_grp_branch.CN_IP_ADDRESS,
  319. };
  320. lBlocks.Add(qjeSession);
  321. lBlocks.Add(qjeEmployee);
  322. lBlocks.Add(qjeEmp2Branch);
  323. lBlocks.Add(qjeBranch);
  324. sMsg = MakeSelectJoinByBlocks(lBlocks, out Command cRes);
  325. ArsenalInterface ai = ArsenalDBMgr.GetInst(cRes);
  326. var qds = ai.RunQueryList<VarificationModel>(cRes);
  327. List<string> ipList = new List<string>();
  328. foreach (var item in qds)
  329. {
  330. if (!string.IsNullOrEmpty(item.ip_address))
  331. {
  332. ipList.AddRange(item.ip_address.Split(',').ToList());
  333. }
  334. }
  335. var count = ipList.Count(x => x == i_crmInput.clientip);
  336. Logger.Info("IP_Address = " + i_crmInput.clientip);
  337. if (ipList.Count > 0 && count < 1 && i_crmInput.clientip != "::1" && i_crmInput.clientip != "127.0.0.1")
  338. {
  339. sMsg = "IP address invalid";
  340. break;
  341. }
  342. }
  343. crmRes = new CSuccessResponseMessage(null, i_crmInput);
  344. }
  345. }
  346. while (false);
  347. if (sMsg != null)
  348. {
  349. crmRes = new CErrorResponseMessage(sMsg, i_crmInput);
  350. }
  351. return crmRes;
  352. }
  353. }
  354. }