权限操作(部分代码,仅共参考)

  1 /// <summary>
  2        /// 判断组是否存在
  3        /// </summary>
  4        /// <param name="web"></param>
  5        /// <param name="groupname"></param>
  6        /// <returns></returns>

  7        public bool IsExistGroup(SPWeb web, string groupname)
  8        {
  9            try
 10            {
 11                foreach (SPGroup grouplist in web.SiteGroups)//判断组是否存在
 12                {
 13                    if (grouplist.ToString().ToLower() == groupname.ToLower())
 14                        return true;
 15                }

 16                return false;
 17            }

 18            catch (Exception)
 19            {
 20                throw;
 21            }

 22        }

 23
 24        /// <summary>
 25        /// 新建组
 26        /// </summary>
 27        /// <param name="web"></param>
 28        /// <param name="groupname"></param>
 29        /// <param name="member"></param>
 30        /// <param name="spuser"></param>
 31        /// <param name="description"></param>
 32        /// <returns></returns>

 33        public bool AddGroup(SPWeb web, string groupname, SPMember member, SPUser spuser, string description)
 34        {
 35            try
 36            {
 37                if (!IsExistGroup(web, groupname))
 38                {
 39                    web.SiteGroups.Add(groupname, member, spuser, description);//新建组
 40                    return true;
 41                }

 42                return false;
 43            }

 44            catch (Exception)
 45            {
 46
 47                throw;
 48            }

 49        }

 50
 51        /// <summary>
 52        /// 判断指定组是否存在用户
 53        /// </summary>
 54        /// <param name="web"></param>
 55        /// <param name="username">Domin\\Name形式</param>
 56        /// <param name="groupname"></param>
 57        /// <returns></returns>

 58        public bool IsExistUser(SPWeb web, string username, string groupname)
 59        {
 60            try
 61            {
 62                foreach (SPUser userlist in web.SiteGroups[groupname].Users)//判断指定组是否存在用户
 63                {
 64                    if (userlist.ToString().ToLower() == username.ToLower())
 65                        return true;
 66                }

 67                return false;
 68            }

 69            catch (Exception)
 70            {
 71                throw;
 72            }

 73        }

 74
 75        /// <summary>
 76        /// 根据指定的组新建用户
 77        /// </summary>
 78        /// <param name="web"></param>
 79        /// <param name="loginname">登录名:Domin\\Name形式</param>
 80        /// <param name="groupname">组名称</param>
 81        /// <param name="email">Email</param>
 82        /// <param name="cnname">中文名</param>
 83        /// <param name="notes">用户说明</param>
 84        /// <returns>bool</returns>

 85        public bool AddUserToGroup(SPWeb web, string loginname, string groupname, string email, string cnname, string notes)
 86        {
 87            try
 88            {
 89                if (!IsExistUser(web, loginname, groupname))
 90                {
 91                    web.SiteGroups[groupname].AddUser(loginname, email, cnname, notes);//新建用户
 92                    return true;
 93                }

 94                return false;
 95            }

 96            catch (Exception)
 97            {
 98
 99                throw;
100            }

101        }

102        /// <summary>
103        /// 组权限分配与定义(New)
104        /// </summary>
105        /// <param name="web"></param>
106        /// <param name="groupname"></param>
107        /// <param name="roles"></param>
108        /// <returns></returns>

109        public bool AddGroupToRoles(SPWeb web, string groupname, string[] roles)
110        {
111            try
112            {
113                string[] _roles = roles;
114                int rolemun = _roles.Length;
115
116                if (IsExistGroup(web, groupname))
117                {
118                    //改变站点继承权
119                    if (!web.HasUniqueRoleDefinitions)
120                    {
121                        web.RoleDefinitions.BreakInheritance(truetrue);//复制父站点角色定义并且保持权限
122                    }

123
124                    //站点继承权改变后重新设置状态
125                    web.AllowUnsafeUpdates = true;
126
127                    //组权限分配与定义(New)
128                    SPRoleDefinitionCollection roleDefinitions = web.RoleDefinitions;
129                    SPRoleAssignmentCollection roleAssignments = web.RoleAssignments;
130                    SPMember memCrossSiteGroup = web.SiteGroups[groupname];
131                    SPPrincipal myssp = (SPPrincipal)memCrossSiteGroup;
132                    SPRoleAssignment myroles = new SPRoleAssignment(myssp);
133                    SPRoleDefinitionBindingCollection roleDefBindings = myroles.RoleDefinitionBindings;
134                    if (rolemun > 0)
135                    {
136                        for (int i = 0; i < rolemun; i++)
137                        {
138                            roleDefBindings.Add(roleDefinitions[_roles[i]]);
139                        }

140                    }

141                    roleAssignments.Add(myroles);
142                    return true;
143                }

144                return false;
145            }

146            catch (Exception)
147            {
148
149                throw;
150            }

151        }

