1. 程式人生 > >Lind.DDD.Manager裡選單許可權的設計

Lind.DDD.Manager裡選單許可權的設計

回到目錄

對於一個後臺管理系統來說,你的許可權設計與安全是重中之重,當你為一個許可權分配一些選單後,當這個許可權的使用者沒有選單許可權時,這個選單的URL是不可以被使用者訪問的,而在之前的設計中,沒有考慮到這點,所以本次Lind.DDD.Manager的升級中,需要把這塊完善一下,將會在8月的Lind.DDD中奉獻給大家,敬請期待!

思路

使用者訪問

==>

mvc根據url找到controller/action

==>

判斷這個URL是否為庫中定義的URL(排除非正常URL,PartialView產生的URL)

==>

判斷使用者是否有這個URL的許可權

==>

正常訪問

層關係圖

實現

使用了MVC環境下的AOP方法攔截技術,它主要通過過濾器(AuthorizeAttribute)來實現對action的攔截,然後注入自己的程式碼,這也是MVC幾大過濾器帶給我們的驚喜!

AuthorizeAttribute 為我們提供了一個使用者授權的過濾器,當用戶訪問Action之前,它將被執行

   // 摘要:
    //     表示一個特性,該特性用於限制呼叫方對操作方法的訪問。
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true
, AllowMultiple = true)] public class AuthorizeAttribute : FilterAttribute, IAuthorizationFilter { // 摘要: // 初始化 System.Web.Mvc.AuthorizeAttribute 類的新例項。 public AuthorizeAttribute(); // 摘要: // 獲取或設定使用者角色。 // // 返回結果: // 使用者角色。
public string Roles { get; set; } // // 摘要: // 獲取此特性的唯一識別符號。 // // 返回結果: // 此特性的唯一識別符號。 public override object TypeId { get; } // // 摘要: // 獲取或設定授權使用者。 // // 返回結果: // 授權使用者。 public string Users { get; set; } // 摘要: // 重寫時,提供一個入口點用於進行自定義授權檢查。 // // 引數: // httpContext: // HTTP 上下文,它封裝有關單個 HTTP 請求的所有 HTTP 特定的資訊。 // // 返回結果: // 如果使用者已經過授權,則為 true;否則為 false。 // // 異常: // System.ArgumentNullException: // httpContext 引數為 null。 protected virtual bool AuthorizeCore(HttpContextBase httpContext); // // 摘要: // 處理未能授權的 HTTP 請求。 // // 引數: // filterContext: // 封裝有關使用 System.Web.Mvc.AuthorizeAttribute 的資訊。filterContext 物件包括控制器、HTTP 上下文、請求上下文、操作結果和路由資料。 protected virtual void HandleUnauthorizedRequest(AuthorizationContext filterContext); // // 摘要: // 在過程請求授權時呼叫。 // // 引數: // filterContext: // 篩選器上下文,它封裝有關使用 System.Web.Mvc.AuthorizeAttribute 的資訊。 // // 異常: // System.ArgumentNullException: // filterContext 引數為 null。 public virtual void OnAuthorization(AuthorizationContext filterContext); // // 摘要: // 在快取模組請求授權時呼叫。 // // 引數: // httpContext: // HTTP 上下文,它封裝有關單個 HTTP 請求的所有 HTTP 特定的資訊。 // // 返回結果: // 對驗證狀態的引用。 // // 異常: // System.ArgumentNullException: // httpContext 引數為 null。 protected virtual HttpValidationStatus OnCacheAuthorization(HttpContextBase httpContext); }

對於我們的選單許可權過濾器,需要繼承它,我們起名為ManagerUrlAttribute,下面是大叔設計的程式碼,大家可以作為參考

    /// <summary>
    /// 後臺URL選單的許可權
    /// 需要考慮到PartialView的問題
    /// </summary>
    public class ManagerUrlAttribute : AuthorizeAttribute
    {
        /// <summary>
        /// 驗證失敗後所指向的控制器和action
        /// 可以在使用特性時為它進行賦值
        /// </summary>
        public ManagerUrlAttribute(string failControllerName = "Home", string failActionName = "Login")
        {
            _failControllerName = failControllerName;
            _failActionName = failActionName;
        }
        /// <summary>
        /// 出錯時要跳轉的控制器
        /// </summary>
        string _failControllerName;
        /// <summary>
        /// 出錯時要跳轉的action
        /// </summary>
        string _failActionName;
        /// <summary>
        /// 選單倉儲
        /// </summary>
        static IExtensionRepository<WebManageMenus> menuRepository = new ManagerEfRepository<WebManageMenus>(new ManagerContext());
        /// <summary>
        /// 所有已經定義的選單項
        /// </summary>
        static List<WebManageMenus> allMenuList = menuRepository.GetModel().ToList();

        public override void OnAuthorization(AuthorizationContext filterContext)
        {

            var menuIdArr = Array.ConvertAll<string, int>(CurrentUser.ExtInfo.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries), i => int.Parse(i));
            var menuUrlArr = allMenuList.Where(i => menuIdArr.Contains(i.Id)).Select(i => i.LinkUrl).ToList();
            var controllerName = filterContext.RouteData.Values["controller"].ToString();
            var actionName = filterContext.RouteData.Values["action"].ToString();
            var isValid = allMenuList.FirstOrDefault(i => i.LinkUrl == "/" + controllerName + "/" + actionName) != null;//是否為有效的URL,過濾分佈檢視

            //當前為正常頁面,不是分佈檢視
            if (isValid)
            {
                //沒有當前URL的許可權,跳到登陸頁
                if (!menuUrlArr.Contains("/" + controllerName + "/" + actionName))
                {
                    filterContext.Result = new RedirectToRouteResult("Default", new RouteValueDictionary { 
                              { "Action",_failActionName },
                              { "Controller", _failControllerName} });
                }
            }
        }

    }

本程式碼解決了分佈檢視在過濾器中的尷尬,將分佈檢視產生的action進行過濾,我們先將所有定義的選單URL取出來,然後使用者訪問時,先判斷當前URL是否為已經定義的URL,如果是,再進行許可權的比較.

 回到目錄