diff options
Diffstat (limited to 'code/api/src/Endpoints/Internal/Root')
5 files changed, 145 insertions, 0 deletions
diff --git a/code/api/src/Endpoints/Internal/Root/GetSessionRoute.cs b/code/api/src/Endpoints/Internal/Root/GetSessionRoute.cs new file mode 100644 index 0000000..82bbb11 --- /dev/null +++ b/code/api/src/Endpoints/Internal/Root/GetSessionRoute.cs @@ -0,0 +1,64 @@ +namespace IOL.GreatOffice.Api.Endpoints.Internal.Root; + +public class GetSessionRoute : RouteBaseSync.WithoutRequest.WithActionResult<GetSessionRoute.SessionResponse> +{ + private readonly MainAppDatabase _database; + private readonly ILogger<GetSessionRoute> _logger; + + public GetSessionRoute(MainAppDatabase database, ILogger<GetSessionRoute> logger) { + _database = database; + _logger = logger; + } + + public class SessionResponse + { + public string Username { get; set; } + public string DisplayName { get; set; } + public Guid UserId { get; set; } + public SessionTenant CurrentTenant { get; set; } + public List<SessionTenant> AvailableTenants { get; set; } + + public class SessionTenant + { + public Guid Id { get; set; } + public string Name { get; set; } + } + } + + [Authorize] + [HttpGet("~/_/session-data")] + public override ActionResult<SessionResponse> Handle() { + var user = _database.Users.Include(c => c.Tenants) + .Select(c => new User() { + Id = c.Id, + Username = c.Username, + FirstName = c.FirstName, + LastName = c.LastName, + Tenants = c.Tenants + }).FirstOrDefault(c => c.Id == LoggedInUser.Id); + + if (user == default) { + return NotFound(); + } + + var currentTenant = user.Tenants.FirstOrDefault(c => c.Id == LoggedInUser.TenantId); + if (currentTenant == default) { + _logger.LogInformation("Could not find current tenant ({tenantId}) for user {userId}", LoggedInUser.TenantId, LoggedInUser.Id); + return NotFound(); + } + + return Ok(new SessionResponse() { + Username = user.Username, + DisplayName = user.DisplayName(), + UserId = user.Id, + CurrentTenant = new SessionResponse.SessionTenant() { + Id = currentTenant.Id, + Name = currentTenant.Name + }, + AvailableTenants = user.Tenants.Select(c => new SessionResponse.SessionTenant() { + Id = c.Id, + Name = c.Name + }).ToList() + }); + } +}
\ No newline at end of file diff --git a/code/api/src/Endpoints/Internal/Root/IsAuthenticatedRoute.cs b/code/api/src/Endpoints/Internal/Root/IsAuthenticatedRoute.cs new file mode 100644 index 0000000..7bb0a86 --- /dev/null +++ b/code/api/src/Endpoints/Internal/Root/IsAuthenticatedRoute.cs @@ -0,0 +1,10 @@ +namespace IOL.GreatOffice.Api.Endpoints.Internal.Root; + +public class IsAuthenticatedRoute : RouteBaseSync.WithoutRequest.WithActionResult +{ + [Authorize] + [HttpGet("~/_/is-authenticated")] + public override ActionResult Handle() { + return Ok(); + } +}
\ No newline at end of file diff --git a/code/api/src/Endpoints/Internal/Root/ReadConfigurationRoute.cs b/code/api/src/Endpoints/Internal/Root/ReadConfigurationRoute.cs new file mode 100644 index 0000000..7270fd8 --- /dev/null +++ b/code/api/src/Endpoints/Internal/Root/ReadConfigurationRoute.cs @@ -0,0 +1,17 @@ +namespace IOL.GreatOffice.Api.Endpoints.Internal.Root; + +public class ReadConfigurationRoute : RouteBaseSync.WithoutRequest.WithActionResult +{ + private readonly VaultService _vaultService; + + public ReadConfigurationRoute(VaultService vaultService) { + _vaultService = vaultService; + } + + [AllowAnonymous] + [HttpGet("~/_/configuration")] + public override ActionResult Handle() { + var config = _vaultService.GetCurrentAppConfiguration(); + return Content(JsonSerializer.Serialize(config.GetPublicVersion()), "application/json"); + } +}
\ No newline at end of file diff --git a/code/api/src/Endpoints/Internal/Root/RefreshConfigurationRoute.cs b/code/api/src/Endpoints/Internal/Root/RefreshConfigurationRoute.cs new file mode 100644 index 0000000..fde4832 --- /dev/null +++ b/code/api/src/Endpoints/Internal/Root/RefreshConfigurationRoute.cs @@ -0,0 +1,15 @@ +namespace IOL.GreatOffice.Api.Endpoints.Internal.Root; + +public class RefreshConfigurationRoute : RouteBaseSync.WithoutRequest.WithoutResult +{ + private readonly VaultService _vaultService; + + public RefreshConfigurationRoute(VaultService vaultService) { + _vaultService = vaultService; + } + + [HttpGet("~/_/refresh-configuration")] + public override void Handle() { + _vaultService.RefreshCurrentAppConfigurationAsync(); + } +}
\ No newline at end of file diff --git a/code/api/src/Endpoints/Internal/Root/ValidateRoute.cs b/code/api/src/Endpoints/Internal/Root/ValidateRoute.cs new file mode 100644 index 0000000..8f0882d --- /dev/null +++ b/code/api/src/Endpoints/Internal/Root/ValidateRoute.cs @@ -0,0 +1,39 @@ +namespace IOL.GreatOffice.Api.Endpoints.Internal.Root; + +public class ValidateRoute : RouteBaseSync.WithRequest<ValidateRoute.QueryParams>.WithActionResult +{ + private readonly EmailValidationService _emailValidation; + private readonly string CanonicalFrontendUrl; + private readonly ILogger<ValidateRoute> _logger; + + public ValidateRoute(VaultService vaultService, EmailValidationService emailValidation, ILogger<ValidateRoute> logger) { + _emailValidation = emailValidation; + _logger = logger; + var c = vaultService.GetCurrentAppConfiguration(); + CanonicalFrontendUrl = c.CANONICAL_FRONTEND_URL; + } + + public class QueryParams + { + [FromQuery] + public Guid Id { get; set; } + } + + [HttpGet("~/_/validate")] + public override ActionResult Handle([FromQuery] QueryParams request) { + var isFulfilled = _emailValidation.FulfillEmailValidationRequest(request.Id, LoggedInUser.Id); + if (!isFulfilled) { + _logger.LogError("Email validation fulfillment failed for request {requestId} and user {userId}", request.Id, LoggedInUser.Id); + return StatusCode(400, $""" +<html> +<body> +<h3>The validation could not be completed</h3> +<p>We are working on fixing this, in the meantime, have patience.</p> +<a href="{CanonicalFrontendUrl}">Click here to go back to {CanonicalFrontendUrl}</a> +</body> +"""); + } + + return Redirect(CanonicalFrontendUrl + "/portal?msg=emailValidated"); + } +}
\ No newline at end of file |