152
153        protected void btnCreateSite_Click(object sender, EventArgs e)
154        {  
155            
156            try
157            {
158                SPSite site = new SPSite("http://contososrv:8010/");
159
160                //string spwt = SPWebTemplate.WebTemplateSTS;//默认选择协作站点模板 
161
162                web = site.OpenWeb("Test/");
163                web.AllowUnsafeUpdates = true;
164                SPMember memUser = web.Users[0];
165                SPUser suser = web.Users[0];
166
167
168                //新建组
169                //web.SiteGroups.Add("skyteam3", memUser, suser, "new skyteam");
170                //web.SiteGroups["skyteam3"].AddUser("adr\\administrator", "pccai@adr.svic1", "蔡", "系统维修人员");
171                //web.Groups["skyteam2"].AddUser("adr\\pccai", "pccai@adr.svic1", "蔡", "系统维修人员");
172
173
174                //新建子站点
175                string currentTemplate = web.WebTemplate;
176                //web.Webs.Add("Test2", "站点名称2", "站点描述2", 2052, "_GLOBAL_#0", true, false);
177
178
179                //打开子站点
180                SPWeb web2 = site.OpenWeb("Test/Test2");
181                web2.AllowUnsafeUpdates = true;
182               // web2.SiteGroups.Add("skyteam6", memUser, suser, "new skyteam");//新建组
183               // web2.SiteGroups["skyteam6"].AddUser("adr\\administrator", "administrator@adr.svic1", "边", "系统维修人员");
184
185
186                //改变站点继承权
187                if (!web2.HasUniqueRoleDefinitions)
188                {
189                    web2.RoleDefinitions.BreakInheritance(truetrue);
190                }

191
192                //站点继承权改变后重新设置状态
193                web2.AllowUnsafeUpdates = true;
194
195
196
197                //添加权限级别 (Role) 
198                //SPRoleDefinition roleDefinition = new SPRoleDefinition();
199                //roleDefinition.Name = "项目角色";
200                //roleDefinition.Description = "项目角色可以批准所有项目情况.";
201                //roleDefinition.BasePermissions = SPBasePermissions.FullMask ^ SPBasePermissions.ManagePermissions;
202                //web2.RoleDefinitions.Add(roleDefinition);
203
204
205                //更改权限级别 (Permissions) 
206                SPRoleDefinitionCollection roles = web2.RoleDefinitions;
207                SPRoleDefinition roleDefinition1 = roles["读取"];
208                roleDefinition1.BasePermissions = SPBasePermissions.AddListItems |
209                    SPBasePermissions.BrowseDirectories |
210                    SPBasePermissions.EditListItems |
211                    SPBasePermissions.DeleteListItems |
212                    SPBasePermissions.AddDelPrivateWebParts;
213                roleDefinition1.Update();
214
215
216                //用户权限分配与定义(New)
217                SPRoleDefinitionCollection roleDefinitions = web2.RoleDefinitions;
218                SPRoleAssignmentCollection roleAssignments = web2.RoleAssignments;
219                SPRoleAssignment roleAssignment = new SPRoleAssignment("adr\\administrator""administrator@Somewhere.com""Display_Name""Notes");
220                SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;
221                roleDefBindings.Add(roleDefinitions["项目角色"]);
222                roleAssignments.Add(roleAssignment);
223
224
225                //权限定义(Old)
226                //SPRoleCollection siteGroups = web2.Roles;
227                //siteGroups.Add("skyteam6", "Description", SPRights.ManageWeb | SPRights.ManageSubwebs);
228
229
230                //获得权限定义
231                SPRoleDefinition sprole = roleDefinitions.GetByType(SPRoleType.Reader);
232                string spname = sprole.Name;
233
234
235                //组权限分配与定义(New)
236                SPRoleDefinitionCollection roleDefinitions1 = web2.RoleDefinitions;
237                SPRoleAssignmentCollection roleAssignments1 = web2.RoleAssignments;
238                SPMember memCrossSiteGroup = web2.SiteGroups["skyteam6"];
239                SPPrincipal myssp = (SPPrincipal)memCrossSiteGroup;
240                SPRoleAssignment myroles = new SPRoleAssignment(myssp);
241                SPRoleDefinitionBindingCollection roleDefBindings1 = myroles.RoleDefinitionBindings;
242                roleDefBindings1.Add(roleDefinitions1["设计"]);
243                roleDefBindings1.Add(roleDefinitions1["读取"]);
244                roleAssignments1.Add(myroles);
245
246
247                //组权限分配与定义(Old)
248                //SPMember member = web2.Roles["skyteam"];
249                //web2.Permissions[member].PermissionMask =
250                //    SPRights.ManageLists | SPRights.ManageListPermissions;
251
252
253
254                //更改列表权限(Old)
255                //SPList list = site.Lists["通知"];
256                //SPPermissionCollection perms = list.Permissions;
257                //SPUserCollection users = site.Users;
258                //SPMember member = users["ADR\\pccai"];
259                //list.Permissions[member].PermissionMask = SPRights.AddListItems | SPRights.EditListItems;
260
261
262
263              //  PermissionCollection perc = web.Permissions;
264                //perc.AddUser("adr\\administrator", "administrator@adr.srvc1", "title", "Notes", PortalRight.AllSiteRights);
265               // SecurityManager.AddRole(context, "title", "descriptions", PortalRight.ManageSite);
266
267
268           
269            }

270            catch (Exception)
271            {
272                throw;
273            }

274
275        }
posted @ 2007-01-12 19:23  pccai  阅读(1211)  评论(0编辑  收藏  举报