【问题标题】:determine roles that have access to an item in Sitecore - in code确定有权访问 Sitecore 中的项目的角色 - 在代码中
【发布时间】:2014-03-22 06:20:02
【问题描述】:

Sitecore 6.5 系统: 在代码中,有没有办法确定哪些角色可以访问特定项目?

我设置了一个外联网,一些项目是“受保护的” - 这意味着匿名帐户的继承已被破坏,并且某些角色已被授予读取权限。我已经构建了一个自定义管道,因此我可以确定用户何时尝试查看受保护的项目,但我需要确定哪些角色有权查看该项目,以便我可以适当地指导他们。

谢谢, 萨德

编辑:

这是相关代码 - 可能有更好的方法(我继承了系统和代码),但我正在尝试利用已经存在的内容。下面代码的问题是,当用户没有权限时,Sitecore.Context.Item 为空。

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

            }
        }
    }
}

【问题讨论】:

    标签: sitecore sitecore6


    【解决方案1】:

    从我的脑海中,您可以检查所有角色是否拥有read 访问所选项目的权限:

    foreach (Role role in RolesInRolesManager.GetAllRoles())
    {
      bool roleCanRead = item.Security.CanRead(role);
    }
    

    EDIT在提供代码示例后:

    您需要尝试以与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;
        }
    }
    

    【讨论】:

    • 谢谢@maras,我不知道 Security.CanRead 可以与角色一起使用(上下文帮助仅将 Account 列为参数)。但是,我在使用此方法时遇到问题,因为未经该项目的许可,Sitecore.Context.Item 为空。
    • 您能否更准确地描述您想要访问该项目的时间?你有一些定制的管道处理器吗?你能粘贴一些代码吗?
    • 我在上面添加了一些代码,但是是的,我有一个自定义管道处理器,最初设置它是为了更好地处理 404 消息传递,我还试图确定用户是否无权访问(很容易完成)以及他们需要担任什么角色才能查看项目(我被卡住的地方)。
    • 您需要尝试以与ItemResolver 相同的方式解析该项目,但使用SecurityDisabler 将其包装起来,之后不要将其设置为Sitecore Context。这将允许您找到请求的项目,尽管用户无权访问它(请参阅更新的答案)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 2011-04-22
    • 2017-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多