diff --git a/WebApp/Endpoints/EventsEndpoints.cs b/WebApp/Endpoints/EventsEndpoints.cs index 32a2faf..d15ce84 100644 --- a/WebApp/Endpoints/EventsEndpoints.cs +++ b/WebApp/Endpoints/EventsEndpoints.cs @@ -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) diff --git a/WebApp/Endpoints/GeneralUseHelperFunctions.cs b/WebApp/Endpoints/GeneralUseHelperFunctions.cs index c20a687..93eeedf 100644 --- a/WebApp/Endpoints/GeneralUseHelperFunctions.cs +++ b/WebApp/Endpoints/GeneralUseHelperFunctions.cs @@ -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) {