From a14544398b138bd25631984e775bcc465c26531c Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 9 Jan 2016 15:39:34 +0100 Subject: [PATCH] New middleware --- app/Http/routes.php | 75 -------------------------- app/Models/Account.php | 50 +++++++++-------- app/Support/Binder/AccountList.php | 50 +++++++++++++++++ app/Support/Binder/BinderInterface.php | 27 ++++++++++ app/Support/Binder/Date.php | 43 +++++++++++++++ app/Support/Domain.php | 32 +++++++++++ 6 files changed, 181 insertions(+), 96 deletions(-) create mode 100644 app/Support/Binder/AccountList.php create mode 100644 app/Support/Binder/BinderInterface.php create mode 100644 app/Support/Binder/Date.php create mode 100644 app/Support/Domain.php diff --git a/app/Http/routes.php b/app/Http/routes.php index d24da4a4de..0bb2848285 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -12,43 +12,6 @@ use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionJournal; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; - -// models -Route::bind( - 'account', - function ($value) { - if (Auth::check()) { - $object = Account::leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id') - ->where('account_types.editable', 1) - ->where('accounts.id', $value) - ->where('user_id', Auth::user()->id) - ->first(['accounts.*']); - if ($object) { - return $object; - } - } - throw new NotFoundHttpException; - } -); -// accounts -Route::bind( - 'accountList', - function ($value) { - if (Auth::check()) { - $ids = explode(',', $value); - /** @var \Illuminate\Support\Collection $object */ - $object = Account::leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id') - ->where('account_types.editable', 1) - ->whereIn('accounts.id', $ids) - ->where('user_id', Auth::user()->id) - ->get(['accounts.*']); - if ($object->count() > 0) { - return $object; - } - } - throw new NotFoundHttpException; - } -); // budget list Route::bind( 'budgetList', @@ -98,44 +61,6 @@ Route::bind( } ); -// Date -Route::bind( - 'start_date', - function ($value) { - if (Auth::check()) { - - try { - $date = new Carbon($value); - } catch (Exception $e) { - Log::error('Could not parse date "' . $value . '" for user #' . Auth::user()->id); - throw new NotFoundHttpException; - } - - return $date; - } - throw new NotFoundHttpException; - } -); - -// Date -Route::bind( - 'end_date', - function ($value) { - if (Auth::check()) { - - try { - $date = new Carbon($value); - } catch (Exception $e) { - Log::error('Could not parse date "' . $value . '" for user #' . Auth::user()->id); - throw new NotFoundHttpException; - } - - return $date; - } - throw new NotFoundHttpException; - } -); - Route::bind( 'tj', function ($value) { if (Auth::check()) { diff --git a/app/Models/Account.php b/app/Models/Account.php index 4f10465e79..ff22ff5fdb 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -1,41 +1,39 @@ belongsTo('FireflyIII\User'); } + public static function routeBinder(Account $value) + { + + if (Auth::check()) { + if ($value->user_id == Auth::user()->id) { + return $value; + } + } + throw new NotFoundHttpException; + } } diff --git a/app/Support/Binder/AccountList.php b/app/Support/Binder/AccountList.php new file mode 100644 index 0000000000..59de12db74 --- /dev/null +++ b/app/Support/Binder/AccountList.php @@ -0,0 +1,50 @@ +where('account_types.editable', 1) + ->whereIn('accounts.id', $ids) + ->where('user_id', Auth::user()->id) + ->get(['accounts.*']); + if ($object->count() > 0) { + return $object; + } + } + throw new NotFoundHttpException; + } +} \ No newline at end of file diff --git a/app/Support/Binder/BinderInterface.php b/app/Support/Binder/BinderInterface.php new file mode 100644 index 0000000000..aba12d6455 --- /dev/null +++ b/app/Support/Binder/BinderInterface.php @@ -0,0 +1,27 @@ +id); + throw new NotFoundHttpException; + } + + return $date; + } +} \ No newline at end of file diff --git a/app/Support/Domain.php b/app/Support/Domain.php new file mode 100644 index 0000000000..a3e13fe3f2 --- /dev/null +++ b/app/Support/Domain.php @@ -0,0 +1,32 @@ + 'FireflyIII\Models\Account', + 'accountList' => 'FireflyIII\Support\Binder\AccountList', + 'start_date' => 'FireflyIII\Support\Binder\Date', + 'end_date' => 'FireflyIII\Support\Binder\Date', + ]; + } + +} \ No newline at end of file