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

147 lines
5.4 KiB
PHP
Raw Normal View History

2017-09-27 08:45:55 -05:00
<?php
2022-12-29 12:41:57 -06:00
2017-09-27 08:45:55 -05:00
/**
* AdminEventHandler.php
2020-01-28 01:45:38 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2017-09-27 08:45:55 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-10-21 01:40:00 -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/>.
2017-09-27 08:45:55 -05:00
*/
declare(strict_types=1);
namespace FireflyIII\Handlers\Events;
2023-06-11 09:12:13 -05:00
use Exception;
2022-10-01 05:21:42 -05:00
use FireflyIII\Events\Admin\InvitationCreated;
2017-09-27 08:45:55 -05:00
use FireflyIII\Events\AdminRequestedTestMessage;
2022-09-24 04:41:07 -05:00
use FireflyIII\Events\NewVersionAvailable;
2022-09-18 08:48:28 -05:00
use FireflyIII\Notifications\Admin\TestNotification;
2022-10-01 05:21:42 -05:00
use FireflyIII\Notifications\Admin\UserInvitation;
2022-09-24 04:41:07 -05:00
use FireflyIII\Notifications\Admin\VersionCheckResult;
2018-03-30 15:40:20 -05:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2022-09-24 04:41:07 -05:00
use FireflyIII\Support\Facades\FireflyConfig;
2023-06-11 09:12:13 -05:00
use Illuminate\Support\Facades\Log;
2022-09-18 08:48:28 -05:00
use Illuminate\Support\Facades\Notification;
2017-09-27 08:45:55 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class AdminEventHandler.
2017-09-27 08:45:55 -05:00
*/
class AdminEventHandler
{
/**
2022-12-29 12:41:57 -06:00
* @param InvitationCreated $event
2022-10-01 05:21:42 -05: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 09:12:13 -05: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 05:21:42 -05:00
}
}
}
2022-09-24 04:41:07 -05:00
/**
* Send new version message to admin.
*
2022-12-29 12:41:57 -06:00
* @param NewVersionAvailable $event
2022-09-24 04:41:07 -05: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 09:12:13 -05: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 04:41:07 -05:00
}
}
}
2022-12-29 12:41:57 -06:00
/**
* Sends a test message to an administrator.
*
* @param AdminRequestedTestMessage $event
*
* @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 09:12:13 -05: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 12:41:57 -06:00
}
2017-11-08 02:05:10 -06:00
}