HTML5技术

Asp.Net MVC+BootStrap+EF6.0实现简单的用户角色权限管理10 - yjq_叶

字号+ 作者:H5之家 来源:博客园 2015-12-01 11:09 我要评论( )

今天把用户的菜单显示和页面的按钮显示都做好了,下面先来个效果图 接下来说下我实现的方法: 首先我在每个方法前面都加了这个属性, 表示当前Action请求为一个具体的功能页面 AdminActionMethod : Attribute { 页面请求路径 ActionUrl { get ; set ; } 页面

今天把用户的菜单显示和页面的按钮显示都做好了,下面先来个效果图

接下来说下我实现的方法:

首先我在每个方法前面都加了这个属性,

表示当前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

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • HTML5 进阶系列:拖放 API 实现拖放排序 - _林鑫

    HTML5 进阶系列:拖放 API 实现拖放排序 - _林鑫

    2017-05-02 11:02

  • 如何在 ASP.NET Core 中发送邮件 - Savorboard

    如何在 ASP.NET Core 中发送邮件 - Savorboard

    2017-05-02 08:02

  • 十二个 ASP.NET Core 例子 - Savorboard

    十二个 ASP.NET Core 例子 - Savorboard

    2017-04-27 16:01

  • ASP.NET MVC5请求管道和生命周期 - 雪飞鸿

    ASP.NET MVC5请求管道和生命周期 - 雪飞鸿

    2017-04-24 08:04

网友点评