SharePoint文档库中文件夹的权限管理

需求:
      有一个文档库, 然后下面有很多的文件夹, 每个文件夹都有不同的权限系统, 因此当需要维护这些权限的时候,非常麻烦. 所以需要一个webpart来管理这些权限的内容.

分析:
     1. 用一个treeview列出文档库中的所有文件夹的内容.
     2. 使用一个gridview列出每个文件夹的权限, 点击不同的文件夹时候显示相应的权限.
     3. gridview有删除的功能.
     4. 可以添加uers到一个group, 把users/groups添加到某些文件夹中.

实现:

1. 创建了一个类显示权限的部分信息.:
    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. 列出相关文件夹的权限:
    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. 删除权限:
    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.  添加user到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.  添加users/groups到指定的文件夹中:
    // 获得选中的permissions
    List<string> ListPermissions = new List<string>();
    foreach (ListItem Item in CheckBoxListPermissions.Items)
    {
        if (Item.Selected)
        {
            ListPermissions.Add(Item.Text);
        }
    }
    // 获得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]);
    }
    // 添加到指定的文件夹中
    SPListItemCollection oSPListItems = myList.Folders;
    foreach (SPListItem oSPItem in oSPListItems)
    {
        if (!oSPItem.HasUniqueRoleAssignments) oSPItem.BreakRoleInheritance(true);
        if (oSPItem.Name == strSelectNode)
        {
            oSPItem.RoleAssignments.Add(oSPRole);
        }
    }
效果:
1.  显示权限的页面:


SharePoint文档库中文件夹的权限管理
2. 添加users/groups 的页面.
 
SharePoint文档库中文件夹的权限管理

 

 

引用地址:http://blog.csdn.net/jiangxng/archive/2008/05/29/2494427.aspx

 

你可能感兴趣的:(SharePoint)