问题
Im working with Umbraco 5.1 beta. On the internet (this information is from previous versions, could not find recent documentation on it) I find that I could ask a node if the user has Access. In that way I want to build up my menu. The thing is, I cant get it to work, the HasAccess and IsProtected properties are not working. What am I doing wrong? Or does it work different in the newer versions of Umbraco? (I also tried it as method, still no result)
This is the code I'm now using:
@inherits RenderViewPage
@using Umbraco.Cms.Web;
@{
var Homepage = @DynamicModel;
while (Homepage.ContentType.Alias != "homePage")
{
Homepage = Homepage.Parent;
}
}
<ul>
<li><a href="@Homepage.Url">Home</a></li>
@foreach (var item in Homepage.Children) {
if(!item.IsProtected || (item.IsProtected && item.HasAccess)) {
if(@item.CurrentTemplate != null) {
var childName = item.Name ?? "(No name yet)";
<li><a href="@item.Url">@childName </a></li>
}
}
}
</ul>
回答1:
If you are just looking to suppress nodes that the user cannot access. Then you can use the WhereCanAccess() method.
Example: (This will hide all child nodes that the user doesn't have access to)
@inherits RenderViewPage
@using Umbraco.Cms.Web;
@{
var Homepage = @DynamicModel;
while (Homepage.ContentType.Alias != "homePage")
{
Homepage = Homepage.Parent;
}
}
<ul>
<li><a href="@Homepage.Url">Home</a></li>
@foreach (var item in Homepage.Children.WhereCanAccess())
{
if(@item.CurrentTemplate != null)
{
var childName = item.Name ?? "(No name yet)";
<li><a href="@item.Url">@childName </a></li>
}
}
</ul>
Trying to find if a node IsProtected seems to be somewhat more complex (although only a couple of lines of code. Well the only way I found find to do it anyway!)
Example: (This just puts an * next to the name of protected menu items)
@inherits RenderViewPage
@using Umbraco.Cms.Web;
@{
var Homepage = @DynamicModel;
while (Homepage.ContentType.Alias != "homePage")
{
Homepage = Homepage.Parent;
}
var appContext = DependencyResolver.Current.GetService<IUmbracoApplicationContext>();
}
<ul>
<li><a href="@Homepage.Url">Home</a></li>
@foreach (var item in Homepage.Children)
{
var isProtected = appContext.Security.PublicAccess.IsProtected(item.Id);
if (@item.CurrentTemplate != null)
{
var childName = item.Name ?? "(No name yet)";
childName = (isProtected) ? "* " + childName : childName;
<li><a href="@item.Url">@childName </a></li>
}
}
</ul>
来源:https://stackoverflow.com/questions/10064231/umbraco-5-ask-if-user-has-permission-to-node