Skip to content

Firefly III: a personal finances manager

Implement multi purpose binder

James Coleeacc1da157ed
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 @@
-<?php
-/**
- * ApiBinder.php
- * Copyright (c) 2018 [email protected]
- *
- * This file is part of Firefly III.
- *
- * Firefly III is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Firefly III is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
- */
-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);
}
}