gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:55:49 24 4
gpt4 key购买 nike

我正在尝试获取用户对文件夹的访问权限以及该文件夹的权限列表。尝试执行类似于 TFS 命令的操作:

TF Permissions "$MyRepository/MyFolder" /r

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

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”,了解它可以访问多少文件夹以进行 checkin 和合并。如何实现?

最佳答案

下面是一个简单的脚本,它可以快速为所有用户提取所有特定于路径的权限。根据需要修改。你错过的诀窍是不仅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);
}

关于c# - 以编程方式获取 TFS 用户的文件夹权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23678807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com