MOSS / WSS 3.0: Checking permissions against an SPWeb

For Sharepoint 2003, I used to check a users rights by getting the user from the SPWeb.AllUsers collection (as this contains all users of groups as well as explicitly named users, unlike SPWeb.Users). I would have then tested whether a user was in a role using something like SPRole SPWeb.Roles[“rolenametogohere”].Users.

In MOSS / WSS 3, you can instead use the following code:

public bool UserHasPermission(string username, string role, string webAbsoluteUrl)
{
    using (SPSite siteColl = new SPSite(url))
    {
        using (SPWeb web = siteColl.OpenWeb())
        {
            SPBasePermissions requiredPerms = Web.RoleDefinitions[role].BasePermissions;
            return web.DoesUserHavePermissions(username, permsMask);
        }
    }
}

Username name is string e.g. nt\robnowik
Role is Sharepoint group e.g. “Conribute”
webAbsoluteUrl is path to web you want to test users permissions against e.g. http://localhost/sites/testsite/myweb/).

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>