【问题标题】:get specific projects that a user is a part of获取用户参与的特定项目
【发布时间】:2014-10-12 20:30:55
【问题描述】:

这里我使用 IIdentityManagementService 按名称检索指定用户。现在我只想检索他们所属的团队项目,并且可以在 TFS 中为其创建任务/工作项。我的程序允许用户在 TFS 中创建任务,我只希望他们能够看到他们有权访问以创建任务/工作项的项目列表。

var tfsTpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://dotnettfs:8080/tfs/"));
identityService = tfsTpc.GetService<IIdentityManagementService>();
userId = identityService.ReadIdentity(
  IdentitySearchFactor.DisplayName,
  strOutlookUser,
  MembershipQuery.Direct,
  ReadIdentityOptions.None
);

userTpc = new TfsTeamProjectCollection(tfsTpc.Uri, userId.Descriptor);
cssService = (ICommonStructureService4)userTpc.GetService(typeof(ICommonStructureService4));

 wis = userTpc.GetService<WorkItemStore>();
 lstAllProjects.AddRange(cssService.ListAllProjects().ToList());
 List<string> lstViewProjectNames = lstAllProjects.Select(a => a.Name).ToList();

现在,当我希望它仅显示检索到的用户有权访问的那些项目时,该列表会显示该软件集合中的所有项目。

然后他们可以创建任务并为其中一个项目指定迭代和区域。

var store = wis.Projects[0]; //should be a specified project, not the first element.
WorkItem pbi = new WorkItem(store.WorkItemTypes["Product Backlog Item"]);

pbi.IterationPath = lstIterations.Where(a => a.Name == selectedIteration.ToString())
                      .Select(a => a.Path).First().ToString();


pbi.AreaPath = lstAreas.Where(a => a.Name == selectedArea.ToString())
                      .Select(a => a.Path).First().ToString();

【问题讨论】:

  • 您是否查看过 GetEffectivePermissions:msdn.microsoft.com/en-us/library/… 通常源代码管理根目录是 $/TeamProjectName。您可能只检查那个的权限。虽然我还没有尝试过,但如果我这样做了,我会尝试。
  • 我找不到对 VersionControl 的引用。根据 Visual Studio 2013,Microsoft.TeamFoundation.VersionControl.Client 不存在。没关系,我找到了。
  • 听起来您缺少参考资料?
  • 你也可以看看这个:codeproject.com/Articles/262673/…
  • 由于某种原因,在我的添加参考搜索中没有返回任何搜索结果。我手动找到的。

标签: c# tfs


【解决方案1】:

我只希望他们能够看到他们参与的项目列表 有权创建任务/工作项。

工作项与区域相关联,区域与团队项目相关联。

基本步骤是:

1) 以相关用户身份连接到 TFS

2) 检索有问题的团队项目

3) 获取相关团队项目的区域

4) 检查每一个创建工作项的能力(您可能可以只对根区域节点进行递归检查)

你需要的用途(可能不需要全部):

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

IIdentityManagementService identityManagementService = tpc.GetService<IIdentityManagementService>();
TfsTeamProjectCollection tpc = GetTfsCollection();
TeamFoundationIdentity identity = identityManagementService.ReadIdentity(IdentitySearchFactor.AccountName, @"Domain\username", MembershipQuery.None, ReadIdentityOptions.None);
TfsTeamProjectCollection impersonatedTPC = new TfsTeamProjectCollection(new Uri(this._tfsUri, this._tfsCollectionName), identity.Descriptor);
WorkItemStore impersonatedWIS = impersonatedTPC.GetService<WorkItemStore>();
ProjectCollection impersonatedProjects = impersonatedWIS.Projects;
foreach (Project p in impersonatedProjects)
{
    if (p.Name == "My Team Project")
    {
        NodeCollection areas = p.AreaRootNodes;
        foreach (Node area in areas)
        {
            if (area.HasWorkItemWriteRightsRecursive)
            {
                //They do have rights
            }
        }
    }
}

请注意,我调用了我自己的用户定义函数 GetTfsCollection()(这只是我构造的类,将根 tfs uri 和集合名称作为字符串传递)。我也没有进行任何异常处理,只是展示了基础知识:

private TfsTeamProjectCollection GetTfsCollection()
{
    return TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(this._tfsUri, this._tfsCollectionName));            
}

【讨论】:

    猜你喜欢
    • 2021-05-25
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多