security: round 2 — multi-tenancy isolation (OrganisationScope, scoped validation, boundary checks)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-14 06:38:19 +02:00
parent 1028498705
commit 090d2b7d89
40 changed files with 603 additions and 64 deletions

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Models;
use App\Models\Scopes\OrganisationScope;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@@ -18,6 +19,11 @@ final class Company extends Model
use HasUlids;
use SoftDeletes;
protected static function booted(): void
{
static::addGlobalScope(new OrganisationScope());
}
protected $fillable = [
'organisation_id',
'name',

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Models;
use App\Enums\CrowdListType;
use App\Models\Scopes\OrganisationScope;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@@ -16,6 +17,14 @@ final class CrowdList extends Model
use HasFactory;
use HasUlids;
/** @var string Used by OrganisationScope to determine filtering strategy */
public string $organisationScopeColumn = 'event_id';
protected static function booted(): void
{
static::addGlobalScope(new OrganisationScope());
}
protected $fillable = [
'event_id',
'crowd_type_id',

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Models;
use App\Models\Scopes\OrganisationScope;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@@ -15,6 +16,11 @@ final class CrowdType extends Model
use HasFactory;
use HasUlids;
protected static function booted(): void
{
static::addGlobalScope(new OrganisationScope());
}
protected $fillable = [
'organisation_id',
'name',

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Models;
use App\Models\Scopes\OrganisationScope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -20,6 +21,14 @@ final class Event extends Model
use HasUlids;
use SoftDeletes;
/** @var string Used by OrganisationScope to determine filtering strategy */
public string $organisationScopeColumn = 'organisation_id';
protected static function booted(): void
{
static::addGlobalScope(new OrganisationScope());
}
/** @var array<string, list<string>> Allowed status transitions */
public const STATUS_TRANSITIONS = [
'draft' => ['published'],

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Models;
use App\Models\Scopes\OrganisationScope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -18,6 +19,14 @@ final class FestivalSection extends Model
use HasUlids;
use SoftDeletes;
/** @var string Used by OrganisationScope to determine filtering strategy */
public string $organisationScopeColumn = 'event_id';
protected static function booted(): void
{
static::addGlobalScope(new OrganisationScope());
}
protected $fillable = [
'event_id',
'name',

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Models;
use App\Models\Scopes\OrganisationScope;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@@ -14,6 +15,14 @@ final class Location extends Model
use HasFactory;
use HasUlids;
/** @var string Used by OrganisationScope to determine filtering strategy */
public string $organisationScopeColumn = 'event_id';
protected static function booted(): void
{
static::addGlobalScope(new OrganisationScope());
}
protected $fillable = [
'event_id',
'name',

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Models;
use App\Enums\IdentityMatchStatus;
use App\Models\Scopes\OrganisationScope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -21,6 +22,14 @@ final class Person extends Model
use HasUlids;
use SoftDeletes;
/** @var string Used by OrganisationScope to determine filtering strategy */
public string $organisationScopeColumn = 'event_id';
protected static function booted(): void
{
static::addGlobalScope(new OrganisationScope());
}
protected $table = 'persons';
protected $fillable = [

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Models;
use App\Models\Scopes\OrganisationScope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -15,6 +16,11 @@ final class PersonTag extends Model
use HasFactory;
use HasUlids;
protected static function booted(): void
{
static::addGlobalScope(new OrganisationScope());
}
protected $fillable = [
'name',
'category',

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Models;
use App\Enums\RegistrationFieldType;
use App\Models\Scopes\OrganisationScope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -16,6 +17,11 @@ final class RegistrationFieldTemplate extends Model
use HasFactory;
use HasUlids;
protected static function booted(): void
{
static::addGlobalScope(new OrganisationScope());
}
protected $fillable = [
'organisation_id',
'label',

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Models;
use App\Enums\RegistrationFieldType;
use App\Models\Scopes\OrganisationScope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -17,6 +18,14 @@ final class RegistrationFormField extends Model
use HasFactory;
use HasUlids;
/** @var string Used by OrganisationScope to determine filtering strategy */
public string $organisationScopeColumn = 'event_id';
protected static function booted(): void
{
static::addGlobalScope(new OrganisationScope());
}
protected $fillable = [
'event_id',
'label',

View File

@@ -4,22 +4,23 @@ declare(strict_types=1);
namespace App\Models\Scopes;
use App\Models\Event;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
/**
* Global scope that filters event-related models by organisation_id.
* Global scope that filters models by organisation_id.
*
* Resolves the organisation from (in order):
* 1. An explicitly provided organisation ID (constructor)
* 2. The route parameter 'organisation'
* Resolution order:
* 1. Explicitly provided organisation ID (constructor)
* 2. Route parameter 'organisation' (object or string)
* 3. Skip scope if no context (CLI, queue jobs, unauthenticated)
*
* IMPORTANT: This scope is route-dependent it only works when the
* 'organisation' parameter is present in the URL (e.g. /organisations/{organisation}/events).
* Routes without an organisation segment (e.g. GET /events/{event}) will NOT be scoped,
* silently bypassing multi-tenancy. All new routes that touch organisation-owned
* data MUST be nested under /organisations/{organisation}/... to guarantee scoping.
* Models declare their scoping strategy via the $organisationScopeColumn property:
* - 'organisation_id' (default) model has a direct organisation_id column
* - 'event_id' model is scoped through events.organisation_id
* - 'festival_section_id' model is scoped through festival_sections.event_id events.organisation_id
*/
final class OrganisationScope implements Scope
{
@@ -29,12 +30,61 @@ final class OrganisationScope implements Scope
public function apply(Builder $builder, Model $model): void
{
$id = $this->organisationId
?? request()->route('organisation')?->id
?? (is_string(request()->route('organisation')) ? request()->route('organisation') : null);
$id = $this->resolveOrganisationId();
if ($id) {
$builder->where($model->getTable() . '.organisation_id', $id);
if ($id === null) {
return;
}
$column = $model->organisationScopeColumn ?? 'organisation_id';
match ($column) {
'organisation_id' => $builder->where($model->getTable() . '.organisation_id', $id),
'event_id' => $builder->whereIn(
$model->getTable() . '.event_id',
Event::withoutGlobalScope(self::class)
->where('organisation_id', $id)
->select('id')
),
'festival_section_id' => $builder->whereIn(
$model->getTable() . '.festival_section_id',
\App\Models\FestivalSection::withoutGlobalScope(self::class)
->whereIn('event_id', Event::withoutGlobalScope(self::class)
->where('organisation_id', $id)
->select('id'))
->select('id')
),
default => null,
};
}
private function resolveOrganisationId(): ?string
{
if ($this->organisationId !== null) {
return $this->organisationId;
}
$route = request()->route();
if ($route === null) {
return null;
}
$org = $route->parameter('organisation');
if ($org instanceof \App\Models\Organisation) {
return $org->id;
}
if (is_string($org) && $org !== '') {
return $org;
}
// Fall back to the event's organisation if we're on an event route
$event = $route->parameter('event');
if ($event instanceof Event) {
return $event->organisation_id;
}
return null;
}
}

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Models;
use App\Enums\ShiftAssignmentStatus;
use App\Models\Scopes\OrganisationScope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
@@ -20,6 +21,14 @@ final class Shift extends Model
use HasUlids;
use SoftDeletes;
/** @var string Used by OrganisationScope to determine filtering strategy */
public string $organisationScopeColumn = 'festival_section_id';
protected static function booted(): void
{
static::addGlobalScope(new OrganisationScope());
}
protected $fillable = [
'festival_section_id',
'time_slot_id',

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Models;
use App\Models\Scopes\OrganisationScope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -17,6 +18,14 @@ final class TimeSlot extends Model
use HasFactory;
use HasUlids;
/** @var string Used by OrganisationScope to determine filtering strategy */
public string $organisationScopeColumn = 'event_id';
protected static function booted(): void
{
static::addGlobalScope(new OrganisationScope());
}
protected $fillable = [
'event_id',
'name',