Uncategorized

SharePoint 2010 における Web パーツのセキュリティの変更点

環境 : SharePoint 2010, Visual Studio 2010

こんにちは。

開発者の方から、SharePoint 2010 における Web パーツのセキュリティに関し、下記のご質問を頂きましたので、記載しておきます。

発生する現象

SharePoint 2010 における Web パーツの標準的な動作では、サイトの「ページの追加とカスタマイズ」(Add and Customize Pages) のアクセス許可レベル (Permission Level) が設定されていないユーザー (グループ) では、Web パーツの挿入や EditorPart を使ったカスタム プロパティ (Custom Property) の編集 (保存) などをおこなうことはできません。(Web パーツのカスタム プロパティについては、こちら を参照してください。)

ここで問題になるのは、サイトの投稿者 (Contributor) です。
SharePoint Server 2007 では、サイトの投稿者 (Contributor) の場合でも Web パーツの挿入とカスタマイズが可能でした。しかし、SharePoint 2010 の場合、既定では、サイトの投稿者 (Contributor) に対して、下図の通り、「ページの追加とカスタマイズ」(Add and Customize Pages) のアクセス許可レベルが付与されていません。(既定では、フル コントロール (Full Control)、または、デザイナー (Designer) などのグループのみが、このアクセス許可レベルを持っています。)

このため、SharePoint 2010 の場合、サイトの投稿者 (Contributor) は、既定では、カスタム Web パーツなどの挿入とカスタマイズをおこなうことができません。(例えば、Visual Studio で簡単なカスタム Web パーツを作成し、SharePoint 2010 に配置をおこなって、投稿者 (Contributor) のユーザーでこの Web パーツの挿入をおこなうとエラーが表示されます。また、カスタム プロパティの編集時も、エラーは表示されませんが、設定は反映されません。)

しかし、イメージ ビューア Web パーツ (ImageWebPart) など、SharePoint 2010 が既定で持っている Web パーツの多くは、投稿者 (Contributor) であっても Web パーツの挿入とカスタマイズ (プロパティ編集) をおこなうことができます。

ここでは、この動作の背景と、カスタム Web パーツにおける対処方法について説明します。

カスタム Web パーツにおける対処方法

結論としては、こうした動作を回避するプロパティが SafeControl 要素の属性として用意されているため、この設定をおこなえば完了です。
以下の (SharePoint 2010 の) ドキュメントで、SafeAgainstScript 属性の説明を参照してください。

MSDN : SafeControl Element (Solution)
http://msdn.microsoft.com/en-us/library/ms451053.aspx

このため、回避方法としては、Web.config に下記の太字の通り設定をおこなうことで、投稿者 (Contributor) であっても Web パーツの追加とカスタム プロパティの編集をおこなうことができます。(SharePoint がインストールする既定の Web パーツの多くには、この属性が設定されています。)

. . .<SafeControls>  . . .  <SafeControl Assembly="(省略)" . . . (省略). . . SafeAgainstScript="True" /></SafeControls>. . .

勿論、属性として、Safe 属性も設定しておく必要がありますが (Safe=”True”)、省略した場合は、Safe 属性は True となるので、特にこの属性の記述は必要ありません。

開発者の方が構築するカスタム Web パーツでこの設定をおこなうには、ご存じの通り、配置マニフェスト (manifest.xml) にこの記述をおこなっておきます。
以下に、Visual Studio 2010 を使って、その設定手順と動作を確認します。

設定方法 (開発手順)

Visual Studio 2010 を使って Web パーツを開発されている方は、以下の手順で設定します。今回は、カスタム プロパティ (EditorPart) を使用した簡単な Web パーツを例に説明します。

まず、Visual Studio 2010 を開き、[空の SharePoint プロジェクト] を新規作成します。(ファーム ソリューションとして作成します。)

ソリューション エクスプローラーでプロジェクト名をマウスで右クリックし、[追加] – [新しい項目] を選択して、[Web パーツ] を新規に追加します。

今回は、以下の通り、コードを記述しましょう。
この Web パーツは、ラベル (Label) を持つ簡単な Web パーツで、カスタム プロパティを使って、このラベルの文字列を Bold / Normal に切り替えることができます。(下記の EditorPart については、こちら の説明を参照してください。)

