Files
PnvPanel/backend/src/PnvPanel.Infrastructure/Identity/CurrentUser.cs
T
Leonid Pershin 7b6fe9ad78 Add Telegram bot integration and enhance user management features
- Introduced Telegram.Bot package for bot functionality.
- Updated user management to include Telegram linking and blocking features.
- Enhanced activation request handling with notifications via Telegram.
- Added new database entities for Telegram link tokens and login requests.
- Implemented traffic synchronization for client stats in the XuiPanelGateway.
- Updated application structure to support new test projects and improved dependency injection for Telegram services.
2026-07-02 01:01:03 +03:00

31 lines
1.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Security.Claims;
using Microsoft.AspNetCore.Http;
using PnvPanel.Application.Common.Interfaces;
namespace PnvPanel.Infrastructure.Identity;
/// <summary>
/// В HTTP-запросах читает JWT-claims. В Telegram-боте (нет HttpContext) вызывающая сторона
/// заранее задаёт пользователя через ICurrentUserSetter.SetUser(...) в рамках DI-scope апдейта.
/// </summary>
internal sealed class CurrentUser(IHttpContextAccessor httpContextAccessor) : ICurrentUser, ICurrentUserSetter
{
private (Guid Id, string Name)? _override;
private ClaimsPrincipal? Principal => httpContextAccessor.HttpContext?.User;
public Guid? UserId => _override?.Id ?? ParseHttpUserId();
public string? UserName => _override?.Name ?? Principal?.FindFirstValue(ClaimTypes.Name);
public bool IsAuthenticated => _override is not null || (Principal?.Identity?.IsAuthenticated ?? false);
public void SetUser(Guid userId, string userName) => _override = (userId, userName);
private Guid? ParseHttpUserId()
{
var value = Principal?.FindFirstValue(ClaimTypes.NameIdentifier);
return Guid.TryParse(value, out var id) ? id : null;
}
}