-
Notifications
You must be signed in to change notification settings - Fork 15
/
MsGraphUserManagementService.cs
166 lines (145 loc) · 6.26 KB
/
MsGraphUserManagementService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using Microsoft.Extensions.Logging;
using Microsoft.Graph;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TMF.Identity.Core.Interfaces;
using TMF.Identity.Core.Model;
using TMF.Identity.Infrastructure.Configuration.Interfaces;
using TMF.Identity.Infrastructure.Logger;
namespace TMF.Identity.Infrastructure.Services
{
public class MsGraphUserManagementService : IUserManagementService
{
private readonly IGraphServiceClient _graphServiceClient;
private readonly IMsGraphServiceConfiguration _msGraphServiceConfiguration;
private readonly ILogger<MsGraphUserManagementService> _logger;
public MsGraphUserManagementService(IGraphServiceClient graphServiceClient,
IMsGraphServiceConfiguration msGraphServiceConfiguration,
ILogger<MsGraphUserManagementService> logger)
{
_graphServiceClient = graphServiceClient
?? throw new ArgumentNullException(nameof(graphServiceClient));
_msGraphServiceConfiguration = msGraphServiceConfiguration
?? throw new ArgumentNullException(nameof(msGraphServiceConfiguration));
_logger = logger
?? throw new ArgumentNullException(nameof(logger));
}
public async Task<UserEntity> GetUserAsync(string userId)
{
try
{
var user = await _graphServiceClient.Users[userId]
.Request()
.Select(e => new
{
e.Id,
e.GivenName,
e.Surname,
e.Identities
})
.GetAsync();
var email = user.Identities.ToList()
.FirstOrDefault(i => i.SignInType == "emailAddress")
?.IssuerAssignedId;
return new UserEntity
{
Id = user.Id,
FirstName = user.GivenName,
LastName = user.Surname,
Email = email
};
}
catch (ServiceException ex)
{
if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return null;
}
else
{
throw;
}
}
}
public async Task<UserEntity> CreateUserAsync(UserEntity userEntity)
{
var user = new User
{
AccountEnabled = true,
GivenName = userEntity.FirstName,
Surname = userEntity.LastName,
DisplayName = string.Concat(userEntity.FirstName, " ", userEntity.LastName),
Identities = new List<ObjectIdentity>
{
new ObjectIdentity
{
SignInType = "emailAddress",
Issuer = _msGraphServiceConfiguration.TenantName,
IssuerAssignedId = userEntity.Email
}
},
PasswordProfile = new PasswordProfile
{
ForceChangePasswordNextSignIn = true,
Password = "TempPass123@"
}
};
var addedUser = await _graphServiceClient.Users
.Request()
.AddAsync(user);
userEntity.Id = addedUser.Id;
return userEntity;
}
public async Task DeleteUserAsync(string userId)
{
await _graphServiceClient.Users[userId]
.Request()
.DeleteAsync();
}
public async Task<IReadOnlyList<UserEntity>> GetAllUsersAsync()
{
List<User> users = new List<User>();
try
{
IGraphServiceUsersCollectionPage iGraphServiceUsersCollectionPage = await _graphServiceClient.Users
.Request()
.Select($"id," +
$" userPrincipalName," +
$" givenName," +
$" surname")
.Top(50)
.GetAsync();
var userPageIterator = PageIterator<User>.CreatePageIterator(_graphServiceClient,
iGraphServiceUsersCollectionPage,
entity => { users.Add(entity); return true; });
await userPageIterator.IterateAsync();
}
catch (ServiceException ex)
{
if (ex.StatusCode == System.Net.HttpStatusCode.TooManyRequests)
{
_logger.LogError(LoggerEvents.MicrosoftGraphTooManyRequests, string.Concat(ex.Message, " ", ex.InnerException?.Message));
var retryAfter = ex.ResponseHeaders.RetryAfter.Delta.Value;
await Task.Delay(TimeSpan.FromSeconds(retryAfter.TotalSeconds));
await GetAllUsersAsync();
}
else
{
throw;
}
}
var userEntities = users
.Select(u => new UserEntity()
{
Id = u.Id,
Email = u.UserPrincipalName,
FirstName = u.GivenName,
LastName = u.Surname
})
.ToList();
return userEntities;
}
}
}