TFS API to create a TFS Group and set permissions?

天涯浪子 提交于 2019-12-02 15:06:57

问题


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

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.


回答1:


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


来源:https://stackoverflow.com/questions/34640524/tfs-api-to-create-a-tfs-group-and-set-permissions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!