2010-10-15 17 views
3

我想檢查用戶是否有權訪問網站集。但我dono如何使用SPSite.DoesUserHavePermissions()使用網站集檢查用戶的權限

什麼是SPReusableAcl?我怎樣才能得到它檢查用戶的權限?

回答

1

MSDN文章(SPWeb.DoesUserHavePermissions Method (String, SPBasePermissions))是否對您有所幫助?示例代碼可以用來檢查用戶是否具有網站集訪問:

using System; 
using Microsoft.SharePoint; 

namespace Test 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      using (SPSite site = new SPSite("http://localhost")) 
      { 
       using (SPWeb web = site.OpenWeb()) 
       { 
        // Make sure the current user can enumerate permissions. 
        if (web.DoesUserHavePermissions(SPBasePermissions.EnumeratePermissions)) 
        { 
         // Specify the permission to check. 
         SPBasePermissions permissionToCheck = SPBasePermissions.ManageLists; 
         Console.WriteLine("The following users have {0} permission:", permissionToCheck); 

         // Check the permissions of users who are explicitly assigned permissions. 
         SPUserCollection users = web.Users; 
         foreach (SPUser user in users) 
         { 
          string login = user.LoginName; 
          if (web.DoesUserHavePermissions(login, permissionToCheck)) 
          { 
           Console.WriteLine(login); 
          } 
         } 
        } 
       } 
      } 
      Console.ReadLine(); 
     } 
    } 
} 

在上面的示例代碼,你就只需要改變你的網站的網址和變量permissionToCheck。 SPBasePermissions有很多可能的權限來檢查,你可以在這裏看到枚舉(SPBasePermissions Enumeration)。

實際上有很多關於如何檢查用戶權限的教程,並且您不限於DoesUserHavePermissions,請參閱以下Google Search

+0

我想你的代碼只是檢查用戶是否有權限到頂級網站,而不是網站集。頂級網站!=網站集。我不正確嗎? – NLV 2010-10-18 03:47:03

+0

頂級網站==網站集 - 但現在並不重要;-)如果您想檢查網站集/子網站的權限不是根,那麼您只需編輯網址(如我所提到的)'新的SPSite (「http:// localhost」)'這個URL也可以是'「http:// mysharepointweb/sites/mySiteCollection」' – 2010-10-18 11:57:42

0

像往常一樣,MSDN示例提供了很好的教科書示例,並不總是適用於真實場景。

在SharePoint 2010上運行的應用程序頁面的上下文中,根據我的理解,此代碼需要包含在對RunWithElevatedPrivileges的調用中,並且即使如此,正如我的意見所暗示的,似乎有一個隱含的catch-22要求。這對我的作品(該LoginName將僅僅是FBA用戶名或「域\用戶」對於現場AD用戶 - 在我們的情況下,使用一個電子郵件地址):

SPSecurity.RunWithElevatedPrivileges(delegate() 
{ 
    using (SPSite elevatedSite = new SPSite(siteCollectionUrl)) 
    { 
     foreach (SPSite siteCollection in elevatedSite.WebApplication.Sites) 
     { 
      using (SPWeb elevatedWeb = siteCollection.OpenWeb()) 
      { 
       bool allowUnsafeUpdates = elevatedWeb.AllowUnsafeUpdates; 
       bool originalCatchValue = SPSecurity.CatchAccessDeniedException; 
       SPSecurity.CatchAccessDeniedException = false; 

       try 
       { 
        elevatedWeb.AllowUnsafeUpdates = true; 

        // You can't verify permissions if the user does not exist and you 
        // can't ensure the user if the user does not have access so we 
        // are stuck with a try-catch 
        SPUser innerUser = elevatedWeb.EnsureUser(loginName); 

        if (null != innerUser) 
        { 
         string splogin = innerUser.LoginName; 
         if (!string.IsNullOrEmpty(splogin) && elevatedWeb.DoesUserHavePermissions(splogin, SPBasePermissions.ViewPages)) 
         { 
          // this user has permissions; any other login - particularly one that 
          // results in an UnauthorizedAccessException - does not 
         } 
        } 
       } 
       catch (UnauthorizedAccessException) 
       { 
        // handle exception 
       } 
       catch (Exception) 
       { 
        // do nothing 
       } 
       finally 
       { 
        elevatedWeb.AllowUnsafeUpdates = allowUnsafeUpdates; 
        // reset the flag 
        SPSecurity.CatchAccessDeniedException = originalCatchValue; 
       } 
      } 
     } 
    } 
});  
0

SPSite.DoesUserHavePermissions(SPReusableAcl, SPBasePermissions);

+0

你能詳細說一下SPReusableAcl是什麼嗎? – axblount 2015-03-27 17:46:06

+0

ReusableAcl:「使用SPWeb,SPList或SPListItem類的ReusableAcl屬性返回網站,列表或列表項的ACL。對於實現ISecurableObject接口的任何類,可以使用ReusableAcl屬性返回ACL。要返回網站集的ACL,請使用SPSite類的GetReusableAclForScope或GetAllReusableAcls方法。「 – 2016-08-16 09:22:34