diff --git a/PARR.API/Authentication/Models/UserCache.cs b/PARR.API/Authentication/Models/UserCache.cs index 5f77509c..a9e0027c 100644 --- a/PARR.API/Authentication/Models/UserCache.cs +++ b/PARR.API/Authentication/Models/UserCache.cs @@ -2,12 +2,18 @@ { public class UserCache { + public Guid? UserId { get; set; } + + public Guid? HostId { get; set; } + public required string UserIp { get; set; } public string Name { get; set; } = string.Empty; public string Description { get; set; } = string.Empty; + public DateTimeOffset? LastLogon { get; set; } + public List Roles { get; set; } = new List(); } } diff --git a/PARR.API/Contracts/V1/ApiRoutes.cs b/PARR.API/Contracts/V1/ApiRoutes.cs index aa1f957a..ce4b5288 100644 --- a/PARR.API/Contracts/V1/ApiRoutes.cs +++ b/PARR.API/Contracts/V1/ApiRoutes.cs @@ -125,6 +125,8 @@ namespace PARR.API.Contracts.V1 public const string getParam = "{id}"; } + #region User + public static class User { public const string GetAll = Base + "/users/"; @@ -151,6 +153,13 @@ namespace PARR.API.Contracts.V1 public const string GetAll = Base + "/roles/"; } + public static class UserProfile + { + public const string Get = Base + "/user-profile/"; + } + + #endregion + } } diff --git a/PARR.API/Contracts/V1/Responses/UserProfileResponse.cs b/PARR.API/Contracts/V1/Responses/UserProfileResponse.cs new file mode 100644 index 00000000..f520de1e --- /dev/null +++ b/PARR.API/Contracts/V1/Responses/UserProfileResponse.cs @@ -0,0 +1,9 @@ +using PARR.API.Authentication.Models; + +namespace PARR.API.Contracts.V1.Responses +{ + public class UserProfileResponse : UserCache + { + + } +} diff --git a/PARR.API/Controllers/V1/UserProfileController.cs b/PARR.API/Controllers/V1/UserProfileController.cs new file mode 100644 index 00000000..b1bd5ae5 --- /dev/null +++ b/PARR.API/Controllers/V1/UserProfileController.cs @@ -0,0 +1,48 @@ +using AutoMapper; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using PARR.API.Contracts.V1; +using PARR.API.Contracts.V1.Responses; +using PARR.API.Contracts.V1.Responses.Base; +using PARR.API.Controllers.V1.Base; +using PARR.API.Services.Interfaces; + +namespace PARR.API.Controllers.V1 +{ + /// + /// Профиль пользователя + /// + [Authorize] + public class UserProfileController : BaseApiController + { + private readonly IAuthService authService; + private readonly IMapper mapper; + + public UserProfileController( + IAuthService authService, + IMapper mapper + ) + { + this.authService = authService; + this.mapper = mapper; + } + + + /// + /// Профиль текущего пользователя + /// + /// + [HttpGet(ApiRoutes.UserProfile.Get)] + public async Task Get() + { + var user = await authService.GetCurrentUserAsync(); + + if (user == null) + return BadRequest(new Response(false, new List { new ErrorModel { Message = "Ошибка при получении текущего пользователя" } })); + + var response = mapper.Map(user); + + return Ok(new Response(response, true)); + } + } +} diff --git a/PARR.API/MappingProfiles/DomainToResponseProfile.cs b/PARR.API/MappingProfiles/DomainToResponseProfile.cs index 0f669b94..14755c8f 100644 --- a/PARR.API/MappingProfiles/DomainToResponseProfile.cs +++ b/PARR.API/MappingProfiles/DomainToResponseProfile.cs @@ -1,4 +1,5 @@ using AutoMapper; +using PARR.API.Authentication.Models; using PARR.API.Contracts.V1.Responses; using PARR.API.MappingProfiles.Resolvers; using PARR.BLL.Helpers; @@ -169,6 +170,8 @@ namespace PARR.API.MappingProfiles CreateMap(); + CreateMap(); + #endregion } diff --git a/PARR.API/Services/Implementations/AuthService.cs b/PARR.API/Services/Implementations/AuthService.cs index ac041597..00e363d9 100644 --- a/PARR.API/Services/Implementations/AuthService.cs +++ b/PARR.API/Services/Implementations/AuthService.cs @@ -13,13 +13,21 @@ namespace PARR.API.Services.Implementations private readonly IUserService userService; private readonly IHostService hostService; private readonly UserCacheSettings userCacheSettings; + private readonly IClientService clientService; - public AuthService(IRedisCacheService cacheService, IUserService userService, IHostService hostService, UserCacheSettings userCacheSettings) + public AuthService( + IRedisCacheService cacheService, + IUserService userService, + IHostService hostService, + UserCacheSettings userCacheSettings, + IClientService clientService + ) { this.cacheService = cacheService; this.userService = userService; this.hostService = hostService; this.userCacheSettings = userCacheSettings; + this.clientService = clientService; } @@ -32,6 +40,17 @@ namespace PARR.API.Services.Implementations } + public async Task GetCurrentUserAsync() + { + var userIp = clientService.GetClientIp()?.ToString(); + + if (userIp == null) + return null; + + return await GetUserAsync(userIp); + } + + public async Task GetUserAsync(string ipAddress) { // смотрим, есть ли в кэше @@ -82,12 +101,18 @@ namespace PARR.API.Services.Implementations }; if (host != null) + { cacheData.Name = host.Ek; + cacheData.HostId = host.Id; + cacheData.LastLogon = host.LastLogon; + } if (user != null) { cacheData.Name = user.Name + (host != null ? $", {host.Ek}" : string.Empty); cacheData.Description = user.Description ?? string.Empty; + cacheData.UserId = user.Id; + cacheData.LastLogon = user.LastLogon; } diff --git a/PARR.API/Services/Interfaces/IAuthService.cs b/PARR.API/Services/Interfaces/IAuthService.cs index d126cda9..e860fcc4 100644 --- a/PARR.API/Services/Interfaces/IAuthService.cs +++ b/PARR.API/Services/Interfaces/IAuthService.cs @@ -32,5 +32,11 @@ namespace PARR.API.Services.Interfaces /// /// Task UnlockUserAsync(string ipAddress); + + /// + /// Получить текущего пользователя + /// + /// + Task GetCurrentUserAsync(); } }