Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
New events and their handlers for user activation and other things.
James Cole3adccff611b7
diff --git a/app/Events/ResendConfirmation.php b/app/Events/ResendConfirmation.phpnew file mode 100644index 0000000000..813334f5aa--- /dev/null+++ b/app/Events/ResendConfirmation.php@@ -0,0 +1,39 @@+<?php+/**+ * ResendConfirmation.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 ResendConfirmation+ *+ * @package FireflyIII\Events+ */+class ResendConfirmation extends Event+{+ use SerializesModels;++ public $ip;+ public $user;++ /**+ * Create a new event instance.+ *+ * @param User $user+ *+ * @param string $ip+ */+ public function __construct(User $user, string $ip)+ {+ $this->user = $user;+ $this->ip = $ip;+ }+}\ No newline at end of filediff --git a/app/Events/UserRegistration.php b/app/Events/UserRegistration.phpindex f9c10b3cc3..9ede0bfc2e 100644--- a/app/Events/UserRegistration.php+++ b/app/Events/UserRegistration.php@@ -27,9 +27,9 @@ class UserRegistration extends Event/*** Create a new event instance.*- * @param User $user+ * @param User $user*- * @return void+ * @param string $ip*/public function __construct(User $user, string $ip){diff --git a/app/Handlers/Events/UserConfirmation.php b/app/Handlers/Events/UserConfirmation.phpnew file mode 100644index 0000000000..355b8d9e42--- /dev/null+++ b/app/Handlers/Events/UserConfirmation.php@@ -0,0 +1,102 @@+<?php+/**+ * UserConfirmation.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\ResendConfirmation;+use FireflyIII\Events\UserRegistration;+use FireflyIII\User;+use Illuminate\Mail\Message;+use Log;+use Mail;+use Preferences;+use Swift_TransportException;++/**+ * Class UserConfirmation+ *+ * @package FireflyIII\Handlers\Events+ */+class UserConfirmation+{+ /**+ * Create the event listener.+ *+ */+ public function __construct()+ {+ //+ }++ /**+ * @param ResendConfirmation $event+ */+ public function resendConfirmation(ResendConfirmation $event)+ {+ $user = $event->user;+ $ipAddress = $event->ip;+ $this->doConfirm($user, $ipAddress);+ }++ /**+ * Handle the event.+ *+ * @param UserRegistration $event+ */+ public function sendConfirmation(UserRegistration $event)+ {+ $user = $event->user;+ $ipAddress = $event->ip;+ $this->doConfirm($user, $ipAddress);+ }++ /**+ * @param User $user+ * @param string $ipAddress+ */+ private function doConfirm(User $user, string $ipAddress)+ {+ Log::debug('Trigger UserConfirmation::doConfirm');++ // if user must confirm account, send email+ $confirmAccount = env('MUST_CONFIRM_ACCOUNT', false);++ // otherwise, auto-confirm:+ if ($confirmAccount === false) {+ Preferences::setForUser($user, 'user_confirmed', true);+ Preferences::setForUser($user, 'user_confirmed_last_mail', 0);++ return;+ }++ // send email message:+ $email = $user->email;+ $code = str_random(16);+ $route = route('do_confirm_account', [$code]);++ // set preferences:+ Preferences::setForUser($user, 'user_confirmed', false);+ Preferences::setForUser($user, 'user_confirmed_last_mail', time());+ Preferences::setForUser($user, 'user_confirmed_code', $code);++ // send email.+ try {+ Mail::send(+ ['emails.confirm-account-html', 'emails.confirm-account'], ['route' => $route, 'ip' => $ipAddress],+ function (Message $message) use ($email) {+ $message->to($email, $email)->subject('Please confirm your Firefly III account');+ }+ );+ } catch (Swift_TransportException $e) {+ Log::error($e->getMessage());+ }+ }++}\ No newline at end of filediff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.phpindex 75412a6df8..6df4248721 100644--- a/app/Providers/EventServiceProvider.php+++ b/app/Providers/EventServiceProvider.php@@ -47,6 +47,10 @@ class EventServiceProvider extends ServiceProvider'FireflyIII\Events\UserRegistration' => ['FireflyIII\Handlers\Events\SendRegistrationMail','FireflyIII\Handlers\Events\AttachUserRole',+ 'FireflyIII\Handlers\Events\UserConfirmation@sendConfirmation',+ ],+ 'FireflyIII\Events\ResendConfirmation' => [+ 'FireflyIII\Handlers\Events\UserConfirmation@resendConfirmation',],];