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

435 lines
16 KiB
PHP
Raw Normal View History

2016-10-22 02:31:27 -05:00
<?php
2022-11-03 23:11:05 -05:00
2016-10-22 02:31:27 -05:00
/**
* UserEventHandler.php
2020-01-28 01:45:38 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2016-10-22 02:31:27 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2016-10-22 02:31:27 -05:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 01:40:00 -05:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 01:40:00 -05:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 01:40:00 -05:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://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;
2020-08-28 14:58:03 -05:00
use Carbon\Carbon;
use Database\Seeders\ExchangeRateSeeder;
2018-04-02 07:42:07 -05:00
use Exception;
2021-10-12 22:57:11 -05:00
use FireflyIII\Events\ActuallyLoggedIn;
2022-10-01 12:06:55 -05:00
use FireflyIII\Events\Admin\InvitationCreated;
2020-08-28 14:58:03 -05:00
use FireflyIII\Events\DetectedNewIPAddress;
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;
2021-07-17 10:26:12 -05:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Mail\ConfirmEmailChangeMail;
2022-10-01 12:06:55 -05:00
use FireflyIII\Mail\InvitationMail;
use FireflyIII\Mail\UndoEmailChangeMail;
2021-08-28 08:47:33 -05:00
use FireflyIII\Models\GroupMembership;
use FireflyIII\Models\UserGroup;
use FireflyIII\Models\UserRole;
2022-09-24 01:23:07 -05:00
use FireflyIII\Notifications\Admin\UserRegistration as AdminRegistrationNotification;
2022-09-24 00:03:03 -05:00
use FireflyIII\Notifications\User\UserLogin;
use FireflyIII\Notifications\User\UserNewPassword;
2022-09-24 01:23:07 -05:00
use FireflyIII\Notifications\User\UserRegistration as UserRegistrationNotification;
2016-10-22 02:31:27 -05:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2022-09-24 01:23:07 -05:00
use FireflyIII\Support\Facades\FireflyConfig;
2017-12-05 13:50:04 -06:00
use FireflyIII\User;
use Illuminate\Auth\Events\Login;
2023-04-01 00:04:42 -05:00
use Illuminate\Support\Facades\Log;
2023-05-29 06:56:55 -05:00
use Illuminate\Support\Facades\Notification;
2016-10-22 02:31:27 -05:00
use Mail;
/**
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.
*
2022-11-03 23:11:05 -05:00
* @param RegisteredUser $event
2016-10-22 02:31:27 -05:00
*/
2022-09-24 00:03:03 -05:00
public function attachUserRole(RegisteredUser $event): void
2016-10-22 02:31:27 -05:00
{
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
// first user ever?
2017-11-15 05:25:49 -06:00
if (1 === $repository->count()) {
2018-08-28 14:48:10 -05:00
Log::debug('User count is one, attach role.');
2016-10-22 02:31:27 -05:00
$repository->attachRole($event->user, 'owner');
}
}
2017-12-05 13:50:04 -06:00
/**
2018-07-07 00:48:10 -05:00
* Fires to see if a user is admin.
*
2022-11-03 23:11:05 -05:00
* @param Login $event
2017-12-05 13:50:04 -06:00
*/
2022-09-24 00:03:03 -05:00
public function checkSingleUserIsAdmin(Login $event): void
2017-12-05 13:50:04 -06:00
{
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
2018-07-07 00:48:10 -05:00
// only act when there is 1 user in the system and he has no admin rights.
if (1 === $count && !$repository->hasRole($user, 'owner')) {
// user is the only user but does not have role "owner".
$role = $repository->getRole('owner');
if (null === $role) {
// create role, does not exist. Very strange situation so let's raise a big fuss about it.
$role = $repository->createRole('owner', 'Site Owner', 'User runs this instance of FF3');
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
$repository->attachRole($user, 'owner');
2017-12-05 13:50:04 -06:00
}
}
2022-12-29 12:41:57 -06:00
/**
* @param RegisteredUser $event
*/
public function createExchangeRates(RegisteredUser $event): void
{
$seeder = new ExchangeRateSeeder();
$seeder->run();
}
/**
2022-11-03 23:11:05 -05:00
* @param RegisteredUser $event
*
* @throws FireflyException
*/
2022-09-24 00:03:03 -05:00
public function createGroupMembership(RegisteredUser $event): void
{
2021-10-25 23:25:41 -05:00
$user = $event->user;
$groupExists = true;
$groupTitle = $user->email;
$index = 1;
2022-11-03 23:11:05 -05:00
/** @var UserGroup $group */
$group = null;
2021-10-25 23:25:41 -05:00
// create a new group.
2021-10-25 23:25:41 -05:00
while (true === $groupExists) {
$groupExists = UserGroup::where('title', $groupTitle)->count() > 0;
2022-03-29 07:58:06 -05:00
if (false === $groupExists) {
2021-10-25 23:25:41 -05:00
$group = UserGroup::create(['title' => $groupTitle]);
break;
}
$groupTitle = sprintf('%s-%d', $user->email, $index);
$index++;
2022-03-29 07:58:06 -05:00
if ($index > 99) {
2021-10-25 23:25:41 -05:00
throw new FireflyException('Email address can no longer be used for registrations.');
}
}
2023-01-02 23:48:53 -06:00
/** @var UserRole|null $role */
2022-03-29 07:58:06 -05:00
$role = UserRole::where('title', UserRole::OWNER)->first();
if (null === $role) {
throw new FireflyException('The user role is unexpectedly empty. Did you run all migrations?');
}
GroupMembership::create(
[
'user_id' => $user->id,
'user_group_id' => $group->id,
'user_role_id' => $role->id,
]
);
$user->user_group_id = $group->id;
$user->save();
}
/**
2018-07-07 00:48:10 -05:00
* Set the demo user back to English.
*
2022-11-03 23:11:05 -05:00
* @param Login $event
*
2021-09-18 03:21:29 -05:00
* @throws FireflyException
*/
2022-09-24 00:03:03 -05:00
public function demoUserBackToEnglish(Login $event): void
{
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
/** @var User $user */
$user = $event->user;
if ($repository->hasRole($user, 'demo')) {
// set user back to English.
app('preferences')->setForUser($user, 'language', 'en_US');
2020-05-18 14:17:59 -05:00
app('preferences')->setForUser($user, 'locale', 'equal');
app('preferences')->mark();
}
}
2020-08-28 14:58:03 -05:00
/**
2022-11-03 23:11:05 -05:00
* @param DetectedNewIPAddress $event
2021-05-24 01:50:17 -05:00
*
2021-09-18 03:21:29 -05:00
* @throws FireflyException
2020-08-28 14:58:03 -05:00
*/
public function notifyNewIPAddress(DetectedNewIPAddress $event): void
{
$user = $event->user;
$email = $user->email;
$ipAddress = $event->ipAddress;
2020-11-01 23:20:49 -06:00
2021-03-21 03:15:40 -05:00
if ($user->hasRole('demo')) {
2020-11-01 23:20:49 -06:00
return; // do not email demo user.
}
2021-03-21 03:15:40 -05:00
$list = app('preferences')->getForUser($user, 'login_ip_history', [])->data;
2020-08-28 14:58:03 -05:00
/** @var array $entry */
foreach ($list as $index => $entry) {
if (false === $entry['notified']) {
2023-06-11 09:12:13 -05:00
try {
Notification::send($user, new UserLogin($ipAddress));
} catch (Exception $e) {
$message = $e->getMessage();
if (str_contains($message, 'Bcc')) {
Log::warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
if (str_contains($message, 'RFC 2822')) {
Log::warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
}
2020-08-28 14:58:03 -05:00
}
$list[$index]['notified'] = true;
}
app('preferences')->setForUser($user, 'login_ip_history', $list);
}
2022-12-29 12:41:57 -06:00
/**
* @param RegisteredUser $event
*/
public function sendAdminRegistrationNotification(RegisteredUser $event): void
{
$sendMail = FireflyConfig::get('notification_admin_new_reg', true)->data;
if ($sendMail) {
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
$all = $repository->all();
foreach ($all as $user) {
if ($repository->hasRole($user, 'owner')) {
2023-06-11 09:12:13 -05:00
try {
Notification::send($user, new AdminRegistrationNotification($event->user));
} catch (Exception $e) {
$message = $e->getMessage();
if (str_contains($message, 'Bcc')) {
Log::warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
if (str_contains($message, 'RFC 2822')) {
Log::warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
}
2022-12-29 12:41:57 -06:00
}
}
}
}
/**
2022-09-24 00:03:03 -05:00
* Send email to confirm email change. Will not be made into a notification, because
* this requires some custom fields from the user and not just the "user" object.
2018-07-07 00:48:10 -05:00
*
2022-11-03 23:11:05 -05:00
* @param UserChangedEmail $event
*
2021-09-18 03:21:29 -05:00
* @throws FireflyException
*/
2022-09-24 00:03:03 -05:00
public function sendEmailChangeConfirmMail(UserChangedEmail $event): void
{
2022-03-29 07:58:06 -05:00
$newEmail = $event->newEmail;
$oldEmail = $event->oldEmail;
$user = $event->user;
$token = app('preferences')->getForUser($user, 'email_change_confirm_token', 'invalid');
2022-04-12 11:19:30 -05:00
$url = route('profile.confirm-email-change', [$token->data]);
2022-09-24 00:03:03 -05:00
try {
2022-04-12 11:19:30 -05:00
Mail::to($newEmail)->send(new ConfirmEmailChangeMail($newEmail, $oldEmail, $url));
2022-12-30 13:25:04 -06:00
} catch (Exception $e) { // intentional generic exception
Log::error($e->getMessage());
throw new FireflyException($e->getMessage(), 0, $e);
}
}
/**
2022-09-24 00:03:03 -05:00
* Send email to be able to undo email change. Will not be made into a notification, because
* this requires some custom fields from the user and not just the "user" object.
2018-07-07 00:48:10 -05:00
*
2022-11-03 23:11:05 -05:00
* @param UserChangedEmail $event
*
2021-09-18 03:21:29 -05:00
* @throws FireflyException
*/
2022-09-24 00:03:03 -05:00
public function sendEmailChangeUndoMail(UserChangedEmail $event): void
{
2022-03-29 07:58:06 -05:00
$newEmail = $event->newEmail;
$oldEmail = $event->oldEmail;
$user = $event->user;
$token = app('preferences')->getForUser($user, 'email_change_undo_token', 'invalid');
2022-11-03 23:11:05 -05:00
$hashed = hash('sha256', sprintf('%s%s', (string)config('app.key'), $oldEmail));
2022-04-12 11:19:30 -05:00
$url = route('profile.undo-email-change', [$token->data, $hashed]);
try {
2022-04-12 11:19:30 -05:00
Mail::to($oldEmail)->send(new UndoEmailChangeMail($newEmail, $oldEmail, $url));
2022-12-30 13:25:04 -06:00
} catch (Exception $e) { // intentional generic exception
Log::error($e->getMessage());
throw new FireflyException($e->getMessage(), 0, $e);
}
}
2016-11-22 14:21:11 -06:00
/**
2018-07-07 00:48:10 -05:00
* Send a new password to the user.
2022-11-03 23:11:05 -05:00
* @param RequestedNewPassword $event
2016-11-22 14:21:11 -06:00
*/
2022-09-24 00:03:03 -05:00
public function sendNewPassword(RequestedNewPassword $event): void
2016-11-22 14:21:11 -06:00
{
2023-06-11 09:12:13 -05:00
try {
Notification::send($event->user, new UserNewPassword(route('password.reset', [$event->token])));
} catch (Exception $e) {
$message = $e->getMessage();
if (str_contains($message, 'Bcc')) {
Log::warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
if (str_contains($message, 'RFC 2822')) {
Log::warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
}
2021-08-28 08:47:33 -05:00
}
2022-12-29 12:41:57 -06:00
/**
* @param InvitationCreated $event
* @return void
2023-02-22 11:03:31 -06:00
* @throws FireflyException
2022-12-29 12:41:57 -06:00
*/
public function sendRegistrationInvite(InvitationCreated $event): void
{
$invitee = $event->invitee->email;
$admin = $event->invitee->user->email;
$url = route('invite', [$event->invitee->invite_code]);
try {
Mail::to($invitee)->send(new InvitationMail($invitee, $admin, $url));
2022-12-30 13:25:04 -06:00
} catch (Exception $e) { // intentional generic exception
2022-12-29 12:41:57 -06:00
Log::error($e->getMessage());
throw new FireflyException($e->getMessage(), 0, $e);
}
}
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.
*
2022-11-03 23:11:05 -05:00
* @param RegisteredUser $event
2016-10-22 02:31:27 -05:00
*
*/
2022-09-24 00:03:03 -05:00
public function sendRegistrationMail(RegisteredUser $event): void
2016-10-22 02:31:27 -05:00
{
2022-09-24 01:23:07 -05:00
$sendMail = FireflyConfig::get('notification_user_new_reg', true)->data;
2018-07-07 00:48:10 -05:00
if ($sendMail) {
2023-06-11 09:12:13 -05:00
try {
Notification::send($event->user, new UserRegistrationNotification());
} catch (Exception $e) {
$message = $e->getMessage();
if (str_contains($message, 'Bcc')) {
Log::warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
if (str_contains($message, 'RFC 2822')) {
Log::warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
}
2022-09-24 01:23:07 -05:00
}
}
2021-04-07 00:28:43 -05:00
2021-03-21 03:15:40 -05:00
/**
2022-11-03 23:11:05 -05:00
* @param ActuallyLoggedIn $event
2022-03-29 08:10:05 -05:00
* @throws FireflyException
2021-03-21 03:15:40 -05:00
*/
2021-10-12 22:57:11 -05:00
public function storeUserIPAddress(ActuallyLoggedIn $event): void
2021-03-21 03:15:40 -05:00
{
2021-10-12 22:44:17 -05:00
Log::debug('Now in storeUserIPAddress');
2021-03-21 03:15:40 -05:00
$user = $event->user;
2022-05-30 22:27:47 -05:00
2022-09-24 00:03:03 -05:00
if ($user->hasRole('demo')) {
2022-05-30 22:27:47 -05:00
Log::debug('Do not log demo user logins');
return;
}
2021-07-17 10:26:12 -05:00
try {
2022-12-30 23:57:05 -06:00
/** @var array $preference */
2021-07-17 10:26:12 -05:00
$preference = app('preferences')->getForUser($user, 'login_ip_history', [])->data;
} catch (FireflyException $e) {
// don't care.
Log::error($e->getMessage());
return;
}
$inArray = false;
$ip = request()->ip();
2021-10-12 22:57:11 -05:00
Log::debug(sprintf('User logging in from IP address %s', $ip));
2021-03-21 03:15:40 -05:00
// update array if in array
foreach ($preference as $index => $row) {
if ($row['ip'] === $ip) {
Log::debug('Found IP in array, refresh time.');
$preference[$index]['time'] = now(config('app.timezone'))->format('Y-m-d H:i:s');
$inArray = true;
}
// clean up old entries (6 months)
$carbon = Carbon::createFromFormat('Y-m-d H:i:s', $preference[$index]['time']);
if ($carbon->diffInMonths(today()) > 6) {
Log::debug(sprintf('Entry for %s is very old, remove it.', $row['ip']));
unset($preference[$index]);
}
}
// add to array if not the case:
if (false === $inArray) {
$preference[] = [
'ip' => $ip,
'time' => now(config('app.timezone'))->format('Y-m-d H:i:s'),
'notified' => false,
];
}
$preference = array_values($preference);
2023-01-02 23:48:53 -06:00
/** @var bool $send */
2023-02-22 11:14:14 -06:00
$send = app('preferences')->getForUser($user, 'notification_user_login', true)->data;
2021-03-21 03:15:40 -05:00
app('preferences')->setForUser($user, 'login_ip_history', $preference);
2022-09-24 04:41:07 -05:00
if (false === $inArray && true === $send) {
2021-03-21 03:15:40 -05:00
event(new DetectedNewIPAddress($user, $ip));
}
}
2016-10-23 05:42:44 -05:00
}