Navigation

Tuesday 4 October 2011

Scaling to 10,000 unique permissions - Part 2 - The Solution

This follows on from my previous post; Part 1 - The Problem

The main requirement was:

  • One SharePoint 2010 site
  • 10,000+ uniquely permissioned objects each with a different user account

In this post we will be discussing the solution which involves programmatically creating unique permissions in a way which will scale for (what should be) well over 10,000 uniquely permissioned items...

Introducing yet another little known SharePoint API call ...

This is only possible because of one of the new SharePoint 2010 API calls;

SPRoleAssignmentCollection.AddToCurrentScopeOnly()

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.sproleassignmentcollection.addtocurrentscopeonly.aspx

This basically adds the specified SPRoleAssignment but does not create any of the Limited Access scopes on the parent objects.

This is pretty straight forward and works in exactly the same way to normal Role Assignments in SharePoint 2010, we simply use the AddToCurrentScopeOnly() method instead of using the Add() method, for example:

 

   1: // fetch the Principal object which we are granting access to
   2: SPUser user = web.EnsureUser("Domain\\UserAccount");
   3:  
   4: // create a Role Assignment binding
   5: SPRoleAssignment roleAssignment = new SPRoleAssignment(user);
   6:  
   7: // apply contribute permissions
   8: roleAssignment.RoleDefinitionBindings.Add(
   9:     web.RoleDefinitions["Contribute"]);
  10:  
  11: // grant permissions to the list item using the CURRENT SCOPE ONLY
  12: // this ensures that Limited Access scopes are NOT created
  13: // for parent objects (we're going to have to do that bit ourselves!)
  14: item.RoleAssignments.AddToCurrentScopeOnly(roleAssignment)

 

It is very important to understand that you still need to grant "Limited Access" (it wasn't put in just for laughs, it does have a purpose). Granting "Limited Access" means that the object has access to core information on parent objects to enable construction of things like the breadcrumb, and retrieval of core files needed to render the interface.

This then means it is up to us (the developers) to go back and create each of those in a more efficient way. The problem is .. you can't assign "Limited Access" programmatically...

What do you mean .. I can't assign Limited Access??

Well, I don't really know why they did this, but if you try and assign it programmatically (Limited Access is actually a "Permission Level" in SharePoint) you will get errors (admittedly you can't do this through the user interface either!).

So, the workaround (again) is to create your own permission level which includes exactly the same permissions that "Limited Access" would have granted. This is:


  • View Application Pages
  • Browse User Information
  • Use Remote Interfaces
  • Use Client Integration Features
  • Open

You can call this anything you like (I called mine "SP Limited Access") as long as you know what it means.

The code to do this is as follows:

 


   1: internal SPRoleDefinition GetLimitedAccessRole(SPWeb web)
   2:         {
   3:             string strRoleDefinition = "SP Limited Access";
   4:  
   5:             // only exists in webs with unique role definitions
   6:             if (web.HasUniqueRoleDefinitions)
   7:             {
   8:                 try
   9:                 {
  10:                     // try to retrieve the role definition
  11:                     return web.RoleDefinitions[strRoleDefinition];
  12:                 }
  13:                 catch (SPException)
  14:                 {
  15:                     // SPException means it does not exist
  16:  
  17:                     // create our custom limited access role
  18:                     SPRoleDefinition roleDef = new SPRoleDefinition();
  19:  
  20:                     // give it a name and description
  21:                     roleDef.Name = "SP Limited Access";
  22:                     roleDef.Description = "Identical to standard " + 
  23:                         "Limited Access rights. " + 
  24:                         "Used to provide access to parent objects of " + 
  25:                         "uniquely permissioned content";
  26:  
  27:                     // apply the base permissions required
  28:                     roleDef.BasePermissions = SPBasePermissions.ViewFormPages 
  29:                         | SPBasePermissions.Open 
  30:                         | SPBasePermissions.BrowseUserInfo 
  31:                         | SPBasePermissions.UseClientIntegration 
  32:                         | SPBasePermissions.UseRemoteAPIs;
  33:  
  34:                     // add it to the web
  35:                     web.RoleDefinitions.Add(roleDef);
  36:                 }
  37:  
  38:                 return web.RoleDefinitions[strRoleDefinition];
  39:             }
  40:             else
  41:             {
  42:                 // try the parent web
  43:                 return GetLimitedAccessRole(web.ParentWeb);
  44:             }
  45:         }

