Umbraco 5 询问用户是否有权访问节点 [英] Umbraco 5 ask if user has permission to node
问题描述
我正在使用 Umbraco 5.1 测试版.在互联网上(此信息来自以前的版本,找不到有关它的最新文档)我发现我可以询问节点用户是否具有访问权限.这样我想建立我的菜单.问题是,我无法让它工作,HasAccess 和 IsProtected 属性不起作用.我究竟做错了什么?或者它在较新版本的 Umbraco 中工作方式不同吗?(作为方法我也试过了,还是没有结果)
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>
推荐答案
如果您只是想抑制用户无法访问的节点.然后就可以使用 WhereCanAccess() 方法了.
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>
尝试查找节点是否为 IsProtected 似乎有点复杂(尽管只有几行代码.好吧,我找到的唯一方法是无论如何!)
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>
这篇关于Umbraco 5 询问用户是否有权访问节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!