【问题标题】:Set and retrieve the Team Administrator in Team Foundation Server or VSTS在 Team Foundation Server 或 VSTS 中设置和检索团队管理员
【发布时间】:2018-07-01 12:34:51
【问题描述】:

TFS 2012 及更高版本以及 VSTS 具有团队管理员的概念。我查看了整个 API,寻找一种通过代码设置和检索值的简单方法,以便更轻松地配置这些设置,但找不到。

通过 Web UI 的服务器对象模型进行反射提供了有关如何执行此操作的提示,但它依赖于许多私有方法来完成此操作。尤其是计算Security Scope Token的部分是隐藏魔法。

【问题讨论】:

    标签: tfs azure-devops tfs-sdk


    【解决方案1】:

    找到this old blogpost from 2013 which details how to do this 花了很多时间,而且我似乎不是唯一一个被私有方法难倒的人。最后他们也最终使用反射来调用私有方法来检索令牌:

    现在可以通过 TFS 团队工具使用此功能:

    检索

    找到与团队匹配的安全组,使用它来计算团队的令牌,获取属于该特殊安全命名空间的人员:

    public List<string> ListTeamAdministrators(string team, out string message)
    {
        // Retrieve the default team.
        TeamFoundationTeam t = this.teamService.ReadTeam(this.projectInfo.Uri, team, null);
        List<string> lst = null;
        message = "";
    
        if (t == null)
        {
            message = "Team [" + team + "] not found";
        }
        else
        {
            // Get security namespace for the project collection.
            ISecurityService securityService = this.teamProjectCollection.GetService<ISecurityService>();
            SecurityNamespace securityNamespace =
                securityService.GetSecurityNamespace(FrameworkSecurity.IdentitiesNamespaceId);
    
            // Use reflection to retrieve a security token for the team.
            var token = GetTeamAdminstratorsToken(t);
    
            // Retrieve an ACL object for all the team members.
            var allMembers = t.GetMembers(this.teamProjectCollection, MembershipQuery.Expanded)
                .ToArray();
            AccessControlList acl =
                securityNamespace.QueryAccessControlList(token, allMembers.Select(m => m.Descriptor), true);
    
            // Retrieve the team administrator SIDs by querying the ACL entries.
            var entries = acl.AccessControlEntries;
            var admins = entries.Where(e => (e.Allow & 15) == 15).Select(e => e.Descriptor.Identifier);
    
            // Finally, retrieve the actual TeamFoundationIdentity objects from the SIDs.
            var adminIdentities = allMembers.Where(m => admins.Contains(m.Descriptor.Identifier));
    
            lst = adminIdentities.Select(i => i.DisplayName).ToList();
        }
        return lst;
    }
    
    private static string GetTeamAdminstratorsToken(TeamFoundationTeam team)
    {
        return IdentityHelper.CreateSecurityToken(team.Identity);
    }
    

    设置

    设置以类似的方式工作。获取令牌,然后将用户唯一标识符添加到访问控制列表:

    IdentityDescriptor descriptor = GetMemberDescriptor(memberId);
    securityNamespace.SetPermissions(token, descriptor, 15, 0, false);
    

    删除

    从列表中删除一个人当然很容易猜到;

    IdentityDescriptor descriptor = GetMemberDescriptor(memberId);
    securityNamespace.RemovePermissions(token, descriptor, 15);
    

    【讨论】:

      猜你喜欢
      • 2011-01-12
      • 1970-01-01
      • 2017-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      • 2016-08-16
      • 1970-01-01
      相关资源
      最近更新 更多