2016-01-06 77 views
0

你好,我想要使用TFS API來創建新組,因爲我有這樣的代碼:TFS API創建TFS組並設置權限?

var teamProjects = this.VersionControlServer.GetAllTeamProjects(false); 
foreach (var teamProject in teamProjects) 
{ 
    var result = _gss.CreateApplicationGroup(teamProject.ArtifactUri.AbsoluteUri, "NewGroup","TestDescription"); 

    //NOW I WANT TO SET THE PERMISSIONS FOR THIS GROUP 
} 

,因爲我需要設置權限「編輯項目級的信息」這組我試過很多方法和不同的方法,但任何東西似乎都能解決我的需求。這對於例如:

var ProjectSecurityToken = AuthorizationSecurityConstants.ProjectSecurityPrefix + teamProject.ArtifactUri.AbsoluteUri; 
var groupACL = securityNamespace.QueryAccessControlList(ProjectSecurityToken, new[] {list[4].Descriptor}, false); 

securityNamespace.SetAccessControlEntry(ProjectSecurityToken, new Microsoft.TeamFoundation.Framework.Client.AccessControlEntry(list[4].Descriptor, 115, 0), true); 

我已經硬編碼「列表[4]」因爲這是我剛創建的組,我需要一些幫助,看看有什麼是錯在我的代碼。我沒有收到任何錯誤消息,也不能正常工作。

+1

在我看來,TFSSecurity命令行是很容易,TFS API添加權限在服務器級別,收藏 - 用戶或組級別或項目級別組。您可以考慮使用TFSSecurity命令行:https://msdn.microsoft.com/Library/vs/alm/TFS/administer/command-line/tfssecurity-cmd#aplus –

+0

TFSSecurity正常工作!但API方法好一點。 我正在尋找一種方法來拒絕項目管理員的工作項定製,有沒有辦法做到這一點? – Fabito

回答

1

我能得到的權限已經通過以下代碼設置:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using Microsoft.TeamFoundation.Client; 
using Microsoft.TeamFoundation.Server; 
using Microsoft.TeamFoundation.VersionControl.Client; 
using Microsoft.TeamFoundation.Framework.Client; 

namespace API 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string project = "http://xxx.xxx.xxx.xxx:8080/tfs"; 
      TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(project)); 
      var tps = tpc.GetService<VersionControlServer>(); 
      var ttt = tps.GetTeamProject("ProjectName"); 
      ISecurityService securityService = tpc.GetService<ISecurityService>(); 
      System.Collections.ObjectModel.ReadOnlyCollection<SecurityNamespace> securityNamespaces = securityService.GetSecurityNamespaces(); 
      IGroupSecurityService gss = tpc.GetService<IGroupSecurityService>(); 
      Identity SIDS = gss.ReadIdentity(SearchFactor.AccountName, "GroupName", QueryMembership.Expanded);//GourName format: [ProjectName]\\GourpName 
      IdentityDescriptor id = new IdentityDescriptor("Microsoft.TeamFoundation.Identity", SIDS.Sid); 
      List<SecurityNamespace> securityList = securityNamespaces.ToList<SecurityNamespace>(); 
      string securityToken; 
      foreach (SecurityNamespace sn in securityList) 
      { 
       if (sn.Description.DisplayName == "Project") 
       { 
        securityToken = "$PROJECT:" + ttt.ArtifactUri.AbsoluteUri; 
        sn.SetPermissions(securityToken, id, 115, 0, true); 
       } 
      }     
     } 
    } 
} 
+0

它按預期工作!真棒! 我直接使用枚舉數來代替整數: sn.SetPermissions(securityToken,id,0,TeamProjectPermissions.AllPermissions,true); 有沒有辦法讓「創建標籤定義」設置?這是未使用此代碼設置的唯一權限。 – Fabito

+0

@Fabito看到這個鏈接:http://roadtoalm.com/2014/07/28/add-permissions-with-tfssecuritythe-ultimate-reference/ –

+0

似乎在鏈接說使用命名空間「標記」,但它doesn' t存在,我也沒有在MSDN或某個微軟網站上找到任何參考。 ( – Fabito