Move code to event handlers instead of registration routine.

This commit is contained in:
James Cole 2016-03-28 19:50:24 +02:00
parent 70c8a524cd
commit cc88d5962e
6 changed files with 168 additions and 29 deletions

View File

@ -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;
}
}

View File

@ -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');
}
}
}

View File

@ -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());
}
}
}

View File

@ -4,6 +4,7 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Controllers\Auth; namespace FireflyIII\Http\Controllers\Auth;
use Auth; use Auth;
use FireflyIII\Events\UserRegistration;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Repositories\User\UserRepositoryInterface; use FireflyIII\Repositories\User\UserRepositoryInterface;
@ -91,14 +92,13 @@ class AuthController extends Controller
/** /**
* Handle a registration request for the application. * Handle a registration request for the application.
* *
* @param UserRepositoryInterface $repository
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* *
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
* @throws FireflyException * @throws FireflyException
* @throws \Illuminate\Foundation\Validation\ValidationException * @throws \Illuminate\Foundation\Validation\ValidationException
*/ */
public function register(UserRepositoryInterface $repository, Request $request) public function register(Request $request)
{ {
$validator = $this->validator($request->all()); $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 // trigger user registration event:
if (Auth::user() instanceof User) { event(new UserRegistration($user, $request->ip()));
$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());
}
// set flash message Auth::login($user);
Session::flash('success', strval(trans('firefly.registered')));
Session::flash('gaEventCategory', 'user');
Session::flash('gaEventAction', 'new-registration');
// first user ever? Session::flash('success', strval(trans('firefly.registered')));
if ($repository->count() == 1) { Session::flash('gaEventCategory', 'user');
$repository->attachRole(Auth::user(), 'owner'); Session::flash('gaEventAction', 'new-registration');
}
return redirect($this->redirectPath()); return redirect($this->redirectPath());
}
throw new FireflyException('The authenticated user object is invalid.');
} }
/** /**

View File

@ -44,6 +44,10 @@ class EventServiceProvider extends ServiceProvider
'Illuminate\Auth\Events\Logout' => [ 'Illuminate\Auth\Events\Logout' => [
'FireflyIII\Handlers\Events\UserEventListener@onUserLogout', 'FireflyIII\Handlers\Events\UserEventListener@onUserLogout',
], ],
'FireflyIII\Events\UserRegistration' => [
'FireflyIII\Handlers\Events\SendRegistrationMail',
'FireflyIII\Handlers\Events\AttachUserRole',
],
]; ];
/** /**

View File

@ -31,6 +31,7 @@ class UserRepository implements UserRepositoryInterface
{ {
$admin = Role::where('name', 'owner')->first(); $admin = Role::where('name', 'owner')->first();
$user->attachRole($admin); $user->attachRole($admin);
$user->save();
return true; return true;
} }