views:

19

answers:

1

Hello

I want to check whether a user has permissions to a site collection. But i dono how to use SPSite.DoesUserHavePermissions().

What is SPReusableAcl? How can i get it for checking the permissions of the user?

A: 

Doesn't the MSDN article (SPWeb.DoesUserHavePermissions Method (String, SPBasePermissions)) help you? The example code can be used to check whether the user has access to a site collection:

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();
        }
    }
}

In the sample code above you would just have to change your Site URL and the Variable permissionToCheck. SPBasePermissions has a lot of possible permissions to check against, you can see the enumeration here (SPBasePermissions Enumeration).

Actually there are a lot of tutorials on how to check some user's permissions and you are not limited to DoesUserHavePermissions, see the following Google Search.

moontear
I guess your code just checks whether the user has permissions to the top level site, not to the site collection. Top level site != Site collection. Ain't I right?
NLV
Top level site == Site collection - but that's not important now ;-) If you want to check the permissions of a site collection/subsite not being the root, you just have to edit the URL (as I mentioned) `new SPSite("http://localhost")` this URL could also be `"http://mysharepointweb/sites/mySiteCollection"`
moontear

related questions