【问题标题】:Umbraco 7 hide navigation nodes user has no access toUmbraco 7 隐藏导航节点用户无权访问
【发布时间】:2014-07-07 13:17:25
【问题描述】:

我在以前的 Umbraco 版本(即 5)中看到了一些示例,这些示例似乎相对简单。例如,请参阅this stackoverflow question

理论上,我可以在节点上使用属性HasAccessIsProtected,或者在选择要使用的节点时使用方法WhereHasAccess

我目前的代码是:

var nodes = @CurrentPage.AncestorsOrSelf(1).First().Children;

这让我得到了页面列表,没问题。但是,我正在努力过滤页面列表,以便登录用户只能看到他们有权访问的内容,而公共访问者看不到受保护的页面。

V5 代码表明这是可能的:

var nodes = @CurrentPage.AncestorsOrSelf(1).First().Children.WhereCanAccess();

但这会导致错误:

'Umbraco.Web.Models.DynamicPublishedContentList' does not contain a definition for 'WhereCanAccess'

Razor cheatsheet for Umbraco 的最新发布版本表明 HasAccess()IsProtected() 是两种都可用的方法,但是当使用其中任何一种时,我得到空值,例如:

@foreach(var node in nodes.WhereCanAccess()) {
    <li>@node.Name / @node.IsProtected / @node.IsProtected() / @node.HasAccess() / @node.HasAccess </li>
}

为所有测试值返回 null(例如 @node.IsProtected)。

似乎我想要实现的目标很简单,但我以错误的方式接近它。有人请指出我的方法的错误!

【问题讨论】:

    标签: razor umbraco7


    【解决方案1】:

    我检查用户对这样的页面的访问:

    var node = [the page you want to verify access to ie. "CurrentPage"];
    var isProtected = umbraco.library.IsProtected(node.id, node.path);
    var hasAccess = umbraco.library.HasAccess(item.id, item.path);
    

    我的顶部菜单代码:

       var homePage = CurrentPage.AncestorsOrSelf(1).First();
        var menuItems = homePage.Children.Where("UmbracoNaviHide == false");
        @foreach (var item in menuItems)
        {
            var loginAcces = umbraco.library.IsProtected(item.id, item.path) && umbraco.library.HasAccess(item.id, item.path);
            var cssClass = loginAcces ? "loginAcces ":"";
            cssClass += CurrentPage.IsDescendantOrSelf(item) ? "current_page_item" :"";                           
    
            if(!umbraco.library.IsProtected(item.id, item.path) || loginAcces){
                [render your item here]
            }
    }
    

    这将隐藏受保护的项目,除非用户已登录并有权访问。

    【讨论】:

    • 就是这样——我显然弄错了语法。呼叫不是item.HasAccess(),而是umbraco.library.HasAccess(item.id, item.path)。非常感谢!
    【解决方案2】:

    感谢@user3815602 我这样做了

    创建了一种扩展方法

    namespace CPalm.Core
    {
        public static class ExtensionMethods
        {
    
            public static bool CurrentUserHasAccess(this IPublishedContent content)
            {
                int contentId = content.Id;
                string contentPath = content.Path;
    
                bool isProtected = umbraco.library.IsProtected(contentId, contentPath);
                if (isProtected)
                {
                    bool hasAccess = umbraco.library.HasAccess(contentId, contentPath);
                    if (!hasAccess)
                        return false;
                }
                return true;
            }
        }
    }
    

    而且可以这样使用

    foreach (IPublishedContent content in CurrentPage.AncestorsOrSelf(1).First().Children)
    {
        if (!content.CurrentUserHasAccess())
            continue;
    
        /* The current user has access to the content */
    
    }
    

    【讨论】:

      【解决方案3】:

      我有另一种方法来实现它。

      Model.Content.Children.Where(o =&gt; Umbraco.IsProtected(o.Id, o.Path)).Any()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多