mirror of
https://github.com/GCMatters/hermes.git
synced 2026-02-04 13:40: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
|
// Input values in JSON file to create event
|
||||||
public record class EventCreateDto
|
public record class EventCreateDto
|
||||||
(
|
(
|
||||||
[Required] int? OrganisationId,
|
|
||||||
[Required][StringLength(50)] string Title,
|
[Required][StringLength(50)] string Title,
|
||||||
[StringLength(500)] string Description,
|
[StringLength(500)] string Description,
|
||||||
[Required][StringLength(100)] string Location,
|
[Required][StringLength(100)] string Location,
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ namespace WebApp.DTOs;
|
|||||||
// Input values in JSON file to update event
|
// Input values in JSON file to update event
|
||||||
public record class EventUpdateDto
|
public record class EventUpdateDto
|
||||||
(
|
(
|
||||||
[Required] int? OrganisationId,
|
|
||||||
[Required][StringLength(50)] string Title,
|
[Required][StringLength(50)] string Title,
|
||||||
[StringLength(500)] string Description,
|
[StringLength(500)] string Description,
|
||||||
[Required][StringLength(100)] string Location,
|
[Required][StringLength(100)] string Location,
|
||||||
|
|||||||
@@ -58,16 +58,10 @@ namespace WebApp.Endpoints
|
|||||||
Organisation? org = await guhf.GetOrganisationFromToken(token);
|
Organisation? org = await guhf.GetOrganisationFromToken(token);
|
||||||
if (org is null) return Results.Unauthorized();
|
if (org is null) return Results.Unauthorized();
|
||||||
|
|
||||||
|
// dodajemy id organizacji z tokenu
|
||||||
Event Eve = newEvent.ToEntity();
|
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;
|
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);
|
dbContext.Events.Add(Eve);
|
||||||
await dbContext.SaveChangesAsync();
|
await dbContext.SaveChangesAsync();
|
||||||
|
|
||||||
@@ -81,12 +75,12 @@ namespace WebApp.Endpoints
|
|||||||
group.MapPut("/{id}",
|
group.MapPut("/{id}",
|
||||||
async (int id, EventUpdateDto updatedEvent, ApplicationDbContext dbContext, GeneralUseHelpers guhf, HttpContext httpContext) =>
|
async (int id, EventUpdateDto updatedEvent, ApplicationDbContext dbContext, GeneralUseHelpers guhf, HttpContext httpContext) =>
|
||||||
{
|
{
|
||||||
|
|
||||||
// Uzyskaj organizację z tokenu
|
// Uzyskaj organizację z tokenu
|
||||||
Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
|
Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
|
||||||
Organisation? org = await guhf.GetOrganisationFromToken(token);
|
Organisation? org = await guhf.GetOrganisationFromToken(token);
|
||||||
if (org is null) return Results.Unauthorized();
|
if (org is null) return Results.Unauthorized();
|
||||||
|
|
||||||
|
Console.Write(org.OrganisationId);
|
||||||
var existingEvent = await dbContext.Events.FindAsync(id);
|
var existingEvent = await dbContext.Events.FindAsync(id);
|
||||||
if (existingEvent is null)
|
if (existingEvent is null)
|
||||||
{
|
{
|
||||||
@@ -97,15 +91,11 @@ namespace WebApp.Endpoints
|
|||||||
// do zmodyfikowania tego (EventId = id) eventu.
|
// do zmodyfikowania tego (EventId = id) eventu.
|
||||||
if (org.OrganisationId != existingEvent.OrganisationId) return Results.StatusCode(403);
|
if (org.OrganisationId != existingEvent.OrganisationId) return Results.StatusCode(403);
|
||||||
|
|
||||||
// Nadpisz organisationId (obecne w updatedEvent,
|
var originalOrgId = existingEvent.OrganisationId;
|
||||||
// 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)
|
dbContext.Entry(existingEvent)
|
||||||
.CurrentValues
|
.CurrentValues
|
||||||
.SetValues(updatedEvent.ToEntity(id));
|
.SetValues(updatedEvent.ToEntity(id));
|
||||||
|
existingEvent.OrganisationId = originalOrgId;
|
||||||
|
|
||||||
dbContext.Entry(existingEvent)
|
dbContext.Entry(existingEvent)
|
||||||
.Collection(Eve => Eve.EventRegistrations)
|
.Collection(Eve => Eve.EventRegistrations)
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ public static class EventMapping
|
|||||||
{
|
{
|
||||||
return new Event()
|
return new Event()
|
||||||
{
|
{
|
||||||
OrganisationId = ECDto.OrganisationId!.Value,
|
|
||||||
Title = ECDto.Title,
|
Title = ECDto.Title,
|
||||||
Description = ECDto.Description,
|
Description = ECDto.Description,
|
||||||
Location = ECDto.Location,
|
Location = ECDto.Location,
|
||||||
@@ -25,7 +24,6 @@ public static class EventMapping
|
|||||||
return new Event()
|
return new Event()
|
||||||
{
|
{
|
||||||
EventId = id,
|
EventId = id,
|
||||||
OrganisationId = EUDto.OrganisationId!.Value,
|
|
||||||
Title = EUDto.Title,
|
Title = EUDto.Title,
|
||||||
Description = EUDto.Description,
|
Description = EUDto.Description,
|
||||||
Location = EUDto.Location,
|
Location = EUDto.Location,
|
||||||
|
|||||||
Reference in New Issue
Block a user