mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-10-15 01:54:43 +00:00
Enforce brewery ownership on update and delete (#267)
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
using Domain.Exceptions;
|
||||
|
||||
namespace Features.Breweries.Repository;
|
||||
|
||||
/// <summary>Shared ownership check for commands that mutate a specific brewery post.</summary>
|
||||
public static class BreweryOwnershipExtensions
|
||||
{
|
||||
/// <exception cref="Domain.Exceptions.NotFoundException">Thrown when no brewery exists with the given ID.</exception>
|
||||
/// <exception cref="Domain.Exceptions.ForbiddenException">Thrown when <paramref name="requestingUserId" /> did not post the brewery.</exception>
|
||||
public static async Task EnsureCallerOwnsBreweryAsync(
|
||||
this IBreweryRepository repository,
|
||||
Guid breweryPostId,
|
||||
Guid requestingUserId,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
Guid postedById =
|
||||
await repository.GetPostedByIdAsync(breweryPostId, cancellationToken)
|
||||
?? throw new NotFoundException($"Brewery with ID {breweryPostId} not found.");
|
||||
|
||||
if (postedById != requestingUserId)
|
||||
throw new ForbiddenException("You are not authorized to modify this brewery.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user