namespace CounsellorBL.BLStructure.SYS { using CounsellorBL.Common; using CounsellorBL.Helper; using MonumentDefine; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OT.COM.ArsenalDB; using OT.COM.LogisticsUtil; using OT.COM.SignalerMessage; using SoldierData.EnterprizeV4; using System; using System.Collections.Generic; using System.Linq; using static CounsellorBL.Common.EntityBaseExtension; public class AuthManageService : SingleDataTableTemplate { public AuthManageService() { dgReadCommandGenerator = readCommandGenerator; dgReadCommandPostDataHandler = readCommandPostDataHandler; dgUpdateCommandGenerator = updateCommandGenerator; } private class ResViewModel { public string uid { get; set; } = null; public string name { get; set; } = null; public string module_name { get; set; } = null; public string parent { get; set; } = null; public string module_UID { get; set; } = null; public string action_name { get; set; } = null; public string action_UID { get; set; } = null; public int sequence { get; set; } = 0; } protected string readCommandGenerator(CRequestMessage i_crmInput, JArray i_jaData, tb_sys_session i_sSessionUser, out Command o_c, [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0, [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "", [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "") { Command cRes = null; string sMsg; try { do { QueryJsonElementCollection lBlocks = new QueryJsonElementCollection(); QueryJsonElement qjeA = lBlocks.GetInst(); qjeA.table = tb_sys_program.TABLENAME; qjeA.displaycols = new List() { tb_sys_program.CN_UID, tb_sys_program.CN_NAME, tb_sys_program.CN_SEQ }; QueryJsonElement qjeB = lBlocks.GetInst(); qjeB.table = tb_sys_module.TABLENAME; qjeB.jointype = QueryJsonElement.JOIN; qjeB.jointable = qjeA; qjeB.joincols = new Dictionary() { { tb_sys_module.CN_UID,tb_sys_program.CN_MODULE_UID }}; qjeB.aliascols = new Dictionary>() { { tb_sys_module .CN_UID, new List(){ "module_UID" } } ,{ tb_sys_module.CN_NAME, new List(){ "module_name" } }}; qjeB.displaycols = new List() { tb_sys_module.CN_CODE, tb_sys_module.CN_SEQ, tb_sys_module.CN_PARENT }; QueryJsonElement qjeC = lBlocks.GetInst(); qjeC.table = tb_sys_program2action.TABLENAME; qjeC.jointype = QueryJsonElement.JOIN; qjeC.jointable = qjeA; qjeC.joincols = new Dictionary() { { tb_sys_program2action.CN_PROGRAM_UID,tb_sys_program.CN_UID }}; qjeC.aliascols = new Dictionary>() { { tb_sys_program2action .CN_UID, new List(){ "action_UID" } } }; qjeC.displaycols = new List() { tb_sys_program2action.CN_ACTION_NAME, tb_sys_program2action.CN_SEQUENCE }; lBlocks.Add(qjeA); lBlocks.Add(qjeB); lBlocks.Add(qjeC); sMsg = MakeSelectJoinByBlocks(lBlocks, out cRes); } while (false); } catch (Exception ex) { LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath); sMsg = $"{nameof(readCommandGenerator)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine})."; #if DEBUG System.Diagnostics.Debug.WriteLine(sMsg); #endif } o_c = cRes; return sMsg; } protected string readCommandPostDataHandler(CRequestMessage i_crmInput, ArsenalInterface i_aiArsenal, Command i_cCmd, JArray i_jaData, tb_sys_session i_sSessionUser, out object o_oReault, [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0, [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "", [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "") { string sMsg = null; object oResultData = null; try { do { List qds = i_aiArsenal.RunQueryList(i_cCmd); if (!i_cCmd.IsSuccess) { sMsg = i_cCmd.LastErrorCode; break; } string[] saCrudArray = { "btnQry", "btnAdd", "btnUpd", "btnDel" }; List lActionList = GetAction(); oResultData = qds .Select(x => new { x.uid, x.name, modulename = x.module_name, moduleparent = x.parent, moduleuid = x.module_UID, actionname = x.action_name, action = lActionList.Where(c => c.uid == x.action_name).Select(c => c.name).SingleOrDefault(), action_id = x.action_UID, x.sequence }) .GroupBy(x => new { x.uid, x.name, x.moduleuid, x.modulename, x.moduleparent }) .Select(x => new { x.Key.uid, x.Key.name, x.Key.moduleuid, x.Key.modulename, x.Key.moduleparent, view = x.Where(c => c.actionname.Trim() == saCrudArray[0]).Select(c => c.action_id).SingleOrDefault(), create = x.Where(c => c.actionname.Trim() == saCrudArray[1]).Select(c => c.action_id).SingleOrDefault(), edit = x.Where(c => c.actionname.Trim() == saCrudArray[2]).Select(c => c.action_id).SingleOrDefault(), delete = x.Where(c => c.actionname.Trim() == saCrudArray[3]).Select(c => c.action_id).SingleOrDefault(), other = x.Where(c => !saCrudArray.Contains(c.actionname.Trim())) .OrderBy(c => c.sequence) .Select(c => new { c.action, c.action_id }) }).ToList(); } while (false); } catch (Exception ex) { LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath); sMsg = $"{nameof(readCommandGenerator)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine})."; #if DEBUG System.Diagnostics.Debug.WriteLine(sMsg); #endif } o_oReault = oResultData; return sMsg; } private List GetAction() { // 查詢action tb_sys_action cData = new tb_sys_action(); cData.SetFullDirtyEx(EColumnFilter.ES_NO); Command cSelect = Command.SetupSelectCmd(cData); ArsenalInterface ai = ArsenalDBMgr.GetInst(cSelect); List qdAction = ai.RunQueryList(cSelect); return qdAction; } private QueryJsonElementCollection getProgramActionQJE(out QueryJsonElement o_qjeA, out QueryJsonElement o_qjeB) { QueryJsonElementCollection lBlocks = new QueryJsonElementCollection(); o_qjeA = lBlocks.GetInst(); o_qjeA.table = tb_sys_program.TABLENAME; o_qjeA.displaycols = new List() { tb_sys_program.CN_UID, tb_sys_program.CN_NAME }; o_qjeB = lBlocks.GetInst(); o_qjeB.table = tb_sys_program2action.TABLENAME; o_qjeB.jointype = QueryJsonElement.LEFT_JOIN; o_qjeB.jointable = o_qjeA; o_qjeB.joincols = new Dictionary() { { tb_sys_program2action.CN_PROGRAM_UID,tb_sys_program.CN_UID }}; o_qjeB.displaycols = new List() { tb_sys_program2action.CN_ACTION_NAME }; lBlocks.Add(o_qjeA); lBlocks.Add(o_qjeB); return lBlocks; } protected string getRoleAuthCommandGenerator(CRequestMessage i_crmInput, JArray i_jaData, tb_sys_session i_sSessionUser, out Command o_cCmd, [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0, [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "", [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "") { return getAuthCommandGeneratorBase(BLWording.ROLE_ID, i_jaData[0][BLWording.ROLE_ID].ToObject(), out o_cCmd); } public CResponseMessage GetRoleAuth(CRequestMessage i_crmInput) { return simpleQuery(i_crmInput, BLWording.QRY_MASTER, getRoleAuthCommandGenerator, actionsPostDataHandler); } protected string getUserAuthCommandGenerator(CRequestMessage i_crmInput, JArray i_jaData, tb_sys_session i_sSessionUser, out Command o_cCmd, [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0, [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "", [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "") { return getAuthCommandGeneratorBase(BLWording.USER_ID, i_jaData[0][BLWording.USER_ID].ToObject(), out o_cCmd); } protected string actionsPostDataHandler(CRequestMessage i_crmInput, ArsenalInterface i_aiArsenal, Command i_cCmd, JArray i_jaData, tb_sys_session i_sSessionUser, out object o_oReault, [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0, [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "", [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "") { string sMsg = null; object oResultData = null; try { do { List qds = i_aiArsenal.RunQueryList(i_cCmd); if (!i_cCmd.IsSuccess) { sMsg = i_cCmd.LastErrorCode; break; } string[] saCrudArray = { "btnQry", "btnAdd", "btnUpd", "btnDel" }; List lActionList = GetAction(); oResultData = qds.Select(x => new { x.uid, x.name, actionname = x.action_name, }) .GroupBy(x => x.uid) .Select(x => new { uid = x.Key, view = x.Any(c => c.actionname == saCrudArray[0]), create = x.Any(c => c.actionname == saCrudArray[1]), edit = x.Any(c => c.actionname == saCrudArray[2]), delete = x.Any(c => c.actionname == saCrudArray[3]), other = x.Where(c => !saCrudArray.Contains(c.actionname)).Select(c => lActionList.Where(z => z.uid == c.actionname).Select(z => z.name)) }).ToList(); } while (false); } catch (Exception ex) { LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath); sMsg = $"{nameof(actionsPostDataHandler)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine})."; #if DEBUG System.Diagnostics.Debug.WriteLine(sMsg); #endif } o_oReault = oResultData; return sMsg; } public CResponseMessage GetUserAuth(CRequestMessage i_crmInput) { return simpleQuery(i_crmInput, BLWording.QRY_MASTER, getUserAuthCommandGenerator, actionsPostDataHandler); } protected string getAuthCommandGeneratorBase(string i_sGrandType, string i_sGrandUID, out Command o_cCmd, [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0, [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "", [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "") { Command cRes = null; string sMsg; try { do { QueryJsonElementCollection lBlocks = getProgramActionQJE(out QueryJsonElement qjeA, out QueryJsonElement qjeB); QueryJsonElement qjeC = lBlocks.GetInst(); qjeC.table = tb_sys_program2action_grant.TABLENAME; qjeC.jointype = QueryJsonElement.LEFT_JOIN; qjeC.jointable = qjeB; qjeC.joincols = new Dictionary() { { tb_sys_program2action_grant.CN_PROGRAM2ACTION_UID,tb_sys_program2action.CN_UID } }; qjeC.displaycols = new List() { tb_sys_program2action_grant.CN_PROGRAM2ACTION_UID }; qjeC.wherecols = new WhereNode(WhereNode.ENodeOperation.ENO_AND, new WhereNode(tb_sys_program2action_grant.CN_GRANT_TYPE, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_program2action_grant), i_sGrandType), new WhereNode(tb_sys_program2action_grant.CN_GRANT_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_program2action_grant), i_sGrandUID) ); lBlocks.Add(qjeC); sMsg = MakeSelectJoinByBlocks(lBlocks, out cRes); } while (false); } catch (Exception ex) { LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath); sMsg = $"{nameof(getAuthCommandGeneratorBase)} unknwon exception. i_sGrandType={i_sGrandType}, i_sGrandUID={i_sGrandUID}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine})."; #if DEBUG System.Diagnostics.Debug.WriteLine(sMsg); #endif } o_cCmd = cRes; return sMsg; } protected string getDepAuthCommandGenerator(CRequestMessage i_crmInput, JArray i_jaData, tb_sys_session i_sSessionUser, out Command o_cCmd, [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0, [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "", [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "") { return getAuthCommandGeneratorBase(BLWording.DEPT_ID, i_jaData[0][BLWording.DEPT_ID].ToObject(), out o_cCmd); } // 部門權限 public CResponseMessage GetDepAuth(CRequestMessage i_crmInput) { return simpleQuery(i_crmInput, BLWording.QRY_MASTER, getDepAuthCommandGenerator, actionsPostDataHandler); } protected string updateCommandGenerator(CRequestMessage i_crmInput, JArray i_jaData, tb_sys_session i_sSessionUser, out List o_lcResult, List i_saQryContainKeys, [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0, [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "", [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "") { string sMsg = null; List lCmds = new List(); try { do { List data = new List(); foreach (JToken jtk in i_jaData) { data.Add(jtk[BLWording.DATA].ToString()); } QueryJsonElementCollection lBlocks = new QueryJsonElementCollection(); QueryJsonElement qjeOrigin = lBlocks.GetInst(); ArsenalInterface ai; string sGrand_type = null; if (i_crmInput.param.ContainsKey(BLWording.ROLE_ID)) { sGrand_type = BLWording.ROLE_ID; } else if (i_crmInput.param.ContainsKey(BLWording.DEPT_ID)) { sGrand_type = BLWording.DEPT_ID; } else if (i_crmInput.param.ContainsKey(BLWording.USER_ID)) { sGrand_type = BLWording.USER_ID; } if (sGrand_type == null) { sMsg = MessageWording.PARAM_NOT_EXPECTED; break; } string sGrand_id = i_crmInput.param[sGrand_type] as string; // 查詢原始資料 qjeOrigin.table = tb_sys_program2action_grant.TABLENAME; qjeOrigin.displaycols = new List() { tb_sys_program2action_grant.CN_UID, tb_sys_program2action_grant.CN_GRANT_TYPE, tb_sys_program2action_grant.CN_GRANT_UID, tb_sys_program2action_grant.CN_PROGRAM2ACTION_UID }; qjeOrigin.wherecols = new WhereNode(WhereNode.ENodeOperation.ENO_AND, new WhereNode(tb_sys_program2action_grant.CN_GRANT_TYPE, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_program2action_grant), sGrand_type), new WhereNode(tb_sys_program2action_grant.CN_GRANT_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_program2action_grant), sGrand_id)); lBlocks.Add(qjeOrigin); sMsg = MakeSelectJoinByBlocks(lBlocks, out Command cRes); if (sMsg != null) { break; } ai = ArsenalDBMgr.GetInst(cRes); List qds = ai.RunQueryList(cRes); if (!cRes.IsSuccess) { sMsg = cRes.LastErrorCode; break; } if (data.Any()) { var origin = qds.Select(x => x.program2action_uid).ToList(); var addIDs = data.Except(origin); var delIDs = origin.Except(data); if (addIDs.Any()) { foreach (string sUID in addIDs) { tb_sys_program2action_grant cNew = new tb_sys_program2action_grant() { grant_type = sGrand_type, grant_uid = sGrand_id, program2action_uid = sUID }; lCmds.Add(Command.SetupInsertCmd(cNew)); } } if (delIDs.Any()) { foreach (string sUID in delIDs) { tb_sys_program2action_grant cCond = new tb_sys_program2action_grant() { grant_type = sGrand_type, grant_uid = sGrand_id, program2action_uid = sUID }; lCmds.Add(Command.SetupDeleteCmd(cCond)); } } } else { tb_sys_program2action_grant cCond = new tb_sys_program2action_grant() { grant_type = sGrand_type, grant_uid = sGrand_id }; lCmds.Add(Command.SetupDeleteCmd(cCond)); } } while (false); } catch (Exception ex) { LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath); sMsg = $"{nameof(updateCommandGenerator)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine})."; #if DEBUG System.Diagnostics.Debug.WriteLine(sMsg); #endif } o_lcResult = lCmds; return sMsg; } protected string getRoleCommandGenerator(CRequestMessage i_crmInput, JArray i_jaData, tb_sys_session i_sSessionUser, out Command o_cCmd, [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0, [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "", [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "") { Command cRes = null; string sMsg = null; try { do { tb_sys_role cData = new tb_sys_role(); cData.SetFullDirty(); cRes = Command.SetupSelectCmd(cData); } while (false); } catch (Exception ex) { LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath); sMsg = $"{nameof(getRoleCommandGenerator)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine})."; #if DEBUG System.Diagnostics.Debug.WriteLine(sMsg); #endif } o_cCmd = cRes; return sMsg; } protected string getRoleCommandPostDataHandler(CRequestMessage i_crmInput, ArsenalInterface i_aiArsenal, Command i_cCmd, JArray i_jaData, tb_sys_session i_sSessionUser, out object o_oReault, [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0, [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "", [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "" ) { string sMsg = null; object oResultData = null; try { do { List qdRole = i_aiArsenal.RunQueryList(i_cCmd); Dictionary resp = new Dictionary(); foreach (var role in qdRole) { resp.Add(role.uid, role.name); } oResultData = resp; } while (false); } catch (Exception ex) { LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath); sMsg = $"{nameof(getRoleCommandPostDataHandler)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine})."; #if DEBUG System.Diagnostics.Debug.WriteLine(sMsg); #endif } o_oReault = oResultData; return sMsg; } /// /// 取得角色 /// /// /// [Auth(false)] public CResponseMessage GetRole(CRequestMessage i_crmInput) { return simpleQuery(i_crmInput, null, getRoleCommandGenerator, getRoleCommandPostDataHandler); } protected string getUserCommandGenerator(CRequestMessage i_crmInput, JArray i_jaData, tb_sys_session i_sSessionUser, out Command o_cCmd, [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0, [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "", [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "") { Command cRes = null; string sMsg = null; try { do { tb_sys_user cData = new tb_sys_user(); cData.SetFullDirty(); cRes = Command.SetupSelectCmd(cData); } while (false); } catch (Exception ex) { LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath); sMsg = $"{nameof(getUserCommandGenerator)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine})."; #if DEBUG System.Diagnostics.Debug.WriteLine(sMsg); #endif } o_cCmd = cRes; return sMsg; } protected string getUserCommandPostDataHandler(CRequestMessage i_crmInput, ArsenalInterface i_aiArsenal, Command i_cCmd, JArray i_jaData, tb_sys_session i_sSessionUser, out object o_oReault, [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0, [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "", [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "" ) { string sMsg = null; object oResultData = null; try { do { List qdUser = i_aiArsenal.RunQueryList(i_cCmd); if (qdUser.Count > 0) { Dictionary resp = new Dictionary(); foreach (var user in qdUser) { resp.Add(user.uid, user.account); } oResultData = resp; } } while (false); } catch (Exception ex) { LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath); sMsg = $"{nameof(getUserCommandPostDataHandler)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine})."; #if DEBUG System.Diagnostics.Debug.WriteLine(sMsg); #endif } o_oReault = oResultData; return sMsg; } [Auth(false)] public CResponseMessage GetUser(CRequestMessage i_crmInput) { return simpleQuery(i_crmInput, null, getUserCommandGenerator, getUserCommandPostDataHandler); } } }