feat(api): UserProfile
This commit is contained in:
@@ -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<UserRoleInCache> Roles { get; set; } = new List<UserRoleInCache>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
9
PARR.API/Contracts/V1/Responses/UserProfileResponse.cs
Normal file
9
PARR.API/Contracts/V1/Responses/UserProfileResponse.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using PARR.API.Authentication.Models;
|
||||
|
||||
namespace PARR.API.Contracts.V1.Responses
|
||||
{
|
||||
public class UserProfileResponse : UserCache
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
48
PARR.API/Controllers/V1/UserProfileController.cs
Normal file
48
PARR.API/Controllers/V1/UserProfileController.cs
Normal file
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Профиль пользователя
|
||||
/// </summary>
|
||||
[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;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Профиль текущего пользователя
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet(ApiRoutes.UserProfile.Get)]
|
||||
public async Task<IActionResult> Get()
|
||||
{
|
||||
var user = await authService.GetCurrentUserAsync();
|
||||
|
||||
if (user == null)
|
||||
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = "Ошибка при получении текущего пользователя" } }));
|
||||
|
||||
var response = mapper.Map<UserProfileResponse>(user);
|
||||
|
||||
return Ok(new Response<UserProfileResponse>(response, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<Role, RoleResponse>();
|
||||
|
||||
CreateMap<UserCache, UserProfileResponse>();
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
|
||||
@@ -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<UserCache?> GetCurrentUserAsync()
|
||||
{
|
||||
var userIp = clientService.GetClientIp()?.ToString();
|
||||
|
||||
if (userIp == null)
|
||||
return null;
|
||||
|
||||
return await GetUserAsync(userIp);
|
||||
}
|
||||
|
||||
|
||||
public async Task<UserCache?> 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -32,5 +32,11 @@ namespace PARR.API.Services.Interfaces
|
||||
/// <param name="ipAddress"></param>
|
||||
/// <returns></returns>
|
||||
Task UnlockUserAsync(string ipAddress);
|
||||
|
||||
/// <summary>
|
||||
/// Получить текущего пользователя
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task<UserCache?> GetCurrentUserAsync();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user