Fix all test channels for user and owner.

This commit is contained in:
James Cole
2024-12-14 06:30:27 +01:00
parent 7d8d773f8f
commit b976239580
23 changed files with 720 additions and 95 deletions

View File

@@ -27,7 +27,7 @@ namespace FireflyIII\Events\Test;
use FireflyIII\Notifications\Notifiables\OwnerNotifiable; use FireflyIII\Notifications\Notifiables\OwnerNotifiable;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
class TestNotificationChannel class OwnerTestNotificationChannel
{ {
use SerializesModels; use SerializesModels;
@@ -39,7 +39,7 @@ class TestNotificationChannel
*/ */
public function __construct(string $channel, OwnerNotifiable $owner) public function __construct(string $channel, OwnerNotifiable $owner)
{ {
app('log')->debug(sprintf('Triggered TestNotificationChannel("%s")', $channel)); app('log')->debug(sprintf('Triggered OwnerTestNotificationChannel("%s")', $channel));
$this->owner = $owner; $this->owner = $owner;
$this->channel = $channel; $this->channel = $channel;
} }

View File

@@ -0,0 +1,46 @@
<?php
/*
* TestEmailChannel.php
* Copyright (c) 2024 james@firefly-iii.org.
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* 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.
*
* 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/.
*/
declare(strict_types=1);
namespace FireflyIII\Events\Test;
use FireflyIII\User;
use Illuminate\Queue\SerializesModels;
class UserTestNotificationChannel
{
use SerializesModels;
public User $user;
public string $channel;
/**
* Create a new event instance.
*/
public function __construct(string $channel, User $user)
{
app('log')->debug(sprintf('Triggered UserTestNotificationChannel("%s")', $channel));
$this->user = $user;
$this->channel = $channel;
}
}

View File

@@ -26,15 +26,15 @@ namespace FireflyIII\Handlers\Events;
use FireflyIII\Events\Admin\InvitationCreated; use FireflyIII\Events\Admin\InvitationCreated;
use FireflyIII\Events\NewVersionAvailable; use FireflyIII\Events\NewVersionAvailable;
use FireflyIII\Events\Security\UnknownUserAttemptedLogin; use FireflyIII\Events\Security\UnknownUserAttemptedLogin;
use FireflyIII\Events\Test\TestNotificationChannel; use FireflyIII\Events\Test\OwnerTestNotificationChannel;
use FireflyIII\Notifications\Admin\UnknownUserLoginAttempt; use FireflyIII\Notifications\Admin\UnknownUserLoginAttempt;
use FireflyIII\Notifications\Admin\UserInvitation; use FireflyIII\Notifications\Admin\UserInvitation;
use FireflyIII\Notifications\Admin\VersionCheckResult; use FireflyIII\Notifications\Admin\VersionCheckResult;
use FireflyIII\Notifications\Notifiables\OwnerNotifiable; use FireflyIII\Notifications\Notifiables\OwnerNotifiable;
use FireflyIII\Notifications\Test\TestNotificationEmail; use FireflyIII\Notifications\Test\OwnerTestNotificationEmail;
use FireflyIII\Notifications\Test\TestNotificationNtfy; use FireflyIII\Notifications\Test\OwnerTestNotificationNtfy;
use FireflyIII\Notifications\Test\TestNotificationPushover; use FireflyIII\Notifications\Test\OwnerTestNotificationPushover;
use FireflyIII\Notifications\Test\TestNotificationSlack; use FireflyIII\Notifications\Test\OwnerTestNotificationSlack;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification; use Illuminate\Support\Facades\Notification;
@@ -125,28 +125,28 @@ class AdminEventHandler
/** /**
* Sends a test message to an administrator. * Sends a test message to an administrator.
*/ */
public function sendTestNotification(TestNotificationChannel $event): void public function sendTestNotification(OwnerTestNotificationChannel $event): void
{ {
Log::debug(sprintf('Now in sendTestNotification("%s")', $event->channel)); Log::debug(sprintf('Now in sendTestNotification("%s")', $event->channel));
switch ($event->channel) { switch ($event->channel) {
case 'email': case 'email':
$class = TestNotificationEmail::class; $class = OwnerTestNotificationEmail::class;
break; break;
case 'slack': case 'slack':
$class = TestNotificationSlack::class; $class = OwnerTestNotificationSlack::class;
break; break;
case 'ntfy': case 'ntfy':
$class = TestNotificationNtfy::class; $class = OwnerTestNotificationNtfy::class;
break; break;
case 'pushover': case 'pushover':
$class = TestNotificationPushover::class; $class = OwnerTestNotificationPushover::class;
break; break;

View File

@@ -31,6 +31,8 @@ use FireflyIII\Events\Admin\InvitationCreated;
use FireflyIII\Events\DetectedNewIPAddress; use FireflyIII\Events\DetectedNewIPAddress;
use FireflyIII\Events\RegisteredUser; use FireflyIII\Events\RegisteredUser;
use FireflyIII\Events\RequestedNewPassword; use FireflyIII\Events\RequestedNewPassword;
use FireflyIII\Events\Test\OwnerTestNotificationChannel;
use FireflyIII\Events\Test\UserTestNotificationChannel;
use FireflyIII\Events\UserChangedEmail; use FireflyIII\Events\UserChangedEmail;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Mail\ConfirmEmailChangeMail; use FireflyIII\Mail\ConfirmEmailChangeMail;
@@ -40,12 +42,21 @@ use FireflyIII\Models\GroupMembership;
use FireflyIII\Models\UserGroup; use FireflyIII\Models\UserGroup;
use FireflyIII\Models\UserRole; use FireflyIII\Models\UserRole;
use FireflyIII\Notifications\Admin\UserRegistration as AdminRegistrationNotification; use FireflyIII\Notifications\Admin\UserRegistration as AdminRegistrationNotification;
use FireflyIII\Notifications\Test\OwnerTestNotificationEmail;
use FireflyIII\Notifications\Test\OwnerTestNotificationNtfy;
use FireflyIII\Notifications\Test\OwnerTestNotificationPushover;
use FireflyIII\Notifications\Test\OwnerTestNotificationSlack;
use FireflyIII\Notifications\Test\UserTestNotificationEmail;
use FireflyIII\Notifications\Test\UserTestNotificationNtfy;
use FireflyIII\Notifications\Test\UserTestNotificationPushover;
use FireflyIII\Notifications\Test\UserTestNotificationSlack;
use FireflyIII\Notifications\User\UserLogin; use FireflyIII\Notifications\User\UserLogin;
use FireflyIII\Notifications\User\UserNewPassword; use FireflyIII\Notifications\User\UserNewPassword;
use FireflyIII\Notifications\User\UserRegistration as UserRegistrationNotification; use FireflyIII\Notifications\User\UserRegistration as UserRegistrationNotification;
use FireflyIII\Repositories\User\UserRepositoryInterface; use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Auth\Events\Login; use Illuminate\Auth\Events\Login;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification; use Illuminate\Support\Facades\Notification;
use Mail; use Mail;
@@ -423,4 +434,59 @@ class UserEventHandler
event(new DetectedNewIPAddress($user, $ip)); event(new DetectedNewIPAddress($user, $ip));
} }
} }
/**
* Sends a test message to an administrator.
*/
public function sendTestNotification(UserTestNotificationChannel $event): void
{
Log::debug(sprintf('Now in (user) sendTestNotification("%s")', $event->channel));
switch ($event->channel) {
case 'email':
$class = UserTestNotificationEmail::class;
break;
case 'slack':
$class = UserTestNotificationSlack::class;
break;
case 'ntfy':
$class = UserTestNotificationNtfy::class;
break;
case 'pushover':
$class = UserTestNotificationPushover::class;
break;
default:
app('log')->error(sprintf('Unknown channel "%s" in (user) sendTestNotification method.', $event->channel));
return;
}
Log::debug(sprintf('Will send %s as a notification.', $class));
try {
Notification::send($event->user, new $class($event->user));
} catch (\Exception $e) { // @phpstan-ignore-line
$message = $e->getMessage();
if (str_contains($message, 'Bcc')) {
app('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')) {
app('log')->warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
}
Log::debug(sprintf('If you see no errors above this line, test notification was sent over channel "%s"', $event->channel));
}
} }

View File

@@ -24,7 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Admin; namespace FireflyIII\Http\Controllers\Admin;
use FireflyIII\Events\Test\TestNotificationChannel; use FireflyIII\Events\Test\OwnerTestNotificationChannel;
use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\NotificationRequest; use FireflyIII\Http\Requests\NotificationRequest;
use FireflyIII\Notifications\Notifiables\OwnerNotifiable; use FireflyIII\Notifications\Notifiables\OwnerNotifiable;
@@ -135,7 +135,7 @@ class NotificationController extends Controller
case 'ntfy': case 'ntfy':
$owner = new OwnerNotifiable(); $owner = new OwnerNotifiable();
app('log')->debug(sprintf('Now in testNotification("%s") controller.', $channel)); app('log')->debug(sprintf('Now in testNotification("%s") controller.', $channel));
event(new TestNotificationChannel($channel, $owner)); event(new OwnerTestNotificationChannel($channel, $owner));
session()->flash('success', (string) trans('firefly.notification_test_executed', ['channel' => $channel])); session()->flash('success', (string) trans('firefly.notification_test_executed', ['channel' => $channel]));
} }

