开发者

Check User Permission to Site or Workspace given a URL

开发者 https://www.devze.com 2022-12-17 09:17 出处:网络
Given a list of Workspaces: http://server/managed_path/sitecoll/basic http开发者_StackOverflow://server/managed_path/sitecoll/blank

Given a list of Workspaces:

http://server/managed_path/sitecoll/basic
http开发者_StackOverflow://server/managed_path/sitecoll/blank
http://server/managed_path/sitecoll/decision
http://server/managed_path/sitecoll/multipage
http://server/managed_path/sitecoll/social

How can I call DoesUserHavePermissions() - or something similar - to find out if the currently logged in user can access the web site?

I am writing a user control to output a list of workspaces they have access to, but when I try to check, I get a variety of errors I can't seem to work around with this code:

foreach (String s in workspaces)
{
    using (SPSite site = new SPSite(s))
    {
        using (SPWeb web = site.OpenWeb(s))
        {
    // web.DoesUserHavePermissions(...)
        }
    }
}


1) The list of URLs in your example are all in the same site collection, just different webs involved, this means you don't have to re-open the SPSite every time, just SPWebs

2) When opening the spsite, use "SystemAccount.Token" as in this example: http://blackninjasoftware.com/2009/04/09/how-to-programmatically-impersonate-users-in-sharepoint/

SPSite tempSite = new SPSite(siteStr); 
SPUserToken systoken = tempSite.SystemAccount.UserToken;
using (SPSite site = new SPSite(siteStr, systoken)) {
   //here goes the foreach loop and you iterate through the workspaces
}

This way you will be able to call "DoesuserHavePermissions" method.

3) This bear in mind that opening and closing SPWebs at runtime will hit performance. Try to cache the results of your code, if possible.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号