diff --git a/app/Notifications/ReturnsAvailableChannels.php b/app/Notifications/ReturnsAvailableChannels.php index c63678a551..56e6de1b0e 100644 --- a/app/Notifications/ReturnsAvailableChannels.php +++ b/app/Notifications/ReturnsAvailableChannels.php @@ -25,20 +25,23 @@ declare(strict_types=1); namespace FireflyIII\Notifications; use FireflyIII\Support\Notifications\UrlValidator; +use FireflyIII\User; use Illuminate\Support\Facades\Log; use NotificationChannels\Pushover\PushoverChannel; use Wijourdil\NtfyNotificationChannel\Channels\NtfyChannel; class ReturnsAvailableChannels { - public static function returnChannels(string $type): array + public static function returnChannels(string $type, ?User $user = null): array { $channels = ['mail']; - if ('owner' === $type) { return self::returnOwnerChannels(); } + if('user' === $type && null !== $user) { + return self::returnUserChannels($user); + } return $channels; @@ -75,6 +78,38 @@ class ReturnsAvailableChannels Log::debug(sprintf('Final channel set in ReturnsAvailableChannels: %s ', implode(', ', $channels))); + return $channels; + } + + private static function returnUserChannels(User $user): array { + $channels = ['mail']; + $slackUrl = app('preferences')->getEncryptedForUser($user, 'slack_webhook_url', '')->data; + if (UrlValidator::isValidWebhookURL($slackUrl)) { + $channels[] = 'slack'; + } + + // validate presence of of Ntfy settings. + if ('' !== (string) app('preferences')->getEncryptedForUser($user,'ntfy_topic', '')->data) { + Log::debug('Enabled ntfy.'); + $channels[] = NtfyChannel::class; + } + if ('' === (string) app('preferences')->getEncryptedForUser($user,'ntfy_topic', '')->data) { + Log::warning('No topic name for Ntfy, channel is disabled.'); + } + + // pushover + $pushoverAppToken = (string) app('preferences')->getEncryptedForUser($user, 'pushover_app_token', '')->data; + $pushoverUserToken = (string) app('preferences')->getEncryptedForUser($user, 'pushover_user_token', '')->data; + if ('' === $pushoverAppToken || '' === $pushoverUserToken) { + Log::warning('[b] No Pushover token, channel is disabled.'); + } + if ('' !== $pushoverAppToken && '' !== $pushoverUserToken) { + Log::debug('Enabled pushover.'); + $channels[] = PushoverChannel::class; + } + + Log::debug(sprintf('Final channel set in ReturnsAvailableChannels (user): %s ', implode(', ', $channels))); + // only the owner can get notifications over return $channels; } diff --git a/app/Notifications/Security/DisabledMFANotification.php b/app/Notifications/Security/DisabledMFANotification.php index 2676f3d5b9..96022b12c8 100644 --- a/app/Notifications/Security/DisabledMFANotification.php +++ b/app/Notifications/Security/DisabledMFANotification.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace FireflyIII\Notifications\Security; +use FireflyIII\Notifications\ReturnsAvailableChannels; use FireflyIII\Support\Notifications\UrlValidator; use FireflyIII\User; use Illuminate\Bus\Queueable; @@ -35,7 +36,7 @@ class DisabledMFANotification extends Notification { use Queueable; - private User $user; + private User $user; /** * Create a new notification instance. @@ -48,13 +49,13 @@ class DisabledMFANotification extends Notification /** * Get the array representation of the notification. * - * @param mixed $notifiable + * @param User $notifiable * * @return array * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function toArray($notifiable) + public function toArray(User $notifiable) { return [ ]; @@ -63,15 +64,15 @@ class DisabledMFANotification extends Notification /** * Get the mail representation of the notification. * - * @param mixed $notifiable + * @param User $notifiable * * @return MailMessage * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function toMail($notifiable) + public function toMail(User $notifiable) { - $subject = (string)trans('email.disabled_mfa_subject'); + $subject = (string) trans('email.disabled_mfa_subject'); return (new MailMessage())->markdown('emails.security.disabled-mfa', ['user' => $this->user])->subject($subject); } @@ -79,15 +80,15 @@ class DisabledMFANotification extends Notification /** * Get the Slack representation of the notification. * - * @param mixed $notifiable + * @param User $notifiable * * @return SlackMessage * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function toSlack($notifiable) + public function toSlack(User $notifiable) { - $message = (string)trans('email.disabled_mfa_slack', ['email' => $this->user->email]); + $message = (string) trans('email.disabled_mfa_slack', ['email' => $this->user->email]); return (new SlackMessage())->content($message); } @@ -95,24 +96,14 @@ class DisabledMFANotification extends Notification /** * Get the notification's delivery channels. * - * @param mixed $notifiable + * @param User $notifiable * * @return array * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function via($notifiable) + public function via(User $notifiable) { - /** @var null|User $user */ - $user = auth()->user(); - $slackUrl = null === $user ? '' : app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data; - if (is_array($slackUrl)) { - $slackUrl = ''; - } - if (UrlValidator::isValidWebhookURL((string)$slackUrl)) { - return ['mail', 'slack']; - } - - return ['mail']; + return ReturnsAvailableChannels::returnChannels('user', $notifiable); } }