MOSS-批量自定义列表项权限

MOSS-批量自定义列表项权限

   MOSS中的文档或列表项的权限默认是继承自所在的文档库的,如果要想单独设置某个文档或列表项的权限,必须要管理员在管理权限中一个个去调整,所以就想通过程序根据企业组织架构来批量生成或初始化所有文档各自的权限。
   要完成此需求需要解决几个问题:
(1)如何提升用户权限(模拟管理员权限)
(2)如何设置用户权限
最终代码分为以下几个步骤实现:
(1)模拟管理员权限
(2)断开继承的权限
(3)自定义列表项权限
代码如下:
#region 清空列表权限(断开继承的权限)
        private void DeleteListPermission(SPListItem item)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()      //用此方法模拟管理员账户运行此事件处理程序
                {
                    using (SPSite site = new SPSite(workflowProperties.SiteId))    //用此方法的话就不用dispose()了
                    {
                        using (SPWeb web = site.OpenWeb(workflowProperties.WebId))  //注意获得web的方法!!!
                        {
                            try
                            {
                                //清空所有权限
                                if (!item.HasUniqueRoleAssignments) item.BreakRoleInheritance(false);   //将此条目取消权限继承,如果是“false”,则将去除所有权限,只保留系统账户,如果是“true”,则将上一级权限复制过来。
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                    }
                }
                    );
        }
        #endregion
        #region 自定义列表项权限
        private void SetListPermission1(SPListItem item, string person, string sPermit)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()      //用此方法模拟管理员账户运行此事件处理程序
                {
                    using (SPSite site = new SPSite(workflowProperties.SiteId))    //用此方法的话就不用dispose()了
                    {
                        using (SPWeb web = site.OpenWeb(workflowProperties.WebId))  //注意获得web的方法!!!
                        {
                            try
                            {
                                SPList list = workflowProperties.List;     //获得触发事件的列表
                                int ishave = 0;//标识是否包含此用户
                                foreach (SPRoleAssignment ra in item.RoleAssignments)
                                {
                                    SPPrincipal sp = ra.Member;
                                    if (person.Contains(sp.Name.ToString()))//如果已包含此用户
                                    {
                                        //remove all permissions of the group
                                        ra.RoleDefinitionBindings.RemoveAll();
                                        //获取站点所有的Permissions
                                        SPRoleDefinitionCollection roles = web.RoleDefinitions;
                                        //获取sPermit权限
                                        SPRoleDefinition viewRoleDefinition = roles[sPermit];
                                        //判断这个Group/User是否已经包含sPermit权限,如果不包含就添加sPermit权限
                                        if (!ra.RoleDefinitionBindings.Contains(viewRoleDefinition))
                                        {
                                            ra.RoleDefinitionBindings.Add(viewRoleDefinition);
                                            ra.Update();
                                        }
                                        ishave = 1;
                                    }
                                }
                                if (ishave == 0)//如果没有包含此用户,则新建
                                {
                                    SPRoleAssignment ra1 = new SPRoleAssignment(web.EnsureUser(person));   //生成一个新的角色分配
                                    ra1.RoleDefinitionBindings.Add(web.RoleDefinitions[sPermit]);    //将此角色分配绑定“批准”权限级别
                                    item.RoleAssignments.Add(ra1);     //将此新权限绑定到列表条目上
                                }
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                    }
                }
                    );
        }
        #endregion
posted @ 2010-08-30 09:22  China2008  阅读(367)  评论(0编辑  收藏  举报