祥叔学编程

祥叔学编程
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

TFS二次开发10——分组(Group)和成员(Member)

Posted on 2014-03-15 17:27  祥叔  阅读(1401)  评论(0编辑  收藏  举报

TFS SDK 10 ——分组(Group)和成员(Member)

 这篇来介绍怎样读取TFS服务器上的用户信息    

 

首先TFS默认有如下分组(Group):

SharePoint Web Application Services

Team Foundation Administrators

Team Foundation Proxy Service Accounts

Team Foundation Service Accounts

Team Foundation Valid Users

Work Item Only View Users   

 

 

其中

Team Foundation Valid Users 包含其他所有分组

Team Foundation Administrators 包含 Team Foundation Service Accounts

然后每一个Collection也有类似如上的默认分组,及同样的包含关系

Project Collection Administrators

Project Collection Build Administrators

Project Collection Build Service Accounts

Project Collection Proxy Service Accounts

Project Collection Service Accounts

Project Collection Test Service Accounts

Project Collection Valid Users

其中

Project Collection Valid Users 包含其他所有分组

Project Collection Administrators包含Project Collection Service Accounts

我们可以在TFS server 端看到这些。

using Microsoft.TeamFoundation.Client;

using Microsoft.TeamFoundation.Server;

using Microsoft.TeamFoundation.VersionControl.Client;

//连接TFS
string tpcURL = "http://127.0.0.1:8080/tfs/defaultcollection";
TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(tpcURL));
IGroupSecurityService gss = (IGroupSecurityService)tpc.GetService(typeof(IGroupSecurityService));//所有关于分组和成员的相关的操作都是基于IGroupSecurityService的。
//1:获取全部用户
//先查出用户的Id。参数 QueryMembership 指定是否包含其下从属分组的用户
Identity sids = gss.ReadIdentity(SearchFactor.AccountName, "Team Foundation Valid Users", QueryMembership.Expanded);

 

//通过id获取用户的信息,包括名称,邮箱等等
var members = gss.ReadIdentities(SearchFactor.Sid, sids.Members, QueryMembership.Expanded);

 

 //2:获取指定项目下的所有分组
 VersionControlServer version = tpc.GetService(typeof(VersionControlServer)) as VersionControlServer;
 TeamProject[] allProjects = version.GetAllTeamProjects(true);
 string projectUrl = allProjects[0].ArtifactUri.AbsoluteUri;
 Identity[] groups = gss.ListApplicationGroups(projectUrl);
 //3:添加分组
 string groupName = "MyGroup";
 string desp = "My Group Description";
 string groupSid = gss.CreateApplicationGroup(projectUrl, groupName, desp);
 //4: 删除分组
 gss.DeleteApplicationGroup(groupSid);
 //5:获取指定分组下的成员
 Identity group = groups[0];
 var gsids = gss.ReadIdentity(SearchFactor.Sid, group.Sid, QueryMembership.Expanded);
 Identity[] gmembers = gss.ReadIdentities(SearchFactor.Sid, gsids.Members, QueryMembership.Expanded);
 //成员不一定是指用户(User) ,也可能是分组(Group)
 //成员的类型有如下几种:
 //gmembers[0].Type== IdentityType.ApplicationGroup;
 //gmembers[0].Type== IdentityType.InvalidIdentity
 //gmembers[0].Type== IdentityType.UnknownIdentityType
 //gmembers[0].Type== IdentityType.WindowsGroup
 //gmembers[0].Type== IdentityType.WindowsUser
 
//6:把指定成员添加到指定分组 string memberSid = gmembers[0].Sid; gss.AddMemberToApplicationGroup(group.Sid, memberSid); //7:把指定成员从指定分组移除 gss.RemoveMemberFromApplicationGroup(group.Sid, memberSid);