以编程方式获取TFS用户的文件夹权限

时间:2014-05-15 12:53:42

标签: c# tfs tfs-sdk

我正在尝试让用户访问文件夹以及该文件夹的权限列表。 尝试做类似于TFS命令的事情:

  

TF权限“$ MyRepository / MyFolder”/ r

到目前为止,我能够获取用户列表,所有子文件夹列表,然后查询每个用户的所有子文件夹的权限并检查“签入或合并”。这是我的工作代码:

string TfsRepository = "$/Some/Base/Repository";
TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer("https://tfs.myserver.net/tfs/DefaultCollection");
VersionControlServer vcs = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
ItemSet items;  

var identityManagementService = tfs.GetService<IIdentityManagementService>();
var collectionWideValidUsers = identityManagementService.ReadIdentity(IdentitySearchFactor.DisplayName,
                                                          "Project Collection Valid Users",
                                                          MembershipQuery.Expanded,
                                                          ReadIdentityOptions.None);


Console.WriteLine("Please wait while retrieving necessary information from {0}:", TfsRepository);
var validMembers = identityManagementService.ReadIdentities(collectionWideValidUsers.Members,
                                                            MembershipQuery.Expanded,
                                                            ReadIdentityOptions.ExtendedProperties);
var memberNames = validMembers
          .Where(_ => !_.IsContainer && _.Descriptor.IdentityType != "Microsoft.TeamFoundation.UnauthenticatedIdentity")
          .Select(_ => _.UniqueName)
          .OrderBy(_ => _)
          .ToList();

string[] permissions;
bool UserCheck = false;
if (memberNames != null && items != null)
{
    foreach (var userName in memberNames)
    {
        Console.WriteLine("Querying User: {0}\n", userName);
        for (int i = 0; i >= items.Items.Length; i++)
        {
            if (items.Items[i].ItemType == ItemType.Folder)
            {
                string TfsRepositoryDir = items.Items[i].ServerItem;
                Console.WriteLine("    Checking:{0}", TfsRepositoryDir);
                permissions = vcs.GetEffectivePermissions(userName, TfsRepositoryDir);

                if (permissions != null && permissions.Length > 0)
                {
                    foreach (string perm in permissions)
                    {
                        if (perm.ToLower() == "checkin" || perm.ToLower() == "merge")
                            UserCheck = true;
                    }

                    if (UserCheck)
                    {
                        string.Join(", ", permissions);
                        Console.Write("User: {0}:  Repo: {1}: Permissions:{2} \n ", userName, TfsRepositoryDir, string.Join(", ", permissions));
                        UserCheck = false;
                    }

                }
            }
        }
    }
}

但您可能已经猜到,这需要很长时间,因为用户数量少于我们在存储库中的文件夹。因此,查询每个文件夹以获得用户的许可需要花费一些时间。

问题:我正在寻找一种方法,以查询每个用户的特定文件夹权限。例如,我想向用户“Ahmed”查询它有多少个文件夹可用于签入和合并。怎么做到这一点?

任何帮助都会非常感激。

由于

1 个答案:

答案 0 :(得分:0)

下面是一个简单的脚本,可以快速为所有用户提取所有特定于路径的权限。根据需要修改。您缺少的技巧是GetPermissions不仅递归地提取权限,而且还会提取应用权限的人。

如果您只需要特定用户的权限,则可以改为使用a different GetPermissions overload

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.Configuration;

void Main()
{
    string TfsRepository = ConfigurationManager.AppSettings["TFS_Repository"];
    string TfsUri = ConfigurationManager.AppSettings["TFS_URI"];
    TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri(TfsUri));
    ttpc.EnsureAuthenticated();
    var data = ttpc
        .GetService<VersionControlServer>()
        .GetPermissions(new string[] { TfsRepository }, RecursionType.Full)
        .SelectMany(rr => rr.Entries.Select(itm => new { itm, rr.ServerItem }))
        .Where(rr=>rr.itm.Allow.Length != 0)
        .Select(rr => new { 
            User = rr.itm.IdentityName,
            Path = rr.ServerItem,
            Permissions = String.Join(",", rr.itm.Allow)
        })
        .OrderBy(itm=>itm.User).ThenBy(itm=>itm.Path);
}