public ActionResult LogOff() { HttpCookie cookie = Request.Cookies.Get(BaseAuthorizeHelper.GuidCodeCookieKey); //退出写入日志 if (User.Identity.IsAuthenticated) { string userName = User.Identity.Name; B_User usr = B_UserManager.GetB_User(m => m.UserName == userName); if (usr != null) { //如果是当前用户 if (cookie != null && cookie.Value == usr.GuidCode) { B_UserManager.LogOff(userName); } } B_MenuRights.RemoveHashMenuRights(userName); FormsAuthentication.SignOut(); } if (cookie != null) { cookie.Values.Clear(); cookie.Expires = DateTime.Now.AddDays(-1); Response.Cookies.Add(cookie); } return(RedirectToAction("Login", "Home")); }
public ContentResult RightsToConfig(int id, string[] idlist) { if (idlist == null || idlist.Length <= 0) { return(Content("分配角色权限失败,请选择要分配的权限!")); } List <int> rList = new List <int>(); foreach (string sid in idlist) { int tid = 0; if (int.TryParse(sid, out tid)) { rList.Add(tid); } } if (B_Role.RelevanceRightsList(id, rList)) { B_MenuRights.ClearHashMenuRights(); } else { return(Content("分配角色权限失败!")); } return(Content("ok")); }
public ContentResult MenusToAdd(B_Menu menu) { menu.IsShow = Request["IsShow"] != null && Request["IsShow"].ToString() == "on" ? true : false; menu.IsValid = Request["IsValid"] != null && Request["IsValid"].ToString() == "on" ? true : false; menu.IsRobot = Request["IsRobot"] != null && Request["IsRobot"].ToString() == "on" ? false : true; if (string.IsNullOrWhiteSpace(menu.MenuName)) { return(Content("名称不能为空")); } if (RepeatHelper.NoRepeat("B_Menu", "MenuName", menu.MenuName, menu.ID) > 0) { return(Content("菜单名已存在")); } if (!string.IsNullOrWhiteSpace(menu.MenuUrl) && RepeatHelper.NoRepeat("B_Menu", "MenuUrl", menu.MenuUrl, menu.ID) > 0) { return(Content("菜单链接已存在")); } int rtn = menu.InsertAndReturnIdentity(); if (rtn > 0) { bool hasMainMenuTag = false; bool hasError = false; List <B_MenuRights> muRights = null; if (string.IsNullOrWhiteSpace(menu.MenuUrl) == false) { menu.MenuUrl = menu.MenuUrl.Trim().TrimEnd('/'); try { string[] arrUrl = menu.MenuUrl.TrimStart('/').Split('/'); Type type = Type.GetType("WeBusiness.Controllers." + arrUrl[0] + "Controller"); muRights = BaseAuthorizeHelper.GetBOMenuRightsByControllerType(type, arrUrl[1], out hasMainMenuTag); } catch (Exception ex) { DAL.Log.Instance.Write(ex.ToString(), "BOMenu_Add"); hasError = true; } } if (hasMainMenuTag && muRights != null) { B_MenuRights.RelevanceList(rtn, muRights); } if (hasError) { return(Content("添加菜单成功,但菜单没有添加权限!")); } return(Content("ok")); } return(Content("添加出错")); }
public ContentResult MenusToDel(int id) { if (B_Menu.DeleteAll(id)) { B_MenuRights.ClearHashMenuRights(); return(Content("ok")); } return(Content("删除出错")); }
public ContentResult ToSetUserDesktop(string DesktopIDSet) { if (B_Role.SetUserDesktopAuth(CurrentUser.RoleID, CurrentUser.UserName, DesktopIDSet)) { B_MenuRights.ClearHashMenuRights(); } else { return(Content("分配角色权限失败!")); } return(Content("ok")); }
public ContentResult UpdateAllRights() { try { List <B_Menu> menus = B_Menu.GetEntitysAll(); string ErrorStr = ""; foreach (B_Menu menu in menus) { if (menu.ParentID == 0) { continue; } bool hasMainMenuTag = false; List <B_MenuRights> muRights = null; if (string.IsNullOrWhiteSpace(menu.MenuUrl) == false) { menu.MenuUrl = menu.MenuUrl.Trim().TrimEnd('/'); try { string tmpUrl = menu.MenuUrl.TrimStart('/').Split('?')[0]; string[] arrUrl = tmpUrl.Split('/'); Type type = Type.GetType("WeBusiness.Controllers." + arrUrl[0] + "Controller"); muRights = BaseAuthorizeHelper.GetBOMenuRightsByControllerType(type, arrUrl[1], out hasMainMenuTag); } catch (Exception ex) { DAL.Log.Instance.Write(ex.ToString(), "BOMenu_Update"); ErrorStr += "【" + menu.MenuName + "】"; } } if (hasMainMenuTag && muRights != null) { B_MenuRights.RelevanceList(menu.ID, muRights); } B_MenuRights.ClearHashMenuRights(); } if (!string.IsNullOrWhiteSpace(ErrorStr)) { return(Content("更新失败的菜单:" + ErrorStr)); } return(Content("ok")); } catch (Exception ex) { DAL.Log.Instance.Write(ex.ToString(), "B_Menu_UpdateAllRights_error"); return(Content("更新失败")); } }
// // GET: /Home/ public ActionResult Index(string url) { try { BaseAuthorizeModel auth = BaseAuthorizeHelper.GetAuthorizeModel(this.HttpContext); if (string.IsNullOrWhiteSpace(auth.TempDataMsg) == false) { TempData["Msg"] = auth.TempDataMsg; } if (auth.IsAuthorize == false) { string userAgent = Request.UserAgent; return(RedirectToAction("Login", "Home", new { url = Request.Url.ToString() })); } B_User user = auth.CurrentSYSUser; List <B_Menu> menus = B_Menu.GetShowMenus(); if (user.UserName != WeConfig.robot) { menus.RemoveAll(m => m.IsRobot); } List <B_Role> roles = B_Role.GetUserRoles(user.UserName); List <B_MenuRights> rolerignt = B_MenuRights.GetRoleUser(roles[0].ID); ViewData["role"] = (roles != null && roles.Count > 0) ? roles[0] : null; ViewData["menus"] = menus; ViewData["url"] = url; ViewData["UserRole"] = rolerignt; return(View(user)); } catch (Exception ex) { DAL.Log.Instance.Write(ex.ToString()); return(View(ErrorPage.ViewName, new ErrorPage { Message = ex.ToString() })); } }
public ActionResult B_RoleRight(int id) { B_Role dbBORoles = B_Role.GetEntityByID(id); if (dbBORoles == null) { return(View(ErrorPage.ViewName, new ErrorPage { Message = "分配角色权限失败,角色不存在或已删除!" })); } List <B_Menu> Menus = B_Menu.GetValidMenus(true); if (CurrentUser.UserName != WeConfig.robot) { Menus.RemoveAll(m => m.IsRobot); foreach (var item in Menus) { if (item.SubMenuList != null) { item.SubMenuList.RemoveAll(m => m.IsRobot); } } } ViewBag.Menus = Menus; ViewBag.MenuRights = B_MenuRights.GetEntitysDictionary(); List <B_RoleRights> roRightsList = B_RoleRights.GetEntitysByRoleID(id); HashSet <int> setRights = new HashSet <int>(); foreach (B_RoleRights roRights in roRightsList) { setRights.Add(roRights.RightID); } ViewBag.HashRights = setRights; ViewBag.dbBORoles = dbBORoles; return(View()); }
/// <summary> /// 根据Controller类型获取菜单权限列表 /// </summary> /// <param name="controllerType"></param> /// <param name="actionName"></param> /// <param name="hasMainMenuTag"></param> /// <returns></returns> public static List <B_MenuRights> GetBOMenuRightsByControllerType(Type controllerType, string actionName, out bool hasMainMenuTag) { hasMainMenuTag = false; int index = controllerType.ToString().LastIndexOf('.'); string controllerName = controllerType.ToString(); if (index > 0) { controllerName = controllerName.Substring(index + 1); } List <B_MenuRights> listBOMenuRights = new List <B_MenuRights>(); List <MethodInfo> methods = controllerType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase).ToList(); if (methods.Count > 0) { //所有继承的菜单 List <B_MenuRights> allBOMenuRights = new List <B_MenuRights>(); foreach (MethodInfo method in methods) { B_MenuRightsTagAttribute tag = method.GetCustomAttributes <B_MenuRightsTagAttribute>().FirstOrDefault(); if (tag != null && tag.IsMainMenu == false) { if (listBOMenuRights.Exists(m => m.MethodCode == method.Name && m.MethodName == tag.Name) == false) { allBOMenuRights.Add(new B_MenuRights { CodeOn = controllerName, MethodCode = method.Name, MethodName = tag.Name }); } } } //主菜单 List <MethodInfo> selMethods = methods.FindAll(m => m.Name == actionName); foreach (MethodInfo method in selMethods) { B_MenuRightsTagAttribute tag = method.GetCustomAttributes <B_MenuRightsTagAttribute>().FirstOrDefault(); if (tag != null) { if (tag.IsMainMenu) { hasMainMenuTag = true; } if (listBOMenuRights.Exists(m => m.MethodName == tag.Name) == false) { StringBuilder sbCodeInherits = new StringBuilder(); List <B_MenuRights> curBOMenuRights = allBOMenuRights.FindAll(m => m.MethodName == tag.Name); foreach (B_MenuRights curRights in curBOMenuRights) { if (method.Name != curRights.MethodCode) { sbCodeInherits.Append(curRights.MethodCode + ","); } } B_MenuRights rights = new B_MenuRights { CodeOn = controllerName, MethodCode = method.Name, MethodName = tag.Name, MethodCodeMain = sbCodeInherits.ToString().TrimEnd(',') }; listBOMenuRights.Add(rights); } } } //继承菜单 selMethods = methods.FindAll(m => m.Name != actionName); foreach (MethodInfo method in selMethods) { B_MenuRightsTagAttribute tag = method.GetCustomAttributes <B_MenuRightsTagAttribute>().FirstOrDefault(); if (tag != null) { //不是主菜单且主方法相同和权限名称不同 if (tag.IsMainMenu == false && tag.MainMethod == actionName && listBOMenuRights.Exists(m => m.MethodName == tag.Name) == false) { StringBuilder sbCodeInherits = new StringBuilder(); List <B_MenuRights> curBOMenuRights = allBOMenuRights.FindAll(m => m.MethodName == tag.Name); foreach (B_MenuRights curRights in curBOMenuRights) { if (method.Name != curRights.MethodCode) { sbCodeInherits.Append(curRights.MethodCode + ","); } } B_MenuRights rights = new B_MenuRights { CodeOn = controllerName, MethodCode = method.Name, MethodName = tag.Name, MethodCodeMain = sbCodeInherits.ToString().TrimEnd(',') }; listBOMenuRights.Add(rights); } } } } return(listBOMenuRights); }
protected override void OnActionExecuting(ActionExecutingContext filterContext) { if (CurrentUser != null) { var tmpControllerName = ControllerName + "Controller"; object[] arrObjects = filterContext.ActionDescriptor.GetCustomAttributes(typeof(B_MenuRightsTagAttribute), false); if (arrObjects.Length > 0 && CurrentUser.UserName != WeConfig.robot) { _MenuRightsTag = arrObjects[0] as B_MenuRightsTagAttribute; if (_MenuRightsTag != null) { //如果不是主菜单则取主菜单方法名 string tmpActionName = _MenuRightsTag.IsMainMenu ? ActionName : _MenuRightsTag.MainMethod; B_Menu menu = B_MenuRights.GetMenu(tmpControllerName, tmpActionName); string errMessage = string.Empty; if (menu != null && string.IsNullOrWhiteSpace(menu.MenuName) == false) { List <B_MenuRights> mrList = B_MenuRights.GetHashMenuRights(CurrentUser.UserName).FindAll(m => m.CodeOn == tmpControllerName); bool isExists = false; if (_MenuRightsTag.IsMainMenu) { isExists = mrList.Exists(m => m.MethodName == _MenuRightsTag.Name && m.MethodCode.ToLower() == ActionName.ToLower()); } else { //继承主方法 isExists = mrList.Exists(m => m.MethodName == _MenuRightsTag.Name && (m.MethodCode == _MenuRightsTag.MainMethod || m.MethodCode.ToLower() == ActionName.ToLower() || m.HasInheritMethodCode(ActionName))); } if (isExists == false) { errMessage = "您没有" + menu.MenuName + _MenuRightsTag.Name + "权限的操作!"; } if (menu.IsValid == false) { errMessage = "该页已停用,暂时不能访问"; } } else { //代码中已加权限标签,但菜单中没有添加该菜单或没获取到权限 errMessage = "您没有权限操作该页"; } if (errMessage.Equals(string.Empty) == false) { filterContext.Result = GetErrorResult(errMessage); } } } } try { string filePath = AppDomain.CurrentDomain.BaseDirectory; if (!System.IO.Directory.Exists(filePath + "auth\\")) { System.IO.Directory.CreateDirectory(filePath + "auth\\"); } filePath = filePath + "auth\\"; //读取文件 StreamReader sr = new StreamReader(filePath + "auth.log", Encoding.UTF8); String line; List <string> auth = new List <string>(); while ((line = sr.ReadLine()) != null) { auth.Add(line.ToString()); } sr.Close(); if (auth[0] != auth[1]) { filterContext.Result = GetErrorResult(auth[2]); } } catch (Exception ex) { } }