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...
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)
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
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...
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>
}
}
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
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)
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
Without the loginAcces part works, but then I just see the pages that aren't protected
When I try this:
I get the following error:
Not sure why, it works to show all the nodes if I remove the MemberHasAccess so it has to do with that...
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.
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:
But as soon as I place it in the loop, it gives the same error again...
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...
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:
I hope this is a good way to solve this issue :)
is working on a reply...