如何设置安全性,以便用户只能在Sitecore中查看/编辑他们的项目?

时间:2016-03-17 09:42:48

标签: c# security sitecore sitecore8 usermanager

Sitecore中是否有任何方法可以创建只能查看和编辑自己创建的项目的角色/用户?如果没有,我怎么能做到这一点?

3 个答案:

答案 0 :(得分:1)

为了解决这个问题,我在sitecore / events config下添加了一个项目:created event。

<event name="item:created" xdt:Transform="Replace" xdt:Locator="Match(name)">
              <handler type="Sirano.Dev.ItemEventHandlers.CustomItemEventHandler, Sirano.Dev" method="OnItemCreated" />
</event>

此事件将运行以下代码:

    protected void OnItemCreated(object sender, EventArgs args)
    {
        if (args == null)
        {
            return;
        }
        var parameters = Event.ExtractParameters(args);
        var item = ((ItemCreatedEventArgs)parameters[0]).Item;
        if (item == null)
        { 
            return; 
        }

        var user = Sitecore.Context.User;

        var accessRules = item.Security.GetAccessRules();

        accessRules.Helper.AddAccessPermission(user,
           AccessRight.ItemRead,
           PropagationType.Any,
           AccessPermission.Allow);

        accessRules.Helper.AddAccessPermission(user,
           AccessRight.ItemWrite,
           PropagationType.Any,
           AccessPermission.Allow);

        item.Editing.BeginEdit();
        item.Security.SetAccessRules(accessRules);
        item.Editing.EndEdit();
    }

答案 1 :(得分:0)

我刚刚完成了这个功能。虽然没有任何开箱即用的功能,但有许多方法可以解决这个问题,包括:

  • item:保存事件处理程序(注意'保存'而不是'保存',事后发生),或
  • 为saveUI管道创建一个处理器,当在UI中执行“保存”按钮或者我们去的方式时执行该处理器
  • 为getContentEditorWarnings管道创建处理器

最后一种方法允许您禁用Sitecore UI并向用户显示消息,但不会阻止代码或API更改项目,这对我们来说是理想的。我们的处理器接受了一个路径列表(在配置中)作为子元素,允许我们将功能限制到站点的某些区域(我们控制对Marketing Control Panel的访问)。

这是我们注入片段的<getContentEditorWarnings>管道的摘录。

....
<processor type="Sitecore.Pipelines.GetContentEditorWarnings.CanWrite, Sitecore.Kernel"/>
<processor type="PingWorks.Pipelines.GetContentEditorWarnings.EditorIsFromAuthorGroup, PingWorks.Pipelines.GetContentEditorWarnings" patch:source="PingWorks.Pipelines.GetContentEditorWarnings.config">
  <ignoredRoles hint="list:AddIgnoredRole">
    <role>sitecore\_UserBase</role>
  </ignoredRoles>
  <paths hint="list:AddPath">
    <path>/sitecore/system/Marketing Control Panel/Taxonomies/</path>
    <path>/sitecore/system/Marketing Control Panel/Campaigns/</path>
    <path>/sitecore/system/Marketing Control Panel/Engagement Plans/</path>
    <path>/sitecore/system/Marketing Control Panel/Experience Analytics/</path>
    <path>/sitecore/system/Marketing Control Panel/FXM/</path>
    <path>/sitecore/system/Marketing Control Panel/Outcomes/</path>
    <path>/sitecore/system/Marketing Control Panel/Path Analyzer/</path>
    <path>/sitecore/system/Marketing Control Panel/Personalization/</path>
    <path>/sitecore/system/Marketing Control Panel/Test Lab/</path>
    <path>/sitecore/system/Marketing Control Panel/Experience Explorer/</path>
    <path>/sitecore/system/Marketing Control Panel/Analytics Filters/</path>
  </paths>
</processor>
<processor type="Sitecore.Pipelines.GetContentEditorWarnings.CanWriteWorkflow, Sitecore.Kernel"/>
...

请注意如何使用Sitecore配置工厂来填充管道执行器将使用子元素创建的实例的属性,以及在这种情况下我们如何通过以下方式向List<string>属性添加字符串::AddIgnoredRole()::AddPath()方法。

在我们的特定情况下,我们只希望编辑编辑器与原始作者属于同一角色组的成员,尽管您的逻辑将比我们的更直接。在我们的案例中,我们还为管理员用户添加了一个覆盖,并添加了一个自定义缓存来存储角色搜索结果,以加快处理速度,因为角色成员资格在我们的情况下不会经常更改。

我将为您节省大部分时间,但其中的关键在于::Process()方法(我必须反映此代码,因为我目前无法访问源代码):< / p>

public void Process(GetContentEditorWarningsArgs args)
{
    string displayName;
    this._item = args.Item;
    if (!this._isValidForProcessing())
        return;

    User user = null;
    List<string> creatorRoles = this._getRolesForUser(this._item.Statistics.CreatedBy, out user);
    List<string> editorRoles = this._getRolesForUser(Context.User);

    // compare creator's roles with current editor to find a match
    if ( creatorRoles.Any() && editorRoles.Any() && editorRoles.Any( r => creatorRoles.Contains(r) ) )
        return;

    // if we haven't already aborted, add a warning to display and block editing
    GetContentEditorWarningsArgs.ContentEditorWarning cew = args.Add();
    cew.IsExclusive = true;
    cew.Key = "EditorIsFromAuthorGroup";
    cew.Title = "Editing restricted";
    cew.Text = $"Editing for this item is restricted. Editors must share a role with the original author, in this case <{user?.DisplayName ?? "Unknown Author"}>.";
}

private bool _isValidForProcessing()
{
    if (this._item == null)
        return false;

    if (Context.IsAdministrator)
        return false;

    if (!this._paths.Any<string>((string p) => this._item.Paths.FullPath.ToLower().StartsWith(p)))
        return false;

    return true;
}

这可能足以让你在需要的地方找到一个良好的开端。

答案 2 :(得分:0)

简单来说,可以使用Sitecore的锁定和编辑功能。用户可以通过Review&gt;锁定在工作流程组中编辑。完成编辑后保存并单击签入。如果登录用户希望查看其锁定的项目,则用户可以右键单击内容树的左侧并选择我的锁定项目以查看我锁定的项目装订线。即使您可以查看Review&gt;中的所有锁定项目我的物品。请注意,这不会对访问权限进行任何更改。但禁止除管理员以外的其他用户解锁项目。

相关问题