public async Task <bool> AddUser(string organization, string projectName, string userId) { if (string.IsNullOrWhiteSpace(organization)) { throw new ArgumentException($"'{nameof(organization)}' cannot be null or whitespace.", nameof(organization)); } if (string.IsNullOrWhiteSpace(projectName)) { throw new ArgumentException($"'{nameof(projectName)}' cannot be null or whitespace.", nameof(projectName)); } if (string.IsNullOrWhiteSpace(userId)) { throw new ArgumentException($"'{nameof(userId)}' cannot be null or whitespace.", nameof(userId)); } VssConnection connection = null; GraphHttpClient graphHttpClient = null; TeamHttpClient teamClient = null; var result = false; try { string url = $"https://dev.azure.com/{organization}"; if (_adoConfig.UsePta) { connection = new VssConnection(new Uri(url), new VssBasicCredential(string.Empty, _adoConfig.AdoPersonalAccessToken)); } else { //connection = new VssConnection(new Uri(url), new VssCredentials(true)); connection = new VssConnection(new Uri(url), new VssClientCredentials(true)); } teamClient = connection.GetClient <TeamHttpClient>(); var allteams = await teamClient.GetTeamsAsync(projectName, null, null, null, true); var defTeamGroupName = $"{projectName} Team"; var defTeam = allteams?.FirstOrDefault(a => a.Name.Contains(defTeamGroupName)); if (defTeam != null) { //var members = await teamClient.GetTeamMembersWithExtendedPropertiesAsync(projectName, defTeam.Id.ToString()); graphHttpClient = connection.GetClient <GraphHttpClient>(); List <SubjectDescriptor> groupSubjectDescriptors = new(); groupSubjectDescriptors.Add(SubjectDescriptor.FromString(defTeam.Identity.Descriptor.Identifier)); var contextCreate = new GraphUserPrincipalNameCreationContext { PrincipalName = userId }; var added = await graphHttpClient.CreateUserAsync(contextCreate, groupSubjectDescriptors); //var membersAfter = await teamClient.GetTeamMembersWithExtendedPropertiesAsync(projectName, defTeam.Id.ToString()); result = true; } } catch (Exception ex) { //Console.WriteLine("Exception during create project: ", ex.Message); throw; } finally { connection?.Dispose(); graphHttpClient?.Dispose(); teamClient?.Dispose(); } return(result); }
public async Task <List <string> > GetPermissions(string organization, string projectName) { if (string.IsNullOrWhiteSpace(organization)) { throw new ArgumentException($"'{nameof(organization)}' cannot be null or whitespace.", nameof(organization)); } if (string.IsNullOrWhiteSpace(projectName)) { throw new ArgumentException($"'{nameof(projectName)}' cannot be null or whitespace.", nameof(projectName)); } VssConnection connection = null; GraphHttpClient graphHttpClient = null; TeamHttpClient teamClient = null; Guid GitSecurityNamespace = Guid.Parse("2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87"); try { string url = $"https://dev.azure.com/{organization}"; if (_adoConfig.UsePta) { connection = new VssConnection(new Uri(url), new VssBasicCredential(string.Empty, _adoConfig.AdoPersonalAccessToken)); } else { //connection = new VssConnection(new Uri(url), new VssCredentials(true)); connection = new VssConnection(new Uri(url), new VssClientCredentials(true)); } //projectName = "First-Ado"; //teamClient = connection.GetClient<TeamHttpClient>(); //var allteams = await teamClient.GetTeamsAsync(projectName, null, null, null, true); //var defTeamGroupName = $"{projectName} Team"; //var defTeam = allteams?.FirstOrDefault(a => a.Name.Contains(defTeamGroupName)); //if (defTeam != null) //{ // var members = await teamClient.GetTeamMembersWithExtendedPropertiesAsync(projectName, defTeam.Id.ToString()); // graphHttpClient = connection.GetClient<GraphHttpClient>(); // List<SubjectDescriptor> groupSubjectDescriptors = new(); // groupSubjectDescriptors.Add(SubjectDescriptor.FromString(defTeam.Identity.Descriptor.Identifier)); // var contextCreate = new GraphUserPrincipalNameCreationContext { PrincipalName = "*****@*****.**"}; // var added = await graphHttpClient.CreateUserAsync(contextCreate, groupSubjectDescriptors); // var membersAfter = await teamClient.GetTeamMembersWithExtendedPropertiesAsync(projectName, defTeam.Id.ToString()); //} // Get a client SecurityHttpClient httpClient = connection.GetClient <SecurityHttpClient>(); IEnumerable <SecurityNamespaceDescription> namespaces = await httpClient.QuerySecurityNamespacesAsync(GitSecurityNamespace); SecurityNamespaceDescription gitNamespace = namespaces.FirstOrDefault(); Dictionary <int, string> permission = new Dictionary <int, string>(); foreach (ActionDefinition actionDef in gitNamespace.Actions) { permission[actionDef.Bit] = actionDef.DisplayName; } return(permission.Values?.ToList()); } catch (Exception ex) { //Console.WriteLine("Exception during create project: ", ex.Message); throw; } finally { connection?.Dispose(); graphHttpClient?.Dispose(); teamClient?.Dispose(); } }