美文网首页菜鸟学编程
Orchard CMS设置菜单和自定义权限检查

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

作者: 菜鸟飞不动 | 来源:发表于2019-02-14 14:17 被阅读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();
            }
        }
    

    相关文章

      网友评论

        本文标题:Orchard CMS设置菜单和自定义权限检查

        本文链接:https://www.haomeiwen.com/subject/imdeeqtx.html