1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Tinyissue package. |
5
|
|
|
* |
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tinyissue\Http\Middleware; |
13
|
|
|
|
14
|
|
|
use Closure; |
15
|
|
|
use Illuminate\Contracts\Auth\Guard; |
16
|
|
|
use Illuminate\Contracts\Foundation\Application; |
17
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class CheckForMaintenanceMode |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* The Guard implementation. |
26
|
|
|
* |
27
|
|
|
* @var Guard |
28
|
|
|
*/ |
29
|
|
|
protected $auth; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The application implementation. |
33
|
|
|
* |
34
|
|
|
* @var \Illuminate\Contracts\Foundation\Application |
35
|
|
|
*/ |
36
|
|
|
protected $app; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Create a new filter instance. |
40
|
|
|
* |
41
|
|
|
* @param Guard $auth |
42
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
43
|
|
|
*/ |
44
|
61 |
|
public function __construct(Guard $auth, Application $app) |
45
|
|
|
{ |
46
|
61 |
|
$this->auth = $auth; |
47
|
61 |
|
$this->app = $app; |
48
|
61 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Handle an incoming request. |
52
|
|
|
* |
53
|
|
|
* @param \Illuminate\Http\Request $request |
54
|
|
|
* @param \Closure $next |
55
|
|
|
* |
56
|
|
|
* @return mixed |
57
|
|
|
* |
58
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\HttpException |
59
|
|
|
*/ |
60
|
61 |
|
public function handle($request, Closure $next) |
61
|
|
|
{ |
62
|
61 |
|
$siteDown = $this->app->isDownForMaintenance(); |
63
|
61 |
|
$isLogin = $request->is('/', 'logout', 'signin'); |
64
|
|
|
|
65
|
|
|
// Allow admin & login page to always view the site event in maintenance mode |
66
|
61 |
|
if ($siteDown && !$isLogin && ($this->auth->guest() || !$this->auth->user()->isAdmin())) { |
|
|
|
|
67
|
|
|
throw new HttpException(503); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Show message to administrator |
71
|
61 |
|
if ($siteDown) { |
72
|
|
|
$this->app['session']->flash('notice-error', trans('tinyissue.site_maintenance_message')); |
73
|
|
|
} |
74
|
|
|
|
75
|
61 |
|
return $next($request); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: