2011-11-21 35 views
3

我想在每個團隊項目中創建一個新的應用程序組並授予其權限。我到目前爲止已經做的是遍歷所有的項目和創建組,如果不存在:授予自定義TFS應用程序組的權利

static void Main(string[] args) 
{ 
    m_TfsServer = new TfsTeamProjectCollection( 
     new Uri("http://server:port/vdir"), 
     System.Net.CredentialCache.DefaultNetworkCredentials, 
     new UICredentialsProvider()); 
    m_TfsServer.EnsureAuthenticated(); 

    m_TfsSecurityService = m_TfsServer.GetService<IGroupSecurityService>(); 

    var structService = m_TfsServer.GetService<ICommonStructureService>(); 
    foreach (var p in structService.ListAllProjects()) 
    { 
     string groupSid; 
     if (!GroupExist(p.Uri, GroupName)) 
     { 
      groupSid = m_TfsSecurityService.CreateApplicationGroup( 
       p.Uri, 
       GroupName, 
       GroupDescription); 
     } 
     else 
     { 
      groupSid = GetApplicationGroupSid(p.Uri, GroupName); 
     } 
     Identity userIdentity = m_TfsSecurityService.ReadIdentityFromSource( 
      SearchFactor.AccountName, 
      UserName); 
     if (!m_TfsSecurityService.IsMember(groupSid, userIdentity.Sid)) 
     { 
      m_TfsSecurityService.AddMemberToApplicationGroup( 
       groupSid, 
       userIdentity.Sid); 
     } 
    } 
} 

private static bool GroupExist(string projectUri, string groupName) 
{ 
    bool result = false; 
    Identity[] groups = 
     m_TfsSecurityService.ListApplicationGroups(projectUri); 
    foreach (Identity group in groups) 
    { 
     result |= group.SecurityGroup && group.DisplayName.Equals(groupName); 
    } 
    return result; 
} 

private static string GetApplicationGroupSid( 
    string projectUri, 
    string groupName) 
{ 
    return m_TfsSecurityService.ListApplicationGroups(projectUri) 
     .Where(g => g.DisplayName.Equals(groupName)) 
     .Select(g => g.Sid) 
     .First(); 
} 

剩下的唯一的事情就是授予「查看項目級信息」權到組。

[編輯]

我發現一些使用VersionControlService授予權利:

var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(ServerUri); 
tfs.EnsureAuthenticated();   
var vcs = tfs.GetService<VersionControlServer>(); 
//vcs.SetPermissions(new SecurityChange[] { }); ??? 

但是我沒有找到任何文件如何授予權的一個羣體,所以我甚至不確定這個解決方案是否是正確的方法。

[/編輯]

有沒有人用誰已經通過TFS API授予權的TFS權限管理或任何經驗?

回答