Skip to content

Commit

Permalink
Merge pull request #25 from cybercog/feature/logout-banned-user
Browse files Browse the repository at this point in the history
Add middleware with force logout banned user
  • Loading branch information
antonkomarev authored Sep 9, 2018
2 parents 2048048 + 9e7205b commit 0ed652c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@ Route::get('/', [
]);
```

If you want force logout banned user on protected routes access, use `LogsOutBannedUser` middleware instead:

```php
protected $routeMiddleware = [
'logs-out-banned-user' => \Cog\Laravel\Ban\Http\Middleware\LogsOutBannedUser::class,
]
```

### Scheduling

After you have performed the basic installation you can start using the `ban:delete-expired` command. In most cases you'll want to schedule these command so you don't have to manually run it everytime you need to delete expired bans and unban models.
Expand Down Expand Up @@ -314,8 +322,8 @@ If you discover any security related issues, please email open@cybercog.su inste

## Contributors

| <a href="https://github.com/antonkomarev">![@antonkomarev](https://avatars.githubusercontent.com/u/1849174?s=110)<br />Anton Komarev</a> |
| :---: |
| <a href="https://github.com/antonkomarev">![@antonkomarev](https://avatars.githubusercontent.com/u/1849174?s=110)<br />Anton Komarev</a> | <a href="https://github.com/badrshs">![@badrshs](https://avatars.githubusercontent.com/u/26596347?s=110)<br />badr aldeen shek salim</a> |
| :---: | :---: |

[Laravel Ban contributors list](../../contributors)

Expand Down
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
verbose="true"
>
<testsuites>
Expand Down
5 changes: 2 additions & 3 deletions src/Http/Middleware/ForbidBannedUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Cog\Laravel\Ban\Http\Middleware;

use Closure;
use Cog\Contracts\Ban\Bannable as BannableContract;
use Illuminate\Contracts\Auth\Guard;

/**
Expand Down Expand Up @@ -48,9 +49,7 @@ public function handle($request, Closure $next)
{
$user = $this->auth->user();

if ($user && $user->isBanned()) {
$this->auth->logout();

if ($user && $user instanceof BannableContract && $user->isBanned()) {
return redirect()->back()->withInput()->withErrors([
'login' => 'This account is blocked.',
]);
Expand Down
66 changes: 66 additions & 0 deletions src/Http/Middleware/LogsOutBannedUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?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.
*/

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.
*
* @package Cog\Laravel\Ban\Http\Middleware
*/
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);
}
}

0 comments on commit 0ed652c

Please sign in to comment.