用于创建 TFS 组并设置权限的 TFS API? [英] TFS API to create a TFS Group and set permissions?

查看:30
本文介绍了用于创建 TFS 组并设置权限的 TFS API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试使用 TFS API 创建一个新组,为此我有以下代码:

Hello I'm trying to use TFS API to create a new group, for it I have this code:

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
}

由于我需要为此组设置编辑项目级信息"权限,因此我尝试了很多方法和不同的方法,但似乎任何方法都可以解决我的需求.例如:

As I need to set the permission "Edit project-level information" for this group I tried lot of methods and different approaches, but anything seems to solve my need. This for example:

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);

我对list[4]"进行了硬编码,因为它是我刚刚创建的组,我需要一些帮助来查看我的代码中有什么问题.我没有收到错误消息,它也不起作用.

I had hard-coded "list[4]" because it was the group I just created, I need some help to see what is wrong in my code. I get no error message and it doesn't work as well.

推荐答案

我可以通过以下代码获得权限设置:

I can get the permissions been set via following code:

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);
                }
            }                
        }
    }
}

这篇关于用于创建 TFS 组并设置权限的 TFS API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