Initial commit
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
using Microsoft.Graph;
|
||||
using Microsoft.Graph.Models;
|
||||
using SharepointToolbox.Web.Core.Models;
|
||||
using AppGraphClientFactory = SharepointToolbox.Web.Infrastructure.Auth.GraphClientFactory;
|
||||
|
||||
namespace SharepointToolbox.Web.Services;
|
||||
|
||||
public class GraphUserDirectoryService : IGraphUserDirectoryService
|
||||
{
|
||||
private readonly AppGraphClientFactory _graphClientFactory;
|
||||
|
||||
public GraphUserDirectoryService(AppGraphClientFactory graphClientFactory)
|
||||
{
|
||||
_graphClientFactory = graphClientFactory;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<GraphDirectoryUser>> GetUsersAsync(
|
||||
TenantProfile profile,
|
||||
bool includeGuests = false,
|
||||
IProgress<int>? progress = null,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var graphClient = await _graphClientFactory.CreateClientAsync(profile);
|
||||
|
||||
var response = await graphClient.Users.GetAsync(config =>
|
||||
{
|
||||
config.QueryParameters.Filter = includeGuests
|
||||
? "accountEnabled eq true"
|
||||
: "accountEnabled eq true and userType eq 'Member'";
|
||||
config.QueryParameters.Select = new[]
|
||||
{ "displayName", "userPrincipalName", "mail", "department", "jobTitle", "userType" };
|
||||
config.QueryParameters.Top = 999;
|
||||
}, ct);
|
||||
|
||||
if (response is null) return Array.Empty<GraphDirectoryUser>();
|
||||
|
||||
var results = new List<GraphDirectoryUser>();
|
||||
var iter = PageIterator<User, UserCollectionResponse>.CreatePageIterator(
|
||||
graphClient, response,
|
||||
user =>
|
||||
{
|
||||
if (ct.IsCancellationRequested) return false;
|
||||
results.Add(new GraphDirectoryUser(
|
||||
user.DisplayName ?? user.UserPrincipalName ?? string.Empty,
|
||||
user.UserPrincipalName ?? string.Empty,
|
||||
user.Mail, user.Department, user.JobTitle, user.UserType));
|
||||
progress?.Report(results.Count);
|
||||
return true;
|
||||
});
|
||||
await iter.IterateAsync(ct);
|
||||
return results;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user