Skip to content

Commit

Permalink
better validation and authorization on edit route
Browse files Browse the repository at this point in the history
  • Loading branch information
fredbradley committed Apr 12, 2024
1 parent 91e4e6f commit cfd0b6d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,7 @@ public function edit(int $reportCycleId, int $teachingSetId, int $teacherId): Vi
$reportCycle = ReportCycles::find($reportCycleId);

if (Gate::check('report-editable', $reportCycle) === false) {
session()->flash('alert-danger', 'This report cycle has ended and is no longer editable.');

return redirect()->route('selfreflection.showget', [
'reportCycle' => $reportCycleId,
'pupilId' => (new PupilData())->pupil_id,
]);
abort(403, 'You are not able to edit this reflection');
}

if (! $this->authorizeEdit($teachingSetId, $teacherId)) {
Expand Down
5 changes: 3 additions & 2 deletions app/Providers/SelfReflectionGateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Providers;

use App\Domains\SelfReflection\Actions\ReportCycles;
use App\Models\User;
use Illuminate\Auth\Access\Response;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Gate;
Expand Down Expand Up @@ -34,7 +35,7 @@ public function boot(): void
return Response::allow();
});

Gate::define('report-editable', function ($user, int|object $reportCycle) {
Gate::define('report-editable', function (User $user, int|object $reportCycle) {

if (is_int($reportCycle)) {
$reportCycle = ReportCycles::find($reportCycle);
Expand All @@ -44,7 +45,7 @@ public function boot(): void
return false;
}

return true;
return $user->isPupil();
});
}
}

0 comments on commit cfd0b6d

Please sign in to comment.