1,清明节闲来无聊,敲代码吧,不知道敲什么,不敲吧,又好像比较颓废,不思进取。遂把以前项目中别的同事负责的权限模块的代码看一看,做俩个Demo。
(1)代码创建组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 |
protected
void CreateGroup_Click( object
sender, EventArgs e) { string
groupName = this .groupTitle.Text; string
groupDescription = "I‘m WanRen,I‘m creating group" ; SPSecurity.RunWithElevatedPrivileges( delegate () { using
(SPSite site = new
SPSite(SPContext.Current.Web.Url)) { using
(SPWeb web = site.OpenWeb()) { web.AllowUnsafeUpdates = true ; //组名,拥有者(我理解为创建者,谁创的就是谁的罢),组里的默认用户,组描述 web.SiteGroups.Add(groupName, web.CurrentUser, null , groupDescription); //获取到刚刚创建的这个组 SPGroup group
= web.SiteGroups[groupName]; //将当前用户添加到改组中 SPUser user = web.EnsureUser( "liuwanren" ); //不要将系统用户往组里添加,是显示不出来的 group .Users.Add(user.LoginName, user.Email, user.Name, user.Notes); group .Update(); //基于这个组定义一个角色 SPRoleAssignment assignment = new
SPRoleAssignment( group ); //创建一个权限级别 SPRoleDefinition definition = web.RoleDefinitions.GetByType(SPRoleType.Administrator); //给角色绑定一个权限 assignment.RoleDefinitionBindings.Add(definition); //将该角色添加到web的角色集合中 web.RoleAssignments.Add(assignment); web.Update(); web.AllowUnsafeUpdates = false ; ScriptManager.RegisterStartupScript( this , this .GetType(), "result" , "alert(‘"
+ "创建成功"
+ "‘);" , true ); } } }); } |
(2)代码删除组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 |
protected
void DeleteGroup_Click( object
sender, EventArgs e) { string
groupName = this .groupTitle.Text; SPSecurity.RunWithElevatedPrivileges( delegate () { using
(SPSite site = new
SPSite(SPContext.Current.Web.Url)) { using
(SPWeb web = site.OpenWeb()) { foreach
(SPGroup group
in web.SiteGroups) { if
( group .Name.Equals(groupName)) { web.SiteGroups.Remove( group .Name); break ; } } } } }); ScriptManager.RegisterStartupScript( this , this .GetType(), "result" , "alert(‘"
+ "删除成功"
+ "‘);" , true ); } |
(3)获取组并从组中移除用户
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 |
protected
void GetGroup_Click( object
sender, EventArgs e) { string
groupName = this .groupTitle.Text; SPSecurity.RunWithElevatedPrivileges( delegate () { using
(SPSite site = new
SPSite(SPContext.Current.Web.Url)) { using
(SPWeb web = site.OpenWeb()) { foreach
(SPGroup group
in web.SiteGroups) { if
( group .Name.Equals(groupName)) { if
( group .Users.Count > 0) { foreach
(SPUser user in
group .Users) { group .Users.Remove(user.LoginName); } } break ; } } } } }); ScriptManager.RegisterStartupScript( this , this .GetType(), "result" , "alert(‘"
+ "获取并移除用户成功"
+ "‘);" , true ); } |
(4)获取组,判断组中是否存在某用户。
做完这个Demo,让我看到到了我这个初级程序员和同事这个高级程序员(5年工作经验)之间的差别所在。
先看我的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 |
protected
void InGroup_Click( object
sender, EventArgs e) { string
groupName = this .groupTitle.Text; string
userLoginName = this .userLoginName.Text; string
inGroup = "用户在组中" ; SPSecurity.RunWithElevatedPrivileges( delegate () { using
(SPSite site = new
SPSite(SPContext.Current.Web.Url)) { using
(SPWeb web = site.OpenWeb()) { SPUser user = web.EnsureUser(userLoginName); foreach
(SPGroup group
in user.Groups) { if
( group .Name.Equals(groupName)) { inGroup = "用户存在组中" ; } else { inGroup = "用户不存在组中" ; } } } } }); ScriptManager.RegisterStartupScript( this , this .GetType(), "result" , "alert(‘"
+ inGroup + "‘);" , true ); } |
同事的代码
1
2
3
4
5
6
7
8
9
10 |
public
static class SPUserExtension { public
static bool InGroup( this
SPUser user, SPGroup group ) { return
user.Groups.Cast<SPGroup>() .Any(g => g.ID == group .ID); } } |
哎,虽然都实现了同样的效果,但是一看这代码立马就显示出了一个程序员水平的高低。这扩展方法、泛型啥的我也都懂。水平在这儿摆着,即使遇到新功能,想到的也只是用最普通的解决办法解决问题。想不到最直接有效的方法。
SharePoint2013代码操作权限组的几个Demo,布布扣,bubuko.com
原文:http://www.cnblogs.com/wanren/p/3652811.html