Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • 3ijtKwijt 36 posts 216 karma points
    Aug 18, 2017 @ 09:01
    3ijtKwijt
    0

    Navigation based on access

    Hi there

    I've been trying to create a navigation based on whether the logged in member has access or not. Yet I can't manage to get it to work. I've looked up alot and it seems I'll need HasAccess or MemberHasAccess but none of these seem to work...

    Anyone has a solution for this? Umbraco 7.5.11

  • Nik 1591 posts 7148 karma points MVP 6x c-trib
    Aug 18, 2017 @ 09:24
    Nik
    0

    Hi,

    What have you been trying? I've got a navigation system in a site that shows different entries to logged in users than to others (not based on roles or anything at this stage)

  • 3ijtKwijt 36 posts 216 karma points
    Aug 18, 2017 @ 09:27
    3ijtKwijt
    0

    I've tried various solutions I found online... What I try to achieve is that the navigationbar shows all the pages that are accessable for the current logged in user. Which should be all the pages with without limited access and all the pages with limited access that the user has access to

    This is one of the things I tried, but didn't seemed to work

    <ul>
        @* If the Url of the current page is "/" then we want to add the class "current_page_item" *@
        @* Otherwise, we set the class to null, that way it will not even be added to the <li> element *@
        <li class="@(CurrentPage.Url == "/" ? "current_page_item" : null)"><a href="/">Home</a></li>
        @foreach (var item in menuItems)
        {   
                var loginAcces = Umbraco.IsProtected(item.id, item.path) && Umbraco.MemberHasAccess(item.Id, item.Path);
            @* If the Id of the item is the same as the Id of the current page then we want to add the class "current_page_item" *@
            @* Otherwise, we set the class to null, that way it will not even be added to the <li> element *@
            if(!Umbraco.IsProtected(item.id, item.path) || loginAcces){
                    <li class="@(CurrentPage.Id == item.Id ? "current_page_item" : null)"><a href="@item.Url">@item.Name</a></li>
                }
            }
    </ul>
    

    Without the loginAcces part works, but then I just see the pages that aren't protected

  • 3ijtKwijt 36 posts 216 karma points
    Aug 21, 2017 @ 10:31
    3ijtKwijt
    1

    When I try this:

    @foreach (var node in homePage.Children.Where("UmbracoNaviHide == false"))
    {
        if (!Umbraco.MemberHasAccess(node.Path))
        {
            continue;
        }
        //member has access, generate menu here
    <li class="@(CurrentPage.Id == node.Id ? "current_page_item" : null)"><a href="@node.Url">@node.Name</a></li>
    }
    

    I get the following error:

    Object reference not set to an instance of an object.
    

    Not sure why, it works to show all the nodes if I remove the MemberHasAccess so it has to do with that...

  • Nik 1591 posts 7148 karma points MVP 6x c-trib
    Aug 21, 2017 @ 10:52
    Nik
    0

    Have you tried stepping through the code to see what is null at the point the exception is thrown? Is the node.Path valid?

    Have you tried updating Umbraco? There are a few newer versions of the 7.5.x stream.

    The basic code looks okay to me, can't see an obvious error there.

  • 3ijtKwijt 36 posts 216 karma points
    Aug 22, 2017 @ 06:48
    3ijtKwijt
    0

    For as far as I could, yes and also tried it on 7.6.4, also the same issue

    Maybe it's because my members are through AD? But I would assume there would be some fix for that though...

    Probably not as when I post this:

    @if(Umbraco.MemberHasAccess(CurrentPage.Id, CurrentPage.Path)){
        <h1>Welcome!</h1>
    }  
    

    But as soon as I place it in the loop, it gives the same error again...

  • 3ijtKwijt 36 posts 216 karma points
    Aug 22, 2017 @ 12:07
    3ijtKwijt
    0

    I just found out that it works fine if there are no / no published pages with public access set up... As soon as I publish a page with public access / add public access it crashes with the same error...

  • 3ijtKwijt 36 posts 216 karma points
    Aug 23, 2017 @ 06:20
    3ijtKwijt
    100

    Update

    So as I have noticed earlier, the issue for this not working is an issue with Umbraco.MemberHasAcces and more specific, this is probably caused due to the package "Active Directory Providers" as it works without this package.

    As I was in need of this, I finally found a way around to get this to work:

    @foreach (var item in menuItems)
    {
        if(Umbraco.IsProtected(item.id, item.Path))
        {
            var userHasRole = false;
            foreach (var role in umbraco.cms.businesslogic.web.Access.GetAccessingMembershipRoles(item.Id,item.Path))
            {
                foreach (var z in Roles.GetRolesForUser(User.Identity.Name))  
                {
                    if(role == z){
                        userHasRole = true;
                        break;
                    }
                }
                if(userHasRole)
                {
                    <li class="@(CurrentPage.Id == item.Id ? "current_page_item" : null)"><a href="@item.Url">@item.Name</a></li>
                }
            }
        }else{
            <li class="@(CurrentPage.Id == item.Id ? "current_page_item" : null)"><a href="@item.Url">@item.Name</a></li>
        }          
    }
    

    I hope this is a good way to solve this issue :)

Please Sign in or register to post replies

Write your reply to:

Draft