firefly-iii/app/Handlers/Events/UserEventHandler.php

114 lines
3.0 KiB
PHP
Raw Normal View History

2016-10-22 02:31:27 -05:00
<?php
/**
* UserEventHandler.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
2016-10-22 02:31:27 -05:00
namespace FireflyIII\Handlers\Events;
use FireflyIII\Events\RegisteredUser;
2016-11-22 14:21:11 -06:00
use FireflyIII\Events\RequestedNewPassword;
use FireflyIII\Mail\RegisteredUser as RegisteredUserMail;
2017-04-27 01:26:58 -05:00
use FireflyIII\Mail\RequestedNewPassword as RequestedNewPasswordMail;
2016-10-22 02:31:27 -05:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Log;
use Mail;
use Swift_TransportException;
/**
* Class UserEventHandler
*
* This class responds to any events that have anything to do with the User object.
*
* The method name reflects what is being done. This is in the present tense.
*
* @package FireflyIII\Handlers\Events
*/
class UserEventHandler
{
/**
* This method will bestow upon a user the "owner" role if he is the first user in the system.
*
* @param RegisteredUser $event
*
* @return bool
*/
public function attachUserRole(RegisteredUser $event): bool
{
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
// first user ever?
if ($repository->count() === 1) {
$repository->attachRole($event->user, 'owner');
}
return true;
}
2016-11-22 14:21:11 -06:00
/**
* @param RequestedNewPassword $event
*
* @return bool
*/
public function sendNewPassword(RequestedNewPassword $event): bool
{
$email = $event->user->email;
$ipAddress = $event->ipAddress;
$token = $event->token;
$url = route('password.reset', [$token]);
// send email.
try {
2017-04-27 01:26:58 -05:00
Mail::to($email)->send(new RequestedNewPasswordMail($url, $ipAddress));
// @codeCoverageIgnoreStart
2016-11-22 14:21:11 -06:00
} catch (Swift_TransportException $e) {
Log::error($e->getMessage());
}
2017-04-27 01:26:58 -05:00
// @codeCoverageIgnoreEnd
2016-11-22 14:21:11 -06:00
return true;
}
2016-10-22 02:31:27 -05:00
/**
* This method will send the user a registration mail, welcoming him or her to Firefly III.
* This message is only sent when the configuration of Firefly III says so.
*
* @param RegisteredUser $event
*
* @return bool
*/
public function sendRegistrationMail(RegisteredUser $event)
{
$sendMail = env('SEND_REGISTRATION_MAIL', true);
if (!$sendMail) {
2017-04-27 01:26:58 -05:00
return true; // @codeCoverageIgnore
2016-10-22 02:31:27 -05:00
}
// get the email address
$email = $event->user->email;
$address = route('index');
$ipAddress = $event->ipAddress;
2016-10-22 02:31:27 -05:00
// send email.
try {
2017-04-27 01:26:58 -05:00
Mail::to($email)->send(new RegisteredUserMail($address, $ipAddress));
// @codeCoverageIgnoreStart
2016-10-22 02:31:27 -05:00
} catch (Swift_TransportException $e) {
Log::error($e->getMessage());
}
2017-04-27 01:26:58 -05:00
// @codeCoverageIgnoreEnd
2016-10-22 02:31:27 -05:00
return true;
}
2016-10-23 05:42:44 -05:00
}