diff --git a/app/Events/Security/System/NewInvitationCreated.php b/app/Events/Security/System/NewInvitationCreated.php new file mode 100644 index 0000000000..3b48d83a97 --- /dev/null +++ b/app/Events/Security/System/NewInvitationCreated.php @@ -0,0 +1,33 @@ +. + */ + +namespace FireflyIII\Events\Security\System; + +use FireflyIII\Events\Event; +use FireflyIII\Models\InvitedUser; +use Illuminate\Queue\SerializesModels; + +class NewInvitationCreated extends Event +{ + use SerializesModels; + + public function __construct(public InvitedUser $invitee) {} +} diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index 4cff16fa8c..0425b68d2e 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace FireflyIII\Http\Controllers\Admin; use FireflyIII\Events\Admin\InvitationCreated; +use FireflyIII\Events\Security\System\NewInvitationCreated; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Middleware\IsDemoUser; @@ -202,7 +203,7 @@ class UserController extends Controller session()->flash('info', trans('firefly.user_is_invited', ['address' => $address])); // event! - event(new InvitationCreated($invitee)); + event(new NewInvitationCreated($invitee)); return redirect(route('settings.users')); } diff --git a/app/Handlers/Events/AdminEventHandler.php b/app/Listeners/Security/System/NotifiesAboutNewInvitation.php similarity index 62% rename from app/Handlers/Events/AdminEventHandler.php rename to app/Listeners/Security/System/NotifiesAboutNewInvitation.php index 12835737a8..af7b66980a 100644 --- a/app/Handlers/Events/AdminEventHandler.php +++ b/app/Listeners/Security/System/NotifiesAboutNewInvitation.php @@ -1,8 +1,7 @@ . */ -declare(strict_types=1); -namespace FireflyIII\Handlers\Events; +namespace FireflyIII\Listeners\Security\System; use Exception; use FireflyIII\Events\Admin\InvitationCreated; +use FireflyIII\Events\Security\System\NewInvitationCreated; +use FireflyIII\Exceptions\FireflyException; +use FireflyIII\Mail\InvitationMail; +use FireflyIII\Models\InvitedUser; use FireflyIII\Notifications\Admin\UserInvitation; use FireflyIII\Notifications\Notifiables\OwnerNotifiable; use FireflyIII\Support\Facades\FireflyConfig; use Illuminate\Support\Facades\Log; +use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Notification; -/** - * Class AdminEventHandler. - */ -class AdminEventHandler +class NotifiesAboutNewInvitation { - public function sendInvitationNotification(InvitationCreated $event): void + public function handle(NewInvitationCreated $event): void + { + $this->sendInvitationNotification($event->invitee); + $this->sendRegistrationInvite($event->invitee); + } + + + private function sendRegistrationInvite(InvitedUser $invitee): void + { + $email = $invitee->email; + $admin = $invitee->user->email; + $url = route('invite', [$invitee->invite_code]); + + try { + Mail::to($email)->send(new InvitationMail($invitee, $admin, $url)); + } catch (Exception $e) { + Log::error($e->getMessage()); + Log::error($e->getTraceAsString()); + + throw new FireflyException($e->getMessage(), 0, $e); + } + } + + private function sendInvitationNotification(InvitedUser $invitee): void { $sendMail = FireflyConfig::get('notification_invite_created', true)->data; if (false === $sendMail) { @@ -44,7 +67,7 @@ class AdminEventHandler } try { - Notification::send(new OwnerNotifiable(), new UserInvitation($event->invitee)); + Notification::send(new OwnerNotifiable(), new UserInvitation($invitee)); } catch (Exception $e) { $message = $e->getMessage(); if (str_contains($message, 'Bcc')) { @@ -62,5 +85,4 @@ class AdminEventHandler } } - // Send new version message to admin. } diff --git a/app/Handlers/Events/UserEventHandler.php b/app/Listeners/Security/User/RespondsToNewLogin.php similarity index 62% rename from app/Handlers/Events/UserEventHandler.php rename to app/Listeners/Security/User/RespondsToNewLogin.php index 4345df507a..5d4d95b8ab 100644 --- a/app/Handlers/Events/UserEventHandler.php +++ b/app/Listeners/Security/User/RespondsToNewLogin.php @@ -1,8 +1,7 @@ . */ -declare(strict_types=1); -namespace FireflyIII\Handlers\Events; +namespace FireflyIII\Listeners\Security\User; -use Exception; -use FireflyIII\Events\Admin\InvitationCreated; -use FireflyIII\Exceptions\FireflyException; -use FireflyIII\Mail\InvitationMail; 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 InvalidArgumentException; -/** - * Class UserEventHandler. - * - * This class responds to any events that have anything to do with the User object. - * - * The method name reflects what is being done. This is in the present tense. - */ -class UserEventHandler +class RespondsToNewLogin { + public function handle(Login $event): void + { + $user = $event->user; + if (!($user instanceof User)) { + throw new InvalidArgumentException(sprintf('User cannot be an instance of %s.', get_class($user))); + } + + $this->checkSingleUserIsAdmin($user); + $this->demoUserBackToEnglish($user); + } + /** * Fires to see if a user is admin. */ - public function checkSingleUserIsAdmin(Login $event): void + private function checkSingleUserIsAdmin(User $user): void { /** @var UserRepositoryInterface $repository */ $repository = app(UserRepositoryInterface::class); - - /** @var User $user */ - $user = $event->user; $count = $repository->count(); - // only act when there is 1 user in the system and he has no admin rights. + // only act when there is 1 user in the system, and he has no admin rights. if (1 === $count && !$repository->hasRole($user, 'owner')) { // user is the only user but does not have role "owner". $role = $repository->getRole('owner'); @@ -74,13 +69,10 @@ class UserEventHandler /** * Set the demo user back to English. */ - public function demoUserBackToEnglish(Login $event): void + private function demoUserBackToEnglish(User $user): void { /** @var UserRepositoryInterface $repository */ $repository = app(UserRepositoryInterface::class); - - /** @var User $user */ - $user = $event->user; if ($repository->hasRole($user, 'demo')) { // set user back to English. Preferences::setForUser($user, 'language', 'en_US'); @@ -90,22 +82,4 @@ class UserEventHandler } } - /** - * @throws FireflyException - */ - public function sendRegistrationInvite(InvitationCreated $event): void - { - $invitee = $event->invitee->email; - $admin = $event->invitee->user->email; - $url = route('invite', [$event->invitee->invite_code]); - - try { - Mail::to($invitee)->send(new InvitationMail($invitee, $admin, $url)); - } catch (Exception $e) { - Log::error($e->getMessage()); - Log::error($e->getTraceAsString()); - - throw new FireflyException($e->getMessage(), 0, $e); - } - } } diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 4ea57c76ce..ad1e5e9603 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -23,7 +23,6 @@ declare(strict_types=1); namespace FireflyIII\Providers; -use FireflyIII\Events\Admin\InvitationCreated; use FireflyIII\Events\DestroyedTransactionGroup; use FireflyIII\Events\Model\TransactionGroup\TriggeredStoredTransactionGroup; use FireflyIII\Events\Preferences\UserGroupChangedPrimaryCurrency; @@ -31,7 +30,6 @@ use FireflyIII\Events\StoredAccount; use FireflyIII\Events\StoredTransactionGroup; use FireflyIII\Events\UpdatedAccount; use FireflyIII\Events\UpdatedTransactionGroup; -use Illuminate\Auth\Events\Login; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; use Laravel\Passport\Events\AccessTokenCreated; use Override; @@ -45,16 +43,6 @@ class EventServiceProvider extends ServiceProvider { protected $listen = [ - Login::class => [ - 'FireflyIII\Handlers\Events\UserEventHandler@checkSingleUserIsAdmin', - 'FireflyIII\Handlers\Events\UserEventHandler@demoUserBackToEnglish', - ], - // is a User related event. - InvitationCreated::class => [ - 'FireflyIII\Handlers\Events\AdminEventHandler@sendInvitationNotification', - 'FireflyIII\Handlers\Events\UserEventHandler@sendRegistrationInvite', - ], - // is a Transaction Journal related event. StoredTransactionGroup::class => ['FireflyIII\Handlers\Events\StoredGroupEventHandler@runAllHandlers'], TriggeredStoredTransactionGroup::class => ['FireflyIII\Handlers\Events\StoredGroupEventHandler@triggerRulesManually'],