diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 60c9c9776e..4d8f4d40a6 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -167,7 +167,7 @@ class Kernel extends HttpKernel 'auth:api' => [ 'throttle:60,1', - 'bindings', + HttpBinder::class, ], ]; diff --git a/app/Http/Middleware/ApiBinder.php b/app/Http/Middleware/ApiBinder.php deleted file mode 100644 index ff0c844f53..0000000000 --- a/app/Http/Middleware/ApiBinder.php +++ /dev/null @@ -1,80 +0,0 @@ -. - */ -declare(strict_types=1); - -namespace FireflyIII\Http\Middleware; - -use Closure; -use FireflyIII\Support\Domain; -use Illuminate\Http\Request; - -/** - * Class ApiBinder - */ -class ApiBinder -{ - /** - * @var array - */ - protected $binders = []; - - /** - * Binder constructor. - */ - public function __construct() - { - $this->binders = Domain::getBindables(); - } - - /** - * Handle an incoming request. - * - * @param \Illuminate\Http\Request $request - * @param \Closure $next - * - * @return mixed - */ - public function handle(Request $request, Closure $next) - { - foreach ($request->route()->parameters() as $key => $value) { - if (isset($this->binders[$key])) { - $boundObject = $this->performBinding($key, $value, $request->route()); - $request->route()->setParameter($key, $boundObject); - } - } - - return $next($request); - } - - /** - * @param $key - * @param $value - * @param $route - * - * @return mixed - */ - private function performBinding($key, $value, $route) - { - $class = $this->binders[$key]; - - return $class::routeBinder($value, $route); - } -} diff --git a/app/Http/Middleware/HttpBinder.php b/app/Http/Middleware/HttpBinder.php index 7408658bb7..fca7d481cf 100644 --- a/app/Http/Middleware/HttpBinder.php +++ b/app/Http/Middleware/HttpBinder.php @@ -24,7 +24,9 @@ namespace FireflyIII\Http\Middleware; use Closure; use FireflyIII\Support\Domain; +use Illuminate\Auth\SessionGuard; use Illuminate\Http\Request; +use Illuminate\Routing\Route; /** * Class HttpBinder @@ -54,9 +56,16 @@ class HttpBinder */ public function handle(Request $request, Closure $next) { + $middleware = $request->route()->middleware(); + $guard = 'web'; + if(in_array('auth:api', $middleware)) { + $guard = 'api'; + } + $guard = auth()->guard($guard); + foreach ($request->route()->parameters() as $key => $value) { if (isset($this->binders[$key])) { - $boundObject = $this->performBinding($key, $value, $request->route()); + $boundObject = $this->performBinding($guard, $key, $value, $request->route()); $request->route()->setParameter($key, $boundObject); } } @@ -71,10 +80,9 @@ class HttpBinder * * @return mixed */ - private function performBinding($key, $value, $route) + private function performBinding($guard, string $key, string $value, Route $route) { $class = $this->binders[$key]; - - return $class::routeBinder($value, $route); + return $class::routeBinder($guard, $value, $route); } }