質問

企業のイントラネットポータルでは、訪問者(認証済みユーザー)をユーザーWebパーツでカスタマイズすることができます。

このカスタマイズを一部のページでのみ制限したいです。

今では、この権限を定義する新しい権限レベル "カスタマイズ"を作成しました。

  • ページを追加してカスタマイズ
  • パーソナルWebパーツの追加/削除
  • パーソナルWebパーツを更新する

    これは、ユーザーWebPartsを追加することを許可するために検索された許可の最小のパーミッションです。

    私のポータルでは、私のポータルの「訪問者」グループを持っています。

    このグループに適用されている場合、 web レベルでカスタム許可レベル(OOB読み取り権限レベルを維持する)は、ページにユーザーWebパーツを追加できます。

    しかし、この権限レベルをページのライブラリレベルまたは具体的なページ level(Breaking Permissionsの継承)で適用する場合は、ユーザーWebPartを追加できません。

    特定のページのみにこの権限を適用することは可能ですか?

    i は、 を除去または更新することができます。それは制限されているWebPartのみを追加します。

    FYI、WebPartsは実際にはカスタム "Manager" WebPartからコードによって追加されます。

    特に、このコードは例外を投げています:

    var mgr = web.GetLimitedWebPartManager(file.ServerRelativeUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.User);
    var wpFile = web.GetCatalog(SPListTemplateType.WebPartCatalog).RootFolder.Files["somewebpart.webpart"];
    System.Web.UI.WebControls.WebParts.WebPart newWebPart;
    using (var raw = wpFile.OpenBinaryStream())
    {
        using (var xr = XmlReader.Create(raw))
        {
            string error;
            newWebPart = mgr.ImportWebPart(xr, out error); 
        }
    }
    
    .

    ImportWebPartメソッドは、あいまいな例外をスローしています:

    Microsoft.SharePoint.ApplicationRuntime.SafeControls+UnsafeControlException: A Web Part or Web Form Control on this Page cannot be displayed or imported. You don't have Add and Customize Pages permissions required to perform this action
       à Microsoft.SharePoint.WebPartPages.WebPartImporter.CreateWebPart(Boolean clearConnections)
       à Microsoft.SharePoint.WebPartPages.WebPartImporter.Import(SPWebPartManager manager, XmlReader reader, Boolean clearConnections, Uri webPartPageUri, SPWeb spWeb)
       à Microsoft.SharePoint.WebPartPages.WebPartImporter.Import(SPWebPartManager manager, XmlReader reader, Boolean clearConnections, SPWeb spWeb)
       à Microsoft.SharePoint.WebPartPages.SPWebPartManager.ImportWebPart(XmlReader reader, String& errorMessage)
       à Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.ImportWebPart(XmlReader reader, String& errorMessage)
       à x.y.z.ImportWebPart(SPLimitedWebPartManager mgr, SPFile wpFile)
    
    .

    ULSログでは、これだけが表示されます。

    WebPartのインポートエラー。アセンブリMicrosoft.SharePoint、Version= 14.0.0.0、Culture= Neutral、PublicKeytoken= 71e9bce111E9429c、typeName。 Microsoft.SharePoint.WebPartPages.contenteditorWebPart

    それはかなり邪魔です。

    [編集] リフレクタ付き、私はWebPartImporter.CreateWebPartメソッドにこの検証を見つけました:

         if ((!this._spWeb.AllowContributorsToEditScriptableParts && !this._spWeb.DoesUserHavePermissions(SPBasePermissions.EmptyMask | SPBasePermissions.AddAndCustomizePages)) && !this._spWeb.SafeControls.SafeAgainstScript(this._type, out unsafeErrorMessage))
            {
                throw new SafeControls.UnsafeControlException(SafeControls.UnsafeControlException.MakeGenericUnsafeExceptionMessage(unsafeErrorMessage));
            }
    
    .

    実際には、WebPartを追加するコードは、SPWebオブジェクトの許可を確認します。

    私のシナリオはサポートされていないと結論するべきですか? :(

役に立ちましたか?

解決

After some experiments, here is seems-to-work solution :

var web = SPContext.Current.Web;

var mgr = web.GetLimitedWebPartManager(file.ServerRelativeUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.User);


var wpFile = web.GetCatalog(SPListTemplateType.WebPartCatalog).RootFolder.Files["somewebpart.webpart"];



/* Elevated the process just for importing the webpart */
var elevatedWeb = GetElevatedWeb(web); // Get an elevated Web object, that points on the same web
var elevatedMgr = elevatedWeb.GetLimitedWebPartManager(file.ServerRelativeUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.User);
System.Web.UI.WebControls.WebParts.WebPart newWebPart;
using (var raw = wpFile.OpenBinaryStream())
{
    using (var xr = XmlReader.Create(raw))
    {
        string error;
        newWebPart = elevatedMgr.ImportWebPart(xr, out error); 
    }
}


/* Import the webpart, created in the elevated context, but into the current user's context. */
mgr.AddWebPart(newWebPart, "Left", 0);

Basically, the idea was to import the webpart under an elevated context, but add the imported web part in the actual current context.

It seems to work, but I'm not very fan o such hacking mechanism.

他のヒント

You want to allow any visitor (any authenticated user) to customize some pages with user web parts...Right?

How we do this where I work, we created a group that includes the accounts of all authenticated users. We can apply this permission group at any level, document through an entire site collection.

Hope this helps you out in some way...

In reviewing the method you find with reflector it does appear that it requires the web level permissions. You might want to consider executing the block with elevated permissions which should get you around the need for giving web level permissions. (Assuming this is a farm solution) If you go that route you might consider adding some extra logic checking in your custom manager web part to make sure the elevated privileges are not being used/invoked incorrectly.

ライセンス: CC-BY-SA帰属
所属していません sharepoint.stackexchange
scroll top