-
app/Policies/PresentationPolicy.php
Open in GitHubuse App\Models\Presentation; use App\Models\User; class PresentationPolicy { // ... public function view(User $user, Presentation $presentation): bool { return $presentation->user_id === $user->id; } // ... }
-
app/Http/Controllers/PresentationController.php
Open in GitHubuse App\Models\User; use Inertia\Inertia; use Inertia\Response; class PresentationController extends Controller { public function show(User $user, string $slug): Response { $presentation = $user ->presentations() ->where('slug', $slug) ->firstOrFail(); if (! $presentation->canBeViewed) { abort(403); } // ... } }
-
app/Models/Presentation.php
Open in GitHubuse Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; class Presentation extends Model implements HasMedia { // ... protected function canBeViewed(): Attribute { return Attribute::make( get: function (mixed $value, array $attributes): bool { if ($this->is_published) { return true; } if (! auth()->check()) { return false; } return auth()->user()->can('view', $this); }, ); } // ... }