Tuesday, January 13, 2009

Security Classes

SPGroup
SPUser
SPRoleAssignment
SPRoleDefinition

Setting Unique Folder Permissions

SPPrincipal principal = userToAdd;
SPFolderCollection submissions = web.GetFolder("Submissions").SubFolders;
SPFolder newFolder = submissions.Add("testfolder");

SPListItem folderItem = newFolder.Item;
folderItem.BreakRoleInheritance(true);
folderItem.Update();
SPRoleAssignment folderRoleAssignment = new SPRoleAssignment(principal); SPRoleAssignmentCollection folderRoleAssignments = folderItem.RoleAssignments; SPRoleDefinitionBindingCollection folderRoleDefBindings = folderRoleAssignment.RoleDefinitionBindings;
folderRoleDefBindings.Add(roleDefinitions["Contribute"]);
folderRoleAssignments.Add(folderRoleAssignment);

Adding User to a SharePoint Group

web.AllowUnsafeUpdates = true;
SPGroup studentsGroup = web.Groups["Students"];
SPUser userToAdd = web.SiteUsers["pilothouse\\testgroup"];
studentsGroup.AddUser(userToAdd);
Response.Write("Added user successfully");

Role Assignments For the Site

SPRoleAssignmentCollection webRoleAssignments =
web.RoleAssignments;

Response.Write("All role assignments in this site: " + "
");

foreach (SPRoleAssignment webRoleAssignment in webRoleAssignments)
{
Response.Write(webRoleAssignment.Member.Name + " "
+ webRoleAssignment.RoleDefinitionBindings[0].Name + " "
+ webRoleAssignment.Parent.ToString() + "
");
}

Role Definitions

// Role definitions are represented as "Permissions Levels"
// in user interface

SPRoleDefinitionCollection roleDefinitions = web.RoleDefinitions;
Response.Write("All role definitions in this site: " + "
");
foreach (SPRoleDefinition roleDefinition in roleDefinitions)
{
Response.Write(roleDefinition.Name + "
");
}

SharePoint Group Users

// All users in "Students" SharePoint group
users = web.Groups["Students"].Users;

Response.Write("All users in \"Students\" SharePoint group: " + "
");
foreach (SPUser user in users)
{
Response.Write(user.Name + "
");
}

SharePoint Group Security Principals

// SharePoint Groups that are Security Principals on this website
SPGroupCollection groups = web.Groups;
Response.Write("All SharePoint groups in this site: "+"
");
foreach (SPGroup group in groups)
{
Response.Write(group.Name + "
");
}

// All SharePoint Groups in this site collection
groups = web.SiteGroups;
Response.Write("All SharePoint groups in this site collection: " + "
"); foreach (SPGroup group in groups)
{
Response.Write(group.Name + "
");
}

Domain User and Group Security Principals

// All domain users and groups
// that are Security Principals for the website
// This is the list you see in /_layouts/user.aspx

SPUserCollection users = web.Users;
Response.Write("All domain user principals in this site: " + "
"
);
foreach (SPUser user in users)
{
Response.Write(user.Name + "
"
);
}

// All domain users and groups within the site collection
users = web.SiteUsers;
Response.Write("All users in this site collection: " + "
"
);
foreach (SPUser user in users)
{
Response.Write(user.Name + "
"
);
}

Security Permission Inheritance

SPSite site = SPContext.Current.Site;
SPWeb web = SPContext.Current.Web;

// Test to see if the site has unique permissions
if (web.HasUniqueRoleAssignments)
{
Response.Write(web.Title + " does not inherit permissions
"
);
}
else
{
Response.Write(web.Title + " inherits permissions
"
);
}
 
Sell my car, Used car for sale, Free Automotive Classifieds