I would like to hide some the menus on the navigation that a member has not access to.
For example if member1 has access to has access to everything then show every menu on the navigation.
But if member2 doesn't have access to meny2, -hide it from the navigation
How can I do this?
Here is my navigation macro code.
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{
int num = 1;
var rootNode = CurrentPage.Site();
<ul id="menu">
<li class="thehome">
<a href="@rootNode.Url" class="drop home"></a>
</li>
@foreach (var mainNode in rootNode.Children().Where("Visible"))
{
int childCount = 1;
int numChildren = mainNode.Children().Count();
<li>
<a href="@mainNode.Url" class="drop">@mainNode.Name</a>
@{
var childNodes = mainNode.Children().Where("Visible");
}
@if (childNodes.Any())
{
<div id=nav-@num class="dropdown_5columns">
<!-- Begin 2 columns container -->
<div class="col_5">
<h2>@mainNode.Name</h2>
</div>
@* note if you want ALL descendants change .Children to .Descendats*@
@foreach (var childNode in childNodes)
{
// if first node or new set of three open the div and ul @: is used to stop razor trying to
// "balance" the tags
if (childCount == 1 || (double)childCount % 3 == 1)
{
@:<div class="col-md-4">
@:<ul class="subnav">
}
<li><a href="@childNode.Url">@childNode.Name</a></li>
// close the div and list if this is either a multiple of 3 or the last one
if ((double)childCount % 3 == 0 || numChildren == childCount)
{
@:</ul>
@:</div>
}
childCount++;
}
</div>
}
</li>
num++;
}
</ul>
}
@using Umbraco.Web;
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{
int num = 1;
var rootNode = Model.Content.AncestorOrSelf("Site");
<ul id="menu">
<li class="thehome">
<a href="@rootNode.Url" class="drop home"></a>
</li>
@foreach (var mainNode in rootNode.Children().Where(x => x.IsVisible()
&& (!Umbraco.IsProtected(x.Id, x.Path) || Umbraco.MemberHasAccess(x.Id, x.Path))))
{
int childCount = 1;
int numChildren = mainNode.Children().Count();
<li>
<a href="@mainNode.Url" class="drop">@mainNode.Name</a>
@{
var childNodes = mainNode.Children().Where(x => x.IsVisible()
&& (!Umbraco.IsProtected(x.Id, x.Path) || Umbraco.MemberHasAccess(x.Id, x.Path)));
}
@if (childNodes.Any())
{
<div id=nav-@num class="dropdown_5columns">
<!-- Begin 2 columns container -->
<div class="col_5">
<h2>@mainNode.Name</h2>
</div>
@* note if you want ALL descendants change .Children to .Descendats*@
@foreach (var childNode in childNodes)
{
// if first node or new set of three open the div and ul @: is used to stop razor trying to
// "balance" the tags
if (childCount == 1 || (double)childCount % 3 == 1)
{
@:<div class="col-md-4">
@:<ul class="subnav">
}
<li><a href="@childNode.Url">@childNode.Name</a></li>
// close the div and list if this is either a multiple of 3 or the last one
if ((double)childCount % 3 == 0 || numChildren == childCount)
{
@:</ul>
@:</div>
}
childCount++;
}
</div>
}
</li>
num++;
}
</ul>
}
@using Umbraco.Web;
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{
int num = 1;
var rootNode = Model.Content.AncestorOrSelf(1);
<ul id="menu">
<li class="thehome">
<a href="@rootNode.Url" class="drop home"></a>
</li>
@foreach (var mainNode in rootNode.Children().Where(x => x.IsVisible()
&& (!Umbraco.IsProtected(x.Id, x.Path) || Umbraco.MemberHasAccess(x.Id, x.Path))))
{
int childCount = 1;
int numChildren = mainNode.Children().Count();
<li>
<a href="@mainNode.Url" class="drop">@mainNode.Name</a>
@{
var childNodes = mainNode.Children().Where(x => x.IsVisible()
&& (!Umbraco.IsProtected(x.Id, x.Path) || Umbraco.MemberHasAccess(x.Id, x.Path)));
}
@if (childNodes.Any())
{
<div id=nav-@num class="dropdown_5columns">
<!-- Begin 2 columns container -->
<div class="col_5">
<h2>@mainNode.Name</h2>
</div>
@* note if you want ALL descendants change .Children to .Descendats*@
@foreach (var childNode in childNodes)
{
// if first node or new set of three open the div and ul @: is used to stop razor trying to
// "balance" the tags
if (childCount == 1 || (double)childCount % 3 == 1)
{
@:<div class="col-md-4">
@:<ul class="subnav">
}
<li><a href="@childNode.Url">@childNode.Name</a></li>
// close the div and list if this is either a multiple of 3 or the last one
if ((double)childCount % 3 == 0 || numChildren == childCount)
{
@:
</ul>
@:
</div>
}
childCount++;
}
</div>
}
</li>
num++;
}
</ul>
}
@foreach (var childNode in childNodes)
{
// if first node or new set of three open the div and ul @: is used to stop razor trying to
// "balance" the tags
if (childCount == 1 || (double)childCount % 3 == 1)
{
@:<div class="col-md-4">
@:<ul class="subnav">
}
<li><a href="@childNode.Url">@childNode.Name</a></li>
// close the div and list if this is either a multiple of 3 or the last one
if ((double)childCount % 3 == 0 || numChildren == childCount)
{
@:
</ul>
@:
</div>
}
childCount++;
}
Hide navigation menus for non-logged in users
I would like to hide some the menus on the navigation that a member has not access to.
For example if member1 has access to has access to everything then show every menu on the navigation. But if member2 doesn't have access to meny2, -hide it from the navigation
How can I do this?
Here is my navigation macro code.
Hi Johan,
You will need to use the MemberHasAccess and IsProtected methods of the UmbracoHelper.
You can find the documentation over here : https://our.umbraco.org/Documentation/Reference/Querying/UmbracoHelper/
Hi Dave, How would the code look like inside my code?
Hi Johan,
Here you can find a code example that "should" work. First thing I did is change from the dynamics syntax to strongly typed.
See this article for more information about tht difference : http://24days.in/umbraco/2015/strongly-typed-vs-dynamic-content-access/
And here is the code example
Dave
I get error
Maybe you change this line :
I was assuming that your top node would be of the documenttype "Site".
Dave
Hmm I still get the same error for some reason.
Can you show me how you call this macro from your template ?
Because it works on my test site.
Dave
This is my macro:
And I call it from this this template:
Hi Johan
Without more details about the error I would do the following to try to reproduce this.
Remove the call to the main navigation macro from the template. If you still get the error than you now the error is somewhere in your template.
If you don't get a error you can add the macro a back.
Than I would try to strip the macro from all it's code and would put back little pieces of code till I have located the error.
Dave
Hi Dave,
The problem seem to be this:
Do you have any idea why it's an error?
It seems you want to render some tags after every third item. You can do this more easy using this syntax :
Dave
I see you do some calculations on the count. Maybe there is a error in that ?
If you remove this.. Does it work ?
Dave
Thank you so much Dave! It's working now.
is working on a reply...