Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Razor Umbraco 7隐藏导航节点用户无权访问_Razor_Umbraco7 - Fatal编程技术网

Razor Umbraco 7隐藏导航节点用户无权访问

Razor Umbraco 7隐藏导航节点用户无权访问,razor,umbraco7,Razor,Umbraco7,我在以前版本的Umbraco(即5)中看到过一些例子,这些例子似乎比较简单。例如,见 理论上,我可以在节点上使用属性HasAccess或IsProtected,或者在选择要使用的节点时使用方法WhereHasAccess 到目前为止,我掌握的代码是: var nodes = @CurrentPage.AncestorsOrSelf(1).First().Children; 这会给我一个页面列表,没问题。然而,我正在努力过滤页面列表,以便登录用户只看到他们有权访问的内容,而公共访问者看不到受保护

我在以前版本的Umbraco(即5)中看到过一些例子,这些例子似乎比较简单。例如,见

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

到目前为止,我掌握的代码是:

var nodes = @CurrentPage.AncestorsOrSelf(1).First().Children;
这会给我一个页面列表,没问题。然而,我正在努力过滤页面列表,以便登录用户只看到他们有权访问的内容,而公共访问者看不到受保护的页面

V5代码表明这是可能的:

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

但这会导致错误:

“Umbraco.Web.Models.DynamicPublishedContentList”不包含“WhereCanAccess”的定义。

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

@foreach(var node in nodes.WhereCanAccess()) {
    <li>@node.Name / @node.IsProtected / @node.IsProtected() / @node.HasAccess() / @node.HasAccess </li>
}
@foreach(nodes.WhereCanAccess()中的var节点){
  • @node.Name/@node.IsProtected/@node.IsProtected()/@node.HasAccess()/@node.HasAccess
  • }
    为所有测试值返回null(例如
    @node.IsProtected


    看起来我试图实现的目标很简单,但我走错了路。请有人指出我的错误

    我检查用户对以下页面的访问:

    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]
            }
    }
    

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

    我这里有其他方法来实现它


    Model.Content.Children.Where(o=>Umbraco.IsProtected(o.Id,o.Path)).Any()

    感谢@user3815602我这样做了

    foreach (IPublishedContent content in CurrentPage.AncestorsOrSelf(1).First().Children)
    {
        if (!content.CurrentUserHasAccess())
            continue;
    
        /* The current user has access to the content */
    
    }
    
    创建了一个扩展方法

    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 */
    
    }
    

    这就是它-我显然是把语法弄错了。调用的不是
    item.HasAccess()
    ,而是
    umbraco.library.HasAccess(item.id,item.path)
    。非常感谢你!