-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathAccountService.cs
More file actions
353 lines (289 loc) · 13 KB
/
AccountService.cs
File metadata and controls
353 lines (289 loc) · 13 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Identity;
using System.Threading.Tasks;
using System.Linq;
using System.Net.Http;
using System.Web;
using AutoMapper;
using HwProj.AuthService.API.Extensions;
using HwProj.Models.Roles;
using HwProj.AuthService.API.Events;
using HwProj.EventBus.Client.Interfaces;
using HwProj.Models.AuthService.DTO;
using HwProj.Models.AuthService.ViewModels;
using HwProj.Models.Result;
using HwProj.Utils.Authorization;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Octokit;
using User = HwProj.Models.AuthService.ViewModels.User;
namespace HwProj.AuthService.API.Services
{
public class AccountService : IAccountService
{
private readonly IUserManager _userManager;
private readonly UserManager<User> _aspUserManager;
private readonly SignInManager<User> _signInManager;
private readonly IAuthTokenService _tokenService;
private readonly IEventBus _eventBus;
private readonly IMapper _mapper;
private readonly IConfiguration _configuration;
private readonly HttpClient _client;
public AccountService(IUserManager userManager,
SignInManager<User> signInManager,
IAuthTokenService authTokenService,
IEventBus eventBus,
IMapper mapper,
UserManager<User> aspUserManager,
IConfiguration configuration,
IHttpClientFactory clientFactory)
{
_userManager = userManager;
_signInManager = signInManager;
_tokenService = authTokenService;
_eventBus = eventBus;
_mapper = mapper;
_aspUserManager = aspUserManager;
_configuration = configuration;
_client = clientFactory.CreateClient();
}
private async Task<AccountDataDto> GetAccountDataAsync(User user)
{
if (user == null) return null;
var userRoles = await _userManager.GetRolesAsync(user);
var userRole = userRoles.FirstOrDefault() ?? Roles.StudentRole;
return user.ToAccountDataDto(userRole);
}
public async Task<AccountDataDto> GetAccountDataAsync(string userId)
{
var user = await _userManager.FindByIdAsync(userId);
return await GetAccountDataAsync(user);
}
public async Task<AccountDataDto[]> GetAccountsDataAsync(string[] userIds)
{
var users = await _aspUserManager.Users
.Where(user => userIds.Contains(user.Id))
.AsNoTracking()
.ToDictionaryAsync(t => t.Id);
var accounts = new AccountDataDto[userIds.Length];
for (var i = 0; i < userIds.Length; i++)
{
var userId = userIds[i];
if (!users.TryGetValue(userId, out var user)) continue;
var roles = await _aspUserManager.GetRolesAsync(user);
accounts[i] = user.ToAccountDataDto(roles.FirstOrDefault() ?? Roles.StudentRole);
}
return accounts;
}
public async Task<AccountDataDto> GetAccountDataByEmailAsync(string email)
{
var user = await _userManager.FindByEmailAsync(email);
return await GetAccountDataAsync(user);
}
public async Task<Result> EditAccountAsync(string id, EditDataDTO model)
{
var user = await _userManager.FindByIdAsync(id);
if (user == null)
{
return Result.Failed("Пользователь не найден");
}
var result = await ChangeUserNameTask(user, model);
return result.Succeeded ? Result.Success() : Result.Failed();
}
public async Task<Result<TokenCredentials>> LoginUserAsync(LoginViewModel model)
{
if (await _userManager.FindByEmailAsync(model.Email)
is var user && user == null)
{
return Result<TokenCredentials>.Failed("Пользователь не найден");
}
var result = await _signInManager.PasswordSignInAsync(
user,
model.Password,
model.RememberMe,
false).ConfigureAwait(false);
if (!result.Succeeded)
{
return Result<TokenCredentials>.Failed(result.TryGetIdentityError());
}
var token = await _tokenService.GetTokenAsync(user);
return Result<TokenCredentials>.Success(token);
}
public async Task<Result<TokenCredentials>> RefreshToken(string userId)
{
return await _userManager.FindByIdAsync(userId) is var user && user == null
? Result<TokenCredentials>.Failed("Пользователь не найден")
: await GetToken(user);
}
public TokenCredentials GetGuestToken(string courseId)
{
return _tokenService.GetGuestToken(courseId);
}
public async Task<Result<TokenCredentials>> RegisterUserAsync(RegisterDataDTO model)
{
if (await _userManager.FindByEmailAsync(model.Email) != null)
{
return Result<TokenCredentials>.Failed("Пользователь уже зарегистрирован");
}
if (!model.IsExternalAuth && model.Password.Length < 6)
{
return Result<TokenCredentials>.Failed("Пароль должен содержать не менее 6 символов");
}
if (!model.IsExternalAuth && model.Password != model.PasswordConfirm)
{
return Result<TokenCredentials>.Failed("Пароли не совпадают");
}
var user = _mapper.Map<User>(model);
user.UserName = user.Email;
var createUserTask = model.IsExternalAuth
? _userManager.CreateAsync(user)
: _userManager.CreateAsync(user, model.Password);
var result = await createUserTask
.Then(() => _userManager.AddToRoleAsync(user, Roles.StudentRole))
.Then(() =>
{
user.EmailConfirmed = true;
return _userManager.UpdateAsync(user);
});
if (result.Succeeded)
{
var newUser = await _userManager.FindByEmailAsync(model.Email).ConfigureAwait(false);
var registerEvent = new StudentRegisterEvent(newUser.Id, newUser.Email, newUser.Name,
newUser.Surname, newUser.MiddleName);
_eventBus.Publish(registerEvent);
if (!model.IsExternalAuth)
{
await SignIn(user, model.Password);
}
return await GetToken(user);
}
return Result<TokenCredentials>.Failed(result.Errors.Select(errors => errors.Description).ToArray());
}
public async Task<Result> InviteNewLecturer(string emailOfInvitedUser)
{
var invitedUser = await _userManager.FindByEmailAsync(emailOfInvitedUser).ConfigureAwait(false);
if (invitedUser == null)
{
return Result.Failed("Пользователь не найден");
}
var result = await _userManager.AddToRoleAsync(invitedUser, Roles.LecturerRole)
.Then(() => _userManager.RemoveFromRoleAsync(invitedUser, Roles.StudentRole)).ConfigureAwait(false);
if (result.Succeeded)
{
var inviteEvent = new InviteLecturerEvent
{
UserId = invitedUser.Id,
UserEmail = invitedUser.Email
};
_eventBus.Publish(inviteEvent);
return Result.Success();
}
return Result.Failed("Пользователь уже является преподавателем");
}
public async Task<IList<User>> GetUsersInRole(string role)
{
return await _userManager.GetUsersInRoleAsync(role);
}
public async Task<Result> RequestPasswordRecovery(RequestPasswordRecoveryViewModel model)
{
var user = await _aspUserManager.FindByEmailAsync(model.Email);
if (user == null) return Result.Failed("Пользователь не найден");
var token = await _aspUserManager.GeneratePasswordResetTokenAsync(user);
if (token == null) return Result.Failed("Произошла внутренняя ошибка");
var passwordRecoveryEvent = new PasswordRecoveryEvent
{
UserId = user.Id,
Name = user.Name,
Surname = user.Surname,
Email = user.Email,
Token = token
};
_eventBus.Publish(passwordRecoveryEvent);
return Result.Success();
}
public async Task<Result> ResetPassword(ResetPasswordViewModel model)
{
var user = await _aspUserManager.FindByIdAsync(model.UserId);
if (user == null) return Result.Failed("Пользователь не найден");
if (model.Password.Length < 6)
{
return Result.Failed("Пароль должен содержать не менее 6 символов");
}
if (model.Password != model.PasswordConfirm)
{
return Result.Failed("Пароль и его подтверждение не совпадают");
}
var result = await _aspUserManager.ResetPasswordAsync(user, model.Token, model.Password);
if (!result.Succeeded)
return Result.Failed(string.Join(", ", result.Errors.Select(t => t.Description)));
var removeTokenResult = await _aspUserManager.RemoveAuthenticationTokenAsync(user,
_aspUserManager.Options.Tokens.PasswordResetTokenProvider,
UserManager<User>.ResetPasswordTokenPurpose);
return removeTokenResult.Succeeded
? Result.Success()
: Result.Failed(string.Join(", ", removeTokenResult.Errors.Select(t => t.Description)));
}
public async Task<GithubCredentials> AuthorizeGithub(string code, string userId)
{
var sourceSettings = _configuration.GetSection("Github");
var parameters = new Dictionary<string, string>
{
{ "client_id", sourceSettings["ClientIdGithub"] },
{ "client_secret", sourceSettings["ClientSecretGithub"] },
{ "code", code },
};
var content = new FormUrlEncodedContent(parameters);
var response = await _client.PostAsync("https://github.com/login/oauth/access_token", content);
var responseContent = await response.Content.ReadAsStringAsync();
var values = HttpUtility.ParseQueryString(responseContent);
var accessToken = values["access_token"];
if (accessToken is null)
{
throw new InvalidOperationException("Ошибка при попытке авторизации");
}
var organizationName = sourceSettings["OrganizationNameGithub"];
var githubClient = new GitHubClient(new ProductHeaderValue(organizationName));
var tokenAuth = new Credentials(accessToken);
githubClient.Credentials = tokenAuth;
var user = await githubClient.User.Current();
var login = user.Login;
var userFromDb = await _userManager.FindByIdAsync(userId);
if (!(login is null))
{
userFromDb.GitHubId = user.Login;
await _userManager.UpdateAsync(userFromDb);
}
var githubCredentials = new GithubCredentials
{
GithubId = user.Login
};
return githubCredentials;
}
private Task<IdentityResult> ChangeUserNameTask(User user, EditDataDTO model)
{
if (!string.IsNullOrWhiteSpace(model.Name))
{
user.Name = model.Name;
}
if (!string.IsNullOrWhiteSpace(model.Surname))
{
user.Surname = model.Surname;
}
if (!string.IsNullOrWhiteSpace(model.MiddleName))
{
user.MiddleName = model.MiddleName;
}
return _userManager.UpdateAsync(user);
}
private async Task SignIn(User user, string password)
{
await _signInManager.PasswordSignInAsync(user, password, false, false)
.ConfigureAwait(false);
}
private async Task<Result<TokenCredentials>> GetToken(User user)
{
return Result<TokenCredentials>.Success(await _tokenService.GetTokenAsync(user).ConfigureAwait(false));
}
}
}