如果由于权限,在Sitecore多列表选定项目中禁止[项目未找到]的方法?

时间:2013-11-22 20:30:56

标签: permissions sitecore

我们的Sitecore内容树通常分为全局和本地(或国家/地区)页面,以便为我们的分支机构的客户提供服务。正如您所期望的那样,全球页面出现在我们网站的所有观看者身上,无论他们的地理位置如何,但我们已经在可本地化的全局页面上实现了某些字段,例如横幅广告,精选内容等

我们已经设置了安全性,因此对于多列表,给定的内容编辑器只能看到他们有权访问的左侧的项目。我们遇到的事情是,其他分支内容编辑器选择的项目显示在多列表的右侧“选定”区域中,如果当前正在查看该项目的内容编辑器没有这些项目的权限,它们显示为[未找到项目]。我们有好心的内容编辑错误地双击那些删除它们,没有意识到他们正在删除其他编辑故意放置的内容。使用我们德国内容编辑器的视图查看其外观的屏幕截图如下:

Example of Item not found presentation

我想要确定的是,是否有一种方法,使用安全性或其他方法,完全抑制当前内容编辑器没有权限的项目的[Item not found]消息。任何有关良好方法的提示(如果可行)都将受到赞赏。

1 个答案:

答案 0 :(得分:6)

您可以创建自己的Multilist类继承自Sitecore.Shell.Applications.ContentEditor.MultilistEx类并覆盖此类的DoRender()方法。在显示文本[Item Not Found]的位置,检查项目是否存在但用户没有访问权限(通过尝试使用SecurityDisabler检索项目)并显示正确的消息。

然后你需要转到core数据库并注册你的字段类型:

sitecore my multilist ex

最后将字段类型切换为新创建的字段 - 您的字段将如下所示:

sitecore custom multilist field result

以下代码是原始反映的MultilistEx代码,您需要进行更改:

using System.Collections;
using System.Web.UI;
using Sitecore;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Globalization;
using Sitecore.Resources;
using Sitecore.SecurityModel;

