高级用户访问Sitecore回收站

时间:2014-12-04 16:14:18

标签: sitecore

是否有人知道允许某个角色的用户查看Sitecore回收站中的所有项目的解决方案?

目前,只有管理员才能看到所有已删除的项目。用户只能看到他们删除的项目。

2 个答案:

答案 0 :(得分:6)

没有开箱即用的方法,SqlArchive.GetEntries检查user.IsAdministrator以显示档案中的所有条目。

您需要实现自定义存档提供程序并覆盖 GetEntries 方法才能从角色中工作。

示例:

public class CustomSqlArchive:SqlArchive {     public CustomSqlArchive(字符串名称,数据库数据库)         :base(名称,数据库)     {     }

protected override IEnumerable<ArchiveEntry> GetEntries(User user, int pageIndex, int pageSize, ID archivalId)
{
    Assert.IsNotNull(archivalId, "archivalId");
    var arrayList = new ArrayList(new[] { "archiveName", this.Name });
    var str1 = "SELECT * FROM \r\n          (SELECT {0}Archive{1}.{0}ArchivalId{1}, {0}Archive{1}.{0}ItemId{1}, {0}ParentId{1}, {0}Name{1}, {0}OriginalLocation{1}, \r\n            {0}ArchiveDate{1}, {0}ArchivedBy{1}, ROW_NUMBER() OVER(ORDER BY {0}ArchiveDate{1} DESC, {0}ArchivalId{1}) as {0}RowNumber{1}\r\n              FROM {0}Archive{1} \r\n           WHERE {0}ArchiveName{1} = {2}archiveName{3}";

    var showAllItems = user.IsInRole("Super User Role") || user.IsAdministrator;

    if (user != null && !showAllItems)
    {
        str1 = str1 + " AND {0}ArchivalId{1} IN (SELECT {0}ArchivalId{1}\r\n          FROM {0}ArchivedVersions{1} WHERE {0}ArchivedBy{1} = {2}archivedBy{3}) ";
        arrayList.AddRange(new[] { "archivedBy", user.Name });
    }

    if (archivalId != ID.Null)
    {
        str1 = str1 + " AND {0}ArchivalId{1} = {2}archivalId{3}";
        arrayList.Add("archivalId");
        arrayList.Add(archivalId);
    }

    var str2 = str1 + ") {0}ArchiveWithRowNumbers{1}";
    if (pageSize != int.MaxValue)
    {
        str2 = str2 + " WHERE {0}RowNumber{1} BETWEEN {2}firstRow{3} AND {2}lastRow{3}";
        var num1 = (pageIndex * pageSize) + 1;
        int num2 = pageSize == int.MaxValue ? int.MaxValue : (pageIndex + 1) * pageSize;
        arrayList.AddRange(new[] { "firstRow", num1.ToString(), "lastRow", num2.ToString() });
    }

    return this.GetEntries(str2 + " ORDER BY {0}ArchiveDate{1} DESC, {0}ArchivalId{1}", arrayList.ToArray());
}

}

然后,您需要将自定义提供程序添加到配置中:

<archives defaultProvider="custom" enabled="true">
    <providers>
        <clear />
        <add name="custom" type="Sitecore.Data.Archiving.SqlArchiveProvider, Sitecore.Kernel" database="*" />
        <add name="sql" type="Sitecore.Data.Archiving.SqlArchiveProvider, Sitecore.Kernel" database="*" />
        <add name="switcher" type="Sitecore.Data.Archiving.SwitchingArchiveProvider, Sitecore.Kernel" />
    </providers>
</archives>

然后添加一个名为超级用户角色的角色,并将您想要拥有该访问权限的任何用户作为成员。

**注意 - 代码未经测试**

答案 1 :(得分:1)

以下是理查德答案的类似方法,但不是复制GetEntries()中的所有逻辑,而是欺骗管理员用户。除SqlArchiveProvider本身外,您还需要实施CustomSqlArchive

SQL档案

public class CustomSqlArchive : SqlArchive
{
    private const string PowerUserRole = @"sitecore\Power User";
    private const string AdminUser = @"sitecore\Admin";

    public AvidSqlArchive(string name, Database database) : base(name, database) { }

    protected override IEnumerable<ArchiveEntry> GetEntries(User user, int pageIndex, int pageSize, ID archivalId)
    {
        if (user != null && Role.Exists(PowerUserRole) && user.IsInRole(PowerUserRole))
        {
            User admin = User.FromName(AdminUser, true);
            return base.GetEntries(admin, pageIndex, pageSize, archivalId);
        }

        return base.GetEntries(user, pageIndex, pageSize, archivalId);
    }
}

SQL档案提供程序

public class CustomSqlArchiveProvider : SqlArchiveProvider
{
    protected override Sitecore.Data.Archiving.Archive GetArchive(XmlNode configNode, Database database)
    {
        string attribute = XmlUtil.GetAttribute("name", configNode);
        return !string.IsNullOrEmpty(attribute) ?
            new CustomSqlArchive(attribute, database) :
            null;
    }
}

配置修补程序

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>

    <!-- Use custom archive that allows users in the "Power User" role to see other user's items by spoofing the admin user -->
    <archives defaultProvider="sql" enabled="true">
      <patch:attribute name="defaultProvider">custom</patch:attribute>
      <providers>
        <clear />
        <add name="custom" type="Example.CustomSqlArchiveProvider, Example" database="*" />
        <add name="sql" type="Sitecore.Data.Archiving.SqlArchiveProvider, Sitecore.Kernel" database="*" />
        <add name="switcher" type="Sitecore.Data.Archiving.SwitchingArchiveProvider, Sitecore.Kernel" />
      </providers>
    </archives>

  </sitecore>
</configuration>