문제

회사 인트라넷 포털에서 방문자 (인증 된 사용자)가 사용자 웹 파트가있는 일부 페이지를 사용자 정의 할 수있게 해줍니다.

이이 사용자 정의를 일부 페이지에서만 제한하려는 것입니다.

지금까지는이 사용 권한을 정의하는 사용자 WebParts 을 사용자 정의하는 새로운 권한 수준을 만들었습니다.

  • 페이지 추가 및 사용자 정의
  • 개인 웹 부품 추가 / 제거
  • 개인 웹 파트 업데이트

    이것은 사용자 WebParts를 추가 할 수 있도록하는 가장 작은 권한 집합입니다.

    내 포털의 " 내 포털 "그룹이 있습니다.

    이 그룹에 적용하면 레벨에서 사용자 정의 권한 수준 (OOB 읽기 권한 수준을 유지), 페이지에 사용자 웹 파트를 추가 할 수 있습니다.

    그러나 페이지의 라이브러리 레벨 또는 특정 페이지 레벨 (사용 권한 상속) 에서이 권한 수준을 적용하면 사용자 웹 파트를 추가 할 수 없습니다.

    특정 페이지에만이 사용 권한을 적용 할 수 있습니까?

    i CAN 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, 버전= 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