TFS SDK 10 ——分組(Group)和成員(Member)服務器
這篇來介紹怎樣讀取TFS服務器上的用戶信息 ui
首先TFS默認有以下分組(Group):spa
SharePoint Web Application Servicescode
Team Foundation Administratorsserver
Team Foundation Proxy Service Accountsblog
Team Foundation Service Accountsip
Team Foundation Valid Usersstring
Work Item Only View Users it
其中io
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);