Skip to content

Firefly III: a personal finances manager

Move code to event handlers instead of registration routine.

James Colecc88d5962ecd
diff --git a/app/Events/UserRegistration.php b/app/Events/UserRegistration.php
new file mode 100644
index 0000000000..f9c10b3cc3
--- /dev/null
+++ b/app/Events/UserRegistration.php
@@ -0,0 +1,39 @@
+<?php
+/**
+ * UserRegistration.php
+ * Copyright (C) 2016 Sander Dorigo
+ *
+ * This software may be modified and distributed under the terms
+ * of the MIT license. See the LICENSE file for details.
+ */
+
+namespace FireflyIII\Events;
+
+use FireflyIII\User;
+use Illuminate\Queue\SerializesModels;
+
+/**
+ * Class UserRegistration
+ *
+ * @package FireflyIII\Events
+ */
+class UserRegistration extends Event
+{
+ use SerializesModels;
+
+ public $ip;
+ public $user;
+
+ /**
+ * Create a new event instance.
+ *
+ * @param User $user
+ *
+ * @return void
+ */
+ public function __construct(User $user, string $ip)
+ {
+ $this->user = $user;
+ $this->ip = $ip;
+ }
+}
\ No newline at end of file
diff --git a/app/Handlers/Events/AttachUserRole.php b/app/Handlers/Events/AttachUserRole.php
new file mode 100644
index 0000000000..8b151b431d
--- /dev/null
+++ b/app/Handlers/Events/AttachUserRole.php
@@ -0,0 +1,51 @@
+<?php
+/**
+ * AttachUserRole.php
+ * Copyright (C) 2016 Sander Dorigo
+ *
+ * This software may be modified and distributed under the terms
+ * of the MIT license. See the LICENSE file for details.
+ */
+
+namespace FireflyIII\Handlers\Events;
+
+
+use FireflyIII\Events\UserRegistration;
+use FireflyIII\Repositories\User\UserRepositoryInterface;
+use Log;
+
+/**
+ * Class AttachUserRole
+ *
+ * @package FireflyIII\Handlers\Events
+ */
+class AttachUserRole
+{
+ /**
+ * Create the event listener.
+ *
+ */
+ public function __construct()
+ {
+ //
+ }
+
+ /**
+ * Handle the event.
+ *
+ * @param UserRegistration $event
+ */
+ public function handle(UserRegistration $event)
+ {
+ Log::debug('Trigger attachuserrole');
+ /** @var UserRepositoryInterface $repository */
+ $repository = app('FireflyIII\Repositories\User\UserRepositoryInterface');
+
+ // first user ever?
+ if ($repository->count() == 1) {
+ Log::debug('Will attach role.');
+ $repository->attachRole($event->user, 'owner');
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/app/Handlers/Events/SendRegistrationMail.php b/app/Handlers/Events/SendRegistrationMail.php
new file mode 100644
index 0000000000..03e74a2e3e
--- /dev/null
+++ b/app/Handlers/Events/SendRegistrationMail.php
@@ -0,0 +1,63 @@
+<?php
+/**
+ * SendRegistrationMail.php
+ * Copyright (C) 2016 Sander Dorigo
+ *
+ * This software may be modified and distributed under the terms
+ * of the MIT license. See the LICENSE file for details.
+ */
+
+namespace FireflyIII\Handlers\Events;
+
+
+use FireflyIII\Events\UserRegistration;
+use Illuminate\Mail\Message;
+use Log;
+use Mail;
+use Swift_TransportException;
+
+/**
+ * Class SendRegistrationMail
+ *
+ * @package FireflyIII\Handlers\Events
+ */
+class SendRegistrationMail
+{
+ /**
+ * Create the event listener.
+ *
+ */
+ public function __construct()
+ {
+ //
+ }
+
+ /**
+ * Handle the event.
+ *
+ * @param UserRegistration $event
+ *
+ * @return void
+ */
+ public function handle(UserRegistration $event)
+ {
+ $sendMail = env('SEND_REGISTRATION_MAIL', true);
+ if (!$sendMail) {
+ return;
+ }
+ // get the email address
+ $email = $event->user->email;
+ $address = route('index');
+ $ipAddress = $event->ip;
+ // send email.
+ try {
+ Mail::send(
+ ['emails.registered-html', 'emails.registered'], ['address' => $address, 'ip' => $ipAddress], function (Message $message) use ($email) {
+ $message->to($email, $email)->subject('Welcome to Firefly III! ');
+ }
+ );
+ } catch (Swift_TransportException $e) {
+ Log::error($e->getMessage());
+ }
+ }
+}
\ No newline at end of file
diff --git a/app/Http/Controllers/Auth/AuthController.php b/app/Http/Controllers/Auth/AuthController.php
index a7146428d8..bfd184d0e3 100644
--- a/app/Http/Controllers/Auth/AuthController.php
+++ b/app/Http/Controllers/Auth/AuthController.php
@@ -4,6 +4,7 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Controllers\Auth;
use Auth;
+use FireflyIII\Events\UserRegistration;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Repositories\User\UserRepositoryInterface;
@@ -91,14 +92,13 @@ class AuthController extends Controller
/**
* Handle a registration request for the application.
*
- * @param UserRepositoryInterface $repository
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
* @throws FireflyException
* @throws \Illuminate\Foundation\Validation\ValidationException
*/
- public function register(UserRepositoryInterface $repository, Request $request)
+ public function register(Request $request)
{
$validator = $this->validator($request->all());
@@ -123,37 +123,18 @@ class AuthController extends Controller
}
- Auth::login($this->create($request->all()));
-
- // get the email address
- if (Auth::user() instanceof User) {
- $email = Auth::user()->email;
- $address = route('index');
- $ipAddress = $request->ip();
- // send email.
- try {
- Mail::send(
- ['emails.registered-html', 'emails.registered'], ['address' => $address, 'ip' => $ipAddress], function (Message $message) use ($email) {
- $message->to($email, $email)->subject('Welcome to Firefly III! ');
- }
- );
- } catch (Swift_TransportException $e) {
- Log::error($e->getMessage());
- }
+ $user = $this->create($request->all());
- // set flash message
- Session::flash('success', strval(trans('firefly.registered')));
- Session::flash('gaEventCategory', 'user');
- Session::flash('gaEventAction', 'new-registration');
+ // trigger user registration event:
+ event(new UserRegistration($user, $request->ip()));
- // first user ever?
- if ($repository->count() == 1) {
- $repository->attachRole(Auth::user(), 'owner');
- }
+ Auth::login($user);
- return redirect($this->redirectPath());
- }
- throw new FireflyException('The authenticated user object is invalid.');
+ Session::flash('success', strval(trans('firefly.registered')));
+ Session::flash('gaEventCategory', 'user');
+ Session::flash('gaEventAction', 'new-registration');
+
+ return redirect($this->redirectPath());
}
/**
diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php
index 5b742be2b7..75412a6df8 100644
--- a/app/Providers/EventServiceProvider.php
+++ b/app/Providers/EventServiceProvider.php
@@ -44,6 +44,10 @@ class EventServiceProvider extends ServiceProvider
'Illuminate\Auth\Events\Logout' => [
'FireflyIII\Handlers\Events\UserEventListener@onUserLogout',
],
+ 'FireflyIII\Events\UserRegistration' => [
+ 'FireflyIII\Handlers\Events\SendRegistrationMail',
+ 'FireflyIII\Handlers\Events\AttachUserRole',
+ ],
];
/**
diff --git a/app/Repositories/User/UserRepository.php b/app/Repositories/User/UserRepository.php
index 78a2a80d5f..a7155867ed 100644
--- a/app/Repositories/User/UserRepository.php
+++ b/app/Repositories/User/UserRepository.php
@@ -31,6 +31,7 @@ class UserRepository implements UserRepositoryInterface
{
$admin = Role::where('name', 'owner')->first();
$user->attachRole($admin);
+ $user->save();
return true;
}