Skip to content

Commit 535424b

Browse files
authored
Add redirect route configuration too allow setting of redirect location when triggered by middleware (#63)
Add redirect_url config used in to middlewares
1 parent 0dd7250 commit 535424b

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

config/ban.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,26 @@
2525

2626
'load_default_migrations' => true,
2727

28+
/*
29+
|---------------------------------------------------------------------------
30+
| URL to redirect banned user to
31+
|---------------------------------------------------------------------------
32+
|
33+
| Provide a url, this is where a banned user will be redirected to
34+
| by the middleware.
35+
|
36+
| For example:
37+
|
38+
| 'redirect_url' => route('banned.user'),
39+
|
40+
| or
41+
|
42+
| 'redirect_url' => '/user/banned',
43+
|
44+
| Leaving the value as null will result in a redirect "back".
45+
|
46+
*/
47+
48+
'redirect_url' => null,
49+
2850
];

src/Http/Middleware/ForbidBannedUser.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,16 @@ public function handle($request, Closure $next)
4747
$user = $this->auth->user();
4848

4949
if ($user && $user instanceof BannableContract && $user->isBanned()) {
50-
return redirect()->back()->withInput()->withErrors([
50+
$redirectUrl = config('ban.redirect_url', null);
51+
$errors = [
5152
'login' => 'This account is blocked.',
52-
]);
53+
];
54+
55+
if ($redirectUrl === null) {
56+
return redirect()->back()->withInput()->withErrors($errors);
57+
} else {
58+
return redirect($redirectUrl)->withInput()->withErrors($errors);
59+
}
5360
}
5461

5562
return $next($request);

src/Http/Middleware/LogsOutBannedUser.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,16 @@ public function handle($request, Closure $next)
5353
$this->auth->logout();
5454
}
5555

56-
return redirect()->back()->withInput()->withErrors([
56+
$redirectUrl = config('ban.redirect_url', null);
57+
$errors = [
5758
'login' => 'This account is blocked.',
58-
]);
59+
];
60+
61+
if ($redirectUrl === null) {
62+
return redirect()->back()->withInput()->withErrors($errors);
63+
} else {
64+
return redirect($redirectUrl)->withInput()->withErrors($errors);
65+
}
5966
}
6067

6168
return $next($request);

0 commit comments

Comments
 (0)