Skip to content

A free open source IT asset/license management system

refactor the Listener to use Company settings and fallback to general webhook settings for notificaitons

Godfrey M7386ae11c8c7
diff --git a/app/Listeners/CheckoutableListener.php b/app/Listeners/CheckoutableListener.php
index 39b9479e00..b5239d5eca 100644
--- a/app/Listeners/CheckoutableListener.php
+++ b/app/Listeners/CheckoutableListener.php
@@ -17,6 +17,7 @@ use App\Models\Accessory;
use App\Models\Asset;
use App\Models\Category;
use App\Models\CheckoutAcceptance;
+use App\Models\Company;
use App\Models\Component;
use App\Models\Consumable;
use App\Models\LicenseSeat;
@@ -82,7 +83,7 @@ class CheckoutableListener
$shouldSendEmailToUser = $this->shouldSendCheckoutEmailToUser($event->checkoutable);
$shouldSendEmailToAlertAddress = $this->shouldSendEmailToAlertAddress($acceptance);
- $shouldSendWebhookNotification = $this->shouldSendWebhookNotification();
+ $shouldSendWebhookNotification = $this->shouldSendWebhookNotification($event->checkoutable);
if ($this->shouldSkipInitialAcceptanceEmail($event, $acceptance)) {
$shouldSendEmailToUser = false;
@@ -127,41 +128,43 @@ class CheckoutableListener
}
if ($shouldSendWebhookNotification) {
+ $webhookSource = $this->webhookSource($event->checkoutable);
+
try {
- if ($this->newMicrosoftTeamsWebhookEnabled()) {
+ if ($this->newMicrosoftTeamsWebhookEnabled($event->checkoutable)) {
$message = $this->getCheckoutNotification($event, $acceptance, true)->toMicrosoftTeams();
- $notification = new TeamsNotification(Setting::getSettings()->webhook_endpoint);
+ $notification = new TeamsNotification($webhookSource->webhook_endpoint);
$notification->success()->sendMessage($message[0], $message[1]); // Send the message to Microsoft Teams
} else {
- Notification::route($this->webhookSelected(), Setting::getSettings()->webhook_endpoint)
+ Notification::route($this->webhookSelected($event->checkoutable), $webhookSource->webhook_endpoint)
->notify($this->getCheckoutNotification($event, $acceptance, true));
}
} catch (ClientException $e) {
$status = $e->getResponse()->getStatusCode();
if (strpos($e->getMessage(), 'channel_not_found') !== false) {
- Log::warning(Setting::getSettings()->webhook_selected.' notification failed: '.$e->getMessage());
+ Log::warning($webhookSource->webhook_selected . ' notification failed: ' . $e->getMessage());
- return redirect()->back()->with('warning', ucfirst(Setting::getSettings()->webhook_selected).trans('admin/settings/message.webhook.webhook_channel_not_found'));
+ return redirect()->back()->with('warning', ucfirst($webhookSource->webhook_selected) . trans('admin/settings/message.webhook.webhook_channel_not_found'));
} else {
if ($status >= 500 || $status === null) {
- Log::error(Setting::getSettings()->webhook_selected.' notification failed: '.$e->getMessage());
+ Log::error($webhookSource->webhook_selected . ' notification failed: ' . $e->getMessage());
} else {
Log::warning('ClientException caught during checkin notification: '.$e->getMessage());
- return redirect()->back()->with('warning', ucfirst(Setting::getSettings()->webhook_selected).trans('admin/settings/message.webhook.webhook_fail'));
+ return redirect()->back()->with('warning', ucfirst($webhookSource->webhook_selected) . trans('admin/settings/message.webhook.webhook_fail'));
}
}
- return redirect()->back()->with('warning', ucfirst(Setting::getSettings()->webhook_selected).trans('admin/settings/message.webhook.webhook_fail'));
+ return redirect()->back()->with('warning', ucfirst($webhookSource->webhook_selected) . trans('admin/settings/message.webhook.webhook_fail'));
} catch (Exception $e) {
- Log::warning(ucfirst(Setting::getSettings()->webhook_selected).' webhook notification failed:', [
+ Log::warning(ucfirst($webhookSource->webhook_selected) . ' webhook notification failed:', [
'error' => $e->getMessage(),
- 'webhook_endpoint' => Setting::getSettings()->webhook_endpoint,
+ 'webhook_endpoint' => $webhookSource->webhook_endpoint,
'event' => $event,
]);
- return redirect()->back()->with('warning', ucfirst(Setting::getSettings()->webhook_selected).trans('admin/settings/message.webhook.webhook_fail'));
+ return redirect()->back()->with('warning', ucfirst($webhookSource->webhook_selected) . trans('admin/settings/message.webhook.webhook_fail'));
}
}
}
@@ -183,7 +186,7 @@ class CheckoutableListener
$shouldSendEmailToUser = $this->checkoutableCategoryShouldSendEmail($event->checkoutable);
$shouldSendEmailToAlertAddress = $this->shouldSendEmailToAlertAddress();
- $shouldSendWebhookNotification = $this->shouldSendWebhookNotification();
+ $shouldSendWebhookNotification = $this->shouldSendWebhookNotification($event->checkoutable);
if (! $shouldSendEmailToUser && ! $shouldSendEmailToAlertAddress && ! $shouldSendWebhookNotification) {
return;
}
@@ -226,39 +229,40 @@ class CheckoutableListener
if ($shouldSendWebhookNotification) {
// Send Webhook notification
+ $webhookSource = $this->webhookSource($event->checkoutable);
try {
- if ($this->newMicrosoftTeamsWebhookEnabled()) {
+ if ($this->newMicrosoftTeamsWebhookEnabled($event->checkoutable)) {
$message = $this->getCheckinNotification($event, true)->toMicrosoftTeams();
- $notification = new TeamsNotification(Setting::getSettings()->webhook_endpoint);
+ $notification = new TeamsNotification($webhookSource->webhook_endpoint);
$notification->success()->sendMessage($message[0], $message[1]); // Send the message to Microsoft Teams
} else {
- Notification::route($this->webhookSelected(), Setting::getSettings()->webhook_endpoint)
+ Notification::route($this->webhookSelected($event->checkoutable), $webhookSource->webhook_endpoint)
->notify($this->getCheckinNotification($event, true));
}
} catch (ClientException $e) {
$status = $e->getResponse()->getStatusCode();
if (strpos($e->getMessage(), 'channel_not_found') !== false) {
- Log::warning(Setting::getSettings()->webhook_selected.' notification failed: '.$e->getMessage());
+ Log::warning($webhookSource->webhook_selected . ' notification failed: ' . $e->getMessage());
- return redirect()->back()->with('warning', ucfirst(Setting::getSettings()->webhook_selected).trans('admin/settings/message.webhook.webhook_channel_not_found'));
+ return redirect()->back()->with('warning', ucfirst($webhookSource->webhook_selected) . trans('admin/settings/message.webhook.webhook_channel_not_found'));
} else {
if ($status >= 500 || $status === null) {
- Log::error(Setting::getSettings()->webhook_selected.' notification failed: '.$e->getMessage());
+ Log::error($webhookSource->webhook_selected . ' notification failed: ' . $e->getMessage());
} else {
Log::warning('ClientException caught during checkin notification: '.$e->getMessage());
- return redirect()->back()->with('warning', ucfirst(Setting::getSettings()->webhook_selected).trans('admin/settings/message.webhook.webhook_fail'));
+ return redirect()->back()->with('warning', ucfirst($webhookSource->webhook_selected) . trans('admin/settings/message.webhook.webhook_fail'));
}
}
} catch (Exception $e) {
- Log::warning(ucfirst(Setting::getSettings()->webhook_selected).' webhook notification failed:', [
+ Log::warning(ucfirst($webhookSource->webhook_selected) . ' webhook notification failed:', [
'error' => $e->getMessage(),
- 'webhook_endpoint' => Setting::getSettings()->webhook_endpoint,
+ 'webhook_endpoint' => $webhookSource->webhook_endpoint,
'event' => $event,
]);
- return redirect()->back()->with('warning', ucfirst(Setting::getSettings()->webhook_selected).trans('admin/settings/message.webhook.webhook_fail'));
+ return redirect()->back()->with('warning', ucfirst($webhookSource->webhook_selected) . trans('admin/settings/message.webhook.webhook_fail'));
}
}
}
@@ -405,7 +409,7 @@ class CheckoutableListener
Log::debug('Notification class: '.$notificationClass);
- return new $notificationClass($checkoutable, $event->checkedOutTo, $event->checkedInBy, $event->note);
+ return new $notificationClass($checkoutable, $event->checkedOutTo, $event->checkedInBy, $event->note, $this->webhookSource($checkoutable));
}
/**
@@ -438,7 +442,7 @@ class CheckoutableListener
break;
}
- return new $notificationClass($checkoutable, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note);
+ return new $notificationClass($checkoutable, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note, $this->webhookSource($checkoutable));
}
private function getCheckoutableForNotification(Model $checkoutable, bool $shouldRefresh): Model
@@ -506,13 +510,15 @@ class CheckoutableListener
}
}
- private function webhookSelected()
+ private function webhookSelected(Model $checkoutable): string
{
- if (Setting::getSettings()->webhook_selected === 'slack' || Setting::getSettings()->webhook_selected === 'general') {
+ $selected = $this->webhookSource($checkoutable)->webhook_selected;
+
+ if ($selected === 'slack' || $selected === 'general') {
return 'slack';
}
- return Setting::getSettings()->webhook_selected;
+ return $selected;
}
private function shouldNotSendAnyNotifications($checkoutable): bool
@@ -520,9 +526,9 @@ class CheckoutableListener
return in_array(get_class($checkoutable), $this->skipNotificationsFor);
}
- private function shouldSendWebhookNotification(): bool
+ private function shouldSendWebhookNotification(Model $checkoutable): bool
{
- return Setting::getSettings() && Setting::getSettings()->webhook_endpoint;
+ return (bool)$this->webhookSource($checkoutable)->webhook_endpoint;
}
private function checkoutableCategoryShouldSendEmail(Model $checkoutable): bool
@@ -534,9 +540,11 @@ class CheckoutableListener
return method_exists($checkoutable, 'checkin_email') && $checkoutable->checkin_email();
}
- private function newMicrosoftTeamsWebhookEnabled(): bool
+ private function newMicrosoftTeamsWebhookEnabled(Model $checkoutable): bool
{
- return Setting::getSettings()->webhook_selected === 'microsoft' && Str::contains(Setting::getSettings()->webhook_endpoint, 'workflows');
+ $source = $this->webhookSource($checkoutable);
+
+ return $source->webhook_selected === 'microsoft' && Str::contains($source->webhook_endpoint, 'workflows');
}
private function shouldSendCheckoutEmailToUser(Model $checkoutable): bool
@@ -644,4 +652,17 @@ class CheckoutableListener
default => null,
};
}
+
+ private function webhookSource(Model $checkoutable): Company|Setting
+ {
+ if ($checkoutable->company_id) {
+ $company = Company::find($checkoutable->company_id);
+
+ if ($company?->webhook_endpoint) {
+ return $company;
+ }
+ }
+
+ return Setting::getSettings();
+ }
}
diff --git a/app/Notifications/CheckoutAssetNotification.php b/app/Notifications/CheckoutAssetNotification.php
index 7225adfef1..61e4ce4472 100644
--- a/app/Notifications/CheckoutAssetNotification.php
+++ b/app/Notifications/CheckoutAssetNotification.php
@@ -4,6 +4,7 @@ namespace App\Notifications;
use App\Helpers\Helper;
use App\Models\Asset;
+use App\Models\Company;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Bus\Queueable;
@@ -39,7 +40,8 @@ class CheckoutAssetNotification extends Notification implements ShouldQueue
public $target,
public User $admin,
public $acceptance, // ???? what is this? (doesn't seem used)
- public $note
+ public $note,
+ public Company|Setting $webhookSource,
) {
if ($this->item->last_checkout) {
$this->last_checkout = Helper::getFormattedDateObject($this->item->last_checkout, 'date',
@@ -61,17 +63,17 @@ class CheckoutAssetNotification extends Notification implements ShouldQueue
{
$notifyBy = [];
- if (Setting::getSettings()->webhook_selected === 'google' && Setting::getSettings()->webhook_endpoint) {
+ if ($this->webhookSource->webhook_selected === 'google' && $this->webhookSource->webhook_endpoint) {
$notifyBy[] = GoogleChatChannel::class;
}
- if (Setting::getSettings()->webhook_selected === 'microsoft' && Setting::getSettings()->webhook_endpoint) {
+ if ($this->webhookSource->webhook_selected === 'microsoft' && $this->webhookSource->webhook_endpoint) {
$notifyBy[] = MicrosoftTeamsChannel::class;
}
- if (Setting::getSettings()->webhook_selected === 'slack' || Setting::getSettings()->webhook_selected === 'general') {
+ if ($this->webhookSource->webhook_selected === 'slack' || $this->webhookSource->webhook_selected === 'general') {
Log::debug('use webhook');
$notifyBy[] = SlackWebhookChannel::class;
@@ -82,13 +84,12 @@ class CheckoutAssetNotification extends Notification implements ShouldQueue
public function toSlack(): SlackMessage
{
- $settings = Setting::getSettings();
$target = $this->target;
$admin = $this->admin;
$item = $this->item;
$note = $this->note;
- $botname = ($settings->webhook_botname) ?: 'Snipe-Bot';
- $channel = ($settings->webhook_channel) ? $settings->webhook_channel : '';
+ $botname = ($this->webhookSource->webhook_botname) ?: 'Snipe-Bot';
+ $channel = ($this->webhookSource->webhook_channel) ? $this->webhookSource->webhook_channel : '';
$fields = [
trans('general.to_user') => '<'.$target->present()->viewUrl().'|'.$target->display_name.'>',
@@ -120,15 +121,14 @@ class CheckoutAssetNotification extends Notification implements ShouldQueue
public function toMicrosoftTeams()
{
- $settings = Setting::getSettings();
$target = $this->target;
$admin = $this->admin;
$item = $this->item;
$note = $this->note;
- if (!Str::contains($settings->webhook_endpoint, 'workflows')) {
+ if (!Str::contains($this->webhookSource->webhook_endpoint, 'workflows')) {
return MicrosoftTeamsMessage::create()
- ->to($settings->webhook_endpoint)
+ ->to($this->webhookSource->webhook_endpoint)
->type('success')
->title(trans('mail.Asset_Checkout_Notification', ['tag' => '']))
->addStartGroupToSection('activityText')
@@ -151,13 +151,12 @@ class CheckoutAssetNotification extends Notification implements ShouldQueue
public function toGoogleChat()
{
- $settings = Setting::getSettings();
$target = $this->target;
$item = $this->item;
$note = $this->note;
return GoogleChatMessage::create()
- ->to($settings->webhook_endpoint)
+ ->to($this->webhookSource->webhook_endpoint)
->card(
Card::create()
->header(