mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Added a new argument to the constructor of the account repository which should correctly inject the user.
This commit is contained in:
parent
fcf16051a2
commit
8008311d9c
49
app/Providers/AccountServiceProvider.php
Normal file
49
app/Providers/AccountServiceProvider.php
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace FireflyIII\Providers;
|
||||||
|
|
||||||
|
use Auth;
|
||||||
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
|
use Illuminate\Foundation\Application;
|
||||||
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class AccountServiceProvider
|
||||||
|
*
|
||||||
|
* @package FireflyIII\Providers
|
||||||
|
*/
|
||||||
|
class AccountServiceProvider extends ServiceProvider
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Bootstrap the application services.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function boot()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register the application services.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function register()
|
||||||
|
{
|
||||||
|
$this->app->bind(
|
||||||
|
'FireflyIII\Repositories\Account\AccountRepositoryInterface',
|
||||||
|
function (Application $app, array $arguments) {
|
||||||
|
if (!isset($arguments[0]) && Auth::check()) {
|
||||||
|
return app('FireflyIII\Repositories\Account\AccountRepository', [Auth::user()]);
|
||||||
|
} else {
|
||||||
|
if (!isset($arguments[0]) && !Auth::check()) {
|
||||||
|
throw new FireflyException('There is no user present.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return app('FireflyIII\Repositories\Account\AccountRepository', $arguments);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -19,6 +19,7 @@ use Illuminate\Support\ServiceProvider;
|
|||||||
use Twig;
|
use Twig;
|
||||||
use TwigBridge\Extension\Loader\Functions;
|
use TwigBridge\Extension\Loader\Functions;
|
||||||
use Validator;
|
use Validator;
|
||||||
|
use Auth;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class FireflyServiceProvider
|
* Class FireflyServiceProvider
|
||||||
@ -82,7 +83,7 @@ class FireflyServiceProvider extends ServiceProvider
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->app->bind('FireflyIII\Repositories\Account\AccountRepositoryInterface', 'FireflyIII\Repositories\Account\AccountRepository');
|
|
||||||
$this->app->bind('FireflyIII\Repositories\Budget\BudgetRepositoryInterface', 'FireflyIII\Repositories\Budget\BudgetRepository');
|
$this->app->bind('FireflyIII\Repositories\Budget\BudgetRepositoryInterface', 'FireflyIII\Repositories\Budget\BudgetRepository');
|
||||||
$this->app->bind('FireflyIII\Repositories\Category\CategoryRepositoryInterface', 'FireflyIII\Repositories\Category\CategoryRepository');
|
$this->app->bind('FireflyIII\Repositories\Category\CategoryRepositoryInterface', 'FireflyIII\Repositories\Category\CategoryRepository');
|
||||||
$this->app->bind('FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface', 'FireflyIII\Repositories\Category\SingleCategoryRepository');
|
$this->app->bind('FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface', 'FireflyIII\Repositories\Category\SingleCategoryRepository');
|
||||||
|
@ -3,7 +3,6 @@ declare(strict_types = 1);
|
|||||||
|
|
||||||
namespace FireflyIII\Repositories\Account;
|
namespace FireflyIII\Repositories\Account;
|
||||||
|
|
||||||
use Auth;
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Config;
|
use Config;
|
||||||
use DB;
|
use DB;
|
||||||
@ -16,6 +15,7 @@ use FireflyIII\Models\Preference;
|
|||||||
use FireflyIII\Models\Transaction;
|
use FireflyIII\Models\Transaction;
|
||||||
use FireflyIII\Models\TransactionJournal;
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use FireflyIII\Models\TransactionType;
|
use FireflyIII\Models\TransactionType;
|
||||||
|
use FireflyIII\User;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
use Illuminate\Pagination\LengthAwarePaginator;
|
use Illuminate\Pagination\LengthAwarePaginator;
|
||||||
@ -33,9 +33,22 @@ use Steam;
|
|||||||
class AccountRepository implements AccountRepositoryInterface
|
class AccountRepository implements AccountRepositoryInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/** @var User */
|
||||||
|
private $user;
|
||||||
/** @var array Valid field names of account_meta */
|
/** @var array Valid field names of account_meta */
|
||||||
private $validFields = ['accountRole', 'ccMonthlyPaymentDate', 'ccType', 'accountNumber'];
|
private $validFields = ['accountRole', 'ccMonthlyPaymentDate', 'ccType', 'accountNumber'];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AccountRepository constructor.
|
||||||
|
*
|
||||||
|
* @param User $user
|
||||||
|
*/
|
||||||
|
public function __construct(User $user)
|
||||||
|
{
|
||||||
|
Log::debug('Constructed for user #' . $user->id . ' (' . $user->email . ')');
|
||||||
|
$this->user = $user;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $types
|
* @param array $types
|
||||||
*
|
*
|
||||||
@ -43,7 +56,7 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function countAccounts(array $types): int
|
public function countAccounts(array $types): int
|
||||||
{
|
{
|
||||||
$count = Auth::user()->accounts()->accountTypeIn($types)->count();
|
$count = $this->user->accounts()->accountTypeIn($types)->count();
|
||||||
|
|
||||||
return $count;
|
return $count;
|
||||||
}
|
}
|
||||||
@ -75,7 +88,7 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function find(int $accountId): Account
|
public function find(int $accountId): Account
|
||||||
{
|
{
|
||||||
return Auth::user()->accounts()->findOrNew($accountId);
|
return $this->user->accounts()->findOrNew($accountId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -87,7 +100,7 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function get(array $ids): Collection
|
public function get(array $ids): Collection
|
||||||
{
|
{
|
||||||
return Auth::user()->accounts()->whereIn('id', $ids)->get(['accounts.*']);
|
return $this->user->accounts()->whereIn('id', $ids)->get(['accounts.*']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -98,7 +111,7 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
public function getAccounts(array $types): Collection
|
public function getAccounts(array $types): Collection
|
||||||
{
|
{
|
||||||
/** @var Collection $result */
|
/** @var Collection $result */
|
||||||
$result = Auth::user()->accounts()->with(
|
$result = $this->user->accounts()->with(
|
||||||
['accountmeta' => function (HasMany $query) {
|
['accountmeta' => function (HasMany $query) {
|
||||||
$query->where('name', 'accountRole');
|
$query->where('name', 'accountRole');
|
||||||
}]
|
}]
|
||||||
@ -126,22 +139,22 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function getCreditCards(Carbon $date): Collection
|
public function getCreditCards(Carbon $date): Collection
|
||||||
{
|
{
|
||||||
$set = Auth::user()->accounts()
|
$set = $this->user->accounts()
|
||||||
->hasMetaValue('accountRole', 'ccAsset')
|
->hasMetaValue('accountRole', 'ccAsset')
|
||||||
->hasMetaValue('ccType', 'monthlyFull')
|
->hasMetaValue('ccType', 'monthlyFull')
|
||||||
->leftJoin('transactions', 'transactions.account_id', '=', 'accounts.id')
|
->leftJoin('transactions', 'transactions.account_id', '=', 'accounts.id')
|
||||||
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
||||||
->whereNull('transactions.deleted_at')
|
->whereNull('transactions.deleted_at')
|
||||||
->where('transaction_journals.date', '<=', $date->format('Y-m-d'))
|
->where('transaction_journals.date', '<=', $date->format('Y-m-d'))
|
||||||
->groupBy('accounts.id')
|
->groupBy('accounts.id')
|
||||||
->get(
|
->get(
|
||||||
[
|
[
|
||||||
'accounts.*',
|
'accounts.*',
|
||||||
'ccType.data as ccType',
|
'ccType.data as ccType',
|
||||||
'accountRole.data as accountRole',
|
'accountRole.data as accountRole',
|
||||||
DB::raw('SUM(`transactions`.`amount`) AS `balance`'),
|
DB::raw('SUM(`transactions`.`amount`) AS `balance`'),
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
return $set;
|
return $set;
|
||||||
}
|
}
|
||||||
@ -166,7 +179,7 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function getFrontpageAccounts(Preference $preference): Collection
|
public function getFrontpageAccounts(Preference $preference): Collection
|
||||||
{
|
{
|
||||||
$query = Auth::user()->accounts()->accountTypeIn(['Default account', 'Asset account']);
|
$query = $this->user->accounts()->accountTypeIn(['Default account', 'Asset account']);
|
||||||
|
|
||||||
if (count($preference->data) > 0) {
|
if (count($preference->data) > 0) {
|
||||||
$query->whereIn('accounts.id', $preference->data);
|
$query->whereIn('accounts.id', $preference->data);
|
||||||
@ -187,16 +200,16 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function getFrontpageTransactions(Account $account, Carbon $start, Carbon $end): Collection
|
public function getFrontpageTransactions(Account $account, Carbon $start, Carbon $end): Collection
|
||||||
{
|
{
|
||||||
$set = Auth::user()
|
$set = $this->user
|
||||||
->transactionjournals()
|
->transactionjournals()
|
||||||
->expanded()
|
->expanded()
|
||||||
->before($end)
|
->before($end)
|
||||||
->after($start)
|
->after($start)
|
||||||
->orderBy('transaction_journals.date', 'DESC')
|
->orderBy('transaction_journals.date', 'DESC')
|
||||||
->orderBy('transaction_journals.order', 'ASC')
|
->orderBy('transaction_journals.order', 'ASC')
|
||||||
->orderBy('transaction_journals.id', 'DESC')
|
->orderBy('transaction_journals.id', 'DESC')
|
||||||
->take(10)
|
->take(10)
|
||||||
->get(TransactionJournal::QUERYFIELDS);
|
->get(TransactionJournal::QUERYFIELDS);
|
||||||
|
|
||||||
return $set;
|
return $set;
|
||||||
}
|
}
|
||||||
@ -210,18 +223,18 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
public function getJournals(Account $account, $page): LengthAwarePaginator
|
public function getJournals(Account $account, $page): LengthAwarePaginator
|
||||||
{
|
{
|
||||||
$offset = ($page - 1) * 50;
|
$offset = ($page - 1) * 50;
|
||||||
$query = Auth::user()
|
$query = $this->user
|
||||||
->transactionJournals()
|
->transactionJournals()
|
||||||
->expanded()
|
->expanded()
|
||||||
->where(
|
->where(
|
||||||
function (Builder $q) use ($account) {
|
function (Builder $q) use ($account) {
|
||||||
$q->where('destination.account_id', $account->id);
|
$q->where('destination.account_id', $account->id);
|
||||||
$q->orWhere('source.account_id', $account->id);
|
$q->orWhere('source.account_id', $account->id);
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
->orderBy('transaction_journals.date', 'DESC')
|
->orderBy('transaction_journals.date', 'DESC')
|
||||||
->orderBy('transaction_journals.order', 'ASC')
|
->orderBy('transaction_journals.order', 'ASC')
|
||||||
->orderBy('transaction_journals.id', 'DESC');
|
->orderBy('transaction_journals.id', 'DESC');
|
||||||
|
|
||||||
$count = $query->count();
|
$count = $query->count();
|
||||||
$set = $query->take(50)->offset($offset)->get(TransactionJournal::QUERYFIELDS);
|
$set = $query->take(50)->offset($offset)->get(TransactionJournal::QUERYFIELDS);
|
||||||
@ -247,7 +260,7 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
|
|
||||||
$ids = array_unique($ids);
|
$ids = array_unique($ids);
|
||||||
if (count($ids) > 0) {
|
if (count($ids) > 0) {
|
||||||
$accounts = Auth::user()->accounts()->whereIn('id', $ids)->where('accounts.active', 1)->get();
|
$accounts = $this->user->accounts()->whereIn('id', $ids)->where('accounts.active', 1)->get();
|
||||||
}
|
}
|
||||||
bcscale(2);
|
bcscale(2);
|
||||||
|
|
||||||
@ -281,12 +294,12 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function getSavingsAccounts(): Collection
|
public function getSavingsAccounts(): Collection
|
||||||
{
|
{
|
||||||
$accounts = Auth::user()->accounts()->accountTypeIn(['Default account', 'Asset account'])->orderBy('accounts.name', 'ASC')
|
$accounts = $this->user->accounts()->accountTypeIn(['Default account', 'Asset account'])->orderBy('accounts.name', 'ASC')
|
||||||
->leftJoin('account_meta', 'account_meta.account_id', '=', 'accounts.id')
|
->leftJoin('account_meta', 'account_meta.account_id', '=', 'accounts.id')
|
||||||
->where('account_meta.name', 'accountRole')
|
->where('account_meta.name', 'accountRole')
|
||||||
->where('accounts.active', 1)
|
->where('accounts.active', 1)
|
||||||
->where('account_meta.data', '"savingAsset"')
|
->where('account_meta.data', '"savingAsset"')
|
||||||
->get(['accounts.*']);
|
->get(['accounts.*']);
|
||||||
$start = clone session('start', new Carbon);
|
$start = clone session('start', new Carbon);
|
||||||
$end = clone session('end', new Carbon);
|
$end = clone session('end', new Carbon);
|
||||||
|
|
||||||
@ -399,7 +412,7 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function sumOfEverything(): string
|
public function sumOfEverything(): string
|
||||||
{
|
{
|
||||||
return strval(Auth::user()->transactions()->sum('amount'));
|
return strval($this->user->transactions()->sum('amount'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -31,8 +31,10 @@ class CacheProperties
|
|||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->properties = new Collection;
|
$this->properties = new Collection;
|
||||||
$this->addProperty(Auth::user()->id);
|
if (Auth::check()) {
|
||||||
$this->addProperty(Prefs::lastActivity());
|
$this->addProperty(Auth::user()->id);
|
||||||
|
$this->addProperty(Prefs::lastActivity());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -150,6 +150,12 @@ return [
|
|||||||
Collective\Html\HtmlServiceProvider::class,
|
Collective\Html\HtmlServiceProvider::class,
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* More service providers.
|
||||||
|
*/
|
||||||
|
FireflyIII\Providers\AccountServiceProvider::class,
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Application Service Providers...
|
* Application Service Providers...
|
||||||
*/
|
*/
|
||||||
@ -159,6 +165,7 @@ return [
|
|||||||
FireflyIII\Providers\RouteServiceProvider::class,
|
FireflyIII\Providers\RouteServiceProvider::class,
|
||||||
FireflyIII\Providers\FireflyServiceProvider::class,
|
FireflyIII\Providers\FireflyServiceProvider::class,
|
||||||
|
|
||||||
|
|
||||||
// own stuff:
|
// own stuff:
|
||||||
// Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,
|
// Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,
|
||||||
// Barryvdh\Debugbar\ServiceProvider::class,
|
// Barryvdh\Debugbar\ServiceProvider::class,
|
||||||
@ -180,21 +187,20 @@ return [
|
|||||||
|
|
||||||
'aliases' => [
|
'aliases' => [
|
||||||
|
|
||||||
'App' => Illuminate\Support\Facades\App::class,
|
'App' => Illuminate\Support\Facades\App::class,
|
||||||
'Artisan' => Illuminate\Support\Facades\Artisan::class,
|
'Artisan' => Illuminate\Support\Facades\Artisan::class,
|
||||||
'Auth' => Illuminate\Support\Facades\Auth::class,
|
'Auth' => Illuminate\Support\Facades\Auth::class,
|
||||||
'Blade' => Illuminate\Support\Facades\Blade::class,
|
'Blade' => Illuminate\Support\Facades\Blade::class,
|
||||||
'Cache' => Illuminate\Support\Facades\Cache::class,
|
'Cache' => Illuminate\Support\Facades\Cache::class,
|
||||||
'Config' => Illuminate\Support\Facades\Config::class,
|
'Config' => Illuminate\Support\Facades\Config::class,
|
||||||
'Cookie' => Illuminate\Support\Facades\Cookie::class,
|
'Cookie' => Illuminate\Support\Facades\Cookie::class,
|
||||||
'Crypt' => Illuminate\Support\Facades\Crypt::class,
|
'Crypt' => Illuminate\Support\Facades\Crypt::class,
|
||||||
'DB' => Illuminate\Support\Facades\DB::class,
|
'DB' => Illuminate\Support\Facades\DB::class,
|
||||||
'Eloquent' => Illuminate\Database\Eloquent\Model::class,
|
'Eloquent' => Illuminate\Database\Eloquent\Model::class,
|
||||||
'Event' => Illuminate\Support\Facades\Event::class,
|
'Event' => Illuminate\Support\Facades\Event::class,
|
||||||
'File' => Illuminate\Support\Facades\File::class,
|
'File' => Illuminate\Support\Facades\File::class,
|
||||||
'Gate' => Illuminate\Support\Facades\Gate::class,
|
'Gate' => Illuminate\Support\Facades\Gate::class,
|
||||||
'Hash' => Illuminate\Support\Facades\Hash::class,
|
'Hash' => Illuminate\Support\Facades\Hash::class,
|
||||||
|
|
||||||
'Lang' => Illuminate\Support\Facades\Lang::class,
|
'Lang' => Illuminate\Support\Facades\Lang::class,
|
||||||
'Log' => Illuminate\Support\Facades\Log::class,
|
'Log' => Illuminate\Support\Facades\Log::class,
|
||||||
'Mail' => Illuminate\Support\Facades\Mail::class,
|
'Mail' => Illuminate\Support\Facades\Mail::class,
|
||||||
|
Loading…
Reference in New Issue
Block a user