Get OrgId only from token

This commit is contained in:
AleksDw
2025-05-18 18:16:26 +02:00
parent bebf47a2ba
commit 69c508ef84
4 changed files with 4 additions and 18 deletions

View File

@@ -58,16 +58,10 @@ namespace WebApp.Endpoints
Organisation? org = await guhf.GetOrganisationFromToken(token);
if (org is null) return Results.Unauthorized();
// dodajemy id organizacji z tokenu
Event Eve = newEvent.ToEntity();
// Wyzeruj EventRegistrations, ponieważ nie są to dane,
// które powinniśmy przyjmować bez zgody wolontariuszy!
Eve.EventRegistrations = [];
Eve.OrganisationId = org.OrganisationId;
// Na wszelki wypadek, gdyby użytkownik wciskał nam kit :D
if (newEvent.OrganisationId is not null && newEvent.OrganisationId != org.OrganisationId) return Results.StatusCode(418);
dbContext.Events.Add(Eve);
await dbContext.SaveChangesAsync();
@@ -81,12 +75,12 @@ namespace WebApp.Endpoints
group.MapPut("/{id}",
async (int id, EventUpdateDto updatedEvent, ApplicationDbContext dbContext, GeneralUseHelpers guhf, HttpContext httpContext) =>
{
// Uzyskaj organizację z tokenu
Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
Organisation? org = await guhf.GetOrganisationFromToken(token);
if (org is null) return Results.Unauthorized();
Console.Write(org.OrganisationId);
var existingEvent = await dbContext.Events.FindAsync(id);
if (existingEvent is null)
{
@@ -97,15 +91,11 @@ namespace WebApp.Endpoints
// 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);
var originalOrgId = existingEvent.OrganisationId;
dbContext.Entry(existingEvent)
.CurrentValues
.SetValues(updatedEvent.ToEntity(id));
existingEvent.OrganisationId = originalOrgId;
dbContext.Entry(existingEvent)
.Collection(Eve => Eve.EventRegistrations)