Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Make sure that all calls are to setUsers
Sander Dorigoee3845c2d099
diff --git a/app/Console/Commands/Tools/Cron.php b/app/Console/Commands/Tools/Cron.phpindex 5a4860adac..b86354e722 100644--- a/app/Console/Commands/Tools/Cron.php+++ b/app/Console/Commands/Tools/Cron.php@@ -161,7 +161,7 @@ class Cron extends Command{$autoBudget = new AutoBudgetCronjob();$autoBudget->setForce($force);- $autoBudget->setUser($this->admin);+ $autoBudget->setUsers($this->users);// set date in cron job:if ($date instanceof Carbon) {$autoBudget->setDate($date);@@ -184,7 +184,7 @@ class Cron extends Command{$updateCheck = new UpdateCheckCronjob();$updateCheck->setForce($force);- $updateCheck->setUser($this->admin);+ $updateCheck->setUsers($this->users);$updateCheck->fire();if ($updateCheck->jobErrored) {@@ -229,7 +229,7 @@ class Cron extends Command{$recurring = new RecurringCronjob();$recurring->setForce($force);- $recurring->setUser($this->admin);+ $recurring->setUsers($this->users);// set date in cron job:if ($date instanceof Carbon) {@@ -255,7 +255,7 @@ class Cron extends Command{$subscriptionWarningJob = new BillWarningCronjob();$subscriptionWarningJob->setForce($force);- $subscriptionWarningJob->setUser($this->admin);+ $subscriptionWarningJob->setUsers($this->users);// set date in cron job:if ($date instanceof Carbon) {$subscriptionWarningJob->setDate($date);@@ -278,7 +278,7 @@ class Cron extends Command{$webhook = new WebhookCronjob();$webhook->setForce($force);- $webhook->setUser($this->admin);+ $webhook->setUsers($this->users);// set date in cron job:if ($date instanceof Carbon) {$webhook->setDate($date);diff --git a/app/Jobs/CreateRecurringTransactions.php b/app/Jobs/CreateRecurringTransactions.phpindex c57a0ddf88..86ad79244d 100644--- a/app/Jobs/CreateRecurringTransactions.php+++ b/app/Jobs/CreateRecurringTransactions.php@@ -114,9 +114,6 @@ class CreateRecurringTransactions implements ShouldQueueLog::debug('Grab all recurrences from the database.');$this->repository->setUser($this->user);$this->recurrences = $this->repository->get();- if ($this->user->hasRole('owner')) {- $this->recurrences = $this->repository->getAll();- }}$result = [];diff --git a/app/Support/Cronjobs/AbstractCronjob.php b/app/Support/Cronjobs/AbstractCronjob.phpindex eb8e41335e..9597749f27 100644--- a/app/Support/Cronjobs/AbstractCronjob.php+++ b/app/Support/Cronjobs/AbstractCronjob.php@@ -41,8 +41,6 @@ abstract class AbstractCronjobpublic int $timeBetweenRuns = 43_200;protected Carbon $date;protected bool $force = false;- protected User $user;- protected bool $isOwner = false;protected Collection $users;/**@@ -67,12 +65,6 @@ abstract class AbstractCronjob$this->force = $force;}- final public function setUser(User $user): void- {- throw new FireflyException(sprintf('Do not use setUser() in %s', get_class($this)));- $this->user = $user;- $this->isOwner = $user->hasRole('owner');- }final public function setUsers(Collection $users): void {$this->users = $users;}diff --git a/app/Support/Cronjobs/ExchangeRatesCronjob.php b/app/Support/Cronjobs/ExchangeRatesCronjob.phpindex 27a917f74f..bf8599921e 100644--- a/app/Support/Cronjobs/ExchangeRatesCronjob.php+++ b/app/Support/Cronjobs/ExchangeRatesCronjob.php@@ -53,7 +53,7 @@ class ExchangeRatesCronjob extends AbstractCronjob// less than half a day ago:if ($lastTime > 0 && $diff <= 43_200) {Log::info(sprintf('It has been %s since the exchange rates cron-job has fired for user #%d.', $diffForHumans, $user->id));- if (false === $this->force || false === $this->isOwner) {+ if (false === $this->force || false === $user->hasRole('owner')) {Log::info(sprintf('The exchange rates cron-job will not fire now for user #%d.', $user->id));$this->message = sprintf('It has been %s since the exchange rates cron-job has fired for user #%d. It will not fire now.', $diffForHumans, $user->id);continue;@@ -63,7 +63,7 @@ class ExchangeRatesCronjob extends AbstractCronjob}if ($lastTime > 0 && $diff > 43_200) {- Log::info(sprintf('It has been %s since the exchange rates cron-job has fired. It will fire now!', $diffForHumans));+ Log::info(sprintf('It has been %s since the exchange rates cron-job has fired for user #%d. It will fire now!', $diffForHumans, $user->id));}$this->fireExchangeRateJob($user);@@ -88,6 +88,7 @@ class ExchangeRatesCronjob extends AbstractCronjob$this->message = 'Exchange rates cron job fired successfully.';AppConfiguration::set(sprintf('last_cer_job_%d', $user->id), (int) $this->date->format('U'));+ Log::info(sprintf('Marked the last time this job has run as "%s" (%d) for user #%d', $this->date->format('Y-m-d H:i:s'), (int) $this->date->format('U'), $user->id));Log::info('Done with exchange rates job task.');}}diff --git a/app/Support/Cronjobs/RecurringCronjob.php b/app/Support/Cronjobs/RecurringCronjob.phpindex 27389fc9d9..39feb1c60c 100644--- a/app/Support/Cronjobs/RecurringCronjob.php+++ b/app/Support/Cronjobs/RecurringCronjob.php@@ -30,6 +30,7 @@ use FireflyIII\Jobs\CreateRecurringTransactions;use FireflyIII\Models\Configuration;use FireflyIII\Support\Facades\AppConfiguration;use FireflyIII\Support\Facades\Preferences;+use FireflyIII\User;use Illuminate\Support\Facades\Log;/**@@ -44,46 +45,48 @@ class RecurringCronjob extends AbstractCronjob{Log::debug(sprintf('Now in %s', __METHOD__));- /** @var Configuration $config */- $config = AppConfiguration::get('last_rt_job', 0);- $lastTime = (int) $config->data;- $diff = now(config('app.timezone'))->getTimestamp() - $lastTime;- $diffForHumans = now(config('app.timezone'))->diffForHumans(Carbon::createFromTimestamp($lastTime), null, true);+ /** @var User $user */+ foreach($this->users as $user) {+ /** @var Configuration $config */+ $config = AppConfiguration::get(sprintf('last_rt_job_%d', $user->id), 0);+ $lastTime = (int)$config->data;+ $diff = now(config('app.timezone'))->getTimestamp() - $lastTime;+ $diffForHumans = now(config('app.timezone'))->diffForHumans(Carbon::createFromTimestamp($lastTime), null, true);- if (0 === $lastTime) {- Log::info('Recurring transactions cron-job has never fired before.');- }- // less than half a day ago:- if ($lastTime > 0 && $diff <= 43_200) {- Log::info(sprintf('It has been "%s" since the recurring transactions cron-job has fired.', $diffForHumans));- if (false === $this->force || false === $this->isOwner) {- Log::info('The cron-job will not fire now.');- $this->message = sprintf('It has been "%s" since the recurring transactions cron-job has fired. It will not fire now.', $diffForHumans);- $this->jobFired = false;- $this->jobErrored = false;- $this->jobSucceeded = false;-- return;+ if (0 === $lastTime) {+ Log::info(sprintf('Recurring transactions cron-job has never fired before for user #%d.', $user->id));}- Log::info('Execution of the recurring transaction cron-job has been FORCED.');- }+ // less than half a day ago:+ if ($lastTime > 0 && $diff <= 43_200) {+ Log::info(sprintf('It has been "%s" since the recurring transactions cron-job has fired for user #%d.', $diffForHumans, $user->id));+ if (false === $this->force || false === $user->hasRole('owner')) {+ Log::info(sprintf('The cron-job will not fire now for user #%d.', $user->id));+ $this->message = sprintf('It has been "%s" since the recurring transactions cron-job has fired for user #%d. It will not fire now.', $diffForHumans, $user->id);+ $this->jobFired = false;+ $this->jobErrored = false;+ $this->jobSucceeded = false;- if ($lastTime > 0 && $diff > 43_200) {- Log::info(sprintf('It has been "%s" since the recurring transactions cron-job has fired. It will fire now!', $diffForHumans));- }+ return;+ }+ Log::info(sprintf('Execution of the recurring transaction cron-job has been FORCED for user #%d.', $user->id));+ }- $this->fireRecurring();+ if ($lastTime > 0 && $diff > 43_200) {+ Log::info(sprintf('It has been "%s" since the recurring transactions cron-job has fired. It will fire now for user #%d!', $diffForHumans, $user->id));+ }+ $this->fireRecurring($user);+ }Preferences::mark();}- private function fireRecurring(): void+ private function fireRecurring(User $user): void{Log::info(sprintf('Will now fire recurring cron job task for date "%s".', $this->date->format('Y-m-d H:i:s')));$job = new CreateRecurringTransactions($this->date);$job->setForce($this->force);- $job->setUser($this->user);+ $job->setUser($user);$job->handle();// get stuff from job:@@ -92,8 +95,8 @@ class RecurringCronjob extends AbstractCronjob$this->jobSucceeded = true;$this->message = 'Recurring transactions cron job fired successfully.';- AppConfiguration::set('last_rt_job', (int) $this->date->format('U'));- Log::info(sprintf('Marked the last time this job has run as "%s" (%d)', $this->date->format('Y-m-d H:i:s'), (int) $this->date->format('U')));+ AppConfiguration::set(sprintf('last_rt_job_%d', $user->id), (int) $this->date->format('U'));+ Log::info(sprintf('Marked the last time this job has run as "%s" (%d) for user #%d', $this->date->format('Y-m-d H:i:s'), (int) $this->date->format('U'), $user->id));Log::info('Done with recurring cron job task.');}}diff --git a/app/Support/Http/Controllers/CronRunner.php b/app/Support/Http/Controllers/CronRunner.phpindex 3321f0c0f0..c54b20ae6d 100644--- a/app/Support/Http/Controllers/CronRunner.php+++ b/app/Support/Http/Controllers/CronRunner.php@@ -46,7 +46,13 @@ trait CronRunner/** @var BillWarningCronjob $billWarning */$billWarning = app(BillWarningCronjob::class);$billWarning->setForce($force);- $billWarning->setUser($user);+ // if the user is owner, run for all users.+ $users = new Collection([$user]);+ if($user->hasRole('owner')) {+ $this->repository = app(UserRepositoryInterface::class);+ $users = $this->repository->allAvailable();+ }+ $billWarning->setUsers($users);$billWarning->setDate($date);try {@@ -97,7 +103,13 @@ trait CronRunner/** @var AutoBudgetCronjob $autoBudget */$autoBudget = app(AutoBudgetCronjob::class);$autoBudget->setForce($force);- $autoBudget->setUser($user);+ // if the user is owner, run for all users.+ $users = new Collection([$user]);+ if($user->hasRole('owner')) {+ $this->repository = app(UserRepositoryInterface::class);+ $users = $this->repository->allAvailable();+ }+ $autoBudget->setUsers($users);$autoBudget->setDate($date);try {@@ -119,7 +131,14 @@ trait CronRunner/** @var RecurringCronjob $recurring */$recurring = app(RecurringCronjob::class);$recurring->setForce($force);- $recurring->setUser($user);++ // if the user is owner, run for all users.+ $users = new Collection([$user]);+ if($user->hasRole('owner')) {+ $this->repository = app(UserRepositoryInterface::class);+ $users = $this->repository->allAvailable();+ }+ $recurring->setUsers($users);$recurring->setDate($date);try {@@ -141,7 +160,13 @@ trait CronRunner/** @var WebhookCronjob $webhook */$webhook = app(WebhookCronjob::class);$webhook->setForce($force);- $webhook->setUser($user);+ // if the user is owner, run for all users.+ $users = new Collection([$user]);+ if($user->hasRole('owner')) {+ $this->repository = app(UserRepositoryInterface::class);+ $users = $this->repository->allAvailable();+ }+ $webhook->setUsers($users);$webhook->setDate($date);try {