2016-10-22 09:31:27 +02:00
|
|
|
<?php
|
2022-11-04 05:11:05 +01:00
|
|
|
|
2016-10-22 09:31:27 +02:00
|
|
|
/**
|
|
|
|
* UserEventHandler.php
|
2020-01-28 08:45:38 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-10-22 09:31:27 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-22 09:31:27 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02: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 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 08:40:00 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02: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 09:31:27 +02:00
|
|
|
*/
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2016-10-22 09:31:27 +02:00
|
|
|
|
|
|
|
namespace FireflyIII\Handlers\Events;
|
|
|
|
|
2020-08-28 21:58:03 +02:00
|
|
|
use Carbon\Carbon;
|
2022-06-06 14:40:19 +02:00
|
|
|
use Database\Seeders\ExchangeRateSeeder;
|
2023-09-20 06:17:56 +02:00
|
|
|
use FireflyIII\Enums\UserRoleEnum;
|
2021-10-13 05:57:11 +02:00
|
|
|
use FireflyIII\Events\ActuallyLoggedIn;
|
2022-10-01 19:06:55 +02:00
|
|
|
use FireflyIII\Events\Admin\InvitationCreated;
|
2020-08-28 21:58:03 +02:00
|
|
|
use FireflyIII\Events\DetectedNewIPAddress;
|
2016-10-22 09:31:27 +02:00
|
|
|
use FireflyIII\Events\RegisteredUser;
|
2016-11-22 21:21:11 +01:00
|
|
|
use FireflyIII\Events\RequestedNewPassword;
|
2017-09-26 08:52:16 +02:00
|
|
|
use FireflyIII\Events\UserChangedEmail;
|
2021-07-17 17:26:12 +02:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2017-09-26 08:52:16 +02:00
|
|
|
use FireflyIII\Mail\ConfirmEmailChangeMail;
|
2022-10-01 19:06:55 +02:00
|
|
|
use FireflyIII\Mail\InvitationMail;
|
2017-09-26 08:52:16 +02:00
|
|
|
use FireflyIII\Mail\UndoEmailChangeMail;
|
2021-08-28 15:47:33 +02:00
|
|
|
use FireflyIII\Models\GroupMembership;
|
|
|
|
use FireflyIII\Models\UserGroup;
|
|
|
|
use FireflyIII\Models\UserRole;
|
2022-09-24 08:23:07 +02:00
|
|
|
use FireflyIII\Notifications\Admin\UserRegistration as AdminRegistrationNotification;
|
2022-09-24 07:03:03 +02:00
|
|
|
use FireflyIII\Notifications\User\UserLogin;
|
|
|
|
use FireflyIII\Notifications\User\UserNewPassword;
|
2022-09-24 08:23:07 +02:00
|
|
|
use FireflyIII\Notifications\User\UserRegistration as UserRegistrationNotification;
|
2016-10-22 09:31:27 +02:00
|
|
|
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
2017-12-05 20:50:04 +01:00
|
|
|
use FireflyIII\User;
|
|
|
|
use Illuminate\Auth\Events\Login;
|
2023-05-29 13:56:55 +02:00
|
|
|
use Illuminate\Support\Facades\Notification;
|
2016-10-22 09:31:27 +02:00
|
|
|
use Mail;
|
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class UserEventHandler.
|
2016-10-22 09:31:27 +02: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-09-24 07:03:03 +02:00
|
|
|
public function attachUserRole(RegisteredUser $event): void
|
2016-10-22 09:31:27 +02:00
|
|
|
{
|
|
|
|
/** @var UserRepositoryInterface $repository */
|
|
|
|
$repository = app(UserRepositoryInterface::class);
|
|
|
|
|
|
|
|
// first user ever?
|
2017-11-15 12:25:49 +01:00
|
|
|
if (1 === $repository->count()) {
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug('User count is one, attach role.');
|
2016-10-22 09:31:27 +02:00
|
|
|
$repository->attachRole($event->user, 'owner');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:50:04 +01:00
|
|
|
/**
|
2018-07-07 07:48:10 +02:00
|
|
|
* Fires to see if a user is admin.
|
2017-12-05 20:50:04 +01:00
|
|
|
*/
|
2022-09-24 07:03:03 +02:00
|
|
|
public function checkSingleUserIsAdmin(Login $event): void
|
2017-12-05 20:50:04 +01:00
|
|
|
{
|
2018-01-21 18:06:57 +01:00
|
|
|
/** @var UserRepositoryInterface $repository */
|
|
|
|
$repository = app(UserRepositoryInterface::class);
|
2017-12-05 20:50:04 +01:00
|
|
|
|
2017-12-29 09:05:35 +01:00
|
|
|
/** @var User $user */
|
2024-01-01 14:43:56 +01:00
|
|
|
$user = $event->user;
|
|
|
|
$count = $repository->count();
|
2017-12-05 20:50:04 +01:00
|
|
|
|
2018-07-07 07:48:10 +02: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');
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error('Could not find role "owner". This is weird.');
|
2018-07-07 07:48:10 +02:00
|
|
|
}
|
|
|
|
|
2023-10-29 06:31:27 +01:00
|
|
|
app('log')->info(sprintf('Gave user #%d role #%d ("%s")', $user->id, $role->id, $role->name));
|
2018-07-07 07:48:10 +02:00
|
|
|
// give user the role
|
|
|
|
$repository->attachRole($user, 'owner');
|
2017-12-05 20:50:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 19:41:57 +01:00
|
|
|
/**
|
2023-11-28 18:57:10 +01:00
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
2022-12-29 19:41:57 +01:00
|
|
|
*/
|
|
|
|
public function createExchangeRates(RegisteredUser $event): void
|
|
|
|
{
|
|
|
|
$seeder = new ExchangeRateSeeder();
|
|
|
|
$seeder->run();
|
|
|
|
}
|
|
|
|
|
2021-09-18 10:26:12 +02:00
|
|
|
/**
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
2022-09-24 07:03:03 +02:00
|
|
|
public function createGroupMembership(RegisteredUser $event): void
|
2021-09-18 10:26:12 +02:00
|
|
|
{
|
2024-01-01 14:43:56 +01:00
|
|
|
$user = $event->user;
|
|
|
|
$groupExists = true;
|
|
|
|
$groupTitle = $user->email;
|
|
|
|
$index = 1;
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-11-04 05:11:05 +01:00
|
|
|
/** @var UserGroup $group */
|
2024-01-01 14:43:56 +01:00
|
|
|
$group = null;
|
2021-10-26 06:25:41 +02:00
|
|
|
|
2021-09-18 10:26:12 +02:00
|
|
|
// create a new group.
|
2023-12-10 06:51:59 +01:00
|
|
|
while (true === $groupExists) { // @phpstan-ignore-line
|
2021-10-26 06:25:41 +02:00
|
|
|
$groupExists = UserGroup::where('title', $groupTitle)->count() > 0;
|
2022-03-29 14:58:06 +02:00
|
|
|
if (false === $groupExists) {
|
2021-10-26 06:25:41 +02:00
|
|
|
$group = UserGroup::create(['title' => $groupTitle]);
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2021-10-26 06:25:41 +02:00
|
|
|
break;
|
|
|
|
}
|
2024-01-01 14:43:56 +01:00
|
|
|
$groupTitle = sprintf('%s-%d', $user->email, $index);
|
2023-12-20 19:35:52 +01:00
|
|
|
++$index;
|
2022-03-29 14:58:06 +02:00
|
|
|
if ($index > 99) {
|
2021-10-26 06:25:41 +02:00
|
|
|
throw new FireflyException('Email address can no longer be used for registrations.');
|
|
|
|
}
|
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
|
|
|
|
/** @var null|UserRole $role */
|
2024-01-01 14:43:56 +01:00
|
|
|
$role = UserRole::where('title', UserRoleEnum::OWNER->value)->first();
|
2021-09-18 10:26:12 +02:00
|
|
|
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-06-12 21:41:58 +02:00
|
|
|
/**
|
2018-07-07 07:48:10 +02:00
|
|
|
* Set the demo user back to English.
|
|
|
|
*
|
2021-09-18 10:21:29 +02:00
|
|
|
* @throws FireflyException
|
2018-06-12 21:41:58 +02:00
|
|
|
*/
|
2022-09-24 07:03:03 +02:00
|
|
|
public function demoUserBackToEnglish(Login $event): void
|
2018-06-12 21:41:58 +02:00
|
|
|
{
|
|
|
|
/** @var UserRepositoryInterface $repository */
|
|
|
|
$repository = app(UserRepositoryInterface::class);
|
|
|
|
|
|
|
|
/** @var User $user */
|
2024-01-01 14:43:56 +01:00
|
|
|
$user = $event->user;
|
2018-06-12 21:41:58 +02:00
|
|
|
if ($repository->hasRole($user, 'demo')) {
|
|
|
|
// set user back to English.
|
|
|
|
app('preferences')->setForUser($user, 'language', 'en_US');
|
2020-05-18 21:17:59 +02:00
|
|
|
app('preferences')->setForUser($user, 'locale', 'equal');
|
2018-06-12 21:41:58 +02:00
|
|
|
app('preferences')->mark();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 21:58:03 +02:00
|
|
|
/**
|
2021-09-18 10:21:29 +02:00
|
|
|
* @throws FireflyException
|
2020-08-28 21:58:03 +02:00
|
|
|
*/
|
|
|
|
public function notifyNewIPAddress(DetectedNewIPAddress $event): void
|
|
|
|
{
|
|
|
|
$user = $event->user;
|
|
|
|
$ipAddress = $event->ipAddress;
|
2020-11-02 06:20:49 +01:00
|
|
|
|
2021-03-21 09:15:40 +01:00
|
|
|
if ($user->hasRole('demo')) {
|
2020-11-02 06:20:49 +01:00
|
|
|
return; // do not email demo user.
|
|
|
|
}
|
|
|
|
|
2024-01-01 14:43:56 +01:00
|
|
|
$list = app('preferences')->getForUser($user, 'login_ip_history', [])->data;
|
2023-11-28 18:57:10 +01:00
|
|
|
if (!is_array($list)) {
|
2023-11-26 12:10:42 +01:00
|
|
|
$list = [];
|
|
|
|
}
|
2020-08-28 21:58:03 +02:00
|
|
|
|
|
|
|
/** @var array $entry */
|
|
|
|
foreach ($list as $index => $entry) {
|
|
|
|
if (false === $entry['notified']) {
|
2023-06-11 16:12:13 +02:00
|
|
|
try {
|
|
|
|
Notification::send($user, new UserLogin($ipAddress));
|
2023-12-20 19:35:52 +01:00
|
|
|
} catch (\Exception $e) { // @phpstan-ignore-line
|
2023-06-11 16:12:13 +02:00
|
|
|
$message = $e->getMessage();
|
|
|
|
if (str_contains($message, 'Bcc')) {
|
2023-10-29 06:31:13 +01:00
|
|
|
app('log')->warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-06-11 16:12:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (str_contains($message, 'RFC 2822')) {
|
2023-10-29 06:31:13 +01:00
|
|
|
app('log')->warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-06-11 16:12:13 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error($e->getMessage());
|
|
|
|
app('log')->error($e->getTraceAsString());
|
2023-06-11 16:12:13 +02:00
|
|
|
}
|
2020-08-28 21:58:03 +02:00
|
|
|
}
|
|
|
|
$list[$index]['notified'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
app('preferences')->setForUser($user, 'login_ip_history', $list);
|
|
|
|
}
|
|
|
|
|
2022-12-29 19:41:57 +01:00
|
|
|
public function sendAdminRegistrationNotification(RegisteredUser $event): void
|
|
|
|
{
|
2024-02-22 20:11:09 +01:00
|
|
|
$sendMail = (bool)app('fireflyconfig')->get('notification_admin_new_reg', true)->data;
|
2022-12-29 19:41:57 +01:00
|
|
|
if ($sendMail) {
|
|
|
|
/** @var UserRepositoryInterface $repository */
|
|
|
|
$repository = app(UserRepositoryInterface::class);
|
|
|
|
$all = $repository->all();
|
|
|
|
foreach ($all as $user) {
|
|
|
|
if ($repository->hasRole($user, 'owner')) {
|
2023-06-11 16:12:13 +02:00
|
|
|
try {
|
|
|
|
Notification::send($user, new AdminRegistrationNotification($event->user));
|
2023-12-20 19:35:52 +01:00
|
|
|
} catch (\Exception $e) { // @phpstan-ignore-line
|
2023-06-11 16:12:13 +02:00
|
|
|
$message = $e->getMessage();
|
|
|
|
if (str_contains($message, 'Bcc')) {
|
2023-10-29 06:31:13 +01:00
|
|
|
app('log')->warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-06-11 16:12:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (str_contains($message, 'RFC 2822')) {
|
2023-10-29 06:31:13 +01:00
|
|
|
app('log')->warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-06-11 16:12:13 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error($e->getMessage());
|
|
|
|
app('log')->error($e->getTraceAsString());
|
2023-06-11 16:12:13 +02:00
|
|
|
}
|
2022-12-29 19:41:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-26 08:52:16 +02:00
|
|
|
/**
|
2022-09-24 07:03:03 +02: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 07:48:10 +02:00
|
|
|
*
|
2021-09-18 10:21:29 +02:00
|
|
|
* @throws FireflyException
|
2017-09-26 08:52:16 +02:00
|
|
|
*/
|
2022-09-24 07:03:03 +02:00
|
|
|
public function sendEmailChangeConfirmMail(UserChangedEmail $event): void
|
2017-09-26 08:52:16 +02:00
|
|
|
{
|
2022-03-29 14:58:06 +02:00
|
|
|
$newEmail = $event->newEmail;
|
|
|
|
$oldEmail = $event->oldEmail;
|
|
|
|
$user = $event->user;
|
|
|
|
$token = app('preferences')->getForUser($user, 'email_change_confirm_token', 'invalid');
|
2022-04-12 18:19:30 +02:00
|
|
|
$url = route('profile.confirm-email-change', [$token->data]);
|
2022-09-24 07:03:03 +02:00
|
|
|
|
2017-09-26 08:52:16 +02:00
|
|
|
try {
|
2023-12-20 19:35:52 +01:00
|
|
|
\Mail::to($newEmail)->send(new ConfirmEmailChangeMail($newEmail, $oldEmail, $url));
|
|
|
|
} catch (\Exception $e) {
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error($e->getMessage());
|
|
|
|
app('log')->error($e->getTraceAsString());
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-11-02 06:25:37 +01:00
|
|
|
throw new FireflyException($e->getMessage(), 0, $e);
|
2017-09-26 08:52:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-09-24 07:03:03 +02: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 07:48:10 +02:00
|
|
|
*
|
2021-09-18 10:21:29 +02:00
|
|
|
* @throws FireflyException
|
2017-09-26 08:52:16 +02:00
|
|
|
*/
|
2022-09-24 07:03:03 +02:00
|
|
|
public function sendEmailChangeUndoMail(UserChangedEmail $event): void
|
2017-09-26 08:52:16 +02:00
|
|
|
{
|
2022-03-29 14:58:06 +02:00
|
|
|
$newEmail = $event->newEmail;
|
|
|
|
$oldEmail = $event->oldEmail;
|
|
|
|
$user = $event->user;
|
|
|
|
$token = app('preferences')->getForUser($user, 'email_change_undo_token', 'invalid');
|
2024-02-22 20:11:09 +01:00
|
|
|
$hashed = hash('sha256', sprintf('%s%s', (string)config('app.key'), $oldEmail));
|
2022-04-12 18:19:30 +02:00
|
|
|
$url = route('profile.undo-email-change', [$token->data, $hashed]);
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2017-09-26 08:52:16 +02:00
|
|
|
try {
|
2023-12-20 19:35:52 +01:00
|
|
|
\Mail::to($oldEmail)->send(new UndoEmailChangeMail($newEmail, $oldEmail, $url));
|
|
|
|
} catch (\Exception $e) {
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error($e->getMessage());
|
|
|
|
app('log')->error($e->getTraceAsString());
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-11-02 06:25:37 +01:00
|
|
|
throw new FireflyException($e->getMessage(), 0, $e);
|
2017-09-26 08:52:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-22 21:21:11 +01:00
|
|
|
/**
|
2018-07-07 07:48:10 +02:00
|
|
|
* Send a new password to the user.
|
2016-11-22 21:21:11 +01:00
|
|
|
*/
|
2022-09-24 07:03:03 +02:00
|
|
|
public function sendNewPassword(RequestedNewPassword $event): void
|
2016-11-22 21:21:11 +01:00
|
|
|
{
|
2023-06-11 16:12:13 +02:00
|
|
|
try {
|
|
|
|
Notification::send($event->user, new UserNewPassword(route('password.reset', [$event->token])));
|
2023-12-20 19:35:52 +01:00
|
|
|
} catch (\Exception $e) { // @phpstan-ignore-line
|
2023-06-11 16:12:13 +02:00
|
|
|
$message = $e->getMessage();
|
|
|
|
if (str_contains($message, 'Bcc')) {
|
2023-10-29 06:31:13 +01:00
|
|
|
app('log')->warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-06-11 16:12:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (str_contains($message, 'RFC 2822')) {
|
2023-10-29 06:31:13 +01:00
|
|
|
app('log')->warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-06-11 16:12:13 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error($e->getMessage());
|
|
|
|
app('log')->error($e->getTraceAsString());
|
2023-06-11 16:12:13 +02:00
|
|
|
}
|
2021-08-28 15:47:33 +02:00
|
|
|
}
|
|
|
|
|
2022-12-29 19:41:57 +01:00
|
|
|
/**
|
2023-02-22 18:03:31 +01:00
|
|
|
* @throws FireflyException
|
2022-12-29 19:41:57 +01:00
|
|
|
*/
|
|
|
|
public function sendRegistrationInvite(InvitationCreated $event): void
|
|
|
|
{
|
|
|
|
$invitee = $event->invitee->email;
|
|
|
|
$admin = $event->invitee->user->email;
|
|
|
|
$url = route('invite', [$event->invitee->invite_code]);
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-12-29 19:41:57 +01:00
|
|
|
try {
|
2023-12-20 19:35:52 +01:00
|
|
|
\Mail::to($invitee)->send(new InvitationMail($invitee, $admin, $url));
|
|
|
|
} catch (\Exception $e) {
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error($e->getMessage());
|
|
|
|
app('log')->error($e->getTraceAsString());
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-12-29 19:41:57 +01:00
|
|
|
throw new FireflyException($e->getMessage(), 0, $e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-22 09:31:27 +02: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-09-24 07:03:03 +02:00
|
|
|
public function sendRegistrationMail(RegisteredUser $event): void
|
2016-10-22 09:31:27 +02:00
|
|
|
{
|
2024-02-22 20:11:09 +01:00
|
|
|
$sendMail = (bool)app('fireflyconfig')->get('notification_user_new_reg', true)->data;
|
2018-07-07 07:48:10 +02:00
|
|
|
if ($sendMail) {
|
2023-06-11 16:12:13 +02:00
|
|
|
try {
|
|
|
|
Notification::send($event->user, new UserRegistrationNotification());
|
2023-12-20 19:35:52 +01:00
|
|
|
} catch (\Exception $e) { // @phpstan-ignore-line
|
2023-06-11 16:12:13 +02:00
|
|
|
$message = $e->getMessage();
|
|
|
|
if (str_contains($message, 'Bcc')) {
|
2023-10-29 06:31:13 +01:00
|
|
|
app('log')->warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-06-11 16:12:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (str_contains($message, 'RFC 2822')) {
|
2023-10-29 06:31:13 +01:00
|
|
|
app('log')->warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-06-11 16:12:13 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error($e->getMessage());
|
|
|
|
app('log')->error($e->getTraceAsString());
|
2023-06-11 16:12:13 +02:00
|
|
|
}
|
2022-09-24 08:23:07 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-07 07:28:43 +02:00
|
|
|
|
2021-03-21 09:15:40 +01:00
|
|
|
/**
|
2022-03-29 15:10:05 +02:00
|
|
|
* @throws FireflyException
|
2021-03-21 09:15:40 +01:00
|
|
|
*/
|
2021-10-13 05:57:11 +02:00
|
|
|
public function storeUserIPAddress(ActuallyLoggedIn $event): void
|
2021-03-21 09:15:40 +01:00
|
|
|
{
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug('Now in storeUserIPAddress');
|
2024-01-01 14:43:56 +01:00
|
|
|
$user = $event->user;
|
2022-05-31 05:27:47 +02:00
|
|
|
|
2022-09-24 07:03:03 +02:00
|
|
|
if ($user->hasRole('demo')) {
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug('Do not log demo user logins');
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-05-31 05:27:47 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-17 17:26:12 +02:00
|
|
|
try {
|
2022-12-31 06:57:05 +01:00
|
|
|
/** @var array $preference */
|
2021-07-17 17:26:12 +02:00
|
|
|
$preference = app('preferences')->getForUser($user, 'login_ip_history', [])->data;
|
|
|
|
} catch (FireflyException $e) {
|
|
|
|
// don't care.
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error($e->getMessage());
|
2021-07-17 17:26:12 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2024-01-01 14:43:56 +01:00
|
|
|
$inArray = false;
|
|
|
|
$ip = request()->ip();
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('User logging in from IP address %s', $ip));
|
2021-03-21 09:15:40 +01:00
|
|
|
|
|
|
|
// update array if in array
|
|
|
|
foreach ($preference as $index => $row) {
|
|
|
|
if ($row['ip'] === $ip) {
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug('Found IP in array, refresh time.');
|
2021-03-21 09:15:40 +01:00
|
|
|
$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']);
|
2023-11-26 12:10:42 +01:00
|
|
|
if (false !== $carbon && $carbon->diffInMonths(today()) > 6) {
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('Entry for %s is very old, remove it.', $row['ip']));
|
2021-03-21 09:15:40 +01:00
|
|
|
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-12-20 19:35:52 +01:00
|
|
|
|
2023-01-03 06:48:53 +01:00
|
|
|
/** @var bool $send */
|
2024-01-01 14:43:56 +01:00
|
|
|
$send = app('preferences')->getForUser($user, 'notification_user_login', true)->data;
|
2021-03-21 09:15:40 +01:00
|
|
|
app('preferences')->setForUser($user, 'login_ip_history', $preference);
|
|
|
|
|
2022-09-24 11:41:07 +02:00
|
|
|
if (false === $inArray && true === $send) {
|
2021-03-21 09:15:40 +01:00
|
|
|
event(new DetectedNewIPAddress($user, $ip));
|
|
|
|
}
|
|
|
|
}
|
2016-10-23 12:42:44 +02:00
|
|
|
}
|