. . .[ToolboxItemAttribute(false)]public class MyDemoWebPart : WebPart{    private Label label1 = new Label();    private bool fontBold;    [Personalizable, WebBrowsable(false)]    public bool FontBold    {        get { return fontBold; }        set        {            label1.Font.Bold = value;            fontBold = value;        }    }    protected override void CreateChildControls()    {        label1.Text = "Hello, SharePoint 2010 New Permission !";        this.Controls.Add(label1);    }    public override EditorPartCollection CreateEditorParts()    {        List<EditorPart> editorParts = new List<EditorPart>();        EditorPart editor = new MyCustomEditor();        editor.ID = ID + "_editor";        editor.Title = "Custom Prop";        editorParts.Add(editor);        EditorPartCollection epc = new EditorPartCollection(editorParts);        return epc;    }}public class MyCustomEditor : EditorPart{    CheckBox isBoldCtrl;    // Editor を表示/非表示にするときに呼ばれます (WebPart -> Editorへ反映)    public override void SyncChanges()    {        MyDemoWebPart part = (MyDemoWebPart)WebPartToEdit;        EnsureChildControls();        isBoldCtrl.Checked = part.FontBold;    }    // Editor で変更後に呼ばれます (Editor -> WebPart へ反映)    public override bool ApplyChanges()    {        MyDemoWebPart part = (MyDemoWebPart)WebPartToEdit;        part.FontBold = isBoldCtrl.Checked;        return true;    }    protected override void CreateChildControls()    {        isBoldCtrl = new CheckBox();        isBoldCtrl.Text = "Set Bold !";        Controls.Add(isBoldCtrl);    }    protected override void RenderContents(HtmlTextWriter writer)    {        isBoldCtrl.RenderControl(writer);    }}

この状態で配置をおこなっても、上述の通り、サイトの投稿者 (Contributor) が、この Web パーツの挿入とプロパティ編集をおこなうことはできません。このため、下記の通り設定をおこないます。

ソリューション エクスプローラーで、上記で作成した Web パーツ (MyDemoWebPart) を選択 (クリック) し、表示されるプロパティ ウィンドウで、下図の [安全なコントロール エントリ] の右の […] ボタンをクリックします。(これにより、配置マニフェストの SafeControl の設定をおこなうことができます。)

表示されるダイアログボックスで、下図の通り、[スクリプトに対して安全] (Safe Against Script) プロパティを True に設定して、[OK] ボタンを押します。

念のため、パッケージ デザイナーを使って、作成された配置マニフェスト (manifest.xml) を確認すると、下図の通り、SafeControl 要素の SafeAgainstScript 属性が設定されているのがわかります。

以上で完了です。

配置をおこなって、投稿者 (Contributor) のユーザーで SharePoint にログインすると、この Web パーツの挿入やカスタム プロパティの編集が可能であることが確認できます。 (下図)

補足 : サンドボックス ソリューション (Sandboxed Solution) の場合

この動作は、サンドボックス ソリューション (Sandboxed Solution) でも、まったく同じです。
サンドボックス ソリューションで作成されるパッケージ (.wsp) に下記の通り記述され、サイトの投稿者 (Contributor) がこの Web パーツの追加とカスタマイズ (カスタム プロパティの編集) をおこなうことができます。

<?xml version="1.0" encoding="utf-8"?><Solution xmlns="http://schemas.microsoft.com/sharepoint/" SolutionId="8ddf4e7e-9131-4353-b032-a526ac468ff9" SharePointProductVersion="14.0">  <Assemblies>    <Assembly Location= . . .>      <SafeControls>        <SafeControl . . . SafeAgainstScript="True" />      </SafeControls>      . . .

 

Categories: Uncategorized

Tagged as:

4 replies»

  1. Is there any way to localize the default title of webpart.
    e.g
    In your MyDemoWebpart.webpart has a property named Title. I want to localize this property.

    Like

  2. Of course, it's OK to use Japanese string for WebPart Title property in .webpart file. (It is displayed without a problem.)
    Are you using UTF-8 encoding for .webpart file ? If the encoding is not UTF-8, localized string is not diplayed correctly. (Of course, VS 2010 project creates webpart file using UTF-8 encoding.)
    The best way is to use a resource file and resource string for Title property.(But, you cannot use a resource file in the sandboxed solution.)

    Like

  3. Thank you FYI,
    Maybe my infomation is missing.
    I mean I want to use a resource file and resource string for the Title property. But when I deploy the resource file to App_GlobalResources, I can not use the resource string for the Title property(e.g
    <property name="Title" type="string">$Resources:resource_file, resource_key;</property>)

    Like

Leave a Reply