通过 Rest API 或客户端 DLL 创建具有权限的 Azure Devops(安全)组,如 'Contributors'

Create Azure Devops (security) group like 'Contributors' with Permissions via Rest API or Client DLL

我必须创建具有特定权限的 Azure Devops 项目级组。因为在一个团队项目中有几个团队不应该互相影响,所以我想创建不同的组并且不想使用即'Contributors'。 我设法创建了一个组并检索了该组(身份)。默认权限是(或当然)'NOT SET'.

获取安全命名空间列表,我可以使用 'QueryAccessControlList' 或 'HasPermissions' 但我需要一个字符串令牌和一个 IdentityDescriptors 列表来获取组的权限。

            var securityNamespaces = securityService.GetSecurityNamespaces();
            foreach (var secns in securityNamespaces)
            {
                secns.QueryAccessControlList(?? string-Token and IdentityDescriptors ?? );
            }

在哪里可以找到这些参数?

我的想法是在门户网站中手动配置一个组,检索设置的权限并将它们设置到我的新组。
或者我可以使用 tfssecurity.exe。但是 /a+ 选项需要参数 'namespace token action identity' 我想这是相同的 'token' 我错过了 :-(
有人可以帮忙吗?

您可以通过以下方式创建项目级安全组 api:

POST https://dev.azure.com/{org}/{proId}/_api/_identity/ManageGroup

请求正文:

{"name":"GroupNamehere","description":"Create a Test Group ","tfid":""}

身份验证使用 PAT 令牌。

然后你可以使用下面的rest api设置权限,这个api是通过按f12从浏览器抓取的。

Request URL: https://dev.azure.com/{org}/{proId}/_api/_security/ManagePermissions?__v=5

示例请求正文:

{"updatePackage": "{"IsRemovingIdentity":false,"TeamFoundationId":"be207790-f8ea-4ce0-9bcf-d8b4920c2af7","DescriptorIdentityType":"Microsoft.TeamFoundation.Identity","DescriptorIdentifier":"S-1-9-1551374245-3902060889-2001379654-2338155045-4044170422-1-3881783123-407997253-3219626845-873774695","PermissionSetId":"52d39943-cb85-4d7f-8fa8-c6baac873819","PermissionSetToken":"vstfs:///Classification/TeamProject/59b994e8-4a77-46a1-8b5d-6a25f10d24b6","RefreshIdentities":false,"Updates":[{"PermissionId":1,"PermissionBit":1048576,"NamespaceId":"52d39943-cb85-4d7f-8fa8-c6baac873819","Token":"$PROJECT:vstfs:///Classification/TeamProject/59b994e8-4a77-46a1-8b5d-6a25f10d24b6:"}],"TokenDisplayName":null}"}

注意:请求体中的"Updates\":[{\"PermissionId\":1,:0表示Not set,1表示Allow,2表示Deny

你也可以参考这些:Set version control permissions by REST API, 。希望这有帮助。

Hugh Lins 的回答可能有效,但我并不完全满意。这里有一些有用的提示(使用 MS DLL 和 .NET API)。

IGroupSecurityService group_security_service;  // declared deprecated
group_security_service = team_project_collection.GetService<IGroupSecurityService>();
...
// create group
group_security_service.CreateApplicationGroup(prjinfo.Uri, sGroupName, sGroupDescription);
...

提取组(即贡献者)的权限我们需要:

  • 组的身份描述符
  • 每个使用的安全名称空间的安全令牌

1.IdentityDescriptor

TeamFoundationIdentity tfs_id = identity_management_service.ReadIdentity(IdentitySearchFactor.LocalGroupName,sGroup, MembershipQuery.Expanded, ReadIdentityOptions.IncludeReadFromSource);
var desc = tfs_id.Descriptor

2.Token 例子在这里:
https://docs.microsoft.com/en-us/azure/devops/cli/security_tokens 我们必须创建一个字符串,它取决于使用的安全命名空间。

var securityNamespaces = securityService.GetSecurityNamespaces();

对于每个安全名称空间,令牌的格式都略有不同。 例如对于 'Project' 命名空间:
$PROJECT:vstfs:///Classification/TeamProject/xxxxxxxx-a1de-4bc8-b751-188eea17c3ba'
vstfs:///...-Uri 来自这里:

prjinfo = common_structure_service4.GetProjectFromName(sTeamProject);
--> prjinfo.Uri

为了调用 QueryAccessControlList,我们需要描述符作为数组参数:

IdentityDescriptor[] tfidDescriptors = new IdentityDescriptor[] {desc};

调用 QueryAccessControlList:

foreach (var secNS in securityNamespaces) 
{
  string sToken = CreateToken(secNS.Description.NamespaceId);
  AccessControlList acl = secNS.QueryAccessControlList(sToken, tfidDescriptors, false);
  foreach (var ace in acl.AccessControlEntries)
  {
     ace.Allow   vs.   ace.Deny  contain bit coded permissions
  }
}

稍后我们可以将权限设置到另一个组。
(我们必须将 INHERIT 和 ALLOW/DENY 分开)。
示例:

...
secNS.RemovePermissions(sToken, desc, inherits);
...
secNS.SetPermissions(sToken, desc, allows, denies, true);

干杯。