"Distribution system - the original first chapter" of the "multi-user role access module problem" Solutions (bit operation + ActionFilterAttribute) ...

 This project is based on the needs of the user to assign permissions, and make the appropriate permissions module browsing, because the project is not large, so there is no permission to use a table to keep my ideas to solve the following, I hope we can give suggestions.

 

User database table structure as follows :

 

Sort the database table:

BankUserMember : rights allocation table ( 1 - Province Line Administrator, 2 - branch administrator, 0 - network responsible person (Member), 4 - Super administrator).

      BankUserInfo : user detailed table.

          BankAgent : agency hierarchy table.

 User permissions to enumerate the following table :

  ///  <the Summary> 
    /// user role
     ///  </ the Summary> 
    public  enum Role
    {
        ///  <the Summary> 
        /// registered members
         ///  </ the Summary> 
        Member = 0 ,
         ///  <the Summary> 
        /// provincial bank administrator
         ///  </ the Summary> 
        provincemanager a = 1 ,
         ///  < Summary> 
        /// branch administrator
         ///  </ Summary> 
        BranchManager = 2 ,
         ///  <Summary> 
        /// Head administrator
         ///  </ Summary> 
        Manager = . 3 ,
         ///  <Summary> 
        // / super administrator
        /// </summary>
        SuperManager = 4
  }

 

  Regular viewing the following code :

    ///  <Summary> 
    /// membership function watch yield
     ///  </ Summary> 
    [Role (Entity.Enum.Role.Member)]
     public  class PerformanceController: BaseController
    {
        [NoCache]
        public ActionResult Index(int? page)
        { 
// all } }

 

    (Provincial bank, branch) Administrator viewing the following code:

 ///  <the Summary> 
    /// (provincial bank, branch) Administrator viewing
     ///  </ the Summary> 
    [Role (Entity.Enum.Role.ProvinceManager | Entity.Enum.Role.BranchManager)]
     public  class AdminPerformanceController: BaseController
    {
        [NoCache]
        public ActionResult Index(int? page)
        { 
// all } }

 

 

  Permissions RoleAttribute filter following code:

 ///  <the Summary> 
    /// Action role-based access control
     ///  </ the Summary> 
    public  class RoleAttribute: ActionFilterAttribute
    {
        ///  <Summary> 
        /// control character
         ///  </ Summary> 
        public Entity.Enum.Role _role { GET ; SET ;}
         ///  <Summary> 
        /// login role
         ///  </ Summary> 
        public  int memberLoginRole = 0 ;

        public RoleAttribute () {}
         ///  <Summary> 
        /// authentication and roles configured
         ///  </ Summary> 
        ///  <param name = "In Flag"> </ param> 
        public RoleAttribute (Entity.Enum. Role role)
        {
            _role = role;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!CheckRole())
            {
                string urlreffer = filterContext.HttpContext.Request.UrlReferrer == null ? string.Empty 
: filterContext.HttpContext.Request.UrlReferrer.AbsoluteUri;
if (string.IsNullOrEmpty(urlreffer)) urlreffer = filterContext.HttpContext.Request.Url == null ? string.Empty : filterContext.HttpContext.Request.Url.AbsoluteUri; string locationUrl = string.Empty; locationUrl = TsingDa.Common.WebConfig.GetWebConfig("website_url", "") + "/Home/Login?ReturnUrl=" +
filterContext.HttpContext.Server.UrlEncode(urlreffer); RedirectResult loginUrl
= new RedirectResult(locationUrl); filterContext.Result = loginUrl; } else { --------- [// verify user's access controller ( using the bit operation and flexible multi-role problem solving ) ] --------- IF ((( int ) _role & ( int ) memberLoginRole) <= 0 && (( int ) _role! = memberLoginRole)) { filterContext.Result = new RedirectResult("/Home/RoleError"); } } base.OnActionExecuting(filterContext); } private bool CheckRole() { // verify that there is no login operation Cookie, the code will be omitted. . . . . . . . } }

 

  

 

 

 

Reproduced in: https: //www.cnblogs.com/Kummy/p/3345386.html

Guess you like

Origin blog.csdn.net/weixin_34239169/article/details/93230585