2024-02-24 15:00:36 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
|
|
using OliverBooth.Data.Web;
|
|
|
|
using OliverBooth.Services;
|
2024-02-24 15:27:03 +00:00
|
|
|
using ISession = OliverBooth.Data.Web.ISession;
|
2024-02-24 15:00:36 +00:00
|
|
|
|
|
|
|
namespace OliverBooth.Pages.Admin;
|
|
|
|
|
|
|
|
public class Index : PageModel
|
|
|
|
{
|
|
|
|
private readonly ILogger<Index> _logger;
|
|
|
|
private readonly IUserService _userService;
|
|
|
|
private readonly ISessionService _sessionService;
|
|
|
|
|
|
|
|
public Index(ILogger<Index> logger, IUserService userService, ISessionService sessionService)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_userService = userService;
|
|
|
|
_sessionService = sessionService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IUser CurrentUser { get; private set; } = null!;
|
|
|
|
|
|
|
|
public IActionResult OnGet()
|
|
|
|
{
|
2024-02-24 15:04:03 +00:00
|
|
|
if (!_sessionService.TryGetSession(Request, out ISession? session))
|
2024-02-24 15:00:36 +00:00
|
|
|
{
|
|
|
|
_logger.LogDebug("Session not found; redirecting");
|
|
|
|
return _sessionService.DeleteSessionCookie(Response);
|
|
|
|
}
|
|
|
|
|
2024-02-24 15:04:03 +00:00
|
|
|
if (!_sessionService.ValidateSession(Request, session))
|
|
|
|
{
|
|
|
|
_logger.LogDebug("Session invalid; redirecting");
|
|
|
|
return _sessionService.DeleteSessionCookie(Response);
|
|
|
|
}
|
|
|
|
|
2024-02-24 15:00:36 +00:00
|
|
|
if (!_userService.TryGetUser(session.UserId, out IUser? user))
|
|
|
|
{
|
|
|
|
_logger.LogDebug("User not found; redirecting");
|
|
|
|
return _sessionService.DeleteSessionCookie(Response);
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentUser = user;
|
|
|
|
return Page();
|
|
|
|
}
|
|
|
|
}
|