namespace My.Assembly.Namespace
{
    public class MyMultilistEx : Sitecore.Shell.Applications.ContentEditor.MultilistEx
    {
        protected override void DoRender(HtmlTextWriter output)
        {
            Assert.ArgumentNotNull(output, "output");
            ArrayList selected;
            IDictionary unselected;
            GetSelectedItems(GetItems(Sitecore.Context.ContentDatabase.GetItem(ItemID)), out selected, out unselected);
            ServerProperties["ID"] = ID;
            string disabledMessage = string.Empty;
            if (ReadOnly)
                disabledMessage = " disabled=\"disabled\"";
            output.Write("<input id=\"" + ID + "_Value\" type=\"hidden\" value=\"" + StringUtil.EscapeQuote(Value) + "\" />");
            output.Write("<table" + GetControlAttributes() + ">");
            output.Write("<tr>");
            output.Write("<td class=\"scContentControlMultilistCaption\" width=\"50%\">" + Translate.Text("All") + "</td>");
            output.Write("<td width=\"20\">" + Images.GetSpacer(20, 1) + "</td>");
            output.Write("<td class=\"scContentControlMultilistCaption\" width=\"50%\">" + Translate.Text("Selected") + "</td>");
            output.Write("<td width=\"20\">" + Images.GetSpacer(20, 1) + "</td>");
            output.Write("</tr>");
            output.Write("<tr>");
            output.Write("<td valign=\"top\" height=\"100%\">");
            output.Write("<select id=\"" + ID + "_unselected\" class=\"scContentControlMultilistBox\" multiple=\"multiple\" size=\"10\"" + disabledMessage + " ondblclick=\"javascript:scContent.multilistMoveRight('" + ID + "')\" onchange=\"javascript:document.getElementById('" + ID + "_all_help').innerHTML=selectedIndex>=0?options[selectedIndex].innerHTML:''\" >");
            foreach (DictionaryEntry dictionaryEntry in unselected)
            {
                Item unselectedItem = dictionaryEntry.Value as Item;
                if (unselectedItem != null)
                    output.Write("<option value=\"" + GetItemValue(unselectedItem) + "\">" + unselectedItem.DisplayName + "</option>");
            }
            output.Write("</select>");
            output.Write("</td>");
            output.Write("<td valign=\"top\">");
            RenderButton(output, "Core/16x16/arrow_blue_right.png", "javascript:scContent.multilistMoveRight('" + ID + "')");
            output.Write("<br />");
            RenderButton(output, "Core/16x16/arrow_blue_left.png", "javascript:scContent.multilistMoveLeft('" + ID + "')");
            output.Write("</td>");
            output.Write("<td valign=\"top\" height=\"100%\">");
            output.Write("<select id=\"" + ID + "_selected\" class=\"scContentControlMultilistBox\" multiple=\"multiple\" size=\"10\"" + disabledMessage + " ondblclick=\"javascript:scContent.multilistMoveLeft('" + ID + "')\" onchange=\"javascript:document.getElementById('" + ID + "_selected_help').innerHTML=selectedIndex>=0?options[selectedIndex].innerHTML:''\">");
            for (int index = 0; index < selected.Count; ++index)
            {
                Item selectedItem = selected[index] as Item;
                if (selectedItem != null)
                {
                    output.Write("<option value=\"" + GetItemValue(selectedItem) + "\">" + selectedItem.DisplayName + "</option>");
                }
                else
                {
                    string path = selected[index] as string;
                    if (path != null)
                    {
                        string optionDisabled = string.Empty;
                        Item linkedItem = Sitecore.Context.ContentDatabase.GetItem(path);
                        Item notAccessibleItem;
                        using (new SecurityDisabler())
                        {
                            notAccessibleItem = Sitecore.Context.ContentDatabase.GetItem(path);
                        }

                        string text;

                        if (linkedItem == null && notAccessibleItem != null)
                        {
                            text = notAccessibleItem.DisplayName + " [You don't have access rights to this item]";
                            optionDisabled = " disabled=\"disabled\"";
                        }
                        else
                        {
                            text = linkedItem == null
                                              ? path + ' ' + Translate.Text("[Item not found]")
                                              : linkedItem.DisplayName + ' ' + Translate.Text("[Not in the selection List]");
                        }
                        output.Write("<option value=\"" + path + "\"" + optionDisabled + ">" + text + "</option>");
                    }
                }
            }
            output.Write("</select>");
            output.Write("</td>");
            output.Write("<td valign=\"top\">");
            RenderButton(output, "Core/16x16/arrow_blue_up.png", "javascript:scContent.multilistMoveUp('" + ID + "')");
            output.Write("<br />");
            RenderButton(output, "Core/16x16/arrow_blue_down.png", "javascript:scContent.multilistMoveDown('" + ID + "')");
            output.Write("</td>");
            output.Write("</tr>");
            output.Write("<tr>");
            output.Write("<td valign=\"top\">");
            output.Write("<div style=\"border:1px solid #999999;font:8pt tahoma;padding:2px;margin:4px 0px 4px 0px;height:14px\" id=\"" + ID + "_all_help\"></div>");
            output.Write("</td>");
            output.Write("<td></td>");
            output.Write("<td valign=\"top\">");
            output.Write("<div style=\"border:1px solid #999999;font:8pt tahoma;padding:2px;margin:4px 0px 4px 0px;height:14px\" id=\"" + ID + "_selected_help\"></div>");
            output.Write("</td>");
            output.Write("<td></td>");
            output.Write("</tr>");
            output.Write("</table>");
        }

        private void RenderButton(HtmlTextWriter output, string icon, string click)
        {
            Assert.ArgumentNotNull(output, "output");
            Assert.ArgumentNotNull(icon, "icon");
            Assert.ArgumentNotNull(click, "click");
            ImageBuilder imageBuilder = new ImageBuilder();
            imageBuilder.Src = icon;
            imageBuilder.Width = 16;
            imageBuilder.Height = 16;
            imageBuilder.Margin = "2px";
            if (!ReadOnly)
                imageBuilder.OnClick = click;
            output.Write((imageBuilder).ToString());
        }

    }
}