Saturday, January 17, 2009

Check whether the current user has rights for specified web site (Sharepoint Object Model)

 

With Sharepoint object model you can check whether the current user has rights to specific SPWeb site, using the following code segment:

public bool UserHasRigtsToSite(string webUrl)
{
  SPUser currentUser = SPContext.Current.Web.CurrentUser;
  bool result = false;
  SPSecurity.RunWithElevatedPrivileges(delegate()
  {
    using (SPSite site = new SPSite(SPContext.Current.Site.ID))
    {
      using (SPWeb web = site.OpenWeb(webUrl))
      {
        try
        {
          result = web.DoesUserHavePermissions(currentUser.LoginName, SPBasePermissions.ViewPages | SPBasePermissions.Open);
        }
        catch (System.IO.FileNotFoundException ex)
        {
          result = false;
        }

      }
    }
  }
  );

  return result;
}

No comments:

Post a Comment