atcheck/app/Http/Middleware/CheckClassesCode.php

36 lines
916 B
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');
2019-12-07 19:49:42 +01:00
$classes = Classes::where('classes_code', $classes_code_from_request)->first();
if(!$classes) {
return redirect('home');
} else {
2019-12-07 19:49:42 +01:00
$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);
}
}