| 
									
										
										
										
											2016-03-29 11:55:25 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * UserConfirmation.php | 
					
						
							| 
									
										
										
										
											2016-04-01 16:44:46 +02:00
										 |  |  |  * Copyright (C) 2016 thegrumpydictator@gmail.com | 
					
						
							| 
									
										
										
										
											2016-03-29 11:55:25 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This software may be modified and distributed under the terms | 
					
						
							|  |  |  |  * of the MIT license.  See the LICENSE file for details. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 12:27:31 +02:00
										 |  |  | declare(strict_types = 1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-29 11:55:25 +02:00
										 |  |  | namespace FireflyIII\Handlers\Events; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-29 13:55:57 +02:00
										 |  |  | use Exception; | 
					
						
							| 
									
										
										
										
											2016-03-29 11:55:25 +02:00
										 |  |  | 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 | 
					
						
							| 
									
										
										
										
											2016-04-06 16:37:28 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							| 
									
										
										
										
											2016-03-29 11:55:25 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-06 16:37:28 +02:00
										 |  |  |     public function resendConfirmation(ResendConfirmation $event): bool | 
					
						
							| 
									
										
										
										
											2016-03-29 11:55:25 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         $user      = $event->user; | 
					
						
							| 
									
										
										
										
											2016-03-29 16:10:51 +02:00
										 |  |  |         $ipAddress = $event->ipAddress; | 
					
						
							| 
									
										
										
										
											2016-03-29 11:55:25 +02:00
										 |  |  |         $this->doConfirm($user, $ipAddress); | 
					
						
							| 
									
										
										
										
											2016-04-25 18:43:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-06 16:37:28 +02:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2016-03-29 11:55:25 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Handle the event. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param  UserRegistration $event | 
					
						
							| 
									
										
										
										
											2016-04-06 16:37:28 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							| 
									
										
										
										
											2016-03-29 11:55:25 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-06 16:37:28 +02:00
										 |  |  |     public function sendConfirmation(UserRegistration $event): bool | 
					
						
							| 
									
										
										
										
											2016-03-29 11:55:25 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         $user      = $event->user; | 
					
						
							| 
									
										
										
										
											2016-03-29 16:10:51 +02:00
										 |  |  |         $ipAddress = $event->ipAddress; | 
					
						
							| 
									
										
										
										
											2016-03-29 11:55:25 +02:00
										 |  |  |         $this->doConfirm($user, $ipAddress); | 
					
						
							| 
									
										
										
										
											2016-04-25 18:43:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-06 16:37:28 +02:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2016-03-29 11:55:25 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param User   $user | 
					
						
							|  |  |  |      * @param string $ipAddress | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function doConfirm(User $user, string $ipAddress) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $confirmAccount = env('MUST_CONFIRM_ACCOUNT', false); | 
					
						
							|  |  |  |         if ($confirmAccount === false) { | 
					
						
							|  |  |  |             Preferences::setForUser($user, 'user_confirmed', true); | 
					
						
							|  |  |  |             Preferences::setForUser($user, 'user_confirmed_last_mail', 0); | 
					
						
							| 
									
										
										
										
											2016-03-29 13:52:51 +02:00
										 |  |  |             Preferences::mark(); | 
					
						
							| 
									
										
										
										
											2016-03-29 11:55:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $email = $user->email; | 
					
						
							|  |  |  |         $code  = str_random(16); | 
					
						
							|  |  |  |         $route = route('do_confirm_account', [$code]); | 
					
						
							|  |  |  |         Preferences::setForUser($user, 'user_confirmed', false); | 
					
						
							|  |  |  |         Preferences::setForUser($user, 'user_confirmed_last_mail', time()); | 
					
						
							|  |  |  |         Preferences::setForUser($user, 'user_confirmed_code', $code); | 
					
						
							|  |  |  |         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) { | 
					
						
							| 
									
										
										
										
											2016-03-29 13:55:57 +02:00
										 |  |  |             Log::error($e->getMessage()); | 
					
						
							|  |  |  |         } catch (Exception $e) { | 
					
						
							| 
									
										
										
										
											2016-03-29 11:55:25 +02:00
										 |  |  |             Log::error($e->getMessage()); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-03-29 13:55:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2016-03-29 11:55:25 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-29 16:17:06 +02:00
										 |  |  | } |