mvc 权限控制

PriceComparison.Entity代码

Role类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations.Schema;

namespace Test.Entity
{
    public class Role
    {
        public int ID { get; set; }

        public string RoleName { get; set; }

        public bool IsDeleted { get; set; }

        public DateTime Created { get; set; }

        public DateTime Modified { get; set; }

        [NotMapped]
        public string PermissionDetail { get; set; }
    }

    public enum Authority
    {
        None = 0,
        Dashboard = 1,
        MatchManagement = 2,
        MatchCategory = 3,
        MatchProduct = 4,
        MatchServiceSwitch = 11,
        StandardProduct = 5,
        BrandManagement = 6,
        RetailerManagement = 7,
        CategoryManagement = 8,
        CombinedCategory = 9,
        RetailerCombinedCategory = 10
    }
}


PermissionsInRoles类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test.Entity
{
    public class PermissionsInRoles
    {
        public long ID { get; set; }

        public int RoleID { get; set; }

        public int FunctionModelID { get; set; }

        public bool IsVisible { get; set; }

        public DateTime Created { get; set; }

        public DateTime Modified { get; set; }
    }
}


Authorization验证类


using PriceComparison.Entity;
using PriceComparison.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Test.Web.App_Start
{
    public static class Authorization
    {
        public static void CheckAuthority(int functionModelID)
        {
            PCContext db = new PCContext();
            User model = System.Web.HttpContext.Current.Session["CurrentUser"] as User;
            int count = db.PermissionsInRoles.Where(obj => obj.RoleID == model.RoleID && obj.FunctionModelID == functionModelID).Count();
            if (count <= 0)
            {
                System.Web.HttpContext.Current.Response.Redirect("/Admin/User/AuthorityError");
            }
        }
    }
}


Controller控制类

using System;
using System.Web;
using System.Web.Mvc;

namespace Test.Web.Areas.Admin.Controllers
{
    public class CategoryController : Controller
    {
        public ActionResult Index()
        {
            Authorization.CheckAuthority((int)Authority.CategoryManagement);
            return View();
        }

        public ActionResult CombinedCategory()
        {
            Authorization.CheckAuthority((int)Authority.CategoryManagement);
            return View();
        }

        public ActionResult CategoryList()
        {
            Authorization.CheckAuthority((int)Authority.CategoryManagement);
            return View();
        }

        public ActionResult CategoryPictureManage()
        {
            Authorization.CheckAuthority((int)Authority.CategoryManagement);
            return View();
        }

        public ActionResult CombinedCategorysOfRetailer(long id)
        {
            Authorization.CheckAuthority((int)Authority.CategoryManagement);
            ViewBag.RetailerID = id;
            return View();
        }

        public ActionResult RetailerCombinedCategory()
        {
            Authorization.CheckAuthority((int)Authority.CategoryManagement);
            return View();
        }

    }
}




你可能感兴趣的:(mvc 权限控制)