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 @@ +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 @@ +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 @@ +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())); + $user = $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()); - } + // trigger user registration event: + event(new UserRegistration($user, $request->ip())); - // set flash message - Session::flash('success', strval(trans('firefly.registered'))); - Session::flash('gaEventCategory', 'user'); - Session::flash('gaEventAction', 'new-registration'); + Auth::login($user); - // first user ever? - if ($repository->count() == 1) { - $repository->attachRole(Auth::user(), 'owner'); - } + Session::flash('success', strval(trans('firefly.registered'))); + Session::flash('gaEventCategory', 'user'); + Session::flash('gaEventAction', 'new-registration'); - return redirect($this->redirectPath()); - } - throw new FireflyException('The authenticated user object is invalid.'); + 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; }