Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
More refactoring for #11544
James Colee215d550b521
diff --git a/app/Events/UserChangedEmail.php b/app/Events/Security/User/UserChangedEmailAddress.phpsimilarity index 83%rename from app/Events/UserChangedEmail.phprename to app/Events/Security/User/UserChangedEmailAddress.phpindex afdd422d52..a4c57411b4 100644--- a/app/Events/UserChangedEmail.php+++ b/app/Events/Security/User/UserChangedEmailAddress.php@@ -1,8 +1,7 @@<?php--/**- * UserChangedEmail.php- * Copyright (c) 2019 [email protected]+/*+ * UserChangedEmailAddress.php+ * Copyright (c) 2026 [email protected]** This file is part of Firefly III (https://github.com/firefly-iii).*@@ -20,17 +19,13 @@* along with this program. If not, see <https://www.gnu.org/licenses/>.*/-declare(strict_types=1);--namespace FireflyIII\Events;+namespace FireflyIII\Events\Security\User;+use FireflyIII\Events\Event;use FireflyIII\User;use Illuminate\Queue\SerializesModels;-/**- * Class UserChangedEmail.- */-class UserChangedEmail extends Event+class UserChangedEmailAddress extends Event{use SerializesModels;diff --git a/app/Events/RequestedNewPassword.php b/app/Events/Security/User/UserRequestedNewPassword.phpsimilarity index 54%rename from app/Events/RequestedNewPassword.phprename to app/Events/Security/User/UserRequestedNewPassword.phpindex 6a0558f324..66383b6e3c 100644--- a/app/Events/RequestedNewPassword.php+++ b/app/Events/Security/User/UserRequestedNewPassword.php@@ -1,8 +1,7 @@<?php--/**- * RequestedNewPassword.php- * Copyright (c) 2019 [email protected]+/*+ * UserRequestedNewPassword.php+ * Copyright (c) 2026 [email protected]** This file is part of Firefly III (https://github.com/firefly-iii).*@@ -20,37 +19,16 @@* along with this program. If not, see <https://www.gnu.org/licenses/>.*/-declare(strict_types=1);--namespace FireflyIII\Events;+namespace FireflyIII\Events\Security\User;+use FireflyIII\Events\Event;use FireflyIII\User;use Illuminate\Queue\SerializesModels;use SensitiveParameter;-/**- * Class RequestedNewPassword.- */-class RequestedNewPassword extends Event+class UserRequestedNewPassword extends Event{use SerializesModels;- /** @var string The users IP address */- public $ipAddress;-- /** @var string The token */- public $token;-- /** @var User The user */- public $user;-- /**- * Create a new event instance. This event is triggered when a users tries to reset his or her password.- */- public function __construct(User $user, #[SensitiveParameter] string $token, string $ipAddress)- {- $this->user = $user;- $this->token = $token;- $this->ipAddress = $ipAddress;- }+ public function __construct(public User $user, #[SensitiveParameter] public string $token, public string $ipAddress) {}}diff --git a/app/Handlers/Events/UserEventHandler.php b/app/Handlers/Events/UserEventHandler.phpindex 3e8df36b3f..73cf918151 100644--- a/app/Handlers/Events/UserEventHandler.php+++ b/app/Handlers/Events/UserEventHandler.php@@ -25,20 +25,14 @@ namespace FireflyIII\Handlers\Events;use Exception;use FireflyIII\Events\Admin\InvitationCreated;-use FireflyIII\Events\RequestedNewPassword;-use FireflyIII\Events\UserChangedEmail;use FireflyIII\Exceptions\FireflyException;-use FireflyIII\Mail\ConfirmEmailChangeMail;use FireflyIII\Mail\InvitationMail;-use FireflyIII\Mail\UndoEmailChangeMail;-use FireflyIII\Notifications\User\UserNewPassword;use FireflyIII\Repositories\User\UserRepositoryInterface;use FireflyIII\Support\Facades\Preferences;use FireflyIII\User;use Illuminate\Auth\Events\Login;use Illuminate\Support\Facades\Log;use Illuminate\Support\Facades\Mail;-use Illuminate\Support\Facades\Notification;/*** Class UserEventHandler.@@ -100,79 +94,6 @@ class UserEventHandler}- /**- * Send email to confirm email change. Will not be made into a notification, because- * this requires some custom fields from the user and not just the "user" object.- *- * @throws FireflyException- */- public function sendEmailChangeConfirmMail(UserChangedEmail $event): void- {- $newEmail = $event->newEmail;- $oldEmail = $event->oldEmail;- $user = $event->user;- $token = Preferences::getForUser($user, 'email_change_confirm_token', 'invalid');- $url = route('profile.confirm-email-change', [$token->data]);-- try {- Mail::to($newEmail)->send(new ConfirmEmailChangeMail($newEmail, $oldEmail, $url));- } catch (Exception $e) {- Log::error($e->getMessage());- Log::error($e->getTraceAsString());-- throw new FireflyException($e->getMessage(), 0, $e);- }- }-- /**- * Send email to be able to undo email change. Will not be made into a notification, because- * this requires some custom fields from the user and not just the "user" object.- *- * @throws FireflyException- */- public function sendEmailChangeUndoMail(UserChangedEmail $event): void- {- $newEmail = $event->newEmail;- $oldEmail = $event->oldEmail;- $user = $event->user;- $token = Preferences::getForUser($user, 'email_change_undo_token', 'invalid');- $hashed = hash('sha256', sprintf('%s%s', (string)config('app.key'), $oldEmail));- $url = route('profile.undo-email-change', [$token->data, $hashed]);-- try {- Mail::to($oldEmail)->send(new UndoEmailChangeMail($newEmail, $oldEmail, $url));- } catch (Exception $e) {- Log::error($e->getMessage());- Log::error($e->getTraceAsString());-- throw new FireflyException($e->getMessage(), 0, $e);- }- }-- /**- * Send a new password to the user.- */- public function sendNewPassword(RequestedNewPassword $event): void- {- try {- Notification::send($event->user, new UserNewPassword(route('password.reset', [$event->token])));- } catch (Exception $e) {- $message = $e->getMessage();- if (str_contains($message, 'Bcc')) {- Log::warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');-- return;- }- if (str_contains($message, 'RFC 2822')) {- Log::warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');-- return;- }- Log::error($e->getMessage());- Log::error($e->getTraceAsString());- }- }-/*** @throws FireflyException*/diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.phpindex 20d6265cb3..d080d04f7d 100644--- a/app/Http/Controllers/ProfileController.php+++ b/app/Http/Controllers/ProfileController.php@@ -23,10 +23,10 @@ declare(strict_types=1);namespace FireflyIII\Http\Controllers;+use FireflyIII\Events\Security\User\UserChangedEmailAddress;use FireflyIII\Support\Facades\Preferences;use Illuminate\Support\Facades\Log;use Exception;-use FireflyIII\Events\UserChangedEmail;use FireflyIII\Exceptions\FireflyException;use FireflyIII\Exceptions\ValidationException;use FireflyIII\Http\Middleware\IsDemoUser;@@ -226,7 +226,7 @@ class ProfileController extends Controller// now actually update user:$repository->changeEmail($user, $newEmail);- event(new UserChangedEmail($user, $newEmail, $oldEmail));+ event(new UserChangedEmailAddress($user, $newEmail, $oldEmail));// force user logout.Auth::guard()->logout();diff --git a/app/Listeners/Security/User/HandlesChangeOfUserEmailAddress.php b/app/Listeners/Security/User/HandlesChangeOfUserEmailAddress.phpnew file mode 100644index 0000000000..1a0684d837--- /dev/null+++ b/app/Listeners/Security/User/HandlesChangeOfUserEmailAddress.php@@ -0,0 +1,91 @@+<?php+/*+ * HandlesChangeOfUserEmailAddress.php+ * Copyright (c) 2026 [email protected]+ *+ * This file is part of Firefly III (https://github.com/firefly-iii).+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program 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 Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ */++namespace FireflyIII\Listeners\Security\User;++use Exception;+use FireflyIII\Events\Security\User\UserChangedEmailAddress;+use FireflyIII\Exceptions\FireflyException;+use FireflyIII\Mail\ConfirmEmailChangeMail;+use FireflyIII\Mail\UndoEmailChangeMail;+use FireflyIII\Support\Facades\Preferences;+use Illuminate\Support\Facades\Log;+use Illuminate\Support\Facades\Mail;++class HandlesChangeOfUserEmailAddress+{+ public function handle(UserChangedEmailAddress $event): void+ {+ $this->sendEmailChangeConfirmMail($event);+ $this->sendEmailChangeUndoMail($event);+ }+++ /**+ * Send email to confirm email change. Will not be made into a notification, because+ * this requires some custom fields from the user and not just the "user" object.+ *+ * @throws FireflyException+ */+ private function sendEmailChangeConfirmMail(UserChangedEmailAddress $event): void+ {+ $newEmail = $event->newEmail;+ $oldEmail = $event->oldEmail;+ $user = $event->user;+ $token = Preferences::getForUser($user, 'email_change_confirm_token', 'invalid');+ $url = route('profile.confirm-email-change', [$token->data]);++ try {+ Mail::to($newEmail)->send(new ConfirmEmailChangeMail($newEmail, $oldEmail, $url));+ } catch (Exception $e) {+ Log::error($e->getMessage());+ Log::error($e->getTraceAsString());++ throw new FireflyException($e->getMessage(), 0, $e);+ }+ }++ /**+ * Send email to be able to undo email change. Will not be made into a notification, because+ * this requires some custom fields from the user and not just the "user" object.+ *+ * @throws FireflyException+ */+ private function sendEmailChangeUndoMail(UserChangedEmailAddress $event): void+ {+ $newEmail = $event->newEmail;+ $oldEmail = $event->oldEmail;+ $user = $event->user;+ $token = Preferences::getForUser($user, 'email_change_undo_token', 'invalid');+ $hashed = hash('sha256', sprintf('%s%s', (string)config('app.key'), $oldEmail));+ $url = route('profile.undo-email-change', [$token->data, $hashed]);++ try {+ Mail::to($oldEmail)->send(new UndoEmailChangeMail($newEmail, $oldEmail, $url));+ } catch (Exception $e) {+ Log::error($e->getMessage());+ Log::error($e->getTraceAsString());++ throw new FireflyException($e->getMessage(), 0, $e);+ }+ }++}diff --git a/app/Listeners/Security/User/SendsUserNewPassword.php b/app/Listeners/Security/User/SendsUserNewPassword.phpnew file mode 100644index 0000000000..ddddfe9cb0--- /dev/null+++ b/app/Listeners/Security/User/SendsUserNewPassword.php@@ -0,0 +1,53 @@+<?php+/*+ * SendsUserNewPassword.php+ * Copyright (c) 2026 [email protected]+ *+ * This file is part of Firefly III (https://github.com/firefly-iii).+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program 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 Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ */++namespace FireflyIII\Listeners\Security\User;++use Exception;+use FireflyIII\Events\Security\User\UserRequestedNewPassword;+use FireflyIII\Notifications\User\UserNewPassword;+use Illuminate\Support\Facades\Log;+use Illuminate\Support\Facades\Notification;++class SendsUserNewPassword+{+ public function handle(UserRequestedNewPassword $event): void+ {+ try {+ Notification::send($event->user, new UserNewPassword(route('password.reset', [$event->token])));+ } catch (Exception $e) {+ $message = $e->getMessage();+ if (str_contains($message, 'Bcc')) {+ Log::warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');++ return;+ }+ if (str_contains($message, 'RFC 2822')) {+ Log::warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');++ return;+ }+ Log::error($e->getMessage());+ Log::error($e->getTraceAsString());+ }+ }++}diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.phpindex c60669e2de..547c6d2f04 100644--- a/app/Providers/EventServiceProvider.php+++ b/app/Providers/EventServiceProvider.php@@ -27,7 +27,6 @@ use FireflyIII\Events\Admin\InvitationCreated;use FireflyIII\Events\DestroyedTransactionGroup;use FireflyIII\Events\Model\TransactionGroup\TriggeredStoredTransactionGroup;use FireflyIII\Events\Preferences\UserGroupChangedPrimaryCurrency;-use FireflyIII\Events\RequestedNewPassword;use FireflyIII\Events\RequestedReportOnJournals;use FireflyIII\Events\RequestedSendWebhookMessages;use FireflyIII\Events\RequestedVersionCheckStatus;@@ -36,7 +35,6 @@ use FireflyIII\Events\StoredTransactionGroup;use FireflyIII\Events\TriggeredAuditLog;use FireflyIII\Events\UpdatedAccount;use FireflyIII\Events\UpdatedTransactionGroup;-use FireflyIII\Events\UserChangedEmail;use Illuminate\Auth\Events\Login;use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;use Laravel\Passport\Events\AccessTokenCreated;@@ -52,9 +50,9 @@ class EventServiceProvider extends ServiceProviderprotected $listen= [// is a User related event.-// RegisteredUser::class => [-// 'FireflyIII\Handlers\Events\UserEventHandler@createExchangeRates',-// ],+ // RegisteredUser::class => [+ // 'FireflyIII\Handlers\Events\UserEventHandler@createExchangeRates',+ // ],// UserAttemptedLogin::class => [// 'FireflyIII\Handlers\Events\UserEventHandler@sendLoginAttemptNotification',// ],@@ -74,17 +72,6 @@ class EventServiceProvider extends ServiceProvider],// is a User related event.- RequestedNewPassword::class => [- 'FireflyIII\Handlers\Events\UserEventHandler@sendNewPassword',- ],- // UserTestsNotificationChannel::class => [- // 'FireflyIII\Handlers\Events\UserEventHandler@sendTestNotification',- // ],- // is a User related event.- UserChangedEmail::class => [- 'FireflyIII\Handlers\Events\UserEventHandler@sendEmailChangeConfirmMail',- 'FireflyIII\Handlers\Events\UserEventHandler@sendEmailChangeUndoMail',- ],InvitationCreated::class => ['FireflyIII\Handlers\Events\AdminEventHandler@sendInvitationNotification','FireflyIII\Handlers\Events\UserEventHandler@sendRegistrationInvite',diff --git a/app/User.php b/app/User.phpindex f029aecff1..0e8ebbb748 100644--- a/app/User.php+++ b/app/User.php@@ -27,7 +27,7 @@ namespace FireflyIII;use Deprecated;use Exception;use FireflyIII\Enums\UserRoleEnum;-use FireflyIII\Events\RequestedNewPassword;+use FireflyIII\Events\Security\User\UserRequestedNewPassword;use FireflyIII\Exceptions\FireflyException;use FireflyIII\Models\Account;use FireflyIII\Models\Attachment;@@ -463,7 +463,7 @@ class User extends Authenticatable{$ipAddress = Request::ip();- event(new RequestedNewPassword($this, $token, $ipAddress));+ event(new UserRequestedNewPassword($this, $token, $ipAddress));}/**