/
LogsOutBannedUser.php
63 lines (53 loc) · 1.45 KB
/
LogsOutBannedUser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
/*
* This file is part of Laravel Ban.
*
* (c) Anton Komarev <a.komarev@cybercog.su>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Cog\Laravel\Ban\Http\Middleware;
use Closure;
use Cog\Contracts\Ban\Bannable as BannableContract;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\StatefulGuard as StatefulGuardContract;
class LogsOutBannedUser
{
/**
* The Guard implementation.
*
* @var \Illuminate\Contracts\Auth\Guard
*/
protected $auth;
/**
* @param \Illuminate\Contracts\Auth\Guard $auth
*/
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
* @throws \Exception
*/
public function handle($request, Closure $next)
{
$user = $this->auth->user();
if ($user && $user instanceof BannableContract && $user->isBanned()) {
if ($this->auth instanceof StatefulGuardContract) {
// TODO: Cover with tests
$this->auth->logout();
}
return redirect()->back()->withInput()->withErrors([
'login' => 'This account is blocked.',
]);
}
return $next($request);
}
}