mirror of
https://github.com/GCMatters/hermes.git
synced 2026-02-04 13:40:13 +01:00
feat: secure existing events endpoints by validating the token cookie
This commit is contained in:
@@ -28,21 +28,23 @@ namespace WebApp.Endpoints
|
||||
|
||||
// GET /events/1
|
||||
group.MapGet("/{id}",
|
||||
async (int id, ApplicationDbContext dbContext, HttpContext httpContext) =>
|
||||
async (int id, ApplicationDbContext dbContext, HttpContext httpContext, GeneralUseHelpers guhf) =>
|
||||
{
|
||||
Event? Eve = await dbContext.Events.FindAsync(id);
|
||||
|
||||
if (Eve is null) return Results.NotFound();
|
||||
|
||||
// Sprawdź, czy token należy do organizacji, a jeżeli tak, to do której.
|
||||
// ...
|
||||
Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
|
||||
Organisation? org = await guhf.GetOrganisationFromToken(token);
|
||||
|
||||
// Jeśli token należy do organizacji, która utworzyła to wydarzenie,
|
||||
// to zwróć także EventRegistrations. W przeciwnym razie usuń to pole
|
||||
// przed jego wysłaniem!
|
||||
// ...
|
||||
Eve.ToEventDetailsDto();
|
||||
if (org is null || org.OrganisationId != Eve.OrganisationId) Eve.EventRegistrations = [];
|
||||
|
||||
return Results.Ok(Eve.ToEventDetailsDto()); //EventDetailsDto
|
||||
return Results.Ok(Eve); //EventDetailsDto
|
||||
})
|
||||
.WithName(GetEventEndpointName);
|
||||
|
||||
@@ -54,13 +56,13 @@ namespace WebApp.Endpoints
|
||||
// Uzyskaj organizację z tokenu
|
||||
Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
|
||||
Organisation? org = await guhf.GetOrganisationFromToken(token);
|
||||
if (org is null) return Results.StatusCode(403);
|
||||
if (org is null) return Results.Unauthorized();
|
||||
|
||||
Event Eve = newEvent.ToEntity();
|
||||
|
||||
// Wyzeruj EventRegistrations, ponieważ nie są to dane,
|
||||
// które powinniśmy przyjmować bez zgody wolontariuszy!
|
||||
// ...
|
||||
Eve.EventRegistrations = [];
|
||||
|
||||
dbContext.Events.Add(Eve);
|
||||
await dbContext.SaveChangesAsync();
|
||||
@@ -79,7 +81,7 @@ namespace WebApp.Endpoints
|
||||
// Uzyskaj organizację z tokenu
|
||||
Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
|
||||
Organisation? org = await guhf.GetOrganisationFromToken(token);
|
||||
if (org is null) return Results.StatusCode(403);
|
||||
if (org is null) return Results.Unauthorized();
|
||||
|
||||
var existingEvent = await dbContext.Events.FindAsync(id);
|
||||
if (existingEvent is null)
|
||||
@@ -89,12 +91,13 @@ namespace WebApp.Endpoints
|
||||
|
||||
// Sprawdź, czy organizacja ma prawo
|
||||
// do zmodyfikowania tego (EventId = id) eventu.
|
||||
// ...
|
||||
if (org.OrganisationId != existingEvent.OrganisationId) return Results.StatusCode(403);
|
||||
|
||||
// Nadpisz organisationId (obecne w updatedEvent,
|
||||
// lecz nie sprawdzane poniżej) na to, co odczytaliśmy
|
||||
// do existingEvent.
|
||||
// ...
|
||||
// ... trzeba by było tworzyć obiekt od nowa, zamiast tego po prostu zwróćmy błąd.
|
||||
if (existingEvent.OrganisationId != updatedEvent.OrganisationId) return Results.StatusCode(403);
|
||||
|
||||
dbContext.Entry(existingEvent)
|
||||
.CurrentValues
|
||||
@@ -121,7 +124,9 @@ namespace WebApp.Endpoints
|
||||
|
||||
// Sprawdź, czy organizacja ma prawo
|
||||
// do usunięcia tego (EventId = id) eventu.
|
||||
// ...
|
||||
Event? Eve = await dbContext.Events.FindAsync(id);
|
||||
if (Eve is null) return Results.NotFound();
|
||||
else if (org.OrganisationId != Eve.OrganisationId) return Results.StatusCode(403);
|
||||
|
||||
await dbContext.Events
|
||||
.Where(Eve => Eve.EventId == id)
|
||||
|
||||
@@ -25,7 +25,7 @@ public class GeneralUseHelpers
|
||||
{
|
||||
// Zwróci null, gdy nie znaleziono użytkownika
|
||||
if (t is null) return null;
|
||||
User? user = await _context.WebUsers.FindAsync(t.Value);
|
||||
User? user = await _context.WebUsers.FindAsync(t.UserId);
|
||||
|
||||
return user;
|
||||
}
|
||||
@@ -35,7 +35,7 @@ public class GeneralUseHelpers
|
||||
User? user = await GetUserFromToken(t);
|
||||
if (user is not null && user.IsOrganisation)
|
||||
{
|
||||
Organisation? org = await _context.Organisations.FindAsync(t.UserId);
|
||||
Organisation? org = await _context.Organisations.FirstOrDefaultAsync(o => o.UserId == t.UserId);
|
||||
|
||||
if (org is null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user