Files
firefly-iii/app/Handlers/Events/AdminEventHandler.php

163 lines
6.4 KiB
PHP
Raw Normal View History

2017-09-27 15:45:55 +02:00
<?php
2022-12-29 19:41:57 +01:00
2017-09-27 15:45:55 +02:00
/**
* AdminEventHandler.php
2020-01-28 08:45:38 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2017-09-27 15:45:55 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-10-21 08:40:00 +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
*
* 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
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +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/>.
2017-09-27 15:45:55 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Handlers\Events;
2022-10-01 12:21:42 +02:00
use FireflyIII\Events\Admin\InvitationCreated;
2017-09-27 15:45:55 +02:00
use FireflyIII\Events\AdminRequestedTestMessage;
2022-09-24 11:41:07 +02:00
use FireflyIII\Events\NewVersionAvailable;
2024-12-08 16:28:22 +01:00
use FireflyIII\Events\Test\TestNotificationChannel;
2022-09-18 15:48:28 +02:00
use FireflyIII\Notifications\Admin\TestNotification;
2022-10-01 12:21:42 +02:00
use FireflyIII\Notifications\Admin\UserInvitation;
2022-09-24 11:41:07 +02:00
use FireflyIII\Notifications\Admin\VersionCheckResult;
2024-12-08 16:28:22 +01:00
use FireflyIII\Notifications\Test\TestNotificationDiscord;
use FireflyIII\Notifications\Test\TestNotificationEmail;
use FireflyIII\Notifications\Test\TestNotificationSlack;
2018-03-30 22:40:20 +02:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2024-12-08 16:28:22 +01:00
use Illuminate\Support\Facades\Log;
2022-09-18 15:48:28 +02:00
use Illuminate\Support\Facades\Notification;
2017-09-27 15:45:55 +02:00
/**
2017-11-15 12:25:49 +01:00
* Class AdminEventHandler.
2017-09-27 15:45:55 +02:00
*/
class AdminEventHandler
{
2022-10-01 12:21:42 +02:00
public function sendInvitationNotification(InvitationCreated $event): void
{
2024-12-08 16:28:22 +01:00
$sendMail = app('fireflyconfig')->get('notification_invite_created', true)->data;
2022-10-01 12:21:42 +02:00
if (false === $sendMail) {
return;
}
/** @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 UserInvitation($event->invitee));
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-10-01 12:21:42 +02:00
}
}
}
2022-09-24 11:41:07 +02:00
/**
* Send new version message to admin.
*/
public function sendNewVersion(NewVersionAvailable $event): void
{
2024-12-08 16:28:22 +01:00
$sendMail = app('fireflyconfig')->get('notification_new_version', true)->data;
2022-09-24 11:41:07 +02:00
if (false === $sendMail) {
return;
}
/** @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 VersionCheckResult($event->message));
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 11:41:07 +02:00
}
}
}
2022-12-29 19:41:57 +01:00
/**
* Sends a test message to an administrator.
*/
2024-12-08 16:28:22 +01:00
public function sendTestNotification(TestNotificationChannel $event): void
2022-12-29 19:41:57 +01:00
{
2024-12-08 16:28:22 +01:00
Log::debug(sprintf('Now in sendTestNotification(#%d, "%s")', $event->user->id, $event->channel));
2022-12-29 19:41:57 +01:00
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
if (!$repository->hasRole($event->user, 'owner')) {
2024-12-08 16:28:22 +01:00
Log::error(sprintf('User #%d is not an owner.', $event->user->id));
2022-12-29 19:41:57 +01:00
return;
}
2024-12-08 16:28:22 +01:00
switch($event->channel) {
case 'email':
$class = TestNotificationEmail::class;
break;
case 'slack':
$class = TestNotificationSlack::class;
break;
case 'discord':
$class = TestNotificationDiscord::class;
break;
default:
app('log')->error(sprintf('Unknown channel "%s" in sendTestNotification method.', $event->channel));
return;
}
Log::debug(sprintf('Will send %s as a notification.', $class));
2023-12-20 19:35:52 +01:00
2023-06-11 16:12:13 +02:00
try {
2024-12-08 16:28:22 +01:00
Notification::send($event->user, new $class($event->user->email));
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
}
2024-12-08 16:28:22 +01:00
Log::debug(sprintf('If you see no errors above this line, test notification was sent over channel "%s"', $event->channel));
2022-12-29 19:41:57 +01:00
}
2017-11-08 09:05:10 +01:00
}