好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

mvc中菜单项的增、删、改权限的其中一种解决方案

mvc中菜单项的增、删、改权限的其中一种解决方案

mvc中菜单项的增、删、改权限的其中一种解决方案

我一直都比较关注 功能项增、删、改查权限的解决方案  也看过园子里一些园友的解决方案  可是一直都没看到我期待的解决方法(也许是我没看见) 

前端时间看到一边扩展的文章 顿时豁然开朗 想出了一种解决方案

功能描述 

   如:菜单管理  路径为:/Back/Menu            我需要为这个页面根据角色或个人等设置 增、删、改权限  下面就以角色为例(需判断多个取并集)

我的解决方案:

   首先用一个关系表 保存菜单 

  菜单ID  角色ID  拥有权限

  1         1           ADD;EDIT;DEL

  2         1            ADD

  3         1            DEL 

角色ID为1的用户拥有 三个菜单项的权限  ( 权限是根据一种约定设定  ADD为增加 EDIT为编辑  DEL为修改   可根据自己喜好设定 )

扩展Html.ActionLink 方法 如:

?

using   System.Web.Mvc.Html;

using   System.Web.Security;

using   IService;

using   Models;

using   System.Collections.Generic;

using   yujiajunMvc;

using   System.Linq;

 

namespace   System.Web.Mvc

{

     public   static   class   HtmlExtensions

     {

         /// <summary>

         /// 判断权限连接

         /// </summary>

         /// <param name="htmlHelper"></param>

         /// <param name="linkText">显示的文字</param>

         /// <param name="operate">当前什么操作 ADD添加 EDIT编辑 DEL删除</param>

         /// <returns></returns>

         public   static   MvcHtmlString ActionLinks( this   HtmlHelper htmlHelper, string   linkText, string   actionName, string   controllName, object   routeValues, object   htmlAttributes, string   operate)

         {

             HttpCookie cookie = HttpContext.Current.Request.Cookies[ "operateItem" ]; //在登录时保存用户权限 在此处获取该用户的权限

             if   (cookie == null   || string .IsNullOrEmpty(operate))

                 return   MvcHtmlString.Empty;

             string   path = HttpContext.Current.Request.RawUrl.ToLower(); // 当前页面路径

             List< string > list = DESEncrypt.Decrypt(cookie.Value).Split( ',' ).ToList(); //解密转为字符串 取得当前用户的功能权限此处我设置为 /Back/Menu/_ADD;EDIT,/Back/UserList/_EDIT;DEL 以此类推  此处注意mvc中路径有些区别 如 /Back/Menu /Back/Menu/  等都是访问同一页面 所以设置约定是需注意此处变化<br>       

             var limit = list.FirstOrDefault(a => a.Contains(path)); //如果当前页面在 权限能找到

             if   (limit != null )

                 if   (limit.Contains(operate)) //并且拥有传递进来的权限  此处需和自己的约定一直

                     return   htmlHelper.ActionLink(linkText, actionName, controllName, routeValues, htmlAttributes); //返回连接

             return   MvcHtmlString.Empty; //没有权限返回为空

         }

         public   static   MvcHtmlString ActionLinkEmpty( this   HtmlHelper htmlHelper, string   linkText, string   operate, string   property = null )

         {

             HttpCookie cookie = HttpContext.Current.Request.Cookies[ "operateItem" ]; //获取该用户的权限

             if   (cookie == null   || string .IsNullOrEmpty(operate))

                 return   MvcHtmlString.Empty;

             string   path = HttpContext.Current.Request.RawUrl.ToLower(); //当前页面路径

             List< string > list = DESEncrypt.Decrypt(cookie.Value).Split( ',' ).ToList(); //解密转为字符串

             var limit = list.FirstOrDefault(a => a.Contains(path));

             if   (limit != null )

                 if   (limit.Contains(operate))

                     return   MvcHtmlString.Create( string .Format( "<a href=\"javascript:void(0)\" id=\"{0}\" class=\"{1}\" {2}>{3}</a>" , operate, operate, property, linkText));

             return   MvcHtmlString.Empty;

         }

     }

}

  前台页面调用

@Html.ActionLinks("添 加","UserADD","Items",null,null,"ADD")

这样就实现了权限判断  

此处还存在一个问题 一直没想到好的解决方案  希望园友们能提供好的解决方案

当用户直接在浏览器输入时  在子页面不好做权限判断  上面也提到了次问题 

?

/Back/Menu/ADD    /Back/Menu/ADD/   /Back/Menu/ADD/1

?

等都是访问同一页面 所以不好根据路径判断

 

 

 

标签:  权限

作者: Leo_wl

    

出处: http://www.cnblogs.com/Leo_wl/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权信息

查看更多关于mvc中菜单项的增、删、改权限的其中一种解决方案的详细内容...

  阅读:49次