/
qwiklly
/
arc-server
Обзор
Документация
Войти
/
qwiklly
/
arc-server
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
trunk
src/AppRemoteConfig/Services/UserService.cs
153 строки
5 KB
CLTanuki
chore: modules exploded
24 янв 2024, 18:53
24 янв 2024, 18:53
6b5ba70
Код
Авторство
О чём код?
namespace AppRemoteConfig.Services { using System.Security.Claims; using AppRemoteConfig.Helpers.Auth; using AppRemoteConfig.Models; using AppRemoteConfig.Models.Database; using AppRemoteConfig.Models.Dto; using AppRemoteConfig.Services.Interfaces; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; public class UserService : IUserService { private readonly ILogger<UserService> _logger; private readonly SignInManager<AppUser> _signInManager; private readonly UserManager<AppUser> _userManager; private readonly ApplicationContext _dbContext; private readonly AuthenticationStateProvider _authenticationStateProvider; public UserService(ApplicationContext dbContext, ILogger<UserService> logger, SignInManager<AppUser> signInManager, UserManager<AppUser> userManager, IHostEnvironmentAuthenticationStateProvider hostAuthentication) { this._logger = logger; this._signInManager = signInManager; this._userManager = userManager; this._dbContext = dbContext; this._authenticationStateProvider = _authenticationStateProvider; } public async Task<IEnumerable<AppUserDto>> ListAppUsers() { var userList = await _userManager.Users.ToListAsync(); return userList.ConvertAll(Converters.AppUserEntityToDto); } public async Task<AppUserDto?> GetAppUser(Guid id) { var user = await _userManager.FindByIdAsync(id.ToString()); return user == null ? null : Converters.AppUserEntityToDto(user); } public async Task<AppUserDto?> PutAppUser(AppUserDto appUser) { var user = await _userManager.FindByIdAsync(appUser.Id.ToString()); user.FirstName = appUser.FirstName; user.LastName = appUser.LastName; user.Patronymic = appUser.Patronymic; user.UserName = appUser.UserName; var result = await _userManager.UpdateAsync(user); return !result.Succeeded ? null : appUser; } public async Task<bool> DeleteAppUser(Guid id) { var user = await _userManager.FindByIdAsync(id.ToString()); if (user == null) { this._logger.LogError("App user with the ID provided was not found"); return false; } var result = await _userManager.DeleteAsync(user); if (!result.Succeeded) { this._logger.LogError("User cannot be deleted"); return false; } return false; } public async Task<bool> LoginUser(AppUserLoginDto userDto) { var user = await _userManager.FindByNameAsync(userDto.UserName); if (user == null) { this._logger.LogError("User does not exist"); return false; } var signInResult = await _signInManager.CheckPasswordSignInAsync(user, userDto.Password, false); if(!signInResult.Succeeded) { this._logger.LogError("Wrong password"); return false; } var authProperties = new AuthenticationProperties { AllowRefresh = true, ExpiresUtc = DateTimeOffset.Now.AddDays(1), IsPersistent = true }; await _signInManager.SignInAsync(user, authProperties); return true; } public async Task<AppUserLoginDto?> RegisterUser(AppUserCreateRequest userCreateRequest) { var user = new AppUser { UserName = userCreateRequest.UserName, FirstName = userCreateRequest.FirstName, LastName = userCreateRequest.LastName, Patronymic = userCreateRequest.Patronymic }; var result = await _userManager.CreateAsync(user, userCreateRequest.Password); if (!result.Succeeded) { this._logger.LogError("User cannot be registered"); return null; } await _userManager.SetEmailAsync(user, userCreateRequest.EMail); var userDto = new AppUserLoginDto { UserName = userCreateRequest.UserName, Password = userCreateRequest.Password }; return userDto; } public async Task<bool> LogoutUser() { await _signInManager.SignOutAsync(); return true; } public async Task<UserInfo> GetUserInfo() { var authState = await this._authenticationStateProvider.GetAuthenticationStateAsync(); var userIdClaim = authState.User.FindFirst(ClaimTypes.NameIdentifier).Value; Guid.TryParse(userIdClaim, out var uuid); return await Task.FromResult(new UserInfo { IsAuthenticated = authState.User.Identity.IsAuthenticated, UserName = authState.User.Identity.Name, Claims = authState.User.Claims.ToDictionary(c => c.Type, c => c.Value) }); } } }