菜鸟学编程

Orchard CMS设置菜单和自定义权限检查

2019-02-14  本文已影响0人  菜鸟飞不动

在Module中的AdminMenu.cs 中添加菜单,设置对应的Permission

这里设置Permission后Orchard会自动根据当前登录的用户的权限,显示/隐藏 后台的菜单

 public class AdminMenu : INavigationProvider 
 {
        public Localizer T { get; set; }
        public string MenuName { get { return "admin"; } }

        public void GetNavigation(NavigationBuilder builder) {
            builder.Add(item => item
                    .Caption(T("管理"))
                    .LinkToFirstChild(false)                   
                    .Add(subItem => subItem
                        .Caption(T("广告来源"))
                        .Position("11.3")
                        .Action("Index", "AdvertisingSourceAdmin")  
                  .Permission(RcclPermission.CampaignAdvertisingSourcePermission)
                    )
                );
        }
}

在代码中检查权限

例如在Controller中可以自定义检查需要的权限。可以调用 Services.Authorizer.Authorize来检查当前用户是否具有正确的权限

    public class AdvertisingSourceAdminController : Controller
    {
        private readonly IOrchardServices _orchardServices;
        public Localizer T { get; set; }
        public AdvertisingSourceAdminController(IOrchardServices orchardServices)
        {
            _orchardServices = orchardServices;
            T = NullLocalizer.Instance;
        }

        public ActionResult Index()
        {
            if (_orchardServices.Authorizer.Authorize(RcclPermission.CampaignAdvertisingSourcePermission, T("Permission denied")))
            {
                return new HttpUnauthorizedResult();
            }
            
            return View();
        }
    }
上一篇下一篇

猜你喜欢

热点阅读