Copied to clipboard

Flag this post as spam?

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


  • Jackson Fisher 4 posts 94 karma points
    Nov 07, 2016 @ 22:06
    Jackson Fisher
    0

    Only show children according to authentication

    I'm trying to control access to certain pages via role-based authentication, and everything works fine except I'm having a hard time finding how to hide children of a page (in a partial view) if the member doesn't have access to those children.

    For instance, I can type (strongly in this case):

    @foreach(var item in Model.Content.Children.Where(item => item.IsVisible())
    

    It will show pages that the user doesn't have authentication for. I'm looking to hide the page if the user doesn't have the authentication to see it, and I can't seem to find anything in examples or documentation (granted I haven't read through it all) that would work out of the box for a partial view page. Is there something like this or would I have to change my approach altogether?

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Nov 08, 2016 @ 00:31
    Marc Goodson
    101

    Hi Jackson

    There is an Umbraco Helper Method called 'MemberHasAccess' that you can use when writing out the children to determine if the current member has access to the pages.

    Umbraco.MemberHasAccess(page.Id, page.Path)

    https://our.umbraco.org/documentation/reference/querying/umbracohelper/

    your foreach loop would become:

    @foreach(var item in Model.Content.Children.Where(item => item.IsVisible() && Umbraco.MemberHasAccess(item.Id,item.Path))
    

    regards

    Marc

  • Jackson Fisher 4 posts 94 karma points
    Nov 08, 2016 @ 16:26
    Jackson Fisher
    0

    Thanks Marc, works great. I'll take a look at Umbraco Helper Methods in the future before I ask.

    FYI for anyone else with this issue, Umbraco.MemberHasAccess(item.Id,item.Path) is obsolete; put Umbraco.MemberHasAccess(item.Path) instead.

Please Sign in or register to post replies

Write your reply to:

Draft