MSDN Magazine - February 2008 - (Page 59) Figure 5 SPWeb site = SPContext.Current.Web; Guid listID = site.Lists.Add(“Proposals”, “Library desc”, SPListTemplateType.DocumentLibrary); SPDocumentLibrary doclib = (SPDocumentLibrary)site.Lists[ListID]; doclib.OnQuickLaunch = true; doclib.BreakRoleInheritance(false); SPUser AllFteGroup = Web.SiteUsers[@”LITWAREINC\AllFTE”]; SPRoleAssignment assignAllFteGroup = new SPRoleAssignment(AllFteGroup); SPRoleDefinition roleDesign = this.Web.RoleDefinitions[“Read”]; assignAllFteGroup.RoleDefinitionBindings.Add(roleDesign); doclib.RoleAssignments.Add(assignAllFteGroup); doclib.Update(); Wrapping Up I know that this discussion has been somewhat of a whirlwind tour through the security model of WSS. I’ve shown you how WSS tracks external security principals at the level of the site collection with a profile within the User Information List and explained how WSS represents these external security principals in the WSS object model using SPUser objects. I’ve also demonstrated how WSS provides support for WSS groups and presented a few programming techniques for elevating privileges and impersonating WSS users. These techniques provide the power and flexibility you need when creating a real-world application. While WSS relies on an underlying system of components to perform authentication, it does take on the responsibility of authorization and access control. The WSS authorization model is largely based on a named set of permissions known as permission levels or roles. A permission level can be assigned to an SPUser object, but in practice you should generally choose to assign permission levels to WSS groups. Code download available at msdn.microsoft.com/msdnmag/code08.aspx. Send your questions and comments for Ted to mmoffice@microsoft.com. Ted Pattison is an author, trainer, and SharePoint MVP who lives in Tampa, Florida. He also delivers advanced SharePoint training to professional developers through his company, Ted Pattison Group (www.TedPattison.net). Ted has just completed his book titled Inside Windows SharePoint Services 3.0 for Microsoft Press. sion-level assignments. For example, the code shown in Figure 5 allows you to create a new document library and to configure it with a unique set of permissions. This sample code breaks the default permission inheritance from the parent using a call to BreakRoleInheritance. If you call BreakRoleInheritance and pass a parameter value of true, the securable object is initially configured with an ACL that is a copy of the parent object’s ACL. If you call to BreakRoleInheritance and pass a parameter value of false, the securable object is initially configured with an empty ACL. That means this document library provides no access to users who are not either owners or site administrators. Windows SharePoint Services 3.0 has added a welcome security enhancement that allows you to configure permissions down to the level of the item or document. This is made possible through the WSS object model, since SPListItem objects also implement the ISecurableObject interface. The code in Figure 6 creates a new document within a document library and then configures it with a unique set of permissions that differ from its parent document library. Note that this code uses a utility method, called WriteDocument, that accepts an SPDocumentLibrary reference and a file name. The method implementation uses the Office Open XML File Formats to create a Word document and to write it back to the document library. The WriteDocument method returns an SPFile reference, which can then be used to access the SPListItem associated with the document, allowing you to break permission inheritance and assign a unique set of permissions. Figure 6 Set of Permissions that Differs from Its Parent SPWeb site = SPContext.Current.Web; Guid listID = site.Lists.Add(“Proposals”, “Library desc”, SPListTemplateType.DocumentLibrary); SPDocumentLibrary doclib = (SPDocumentLibrary)Web.Lists[ListID]; doclib.OnQuickLaunch = true; doclib.Update(); SPFile doc1 = WriteDocument(doclib, “Adventure Works Merger.docx”); doc1.Item.BreakRoleInheritance(false); SPGroup group = Web.Groups[“Litware Contact Managers”]; SPRoleAssignment assignContribute = new SPRoleAssignment(group); SPRoleDefinition roleContibute = this.Web.RoleDefinitions[“Contribute”]; assignContribute.RoleDefinitionBindings.Add(roleContibute); doc1.Item.RoleAssignments.Add(assignContribute); doc1.Item.Update(); Office Space february2008 59 http://msdn.microsoft.com/msdnmag/code08.aspx http://www.TedPattison.net http://msdn.microsoft.com/msdnmag/cdrom/2006 http://msdn.microsoft.com/msdnmag/cdrom/2006
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.