Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rewrite the middlewares for check roles to become only one which can handle all roles #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions app/Http/Controllers/GradeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
use App\Models\Student;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Routing\Controllers\HasMiddleware;

class GradeController extends Controller implements HasMiddleware
class GradeController extends Controller
{
public function courseIndex(Course $course)
{
Expand Down Expand Up @@ -82,14 +81,4 @@ public function destroy(Grade $grade)

return back();
}

public static function middleware()
{
return [
function (Request $request, \Closure $next) {
Student::findOrFail(Auth::id());
return $next($request);
},
];
}
}
10 changes: 0 additions & 10 deletions app/Http/Controllers/StudentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,4 @@ public function show(Student $student)
{
return view('students.show', compact('student'));
}

public static function middleware()
{
return [
function (Request $request, \Closure $next) {
Manager::findOrFail(Auth::id());
return $next($request);
},
];
}
}
11 changes: 0 additions & 11 deletions app/Http/Controllers/StudyPlanController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Models\StudyPlan;
use App\Models\Course;
use App\Models\Manager;
use Illuminate\Http\Request;

class StudyPlanController extends Controller
Expand Down Expand Up @@ -57,14 +56,4 @@ public function update(Request $request, StudyPlan $studyPlan)
}
return redirect(route('study_plans.show', $studyPlan));
}

public static function middleware()
{
return [
function (Request $request, \Closure $next) {
Manager::findOrFail(Auth::id());
return $next($request);
},
];
}
}
30 changes: 30 additions & 0 deletions app/Http/Middleware/CheckTypeOfUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Container\Attributes\CurrentUser;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Facades\Auth;


class CheckTypeOfUser
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next, string $role)
{

$user = Auth::user();
if ($user->type !== $role) {
abort(403, 'Accès interdit : rôle non autorisé.');
}

return $next($request);
}
}
15 changes: 10 additions & 5 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Http\Controllers\GradeController;
use App\Http\Controllers\StudentController;
use App\Http\Controllers\StudyPlanController;
use App\Http\Middleware\CheckTypeOfUser;

Route::get('/', function () {
return redirect(route('dashboard'));
Expand All @@ -19,10 +20,14 @@
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');

Route::get('/courses/{course}/grades', [GradeController::class, 'courseIndex'])->name('courses.grades.index');
Route::resource('grades', GradeController::class);
Route::resource('students', StudentController::class)->only('index', 'show');
Route::resource('study_plans', StudyPlanController::class)->except('edit', 'destroy');
Route::middleware(['auth', CheckTypeOfUser::class . ':App\Models\Student'])->group(function () {
Route::get('/courses/{course}/grades', [GradeController::class, 'courseIndex'])->name('courses.grades.index');
Route::resource('grades', GradeController::class);
});
Route::middleware(['auth', CheckTypeOfUser::class . ':App\Models\Manager'])->group(function () {
Route::resource('students', StudentController::class)->only('index', 'show');
Route::resource('study_plans', StudyPlanController::class)->except('edit', 'destroy');
});
});

require __DIR__.'/auth.php';
require __DIR__ . '/auth.php';