atcheck/app/Http/Middleware/CheckClassesCode.php

42 lines
1.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Middleware;
use App\Classes;
use App\Subject;
use Illuminate\Support\Facades\Auth;
use Closure;
class CheckClassesCode
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$classes_code_from_request = $request->input('classes_code');
$classes_code_from_session = session()->get('CLASSES_CODE');
$classes_id = session()->get('CLASSES_ID');
$classes = Classes::find($classes_id);
if(!$classes) {
return redirect('home');
} else {
if ($classes_code_from_request != $classes_code_from_session) {
return redirect('home');
} else {
$user_id = Subject::where('id', $classes->subject_id)->first()->user_id;
if (!$user_id || $user_id != Auth::id()) {
return redirect('home');
}
}
}
$request->attributes->add(['classes_id' => $classes->id]);
return $next($request);
}
}