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/  等都是訪問同一頁面 所以設置約定是需注意此處變化
        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
等都是訪問同一頁面 所以不好根據路徑判斷


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM