mirror of
https://github.com/GCMatters/hermes.git
synced 2026-02-04 05:30:13 +01:00
Get OrgId only from token
This commit is contained in:
@@ -6,7 +6,6 @@ namespace WebApp.DTOs;
|
||||
// Input values in JSON file to create event
|
||||
public record class EventCreateDto
|
||||
(
|
||||
[Required] int? OrganisationId,
|
||||
[Required][StringLength(50)] string Title,
|
||||
[StringLength(500)] string Description,
|
||||
[Required][StringLength(100)] string Location,
|
||||
|
||||
@@ -6,7 +6,6 @@ namespace WebApp.DTOs;
|
||||
// Input values in JSON file to update event
|
||||
public record class EventUpdateDto
|
||||
(
|
||||
[Required] int? OrganisationId,
|
||||
[Required][StringLength(50)] string Title,
|
||||
[StringLength(500)] string Description,
|
||||
[Required][StringLength(100)] string Location,
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -10,7 +10,6 @@ public static class EventMapping
|
||||
{
|
||||
return new Event()
|
||||
{
|
||||
OrganisationId = ECDto.OrganisationId!.Value,
|
||||
Title = ECDto.Title,
|
||||
Description = ECDto.Description,
|
||||
Location = ECDto.Location,
|
||||
@@ -25,7 +24,6 @@ public static class EventMapping
|
||||
return new Event()
|
||||
{
|
||||
EventId = id,
|
||||
OrganisationId = EUDto.OrganisationId!.Value,
|
||||
Title = EUDto.Title,
|
||||
Description = EUDto.Description,
|
||||
Location = EUDto.Location,
|
||||
|
||||
Reference in New Issue
Block a user