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

149 lines
5.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;
2023-06-11 16:12:13 +02:00
use Exception;
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;
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;
2018-03-30 22:40:20 +02:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2022-09-24 11:41:07 +02:00
use FireflyIII\Support\Facades\FireflyConfig;
2023-06-11 16:12:13 +02: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
{
/**
2023-06-21 12:34:58 +02:00
* @param InvitationCreated $event
2023-07-15 16:02:42 +02:00
*
2022-10-01 12:21:42 +02:00
* @return void
*/
public function sendInvitationNotification(InvitationCreated $event): void
{
$sendMail = FireflyConfig::get('notification_invite_created', true)->data;
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));
} 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-10-01 12:21:42 +02:00
}
}
}
2022-09-24 11:41:07 +02:00
/**
* Send new version message to admin.
*
2023-06-21 12:34:58 +02:00
* @param NewVersionAvailable $event
2023-07-15 16:02:42 +02:00
*
2022-09-24 11:41:07 +02:00
* @return void
*/
public function sendNewVersion(NewVersionAvailable $event): void
{
$sendMail = FireflyConfig::get('notification_new_version', true)->data;
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));
} 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 11:41:07 +02:00
}
}
}
2022-12-29 19:41:57 +01:00
/**
* Sends a test message to an administrator.
*
2023-06-21 12:34:58 +02:00
* @param AdminRequestedTestMessage $event
2022-12-29 19:41:57 +01:00
*
* @return void
*/
public function sendTestMessage(AdminRequestedTestMessage $event): void
{
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
if (!$repository->hasRole($event->user, 'owner')) {
return;
}
2023-06-11 16:12:13 +02:00
try {
Notification::send($event->user, new TestNotification($event->user->email));
} 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 19:41:57 +01:00
}
2017-11-08 09:05:10 +01:00
}