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

217 lines
6.6 KiB
PHP
Raw Normal View History

2016-10-22 02:31:27 -05:00
<?php
/**
* UserEventHandler.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2016-10-22 02:31:27 -05:00
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
2016-10-22 02:31:27 -05:00
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2016-10-22 02:31:27 -05:00
*/
declare(strict_types=1);
2016-10-22 02:31:27 -05:00
namespace FireflyIII\Handlers\Events;
2018-04-02 07:42:07 -05:00
use Exception;
2016-10-22 02:31:27 -05:00
use FireflyIII\Events\RegisteredUser;
2016-11-22 14:21:11 -06:00
use FireflyIII\Events\RequestedNewPassword;
use FireflyIII\Events\UserChangedEmail;
2018-01-21 11:06:57 -06:00
use FireflyIII\Factories\RoleFactory;
use FireflyIII\Mail\ConfirmEmailChangeMail;
use FireflyIII\Mail\RegisteredUser as RegisteredUserMail;
2017-04-27 01:26:58 -05:00
use FireflyIII\Mail\RequestedNewPassword as RequestedNewPasswordMail;
use FireflyIII\Mail\UndoEmailChangeMail;
2016-10-22 02:31:27 -05:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2017-12-05 13:50:04 -06:00
use FireflyIII\User;
use Illuminate\Auth\Events\Login;
2016-10-22 02:31:27 -05:00
use Log;
use Mail;
use Preferences;
2016-10-22 02:31:27 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class UserEventHandler.
2016-10-22 02:31:27 -05:00
*
* 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.
*/
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?
2017-11-15 05:25:49 -06:00
if (1 === $repository->count()) {
2016-10-22 02:31:27 -05:00
$repository->attachRole($event->user, 'owner');
}
return true;
}
2017-12-05 13:50:04 -06:00
/**
* @param Login $event
*
* @return bool
*/
public function checkSingleUserIsAdmin(Login $event): bool
{
2018-01-21 11:06:57 -06:00
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
2017-12-05 13:50:04 -06:00
2017-12-29 02:05:35 -06:00
/** @var User $user */
2017-12-05 13:50:04 -06:00
$user = $event->user;
2018-01-21 11:06:57 -06:00
$count = $repository->count();
2017-12-05 13:50:04 -06:00
if ($count > 1) {
// if more than one user, do nothing.
Log::debug(sprintf('System has %d users, will not change users roles.', $count));
return true;
}
// user is only user but has admin role
2017-12-22 11:32:43 -06:00
if (1 === $count && $user->hasRole('owner')) {
2017-12-05 13:50:04 -06:00
Log::debug(sprintf('User #%d is only user but has role owner so all is well.', $user->id));
return true;
}
// user is the only user but does not have role "owner".
2018-01-21 11:06:57 -06:00
$role = $repository->getRole('owner');
2018-04-02 07:42:07 -05:00
if (null === $role) {
2017-12-05 13:50:04 -06:00
// create role, does not exist. Very strange situation so let's raise a big fuss about it.
2018-01-21 11:06:57 -06:00
$role = $repository->createRole('owner', 'Site Owner', 'User runs this instance of FF3');
2017-12-05 13:50:04 -06:00
Log::error('Could not find role "owner". This is weird.');
}
Log::info(sprintf('Gave user #%d role #%d ("%s")', $user->id, $role->id, $role->name));
// give user the role
2018-01-21 11:06:57 -06:00
$repository->attachRole($user, 'owner');
2017-12-05 13:50:04 -06:00
return true;
}
/**
* @param UserChangedEmail $event
*
* @return bool
*/
public function sendEmailChangeConfirmMail(UserChangedEmail $event): bool
{
$newEmail = $event->newEmail;
$oldEmail = $event->oldEmail;
$user = $event->user;
$ipAddress = $event->ipAddress;
$token = Preferences::getForUser($user, 'email_change_confirm_token', 'invalid');
$uri = route('profile.confirm-email-change', [$token->data]);
try {
Mail::to($newEmail)->send(new ConfirmEmailChangeMail($newEmail, $oldEmail, $uri, $ipAddress));
// @codeCoverageIgnoreStart
2018-04-02 07:42:07 -05:00
} catch (Exception $e) {
Log::error($e->getMessage());
}
// @codeCoverageIgnoreEnd
return true;
}
/**
* @param UserChangedEmail $event
*
* @return bool
*/
public function sendEmailChangeUndoMail(UserChangedEmail $event): bool
{
$newEmail = $event->newEmail;
$oldEmail = $event->oldEmail;
$user = $event->user;
$ipAddress = $event->ipAddress;
$token = Preferences::getForUser($user, 'email_change_undo_token', 'invalid');
$uri = route('profile.undo-email-change', [$token->data, hash('sha256', $oldEmail)]);
try {
Mail::to($oldEmail)->send(new UndoEmailChangeMail($newEmail, $oldEmail, $uri, $ipAddress));
// @codeCoverageIgnoreStart
2018-04-02 07:42:07 -05:00
} catch (Exception $e) {
Log::error($e->getMessage());
}
// @codeCoverageIgnoreEnd
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
2018-04-02 07:42:07 -05:00
} catch (Exception $e) {
2016-11-22 14:21:11 -06:00
Log::error($e->getMessage());
}
2017-06-05 04:12:50 -05:00
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;
$uri = route('index');
2016-10-22 02:31:27 -05:00
$ipAddress = $event->ipAddress;
2016-10-22 02:31:27 -05:00
// send email.
try {
Mail::to($email)->send(new RegisteredUserMail($uri, $ipAddress));
2017-04-27 01:26:58 -05:00
// @codeCoverageIgnoreStart
2018-04-02 07:42:07 -05:00
} catch (Exception $e) {
2016-10-22 02:31:27 -05:00
Log::error($e->getMessage());
}
2017-06-05 04:12:50 -05:00
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
}