lms/app/Http/Middleware/CheckEnroll.php
Ahmed Darrazi 1b8be73219
All checks were successful
Build & Push Docker Image / docker (push) Successful in 1m48s
bugfix
2025-12-19 15:37:20 +01:00

58 lines
1.5 KiB
PHP

<?php
namespace App\Http\Middleware;
use App\Models\Course\Course;
use App\Models\Course\CourseEnrollment;
use App\Models\Course\WatchHistory;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;
class CheckEnroll
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$user = Auth::user();
if ($user->role == 'admin') {
return $next($request);
}
$watchHistory = $request->route('watch_history');
if (!($watchHistory instanceof WatchHistory)) {
$watchHistory = WatchHistory::find($watchHistory);
}
if (!$watchHistory) {
return back()->with('error', 'Invalid watch history');
}
$course = Course::find($watchHistory->course_id);
if (!$course) {
return back()->with('error', 'Invalid course');
}
if ($user->role == 'instructor' && $user->instructor_id == $course->instructor_id) {
return $next($request);
}
$enrollment = CourseEnrollment::where('user_id', $user->id)
->where('course_id', $watchHistory->course_id)
->first();
if ($enrollment) {
return $next($request);
}
return back()->with('error', 'You are not enrolled in this course');
}
}