From 241ad25e2fc6a6940513c73a9264050c29e90ff0 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 16 Sep 2023 08:44:20 +0200 Subject: [PATCH] Better slack alert validation --- .../Commands/Integrity/UpdateGroupInformation.php | 2 +- app/Notifications/Admin/TestNotification.php | 4 +--- app/Notifications/Admin/UserInvitation.php | 4 +--- app/Notifications/Admin/UserRegistration.php | 4 +--- app/Notifications/Admin/VersionCheckResult.php | 4 +--- app/Notifications/User/UserLogin.php | 1 + app/User.php | 9 +++++---- composer.json | 2 +- composer.lock | 2 +- 9 files changed, 13 insertions(+), 19 deletions(-) diff --git a/app/Console/Commands/Integrity/UpdateGroupInformation.php b/app/Console/Commands/Integrity/UpdateGroupInformation.php index 7b0d80f31d..305026d943 100644 --- a/app/Console/Commands/Integrity/UpdateGroupInformation.php +++ b/app/Console/Commands/Integrity/UpdateGroupInformation.php @@ -126,7 +126,7 @@ class UpdateGroupInformation extends Command return; } if (0 !== $result) { - $this->friendlyPositive(sprintf('Moved %d %s objects to the correct group.', $result, str_replace('FireflyIII\\Models\\', '', $className))); + $this->friendlyPositive(sprintf('User #%d: Moved %d %s objects to the correct group.', $user->id, $result, str_replace('FireflyIII\\Models\\', '', $className))); } } } diff --git a/app/Notifications/Admin/TestNotification.php b/app/Notifications/Admin/TestNotification.php index f4277b34cc..b79a21f36d 100644 --- a/app/Notifications/Admin/TestNotification.php +++ b/app/Notifications/Admin/TestNotification.php @@ -99,9 +99,7 @@ class TestNotification extends Notification */ public function via($notifiable) { - /** @var User|null $user */ - $user = auth()->user(); - $slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data; + $slackUrl = app('fireflyconfig')->get('slack_webhook_url', '')->data; if (UrlValidator::isValidWebhookURL($slackUrl)) { return ['mail', 'slack']; } diff --git a/app/Notifications/Admin/UserInvitation.php b/app/Notifications/Admin/UserInvitation.php index 19f4c5668a..60d05bab97 100644 --- a/app/Notifications/Admin/UserInvitation.php +++ b/app/Notifications/Admin/UserInvitation.php @@ -102,9 +102,7 @@ class UserInvitation extends Notification */ public function via($notifiable) { - /** @var User|null $user */ - $user = auth()->user(); - $slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data; + $slackUrl = app('fireflyconfig')->get('slack_webhook_url', '')->data; if (UrlValidator::isValidWebhookURL($slackUrl)) { return ['mail', 'slack']; } diff --git a/app/Notifications/Admin/UserRegistration.php b/app/Notifications/Admin/UserRegistration.php index ab1174bd15..4b0bce8971 100644 --- a/app/Notifications/Admin/UserRegistration.php +++ b/app/Notifications/Admin/UserRegistration.php @@ -99,9 +99,7 @@ class UserRegistration extends Notification */ public function via($notifiable) { - /** @var User|null $user */ - $user = auth()->user(); - $slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data; + $slackUrl = app('fireflyconfig')->get('slack_webhook_url', '')->data; if (UrlValidator::isValidWebhookURL($slackUrl)) { return ['mail', 'slack']; } diff --git a/app/Notifications/Admin/VersionCheckResult.php b/app/Notifications/Admin/VersionCheckResult.php index 3555f52538..95af203bfd 100644 --- a/app/Notifications/Admin/VersionCheckResult.php +++ b/app/Notifications/Admin/VersionCheckResult.php @@ -103,9 +103,7 @@ class VersionCheckResult extends Notification */ public function via($notifiable) { - /** @var User|null $user */ - $user = auth()->user(); - $slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data; + $slackUrl = app('fireflyconfig')->get('slack_webhook_url', '')->data; if (UrlValidator::isValidWebhookURL($slackUrl)) { return ['mail', 'slack']; } diff --git a/app/Notifications/User/UserLogin.php b/app/Notifications/User/UserLogin.php index 0e6ddd5cdb..5880947378 100644 --- a/app/Notifications/User/UserLogin.php +++ b/app/Notifications/User/UserLogin.php @@ -33,6 +33,7 @@ use Illuminate\Notifications\Notification; use Illuminate\Support\Facades\Log; use Illuminate\Notifications\Messages\SlackMessage; + /** * Class UserLogin */ diff --git a/app/User.php b/app/User.php index 3796626366..b8c27701e3 100644 --- a/app/User.php +++ b/app/User.php @@ -480,15 +480,16 @@ class User extends Authenticatable if ($notification instanceof TestNotification) { return app('fireflyconfig')->get('slack_webhook_url', '')->data; } + if ($notification instanceof UserInvitation) { + return app('fireflyconfig')->get('slack_webhook_url', '')->data; + } if ($notification instanceof UserRegistration) { return app('fireflyconfig')->get('slack_webhook_url', '')->data; } if ($notification instanceof VersionCheckResult) { return app('fireflyconfig')->get('slack_webhook_url', '')->data; } - if ($notification instanceof UserInvitation) { - return app('fireflyconfig')->get('slack_webhook_url', '')->data; - } + return app('preferences')->getForUser($this, 'slack_webhook_url', '')->data; } @@ -598,7 +599,7 @@ class User extends Authenticatable */ public function userGroup(): BelongsTo { - return $this->belongsTo(UserGroup::class, ); + return $this->belongsTo(UserGroup::class,); } /** diff --git a/composer.json b/composer.json index 6422159e03..f0143e13a5 100644 --- a/composer.json +++ b/composer.json @@ -91,7 +91,7 @@ "laravel/framework": "^10", "laravel/passport": "11.*", "laravel/sanctum": "^3.3", - "laravel/slack-notification-channel": "^3", + "laravel/slack-notification-channel": "^3.0", "laravel/ui": "^4.2", "league/commonmark": "2.*", "league/csv": "^9.10", diff --git a/composer.lock b/composer.lock index 1e312ffd75..bbe4f0fafd 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "351a9da77c2fa38c0f4f13ef059926ee", + "content-hash": "639b971ea13ea3e6ed2f57f862a195b8", "packages": [ { "name": "bacon/bacon-qr-code",