今天把用户的菜单显示和页面的按钮显示都做好了,下面先来个效果图
接下来说下我实现的方法:
首先我在每个方法前面都加了这个属性,
表示当前Action请求为一个具体的功能页面 AdminActionMethod : Attribute { 页面请求路径 ActionUrl { get; set; } 页面操作代码 RoleCode { get; set; } 页面返回类型(0:返回页面,1返回json格式) ActionResultType { get; set; } }
View Code当不需要验证的时候只要设置RoleCode="NoNeedAuthory"就行。
然后每次在执行方法之间都会进入到
public class AdminAuthory : ActionFilterAttribute { OnActionExecuting(ActionExecutingContext filterContext) { skipAuthorization = filterContext.ActionDescriptor.IsDefined(typeof(NoNeedAdminAuthory), inherit: true) || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(NoNeedAdminAuthory), inherit: true); if (skipAuthorization) { return; } (!WebCookieHelper.AdminCheckLogin()) { filterContext.Result = , true); return; } (WebCookieHelper.GetAdminId(5) == 1) { return; } customAttributes = filterContext.ActionDescriptor.GetCustomAttributes(true); if (customAttributes != null && customAttributes.Length > 0) { for (int i = 0; i < customAttributes.Count(); i++) { ) {actionCode = (customAttributes[i] actionUrl = (customAttributes[i] actionResultType = (customAttributes[i] (actionCode == ) {; } else { //判断权限是否符合 List<AuthorDesign.Model.AdminPageAction> pageActionList = AdminMenuHelper.GetNowAdminMenu(); var pageSelect = pageActionList.Where(m => m.PageUrl == actionUrl); if (pageSelect != null && pageSelect.Count() > 0) { //判断有无执行该页面的权利 codeList = AdminMenuHelper.LoadActionCodeList(); codeSelect = codeList.Where(m => m.ActionCode == actionCode); if (codeSelect != null && codeSelect.Count() > 0) { int codeId = codeSelect.First().Id; JavaScriptSerializer serializer = new JavaScriptSerializer(); roleActionList = pageSelect.First().RoleActionList; List<AuthorDesign.Web.Areas.Admin.Models.RolePageActionModel> roleActionListModel = serializer.Deserialize<List<AuthorDesign.Web.Areas.Admin.Models.RolePageActionModel>>(roleActionList); if (roleActionListModel != null && roleActionListModel.Where(m => m.ActionId == codeId && m.actionChecked == 1).Count() > 0) { //判断CodeId在管理员动作列表中是否为选择状态 List<AuthorDesign.Web.Areas.Admin.Models.RolePageActionModel> adminActionListModel = serializer.Deserialize<List<AuthorDesign.Web.Areas.Admin.Models.RolePageActionModel>>(pageSelect.First().AdminActionList); if (adminActionListModel != null && adminActionListModel.Where(m => m.ActionId == codeId && m.actionChecked == 1).Count() > 0) { StringBuilder sb = new StringBuilder(); sb.Append(); (var item in roleActionListModel) { if (item.actionChecked == 1) { var needChangeAction = adminActionListModel.Where(m => m.ActionId == item.ActionId).FirstOrDefault(); if (needChangeAction == null) { item.actionChecked = 0; } else if (needChangeAction.actionChecked == 0) { item.actionChecked = 0; } } AuthorDesign.Model.PageAction OneAction = codeList.Where(m => m.Id == item.ActionId).FirstOrDefault(); sb.Append().Append().Append().Append().Append().Append().Append(OneAction == ).Append().Append().Append().Append().Append().Append(item.actionChecked).Append().Append(); } sb.Remove(sb.Length - 1, 1); sb.Append(); filterContext.Controller.ViewBag.CanOperationActionList = sb.ToString(); } else { if (actionResultType == 0) { filterContext.Result = , true); } else if (actionResultType == 1) { filterContext.Result = , message = } }; } return; } } else { if (actionResultType == 0) { filterContext.Result = , true); } else if (actionResultType == 1) { filterContext.Result = , message = } }; } return; } } else { if (actionResultType == 0) { filterContext.Result = , true); } else if (actionResultType == 1) { filterContext.Result = , message = } }; } return; } } else { if (actionResultType == 0) { filterContext.Result = , true); } else if (actionResultType == 1) { filterContext.Result = , message = } }; } return; } } } } } base.OnActionExecuting(filterContext); } } [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public class NoNeedAdminAuthory : Attribute { }
View Code