View File

@@ -23,12 +23,15 @@ declare(strict_types=1);
namespace FireflyIII\Http\Controllers; namespace FireflyIII\Http\Controllers;
use FireflyIII\Events\Test\UserTestNotificationChannel;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Requests\PreferencesRequest;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType; use FireflyIII\Models\AccountType;
use FireflyIII\Models\Preference; use FireflyIII\Models\Preference;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Support\Notifications\UrlValidator; use FireflyIII\Support\Notifications\UrlValidator;
use FireflyIII\User;
use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@@ -110,13 +113,13 @@ class PreferencesController extends Controller
// notifications settings // notifications settings
$slackUrl = app('preferences')->getEncrypted('slack_webhook_url', '')->data; $slackUrl = app('preferences')->getEncrypted('slack_webhook_url', '')->data;
$pushoverAppToken = app('preferences')->getEncrypted('pushover_app_token', '')->data; $pushoverAppToken = (string) app('preferences')->getEncrypted('pushover_app_token', '')->data;
$pushoverUserToken = app('preferences')->getEncrypted('pushover_user_token', '')->data; $pushoverUserToken = (string) app('preferences')->getEncrypted('pushover_user_token', '')->data;
$ntfyServer = app('preferences')->getEncrypted('ntfy_server', 'https://ntfy.sh')->data; $ntfyServer = app('preferences')->getEncrypted('ntfy_server', 'https://ntfy.sh')->data;
$ntfyTopic = app('preferences')->getEncrypted('ntfy_topic', '')->data; $ntfyTopic = (string) app('preferences')->getEncrypted('ntfy_topic', '')->data;
$ntfyAuth = app('preferences')->get('ntfy_auth', false)->data; $ntfyAuth = app('preferences')->get('ntfy_auth', false)->data;
$ntfyUser = app('preferences')->getEncrypted('ntfy_user', '')->data; $ntfyUser = app('preferences')->getEncrypted('ntfy_user', '')->data;
$ntfyPass = app('preferences')->getEncrypted('ntfy_pass', '')->data; $ntfyPass = (string) app('preferences')->getEncrypted('ntfy_pass', '')->data;
$channels = config('notifications.channels'); $channels = config('notifications.channels');
$forcedAvailability = []; $forcedAvailability = [];
@@ -175,6 +178,7 @@ class PreferencesController extends Controller
'ntfyServer', 'ntfyServer',
'ntfyTopic', 'ntfyTopic',
'ntfyAuth', 'ntfyAuth',
'channels',
'ntfyUser', 'ntfyUser',
'forcedAvailability', 'forcedAvailability',
'ntfyPass', 'ntfyPass',
@@ -206,7 +210,7 @@ class PreferencesController extends Controller
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.NPathComplexity)
*/ */
public function postIndex(Request $request) public function postIndex(PreferencesRequest $request)
{ {
// front page accounts // front page accounts
$frontpageAccounts = []; $frontpageAccounts = [];
@@ -219,10 +223,8 @@ class PreferencesController extends Controller
// extract notifications: // extract notifications:
$all = $request->all(); $all = $request->all();
foreach (config('notifications.notifications.user') as $key => $info) {
exit('fix the reference to the available notifications.'); $key = sprintf('notification_%s', $key);
foreach (config('firefly.available_notifications') as $option) {
$key = sprintf('notification_%s', $option);
if (array_key_exists($key, $all)) { if (array_key_exists($key, $all)) {
app('preferences')->set($key, true); app('preferences')->set($key, true);
} }
@@ -238,16 +240,20 @@ class PreferencesController extends Controller
session()->forget('end'); session()->forget('end');
session()->forget('range'); session()->forget('range');
// slack URL: // notification settings, cannot be set by the demo user.
if (!auth()->user()->hasRole('demo')) { if (!auth()->user()->hasRole('demo')) {
$url = (string) $request->get('slackUrl');
if (UrlValidator::isValidWebhookURL($url)) { $variables = ['slack_webhook_url', 'pushover_app_token', 'pushover_user_token', 'ntfy_server', 'ntfy_topic', 'ntfy_user', 'ntfy_pass'];
app('preferences')->set('slack_webhook_url', $url); foreach ($variables as $variable) {
if ('' === $all[$variable]) {
app('preferences')->delete($variable);
} }
if ('' === $url) { if ('' !== $all[$variable]) {
app('preferences')->delete('slack_webhook_url'); app('preferences')->setEncrypted($variable, $all[$variable]);
} }
} }
app('preferences')->set('ntfy_auth', $all['ntfy_auth'] ?? false);
}
// custom fiscal year // custom fiscal year
$customFiscalYear = 1 === (int) $request->get('customFiscalYear'); $customFiscalYear = 1 === (int) $request->get('customFiscalYear');
@@ -313,4 +319,29 @@ class PreferencesController extends Controller
return redirect(route('preferences.index')); return redirect(route('preferences.index'));
} }
public function testNotification(Request $request): mixed
{
$all = $request->all();
$channel = $all['channel'] ?? '';
switch ($channel) {
default:
session()->flash('error', (string) trans('firefly.notification_test_failed', ['channel' => $channel]));
break;
case 'email':
case 'slack':
case 'pushover':
case 'ntfy':
/** @var User $user */
$user = auth()->user();
app('log')->debug(sprintf('Now in testNotification("%s") controller.', $channel));
event(new UserTestNotificationChannel($channel, $user));
session()->flash('success', (string) trans('firefly.notification_test_executed', ['channel' => $channel]));
}
return '';
}
} }