I've created my new Limited Access Permission Level .. now what?

One thing does need to be made clear, there is absolutely no point you just creating all of the Security Scopes that SharePoint would have created (you'll end up with the same mess we were trying to avoid in the first place).

The solution is to create a group for all of the "Limited Access" users for that List or Web. It really is up to you whether you use Active Directory Security Groups or SharePoint Groups. I decided to use AD security groups; mainly because I didn't want to clog up the Site Collection "groups" functionality, and didn't want idiot Site Collection admins from removing the group members (or worse .. the groups themselves!) and breaking the site collection.


Note - I haven't included the code to create and modify Active Directory Security Groups here, if nothing else because there are thousands of resources out there showing you how to modify AD groups programmatically, and Code Project has a particularly good reference: Howto: (Almost) Everything In Active Directory via C#


You will need to create a group for each parent object which has unique permissions although in my example it is only really the SPWeb (web site) that we are worried about as the libraries and folders are well within the security scope threshold.

So we have our 20 libraries and our root web site. So in our example we would have to create 21 different AD security groups:


  • One group to store all Limited Access users for the root web site

  • 20 groups to store all Limited Access for the libraries (one for each library)

Then, following this example you can then use the following code to grant “Limited Access” to one of the libraries (and just rinse and repeat for the other libraries and the root web site);

 


   1: // fetch the "SP Limited Access" role definition
   2: SPRoleDefinition limitedAccessRole = GetLimitedAccessRole(web);
   3:  
   4: // get SPPrincipal object for the AD Group we created
   5: SPUser adGroup = web.EnsureUser("My Custom AD Group Name");
   6:  
   7: // set the role assignments for this group
   8: SPRoleAssignment roleAssignment = new SPRoleAssignment(adGroup);
   9: roleAssignment.RoleDefinitionBindings.Add(limitedAccessRole);
  10:  
  11: // grant "Limited Access" to the AD Group for this list
  12: // we only have to do this once! After this we simply 
  13: // need to add members to this AD Group every time we 
  14: // add users to one of the parent objects!
  15: list.RoleAssignments.AddToCurrentScopeOnly(roleAssignment)

So having done this for all of the parent objects we now have our 21 custom Active Directory groups, each one of which has been granted “Limited Access” to one of the required “parent” objects for our folders.

From here on in it should be smooth sailing. You simply need to make sure that every time you programmatically add a new user to one of the folders you also make sure they get added to the relevant AD Groups (so that the “Limited Access” chain is not broken).

The following diagram really explains what we have done:

Folders_New

I have tested this model for over 16,000 unique AD accounts across hundreds of folders in hundreds of document libraries and I cannot notice any discernable drop off in performance (nothing that can’t be explained by simply having a really large number of libraries and folders anyway!) so initial tests show that this is working very well indeed :)

What I also ended up doing (to make this slightly more robust) is to build my own application page which users can use to Grant Permissions through the UI (so we don’t need to write custom code every time a new “Limited Access” scope is needed).

I then wrote an HttpModule to auto-redirect any requests to the out-of-the-box page (_layouts/AclInv.aspx) to the custom page so that if anyone tried to use the native user interface it would ALWAYS be executing my own custom code (which creates all of the AD Groups and SP Limited Access scopes programmatically, without the user having to worry about it!)

The great thing about this solution is that it doesn't matter how many users or groups you are adding to your SharePoint site .. you only ever have 1 Limited Access security scope for each List / Web!



Thanks for sticking with me through these two posts .. if you made it this far then thanks for reading and I would love to hear your comments! :)



4 comments:

  1. Exelent post! How do you think if i add a "NT AUTHORITY\authenticated users" to "SP Limited Access" group it should work?
    In my case i have only one document library.

    ReplyDelete
  2. Great article. I'll have to revisit the drop box in SLK now.

    Looking at the capacity management document you had to use AD groups instead of SharePoint groups as SP groups should be limited to 5000 AD principals.

    ReplyDelete
  3. funja, yes technically that would work, although obviously you would have security concerns to deal with!

    Richard, correct .. AD Groups would be the way to go if you have LOTS of unique accounts. SP Groups could work for smaller numbers.

    ReplyDelete
  4. Hi Martin

    Great posts. Few people create SPLists with more than a couple of hundreds items. But those who do, really needs to be aware of this!

    ReplyDelete

This blog has been moved to www.martinhatch.com

Note: only a member of this blog may post a comment.