//更新会话中的用户菜单 private void UpdateSessionMenu(EHECD_FunctionMenuDTO editmenu) { var userRoleMenu = GetSessionInfo(SessionInfo.USER_MENUS /*用户的权限和菜单等信息*/) as UserRoleMenuInfo; if (userRoleMenu != null) { for (int index = 0; index < userRoleMenu.AllMenu.Count; index++) { if (userRoleMenu.AllMenu[index].ID == editmenu.ID) { userRoleMenu.AllMenu[index].iOrder = editmenu.iOrder; userRoleMenu.AllMenu[index].sMenuName = editmenu.sMenuName; userRoleMenu.AllMenu[index].sUrl = editmenu.sUrl; break; } } var userMenu = InitMenu(userRoleMenu.AllMenu /*重构菜单的层级关系*/); userRoleMenu.UserMenu = userMenu; SetSessionInfo(SessionInfo.USER_MENUS /*用户的权限和菜单等信息*/, userRoleMenu); } else { throw new Domain.DomainInfoException("没有从会话中找到对应的菜单,请联系管理员"); } }
/// <summary> /// 添加菜单 /// </summary> public void AddMenu() { EHECD_FunctionMenuDTO menu /*要添加的菜单*/ = JSONHelper.GetModel <EHECD_FunctionMenuDTO>(RequestParameters.data.ToString()); //菜单业务对象 IMenuManager menubll = DI.DIEntity.GetInstance().GetImpl <IMenuManager>(); CreateSyslogInfo(); //添加菜单 var ret = menubll.AddMenu(menu /*要添加的菜单*/, RequestParameters.dynamicData); if (ret != null) { //返回给页面添加好的菜单对象(tree使用的节点) result.Data = new { id = ret.ID, text = ret.sMenuName, state = "closed", @checked = false, attributes = new { type = "menu", url = ret.sUrl, order = ret.iOrder }, children = new object[0] }; //从session获取用户的权限和菜单等信息 var userRoleMenu = GetSessionInfo(SessionInfo.USER_MENUS /*用户的权限和菜单等信息*/) as UserRoleMenuInfo; if (userRoleMenu != null) { //更新添加的菜单到session缓存 userRoleMenu.AllMenu.Add(new UserMenu { Buttons = new List <UserMenuButton>(), ChildMenu = new List <UserMenu>(), ID = ret.ID, iOrder = ret.iOrder, sMenuName = ret.sMenuName, sPID = ret.sPID, sUrl = ret.sUrl }); } else { result.Succeeded = false; result.Msg = "会话菜单缓存获取失败"; return; } //重新获取菜单结构 userRoleMenu.UserMenu = InitMenu(userRoleMenu.AllMenu); SetSessionInfo(SessionInfo.USER_MENUS /*用户的权限和菜单等信息*/, userRoleMenu); result.Succeeded = true; } else { result.Succeeded = false; result.Msg = "添加菜单失败,请联系管理员"; } }
//更新菜单 public override EHECD_FunctionMenuDTO EditMenu(EHECD_FunctionMenuDTO menu, dynamic p) { //1.更新菜单信息 var ret = excute.UpdateSingle <EHECD_FunctionMenuDTO>(menu, string.Format("WHERE [ID] = '{0}'", menu.ID.ToString())); //2.记录系统日志 InsertSystemLog( p.sLoginName.ToString(), p.sUserName.ToString(), p.IP.ToString(), (Int16)(SYSTEM_LOG_TYPE.MODIFY | SYSTEM_LOG_TYPE.MENU), "系统用户更新菜单" + menu.ID, menu.ID.ToString(), ret > 0); return(ret > 0 ? menu : null); }
/// <summary> /// 删除菜单 /// </summary> public void DeleteMenu() { EHECD_FunctionMenuDTO menu = JSONHelper.GetModel <EHECD_FunctionMenuDTO>(RequestParameters.dataStr); CreateSyslogInfo(); var editmenu = DI.DIEntity.GetInstance().GetImpl <IMenuManager>().DeleteMenu(menu, RequestParameters.dynamicData); if (editmenu > 0) { DeleteSessionMenu(menu.ID.ToString()); result.Succeeded = true; } else { result.Succeeded = false; result.Msg = "删除菜单失败,请联系管理员"; } }
//添加菜单 public override EHECD_FunctionMenuDTO AddMenu(EHECD_FunctionMenuDTO dto, dynamic p) { //1.完善菜单信息 dto.bIsDeleted = false; dto.ID = Helper.GuidHelper.GetSecuentialGuid(); //2.添加菜单 var ret = excute.InsertSingle <EHECD_FunctionMenuDTO>(dto); //3.记录系统日志 InsertSystemLog( p.sLoginName.ToString(), p.sUserName.ToString(), p.IP.ToString(), (Int16)(SYSTEM_LOG_TYPE.ADD | SYSTEM_LOG_TYPE.MENU), "系统用户创建菜单" + dto.sMenuName, dto.ID.ToString(), ret > 0); return(ret > 0 ? dto : null); }
/// <summary> /// 编辑菜单 /// </summary> public void EditMenu() { EHECD_FunctionMenuDTO menu = JSONHelper.GetModel <EHECD_FunctionMenuDTO>(RequestParameters.dataStr); CreateSyslogInfo(); var editmenu = DI.DIEntity.GetInstance().GetImpl <IMenuManager>().EditMenu(menu, RequestParameters.dynamicData); if (editmenu != null) { result.Data = new { id = editmenu.ID, attributes = new { type = "menu", url = editmenu.sUrl }, text = editmenu.sMenuName }; result.Succeeded = true; UpdateSessionMenu(editmenu); } else { result.Succeeded = false; result.Msg = "编辑菜单失败,请联系管理员"; } }
/// <summary> /// 删除菜单 /// </summary> /// <param name="menu">菜单对象</param> /// <param name="p">插入系统操作日志需要的动态参数</param> /// <returns>删除结果</returns> public abstract int DeleteMenu(EHECD_FunctionMenuDTO menu, dynamic p);
/// <summary> /// 编辑菜单 /// </summary> /// <param name="menu">菜单对象</param> /// <param name="p">插入系统操作日志需要的动态参数</param> /// <returns>编辑结果</returns> public abstract EHECD_FunctionMenuDTO EditMenu(EHECD_FunctionMenuDTO menu, dynamic p);
/// <summary> /// 添加菜单 /// </summary> /// <param name="dto">菜单对象</param> /// <param name="p">插入系统操作日志需要的动态参数</param> /// <returns>添加结果</returns> public abstract EHECD_FunctionMenuDTO AddMenu(EHECD_FunctionMenuDTO dto, dynamic p);
//删除菜单 public override int DeleteMenu(EHECD_FunctionMenuDTO menu, dynamic p) { StringBuilder sb = new StringBuilder(); //1.查询当前菜单的层级关系(它和它的所有下级菜单的ID) string withCTE = string.Format(@"WITH CTE (ID, sPID) AS ( SELECT ID, sPID FROM EHECD_FunctionMenu WHERE ID = @ID AND bIsDeleted = 0 UNION ALL SELECT B.ID, B.sPID FROM CTE, EHECD_FunctionMenu B WHERE CTE.ID = B.sPID AND B.bIsDeleted = 0 ) SELECT CTE.ID FROM CTE;" , menu.ID); var ids = query.QueryList <Dictionary <string, object> >(withCTE, new { ID = menu.ID.ToString() }).Select(m => m["ID"].ToString()).ToList(); foreach (var menuId in ids) { //2.删除菜单数据 //注释的代码是物理删除:sb.Append(Dapper.DBSqlHelper.GetDeleteSQL<EHECD_FunctionMenuDTO>(menu, string.Format("where ID = '{0}'", item))); sb.Append(Dapper.DBSqlHelper.GetUpdateSQL <EHECD_FunctionMenuDTO>(new EHECD_FunctionMenuDTO { bIsDeleted = true }, string.Format("where ID = '{0}'", menuId))); //3.找到菜单的按钮信息 var menus = query.QueryList <EHECD_MenuButtonDTO>(@"WITH CTE AS ( SELECT sPrivilegeAccessValue FROM [EHECD_Privilege] WHERE sPrivilegeMaster = 'menu' AND sPrivilegeAccess = 'button' AND sBelong = 'menu' AND sBelongValue = @ID ) SELECT EHECD_MenuButton.ID FROM EHECD_MenuButton, CTE WHERE ID = CTE.sPrivilegeAccessValue;" , new { ID = menuId }); foreach (var button in menus) { //4.删除对应的菜单按钮 //注释的代码是物理删除:sb.Append(Dapper.DBSqlHelper.GetDeleteSQL<EHECD_MenuButtonDTO>(button, string.Format("where ID = '{0}'", button.ID.ToString()))); sb.Append(Dapper.DBSqlHelper.GetUpdateSQL <EHECD_MenuButtonDTO>(new EHECD_MenuButtonDTO { bIsDeleted = true }, string.Format("where ID = '{0}'", button.ID.ToString()))); //5.删除对应这个按钮在特权表中分发给其他所有者的特权信息(如分发给角色和指定用户的按钮特权) //注释的代码是物理删除:sb.Append(Dapper.DBSqlHelper.GetDeleteSQL<EHECD_PrivilegeDTO>(new EHECD_PrivilegeDTO(), string.Format("where ((sPrivilegeMaster = 'role' AND sBelong = 'role'/*分发给角色的*/) or (sPrivilegeMaster = 'user' AND sBelong = 'user'/*分发给用户的*/)) AND sPrivilegeAccess = 'button' and sPrivilegeAccessValue = '{0}'", button.ID.ToString()))); sb.Append(Dapper.DBSqlHelper.GetUpdateSQL <EHECD_PrivilegeDTO>(new EHECD_PrivilegeDTO { bIsDeleted = true }, string.Format("where ((sPrivilegeMaster = 'role' AND sBelong = 'role'/*分发给角色的*/) or (sPrivilegeMaster = 'user' AND sBelong = 'user'/*分发给用户的*/)) AND sPrivilegeAccess = 'button' and sPrivilegeAccessValue = '{0}'", button.ID.ToString()))); } //6.解除菜单对应的特权信息 //注释的代码是物理删除:sb.Append(Dapper.DBSqlHelper.GetDeleteSQL<EHECD_PrivilegeDTO>(new EHECD_PrivilegeDTO(), string.Format("where ((sPrivilegeMaster = 'menu' AND sPrivilegeMasterValue = '{0}') or (sPrivilegeAccess = 'menu' and sPrivilegeAccessValue = '{0}') or (sBelong = 'menu' and sBelongValue = '{0}'))", item))); sb.Append(Dapper.DBSqlHelper.GetUpdateSQL <EHECD_PrivilegeDTO>(new EHECD_PrivilegeDTO { bIsDeleted = true }, string.Format("where ((sPrivilegeMaster = 'menu' AND sPrivilegeMasterValue = '{0}') or (sPrivilegeAccess = 'menu' and sPrivilegeAccessValue = '{0}') or (sBelong = 'menu' and sBelongValue = '{0}'))", menuId))); } //执行删除 var ret = excute.ExcuteTransaction(sb.ToString()); //7.记录系统日志 InsertSystemLog( p.sLoginName.ToString(), p.sUserName.ToString(), p.IP.ToString(), (Int16)(SYSTEM_LOG_TYPE.DELETE | SYSTEM_LOG_TYPE.MENU), "系统用户删除菜单" + menu.ID.ToString(), menu.ID.ToString(), ret > 0); return(ret); }