View File

@@ -0,0 +1,52 @@
<?php
/*
* PreferencesRequest.php
* Copyright (c) 2024 james@firefly-iii.org.
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* 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.
*
* 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/.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Requests;
use FireflyIII\Rules\Admin\IsValidSlackOrDiscordUrl;
use FireflyIII\Support\Request\ChecksLogin;
use Illuminate\Foundation\Http\FormRequest;
class PreferencesRequest extends FormRequest
{
use ChecksLogin;
/**
* Rules for this request.
*/
public function rules(): array
{
$rules = [
'slack_webhook_url' => ['nullable', 'url', 'min:1', new IsValidSlackOrDiscordUrl()],
'ntfy_server' => ['nullable', 'url', 'min:1'],
'ntfy_user' => ['required_with:ntfy_pass,ntfy_auth', 'nullable', 'string', 'min:1'],
'ntfy_pass' => ['required_with:ntfy_user,ntfy_auth', 'nullable', 'string', 'min:1'],
];
foreach (config('notifications.notifications.user') as $key => $info) {
$rules[sprintf('notification_%s', $key)] = 'in:0,1';
}
return $rules;
}
}

View File

@@ -26,6 +26,7 @@ namespace FireflyIII\Notifications;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Support\Facades\FireflyConfig; use FireflyIII\Support\Facades\FireflyConfig;
use FireflyIII\Support\Facades\Preferences;
use FireflyIII\User; use FireflyIII\User;
class ReturnsSettings class ReturnsSettings
@@ -49,6 +50,13 @@ class ReturnsSettings
'ntfy_pass' => '', 'ntfy_pass' => '',
]; ];
if('user' === $type && null !== $user) {
$settings['ntfy_server'] = Preferences::getEncryptedForUser($user, 'ntfy_server', 'https://ntfy.sh')->data;
$settings['ntfy_topic'] = Preferences::getEncryptedForUser($user, 'ntfy_topic', '')->data;
$settings['ntfy_auth'] = Preferences::getForUser($user, 'ntfy_auth', false)->data;
$settings['ntfy_user'] = Preferences::getEncryptedForUser($user, 'ntfy_user', '')->data;
$settings['ntfy_pass'] = Preferences::getEncryptedForUser($user, 'ntfy_pass', '')->data;
}
if ('owner' === $type) { if ('owner' === $type) {
$settings['ntfy_server'] = FireflyConfig::getEncrypted('ntfy_server', 'https://ntfy.sh')->data; $settings['ntfy_server'] = FireflyConfig::getEncrypted('ntfy_server', 'https://ntfy.sh')->data;
$settings['ntfy_topic'] = FireflyConfig::getEncrypted('ntfy_topic', '')->data; $settings['ntfy_topic'] = FireflyConfig::getEncrypted('ntfy_topic', '')->data;

View File

@@ -32,7 +32,7 @@ use Illuminate\Notifications\Notification;
/** /**
* Class TestNotification * Class TestNotification
*/ */
class TestNotificationEmail extends Notification class OwnerTestNotificationEmail extends Notification
{ {
use Queueable; use Queueable;

View File

@@ -36,7 +36,7 @@ use Wijourdil\NtfyNotificationChannel\Channels\NtfyChannel;
/** /**
* Class TestNotification * Class TestNotification
*/ */
class TestNotificationNtfy extends Notification class OwnerTestNotificationNtfy extends Notification
{ {
use Queueable; use Queueable;

View File

@@ -36,7 +36,7 @@ use NotificationChannels\Pushover\PushoverMessage;
/** /**
* Class TestNotification * Class TestNotification
*/ */
class TestNotificationPushover extends Notification class OwnerTestNotificationPushover extends Notification
{ {
use Queueable; use Queueable;

View File

@@ -0,0 +1,86 @@
<?php
/*
* TestNotification.php
* Copyright (c) 2022 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* 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.
*
* 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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Notifications\Test;
use FireflyIII\Notifications\Notifiables\OwnerNotifiable;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
// use Illuminate\Notifications\Slack\SlackMessage;
/**
* Class TestNotification
*/
class OwnerTestNotificationSlack extends Notification
{
use Queueable;
private OwnerNotifiable $owner;
/**
* Create a new notification instance.
*/
public function __construct(OwnerNotifiable $owner)
{
$this->owner = $owner;
}
/**
* Get the array representation of the notification.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* @return array
*/
public function toArray(OwnerNotifiable $notifiable)
{
return [
];
}
/**
* Get the Slack representation of the notification.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function toSlack(OwnerNotifiable $notifiable)
{
return new SlackMessage()->content((string) trans('email.admin_test_subject'));
// return new SlackMessage()->text((string) trans('email.admin_test_subject'))->to($url);
}
/**
* Get the notification's delivery channels.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* @return array
*/
public function via(OwnerNotifiable $notifiable)
{
return ['slack'];
}
}

View File

@@ -0,0 +1,95 @@
<?php
/*
* TestNotification.php
* Copyright (c) 2022 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* 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.
*
* 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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Notifications\Test;
use FireflyIII\Notifications\Notifiables\OwnerNotifiable;
use FireflyIII\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
/**
* Class TestNotification
*/
class UserTestNotificationEmail extends Notification
{
use Queueable;
private User $user;
/**
* Create a new notification instance.
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Get the array representation of the notification.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* @return array
*/
public function toArray(User $notifiable)
{
return [
];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* @return MailMessage
*/
public function toMail(User $notifiable)
{
$address = (string) $notifiable->email;
return (new MailMessage())
->markdown('emails.admin-test', ['email' => $address])
->subject((string) trans('email.admin_test_subject'))
;
}
/**
* Get the notification's delivery channels.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* @param User $notifiable
*
* @return array
*/
public function via(User $notifiable)
{
return ['mail'];
}
}

View File

@@ -0,0 +1,99 @@
<?php
/*
* TestNotification.php
* Copyright (c) 2022 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* 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.
*
* 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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Notifications\Test;
use FireflyIII\Notifications\ReturnsSettings;
use FireflyIII\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Ntfy\Message;
use Wijourdil\NtfyNotificationChannel\Channels\NtfyChannel;
// use Illuminate\Notifications\Slack\SlackMessage;
/**
* Class TestNotification
*/
class UserTestNotificationNtfy extends Notification
{
use Queueable;
public User $user;
/**
* Create a new notification instance.
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Get the array representation of the notification.
*
* @param User $notifiable
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* @return array
*/
public function toArray(User $notifiable)
{
return [
];
}
public function toNtfy(User $user): Message
{
$settings = ReturnsSettings::getSettings('ntfy', 'user', $user);
// overrule config.
config(['ntfy-notification-channel.server' => $settings['ntfy_server']]);
config(['ntfy-notification-channel.topic' => $settings['ntfy_topic']]);
if ($settings['ntfy_auth']) {
// overrule auth as well.
config(['ntfy-notification-channel.authentication.enabled' => true]);
config(['ntfy-notification-channel.authentication.username' => $settings['ntfy_user']]);
config(['ntfy-notification-channel.authentication.password' => $settings['ntfy_pass']]);
}
$message = new Message();
$message->topic($settings['ntfy_topic']);
$message->title((string) trans('email.admin_test_subject'));
$message->body((string) trans('email.admin_test_message', ['channel' => 'ntfy']));
$message->tags(['white_check_mark']);
return $message;
}
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function via(User $user)
{
return [NtfyChannel::class];
}
}

View File

@@ -0,0 +1,83 @@
<?php
/*
* TestNotification.php
* Copyright (c) 2022 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* 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.
*
* 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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Notifications\Test;
use FireflyIII\Notifications\Notifiables\OwnerNotifiable;
use FireflyIII\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Log;
use NotificationChannels\Pushover\PushoverChannel;
use NotificationChannels\Pushover\PushoverMessage;
// use Illuminate\Notifications\Slack\SlackMessage;
/**
* Class TestNotification
*/
class UserTestNotificationPushover extends Notification
{
use Queueable;
private User $user;
/**
* Create a new notification instance.
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Get the array representation of the notification.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* @return array
*/
public function toArray(User $notifiable)
{
return [
];
}
public function toPushover(User $notifiable): PushoverMessage
{
Log::debug('Now in (user) toPushover()');
return PushoverMessage::create((string)trans('email.admin_test_message', ['channel' => 'Pushover']))
->title((string)trans('email.admin_test_subject'))
;
}
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function via(User $notifiable)
{
return [PushoverChannel::class];
}
}

View File

@@ -34,7 +34,7 @@ use Illuminate\Notifications\Notification;
/** /**
* Class TestNotification * Class TestNotification
*/ */
class TestNotificationSlack extends Notification class UserTestNotificationSlack extends Notification
{ {
use Queueable; use Queueable;

View File

@@ -49,7 +49,8 @@ use FireflyIII\Events\Security\MFAUsedBackupCode;
use FireflyIII\Events\Security\UnknownUserAttemptedLogin; use FireflyIII\Events\Security\UnknownUserAttemptedLogin;
use FireflyIII\Events\StoredAccount; use FireflyIII\Events\StoredAccount;
use FireflyIII\Events\StoredTransactionGroup; use FireflyIII\Events\StoredTransactionGroup;
use FireflyIII\Events\Test\TestNotificationChannel; use FireflyIII\Events\Test\OwnerTestNotificationChannel;
use FireflyIII\Events\Test\UserTestNotificationChannel;
use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Events\TriggeredAuditLog;
use FireflyIII\Events\UpdatedAccount; use FireflyIII\Events\UpdatedAccount;
use FireflyIII\Events\UpdatedTransactionGroup; use FireflyIII\Events\UpdatedTransactionGroup;
@@ -130,13 +131,16 @@ class EventServiceProvider extends ServiceProvider
RequestedNewPassword::class => [ RequestedNewPassword::class => [
'FireflyIII\Handlers\Events\UserEventHandler@sendNewPassword', 'FireflyIII\Handlers\Events\UserEventHandler@sendNewPassword',
], ],
UserTestNotificationChannel::class => [
'FireflyIII\Handlers\Events\UserEventHandler@sendTestNotification',
],
// is a User related event. // is a User related event.
UserChangedEmail::class => [ UserChangedEmail::class => [
'FireflyIII\Handlers\Events\UserEventHandler@sendEmailChangeConfirmMail', 'FireflyIII\Handlers\Events\UserEventHandler@sendEmailChangeConfirmMail',
'FireflyIII\Handlers\Events\UserEventHandler@sendEmailChangeUndoMail', 'FireflyIII\Handlers\Events\UserEventHandler@sendEmailChangeUndoMail',
], ],
// admin related // admin related
TestNotificationChannel::class => [ OwnerTestNotificationChannel::class => [
'FireflyIII\Handlers\Events\AdminEventHandler@sendTestNotification', 'FireflyIII\Handlers\Events\AdminEventHandler@sendTestNotification',
], ],
NewVersionAvailable::class => [ NewVersionAvailable::class => [

View File

@@ -105,6 +105,26 @@ class Preferences
return $this->getForUser($user, $name, $default); return $this->getForUser($user, $name, $default);
} }
public function getEncryptedForUser(User $user, string $name, null|array|bool|int|string $default = null): ?Preference
{
$result = $this->getForUser($user, $name, $default);
if ('' === $result->data) {
Log::warning(sprintf('Empty encrypted preference found: "%s"', $name));
return $result;
}
try {
$result->data = decrypt($result->data);
} catch (DecryptException $e) {
Log::error(sprintf('Could not decrypt preference "%s": %s', $name, $e->getMessage()));
return $result;
}
return $result;
}
public function getForUser(User $user, string $name, null|array|bool|int|string $default = null): ?Preference public function getForUser(User $user, string $name, null|array|bool|int|string $default = null): ?Preference
{ {

View File

@@ -392,22 +392,12 @@ class User extends Authenticatable
return $this->belongsToMany(Role::class); return $this->belongsToMany(Role::class);
} }
public function routeNotificationForPushover(Notification $notification) public function routeNotificationForPushover()
{ {
// this check does not validate if the user is owner, Should be done by notification itself. $appToken = (string) app('preferences')->getEncrypted('pushover_app_token', '')->data;
$appToken = (string) app('fireflyconfig')->getEncrypted('pushover_app_token', '')->data; $userToken = (string) app('preferences')->getEncrypted('pushover_user_token', '')->data;
$userToken = (string) app('fireflyconfig')->getEncrypted('pushover_user_token', '')->data;
if (property_exists($notification, 'type') && 'owner' === $notification->type) { return PushoverReceiver::withUserKey($userToken)->withApplicationToken($appToken);
return PushoverReceiver::withUserKey($userToken)
->withApplicationToken($appToken)
;
}
throw new FireflyException('No pushover token found.');
// return PushoverReceiver::withUserKey((string) config('services.pushover.user_token'))
// ->withApplicationToken((string) config('services.pushover.token'));
// return (string) config('services.pushover.token');
} }
/** /**

View File

@@ -23,10 +23,19 @@
$(document).ready(function () { $(document).ready(function () {
"use strict"; "use strict";
if (!Modernizr.inputtypes.date) { if (!Modernizr.inputtypes.date) {
$('input[type="date"]').datepicker( $('input[type="date"]').datepicker({
{
dateFormat: 'yy-mm-dd' dateFormat: 'yy-mm-dd'
}
);
}
}); });
}
$('.submit-test').click(submitTest);
});
function submitTest(e) {
var current = $(e.currentTarget);
var channel = current.data('channel');
$.post(postUrl, {channel: channel}, function () {
window.location.reload(true);
});
return false;
}

View File

@@ -1290,6 +1290,7 @@ return [
'create_recurring_from_transaction' => 'Create recurring transaction based on transaction', 'create_recurring_from_transaction' => 'Create recurring transaction based on transaction',
// preferences // preferences
'test_notifications_buttons' => 'To test your configuration, use the buttons below. Please note that the buttons have no spam control.',
'dark_mode_option_browser' => 'Let your browser decide', 'dark_mode_option_browser' => 'Let your browser decide',
'dark_mode_option_light' => 'Always light', 'dark_mode_option_light' => 'Always light',
'dark_mode_option_dark' => 'Always dark', 'dark_mode_option_dark' => 'Always dark',
@@ -1388,7 +1389,7 @@ return [
'pref_notifications' => 'Notifications', 'pref_notifications' => 'Notifications',
'pref_notifications_help' => 'Indicate if these are notifications you would like to get. Some notifications may contain sensitive financial information.', 'pref_notifications_help' => 'Indicate if these are notifications you would like to get. Some notifications may contain sensitive financial information.',
'pref_notifications_settings' => 'Notifications settings', 'pref_notifications_settings' => 'Notifications settings',
'pref_notifications_settings_help' => 'Use these settings to configure your notification channels. Please note that notifications will be sent to ALL channels.', 'pref_notifications_settings_help' => 'Use these settings to configure your notification channels. Please note that notifications will be sent to ALL channels. Please save your settings FIRST.',
'slack_url_label' => 'Slack "incoming webhook" URL', 'slack_url_label' => 'Slack "incoming webhook" URL',
'discord_url_label' => 'Discord webhook URL', 'discord_url_label' => 'Discord webhook URL',
@@ -2481,7 +2482,7 @@ return [
'delete_user' => 'Delete user :email', 'delete_user' => 'Delete user :email',
'user_deleted' => 'The user has been deleted', 'user_deleted' => 'The user has been deleted',
'send_test_email' => 'Send test email message', 'send_test_email' => 'Send test email message',
'send_test_email_text' => 'To see if your installation is capable of sending email or posting Slack messages, please press this button. You will not see an error here (if any), <strong>the log files will reflect any errors</strong>. You can press this button as many times as you like. There is no spam control. The message will be sent to <code>:email</code> and should arrive shortly.', 'send_test_email_text' => 'To see if your installation is capable of sending a notification, please press this button. You will not see an error here (if any), <strong>the log files will reflect any errors</strong>. You can press this button as many times as you like. There is no spam control. The message will be sent to <code>:email</code> and should arrive shortly.',
'send_message' => 'Send message', 'send_message' => 'Send message',
'send_test_triggered' => 'Test was triggered. Check your inbox and the log files.', 'send_test_triggered' => 'Test was triggered. Check your inbox and the log files.',
'give_admin_careful' => 'Users who are given admin rights can take away yours. Be careful.', 'give_admin_careful' => 'Users who are given admin rights can take away yours. Be careful.',
@@ -2514,7 +2515,7 @@ return [
'notification_channel_name_pushover' => 'Pushover', 'notification_channel_name_pushover' => 'Pushover',
'notification_channel_name_gotify' => 'Gotify', 'notification_channel_name_gotify' => 'Gotify',
'notification_channel_name_pushbullet' => 'Pushbullet', 'notification_channel_name_pushbullet' => 'Pushbullet',
'channel_not_available' => 'not available yet', 'channel_not_available' => 'not available',
'configure_channel_in_env' => 'needs environment variables', 'configure_channel_in_env' => 'needs environment variables',
'test_notification_channel_name_email' => 'Test email', 'test_notification_channel_name_email' => 'Test email',
'test_notification_channel_name_slack' => 'Test Slack', 'test_notification_channel_name_slack' => 'Test Slack',

View File

@@ -307,13 +307,33 @@
{# view range #} {# view range #}
<div class="preferences-box"> <div class="preferences-box">
<h3>{{ 'pref_notifications'|_ }}</h3> <h3>{{ 'pref_notifications'|_ }}</h3>
<p class="text-info">{{ 'pref_notifications_help'|_ }}</p> <p>
{{ 'available_channels_expl'|_ }}
</p>
<ul>
{% for name,info in channels %}
<li>
{% if true == info.enabled and true == forcedAvailability[name] %}
☑️ {{ trans('firefly.notification_channel_name_'~name) }}
{% if 0 == info.ui_configurable %}({{ 'configure_channel_in_env'|_ }}) {% endif %}
{% endif %}
{% if false == info.enabled or false == forcedAvailability[name] %}
⚠️ {{ trans('firefly.notification_channel_name_'~name) }} ({{ 'channel_not_available'|_ }})
{% endif %}
</li>
{% endfor %}
</ul>
<p>{{ 'pref_notifications_help'|_ }}</p>
{% for id, info in notifications %} {% for id, info in notifications %}
<div class="form-group"> <div class="form-group">
<div class="col-sm-10"> <div class="col-sm-10">
<div class="checkbox"> <div class="checkbox">
<label> <label>
<input {% if not info.configurable %}disabled{% endif %} type="checkbox" name="notification_{{ id }}" {{ info.enabled == true ? 'checked' : '' }} value="1"> {% if info.configurable %}
<input {% if not info.configurable %}readonly{% endif %} type="checkbox" name="notification_{{ id }}" {{ info.enabled == true ? 'checked' : '' }} value="1">
{% else %}
<input readonly disabled type="checkbox" checked value="1">
{% endif %}
{{ trans('firefly.pref_notification_' ~ id) }} {{ trans('firefly.pref_notification_' ~ id) }}
</label> </label>
</div> </div>
@@ -325,8 +345,8 @@
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12"> <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<div class="preferences-box"> <div class="preferences-box">
<h3>{{ 'pref_notifications_settings'|_ }}</h3> <h3>{{ 'pref_notifications_settings'|_ }}</h3>
<p class="text-info">{{ 'pref_notifications_settings_help'|_ }}</p> <p>{{ 'pref_notifications_settings_help'|_ }}</p>
{{ ExpandedForm.text('slackUrl',slackUrl,{'label' : 'slack_url_label'|_, helpText: 'slack_discord_double'|_}) }} {{ ExpandedForm.text('slack_webhook_url',slackUrl,{'label' : 'slack_url_label'|_, helpText: 'slack_discord_double'|_}) }}
{{ ExpandedForm.text('pushover_app_token', pushoverAppToken, {}) }} {{ ExpandedForm.text('pushover_app_token', pushoverAppToken, {}) }}
{{ ExpandedForm.text('pushover_user_token', pushoverUserToken, {}) }} {{ ExpandedForm.text('pushover_user_token', pushoverUserToken, {}) }}
@@ -336,7 +356,18 @@
{{ ExpandedForm.checkbox('ntfy_auth','1', ntfyAuth, {}) }} {{ ExpandedForm.checkbox('ntfy_auth','1', ntfyAuth, {}) }}
{{ ExpandedForm.text('ntfy_user', ntfyUser, {}) }} {{ ExpandedForm.text('ntfy_user', ntfyUser, {}) }}
{{ ExpandedForm.passwordWithValue('ntfy_pass', ntfyPass, {}) }} {{ ExpandedForm.passwordWithValue('ntfy_pass', ntfyPass, {}) }}
<p>
{{ 'pref_notifications_settings_help'|_ }}
</p>
<div class="btn-group">
{% for name,info in channels %}
{% if true == info.enabled and true == forcedAvailability[name] %}
<a href="#" data-channel="{{ name }}" class="btn btn-default submit-test">
{{ trans('firefly.test_notification_channel_name_'~name) }}
</a>
{% endif %}
{% endfor %}
</div>
</div> </div>
</div> </div>
</div> </div>
@@ -349,7 +380,7 @@
<div class="col-lg-12 col-md-12 col-sm-12"> <div class="col-lg-12 col-md-12 col-sm-12">
<div class="form-group"> <div class="form-group">
<div class="col-sm-12"> <div class="col-sm-12">
<button type="submit" class="btn btn-success btn-lg">{{ 'pref_save_settings'|_ }}</button> <button type="submit" name="form_submit" value="form_submit" class="btn btn-success btn-lg">{{ 'pref_save_settings'|_ }}</button>
</div> </div>
</div> </div>
</div> </div>
@@ -357,6 +388,9 @@
</form> </form>
{% endblock %} {% endblock %}
{% block scripts %} {% block scripts %}
<script type="text/javascript" nonce="{{ JS_NONCE }}">
var postUrl = "{{ route('preferences.test-notification') }}";
</script>
<script type="text/javascript" src="v1/js/lib/modernizr-custom.js?v={{ FF_VERSION }}" <script type="text/javascript" src="v1/js/lib/modernizr-custom.js?v={{ FF_VERSION }}"
nonce="{{ JS_NONCE }}"></script> nonce="{{ JS_NONCE }}"></script>
<script type="text/javascript" src="v1/js/lib/jquery-ui.min.js?v={{ FF_VERSION }}" nonce="{{ JS_NONCE }}"></script> <script type="text/javascript" src="v1/js/lib/jquery-ui.min.js?v={{ FF_VERSION }}" nonce="{{ JS_NONCE }}"></script>

View File

@@ -806,6 +806,7 @@ Route::group(
static function (): void { static function (): void {
Route::get('', ['uses' => 'PreferencesController@index', 'as' => 'index']); Route::get('', ['uses' => 'PreferencesController@index', 'as' => 'index']);
Route::post('', ['uses' => 'PreferencesController@postIndex', 'as' => 'update']); Route::post('', ['uses' => 'PreferencesController@postIndex', 'as' => 'update']);
Route::post('test-notification', ['uses' => 'PreferencesController@testNotification', 'as' => 'test-notification']);
} }
); );