|
3 | 3 | namespace CodeZero\StageFront\Middleware; |
4 | 4 |
|
5 | 5 | use Closure; |
| 6 | +use CodeZero\StageFront\Shield; |
6 | 7 | use Illuminate\Support\Facades\Config; |
7 | | -use Illuminate\Support\Facades\Session; |
8 | 8 |
|
9 | 9 | class RedirectIfStageFrontIsEnabled |
10 | 10 | { |
11 | 11 | /** |
12 | | - * Handle an incoming request. |
| 12 | + * Shield instance. |
13 | 13 | * |
14 | | - * @param \Illuminate\Http\Request $request |
15 | | - * @param \Closure $next |
| 14 | + * @var \CodeZero\StageFront\Shield |
| 15 | + */ |
| 16 | + protected $shield; |
| 17 | + |
| 18 | + /** |
| 19 | + * Create a new RedirectIfStageFrontIsEnabled instance. |
16 | 20 | * |
17 | | - * @return mixed |
| 21 | + * @param \CodeZero\StageFront\Shield $shield |
18 | 22 | */ |
19 | | - public function handle($request, Closure $next) |
| 23 | + public function __construct(Shield $shield) |
20 | 24 | { |
21 | | - $disabled = ! Config::get('stagefront.enabled', false); |
22 | | - $unlocked = Session::get('stagefront.unlocked', false); |
23 | | - $stageFrontUrl = Config::get('stagefront.url'); |
24 | | - $ignoredUrls = Config::get('stagefront.ignore_urls', []); |
25 | | - array_push($ignoredUrls, $stageFrontUrl); |
26 | | - |
27 | | - if ($unlocked || $disabled || $this->urlIsIgnored($request, $ignoredUrls)) { |
28 | | - return $next($request); |
29 | | - } |
30 | | - |
31 | | - return redirect($stageFrontUrl); |
| 25 | + $this->shield = $shield; |
32 | 26 | } |
33 | 27 |
|
34 | 28 | /** |
35 | | - * Check if a URL should be ignored. |
| 29 | + * Handle an incoming request. |
36 | 30 | * |
37 | 31 | * @param \Illuminate\Http\Request $request |
38 | | - * @param array $ignoredUrls |
| 32 | + * @param \Closure $next |
39 | 33 | * |
40 | | - * @return bool |
| 34 | + * @return mixed |
41 | 35 | */ |
42 | | - protected function urlIsIgnored($request, $ignoredUrls) |
| 36 | + public function handle($request, Closure $next) |
43 | 37 | { |
44 | | - foreach ($ignoredUrls as $url) { |
45 | | - $url = trim($url, '/'); |
| 38 | + $stageFrontUrl = Config::get('stagefront.url'); |
46 | 39 |
|
47 | | - if ($request->is($url)) { |
48 | | - return true; |
49 | | - } |
| 40 | + if ($this->shield->requiresLogin()) { |
| 41 | + return redirect($stageFrontUrl); |
50 | 42 | } |
51 | 43 |
|
52 | | - return false; |
| 44 | + return $next($request); |
53 | 45 | } |
54 | 46 | } |
0 commit comments