mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-12-16 18:15:08 -05:00
Co-authored-by: Amelia <77553571+Fesaa@users.noreply.github.com> Co-authored-by: Robbie Davis <robbie@therobbiedavis.com>
35 lines
1.0 KiB
C#
35 lines
1.0 KiB
C#
using API.Services.Store;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace API.Controllers;
|
|
|
|
#nullable enable
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
[Authorize]
|
|
public class BaseApiController : ControllerBase
|
|
{
|
|
private IUserContext? _userContext;
|
|
|
|
/// <summary>
|
|
/// Gets the current user context. Available in all derived controllers.
|
|
/// </summary>
|
|
protected IUserContext UserContext =>
|
|
_userContext ??= HttpContext.RequestServices.GetRequiredService<IUserContext>();
|
|
|
|
/// <summary>
|
|
/// Gets the current authenticated user's ID.
|
|
/// Throws if user is not authenticated.
|
|
/// </summary>
|
|
protected int UserId => UserContext.GetUserIdOrThrow();
|
|
|
|
/// <summary>
|
|
/// Gets the current authenticated user's username.
|
|
/// </summary>
|
|
/// <remarks>Warning! Username's can contain .. and /, do not use folders or filenames explicitly with the Username</remarks>
|
|
protected string? Username => UserContext.GetUsername();
|
|
}
|