2020-04-12 00:26:44 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
2022-08-30 23:30:08 +02:00
|
|
|
use App\Providers\RouteServiceProvider;
|
|
|
|
use App\Setting\NamiSettings;
|
2020-04-12 00:26:44 +02:00
|
|
|
use Closure;
|
|
|
|
|
|
|
|
class RedirectIfNotInitializedMiddleware
|
|
|
|
{
|
2022-08-31 00:33:36 +02:00
|
|
|
public array $dontRedirect = ['initialize.form', 'initialize.store'];
|
2020-04-12 00:26:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
2022-03-11 20:19:17 +01:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
*
|
2020-04-12 00:26:44 +02:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next)
|
|
|
|
{
|
2022-09-05 23:21:56 +02:00
|
|
|
if (1 === preg_match('/\/telescope/', $request->url())) {
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
2022-08-30 23:30:08 +02:00
|
|
|
if ($this->initialized() && request()->routeIs(['initialize.form'])) {
|
|
|
|
return redirect()->to(RouteServiceProvider::HOME);
|
|
|
|
}
|
|
|
|
|
2022-03-11 20:19:17 +01:00
|
|
|
if (!$this->shouldRedirect()) {
|
2020-04-12 00:26:44 +02:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->initialized()) {
|
2022-08-31 00:33:36 +02:00
|
|
|
return redirect()->route('initialize.form');
|
2020-04-12 00:26:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
2022-02-12 16:16:56 +01:00
|
|
|
public function shouldRedirect(): bool
|
|
|
|
{
|
2022-01-02 12:32:57 +01:00
|
|
|
return !request()->routeIs($this->dontRedirect) && auth()->check();
|
2020-04-12 00:26:44 +02:00
|
|
|
}
|
|
|
|
|
2022-02-12 16:16:56 +01:00
|
|
|
public function initialized(): bool
|
|
|
|
{
|
2022-08-30 23:30:08 +02:00
|
|
|
return 0 !== app(NamiSettings::class)->default_group_id;
|
2020-04-12 00:26:44 +02:00
|
|
|
}
|
|
|
|
}
|