Requirements:
There is a document library, and there are many folders below, each folder has a different permission system, so it is very troublesome to maintain these permissions. therefore, a webpart is required to manage the content of these permissions.
Analysis:
1. Use a treeview to list the contents of all folders in the document library.
2. Use a gridview to list the permissions of each folder. When you click a folder, the corresponding permissions are displayed.
3. The gridview function is deleted.
4. You can add uers to a group and add users/groups to some folders.
Implementation:
1. A class is created to display part of the permission information .:
Class Permission <T>
{
Private T _ strUsers;
Private T _ strPermissions;
Public T Users
{
Get {return _ strUsers ;}
Set {_ strUsers = value ;}
}
Public T Permissions
{
Get {return _ strPermissions ;}
Set {_ strPermissions = value ;}
}
Public Permission ()
{
}
Public Permission (T strUsers, T strPermissions)
{
_ StrUsers = strUsers;
_ StrPermissions = strPermissions;
}
}
2. Permission to list related folders:
List <Permission <string> GetItemsPermissions (string ItemName)
{
List <Permission <string> subListP = new List <Permission <string> ();
SPListItemCollection oSPListItems = currentList. Folders;
Foreach (SPListItem oSPItem in oSPListItems)
{
If (oSPItem. Name = ItemName)
{
SPRoleAssignmentCollection oSPRoles = oSPItem. RoleAssignments;
SubListP = GetRoles (oSPRoles );
}
}
Return subListP;
}
List <Permission <string> GetRoles (SPRoleAssignmentCollection oSPRoles)
{
List <Permission <string> resultList = new List <Permission <string> ();
Foreach (SPRoleAssignment oSPRole in oSPRoles)
{
SPRoleDefinitionBindingCollection oSPRoleDefinitions = oSPRole. RoleDefinitionBindings;
String strPermissionLevelName = string. Empty;
Foreach (SPRoleDefinition oSPRoleDefinition in oSPRoleDefinitions)
{
StrPermissionLevelName + = oSPRoleDefinition. Name + ",";
}
StrPermissionLevelName = strPermissionLevelName. Substring (0, strPermissionLevelName. Length-1 );
Permission <string> subPermission = new Permission <string> (oSPRole. Member. Name, strPermissionLevelName );
ResultList. Add (subPermission );
}
Return resultList;
}
3. Delete permission:
Void DeleteRole (SPRoleAssignmentCollection oSPRoles, string strGroupName)
{
Foreach (SPRoleAssignment oSPRole in oSPRoles)
{
If (oSPRole. Member. Name = strGroupName)
{
SPPrincipal currentPrincipal = oSPRole. Member;
OSPRoles. Remove (currentPrincipal );
Break;
}
}
}
// Delete permissions in selected folder.
SPListItemCollection oSPListItems = myList. Folders;
Foreach (SPListItem oSPItem in oSPListItems)
{
If (oSPItem. Name = strNodeName)
{
If (! OSPItem. HasUniqueRoleAssignments) oSPItem. BreakRoleInheritance (true );
SPRoleAssignmentCollection oSPRoles = oSPItem. RoleAssignments;
Foreach (string strGroup in ListGroups)
{
DeleteRole (oSPRoles, strGroup );
}
}
}
4. Add user to group:
Int AddUserToGroup (string strUserName, string strGroupName, SPWeb myWeb)
{
SPUser oSPUser = GetSPUser (strUserName, myWeb );
Int GroupId = 0;
If (oSPUser! = Null)
{
SPGroupCollection oSPGroups = myWeb. Groups;
Foreach (SPGroup oSPGroup in oSPGroups)
{
If (oSPGroup. Name = strGroupName)
{
OSPGroup. AddUser (oSPUser );
GroupId = oSPGroup. ID;
}
}
}
Return GroupId;
}
5. Add users/groups to the specified folder:
// Obtain the selected permissions
List <string> ListPermissions = new List <string> ();
Foreach (ListItem Item in CheckBoxListPermissions. Items)
{
If (Item. Selected)
{
ListPermissions. Add (Item. Text );
}
}
// Obtain the user/group
If (currentUser! = Null)
{
OSPRole = new SPRoleAssignment (currentUser );
}
Else if (currentGroup! = Null)
{
OSPRole = new SPRoleAssignment (currentGroup );
}
Foreach (string strRoleDefinition in ListPermissions)
{
OSPRole. RoleDefinitionBindings. Add (myWeb. RoleDefinitions [strRoleDefinition]);
}
// Add to the specified folder
SPListItemCollection oSPListItems = myList. Folders;
Foreach (SPListItem oSPItem in oSPListItems)
{
If (! OSPItem. HasUniqueRoleAssignments) oSPItem. BreakRoleInheritance (true );
If (oSPItem. Name = strSelectNode)
{
OSPItem. RoleAssignments. Add (oSPRole );
}
}
Effect:
1. display the permission page:
2. Add users/groups pages.
Reference address: http://blog.csdn.net/jiangxng/archive/2008/05/29/2494427.aspx