Sitecore 6.5 系统: 在代码中,有没有办法确定哪些角色可以访问特定项目?
我设置了一个外联网,有些项目是"受保护的" - 意味着匿名帐户已经破坏了继承,并且某些角色已被授予读取权限。我已经构建了一个自定义管道,因此我可以确定用户何时尝试查看受保护的项目,但我需要确定哪些角色有权查看该项目,以便我可以适当地指导它们。
谢谢, 萨德
修改
以下是相关代码 - 可能有更好的方法(我继承了系统和代码),但我试图利用已经存在的内容。以下代码的问题是,当用户没有权限时,Sitecore.Context.Item为null。
public class NotFoundProcessor : Sitecore.Pipelines.HttpRequest.HttpRequestProcessor
{
public override void Process(Sitecore.Pipelines.HttpRequest.HttpRequestArgs args)
{
if (args.PermissionDenied)
{
//determine what role would give the user access
foreach(Sitecore.Security.Accounts.Role role in Sitecore.Security.Accounts.RolesInRolesManager.GetAllRoles())
{
bool roleCanRead = Sitecore.Context.Item.Security.CanRead(role);
//... do stuff here
}
}
}
}
答案 0 :(得分:2)
如果他们拥有read
访问所选项目的权限,您可以检查所有角色:
foreach (Role role in RolesInRolesManager.GetAllRoles())
{
bool roleCanRead = item.Security.CanRead(role);
}
编辑:
您需要尝试以与ItemResolver
相同的方式解决该项目,但请将其与SecurityDisabler
包装在一起,然后再将其设置为Sitecore Context
。这将允许您找到所请求的项目,尽管用户无法访问它:
public class NotFoundProcessor : HttpRequestProcessor
{
public override void Process(HttpRequestArgs args)
{
if (args.PermissionDenied)
{
Item item = GetItemUsingSecurityDisabler(args);
//determine what role would give the user access
foreach(Role role in RolesInRolesManager.GetAllRoles())
{
bool roleCanRead = item.Security.CanRead(role);
//... do stuff here
}
}
}
}
private Item GetItemUsingSecurityDisabler(HttpRequestArgs args)
{
using (new SecurityDisabler())
{
string path = MainUtil.DecodeName(args.Url.ItemPath);
Item item = args.GetItem(path);
if (item == null)
{
path = args.Url.ItemPath;
item = args.GetItem(path);
}
if (item == null)
{
path = args.LocalPath;
item = args.GetItem(path);
}
if (item == null)
{
path = MainUtil.DecodeName(args.LocalPath);
item = args.GetItem(path);
}
SiteContext site = Sitecore.Context.Site;
string rootPath = site != null ? site.RootPath : string.Empty;
if (item == null)
{
path = FileUtil.MakePath(rootPath, args.LocalPath, '/');
item = args.GetItem(path);
}
if (item == null)
{
path = MainUtil.DecodeName(FileUtil.MakePath(rootPath, args.LocalPath, '/'));
item = args.GetItem(path);
}
// I've ommited resolving item using DisplayName but you can add if necessary here
return item;
}
}