Skip to content

A free open source IT asset/license management system

add component checkout notification, update checkout blade, update listener

Godfrey M500cbf5d9225
diff --git a/app/Listeners/CheckoutableListener.php b/app/Listeners/CheckoutableListener.php
index 9766c88da1..0fb4abd188 100644
--- a/app/Listeners/CheckoutableListener.php
+++ b/app/Listeners/CheckoutableListener.php
@@ -25,6 +25,8 @@ use App\Notifications\CheckinAssetNotification;
use App\Notifications\CheckinLicenseSeatNotification;
use App\Notifications\CheckoutAccessoryNotification;
use App\Notifications\CheckoutAssetNotification;
+use App\Notifications\CheckoutComponentNotification;
+use App\Notifications\CheckoutComponentsNotification;
use App\Notifications\CheckoutConsumableNotification;
use App\Notifications\CheckoutLicenseSeatNotification;
use GuzzleHttp\Exception\ClientException;
@@ -269,6 +271,9 @@ class CheckoutableListener
case LicenseSeat::class:
$notificationClass = CheckinLicenseSeatNotification::class;
break;
+// case Component::class:
+// $notificationClass = CheckinComponentNotification::class;
+// break;
}
Log::debug('Notification class: '.$notificationClass);
@@ -299,6 +304,9 @@ class CheckoutableListener
case LicenseSeat::class:
$notificationClass = CheckoutLicenseSeatNotification::class;
break;
+ case Component::class:
+ $notificationClass = CheckoutComponentNotification::class;
+ break;
}
diff --git a/app/Models/Component.php b/app/Models/Component.php
index df3289e52f..997579a021 100644
--- a/app/Models/Component.php
+++ b/app/Models/Component.php
@@ -2,6 +2,7 @@
namespace App\Models;
+use App\Helpers\Helper;
use App\Models\Traits\HasUploads;
use App\Models\Traits\Searchable;
use App\Presenters\Presentable;
@@ -203,6 +204,36 @@ class Component extends SnipeModel
{
return $this->belongsTo(\App\Models\Manufacturer::class, 'manufacturer_id');
}
+ /**
+ * Determine whether this asset requires acceptance by the assigned user
+ *
+ * @author [A. Gianotto] [<[email protected]>]
+ * @since [v4.0]
+ * @return bool
+ */
+ public function requireAcceptance()
+ {
+ return $this->category->require_acceptance;
+ }
+
+ /**
+ * Checks for a category-specific EULA, and if that doesn't exist,
+ * checks for a settings level EULA
+ *
+ * @author [A. Gianotto] [<[email protected]>]
+ * @since [v4.0]
+ * @return string | false
+ */
+ public function getEula()
+ {
+ if ($this->category->eula_text) {
+ return Helper::parseEscapedMarkedown($this->category->eula_text);
+ } elseif ((Setting::getSettings()->default_eula_text) && ($this->category->use_default_eula == '1')) {
+ return Helper::parseEscapedMarkedown(Setting::getSettings()->default_eula_text);
+ } else {
+ return null;
+ }
+ }
/**
* Establishes the component -> action logs relationship
diff --git a/app/Notifications/CheckoutComponentNotification.php b/app/Notifications/CheckoutComponentNotification.php
new file mode 100644
index 0000000000..a5e9b79463
--- /dev/null
+++ b/app/Notifications/CheckoutComponentNotification.php
@@ -0,0 +1,164 @@
+<?php
+
+namespace App\Notifications;
+
+use App\Models\Component;
+
+use App\Models\Setting;
+use App\Models\User;
+use Illuminate\Bus\Queueable;
+use Illuminate\Notifications\Channels\SlackWebhookChannel;
+use Illuminate\Notifications\Messages\SlackMessage;
+use Illuminate\Notifications\Notification;
+use Illuminate\Support\Str;
+use NotificationChannels\GoogleChat\Card;
+use NotificationChannels\GoogleChat\GoogleChatChannel;
+use NotificationChannels\GoogleChat\GoogleChatMessage;
+use NotificationChannels\GoogleChat\Section;
+use NotificationChannels\GoogleChat\Widgets\KeyValue;
+use NotificationChannels\MicrosoftTeams\MicrosoftTeamsChannel;
+use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage;
+
+
+class CheckoutComponentNotification extends Notification
+{
+ use Queueable;
+ /**
+ * @var
+ */
+ private $params;
+
+ /**
+ * Create a new notification instance.
+ *
+ * @param $params
+ */
+ public function __construct(Component $component, $checkedOutTo, User $checkedOutBy, $acceptance, $note)
+ {
+ $this->item = $component;
+ $this->admin = $checkedOutBy;
+ $this->note = $note;
+ $this->target = $checkedOutTo;
+ $this->acceptance = $acceptance;
+ $this->qty = $component->checkout_qty;
+
+ $this->settings = Setting::getSettings();
+ }
+
+ /**`
+ * Get the notification's delivery channels.
+ *
+ * @return array
+ */
+ public function via()
+ {
+ $notifyBy = [];
+ if (Setting::getSettings()->webhook_selected == 'google' && Setting::getSettings()->webhook_endpoint) {
+
+ $notifyBy[] = GoogleChatChannel::class;
+ }
+
+ if (Setting::getSettings()->webhook_selected == 'microsoft' && Setting::getSettings()->webhook_endpoint) {
+
+ $notifyBy[] = MicrosoftTeamsChannel::class;
+ }
+
+ if (Setting::getSettings()->webhook_selected == 'slack' || Setting::getSettings()->webhook_selected == 'general' ) {
+ $notifyBy[] = SlackWebhookChannel::class;
+ }
+
+ return $notifyBy;
+ }
+
+ public function toSlack()
+ {
+ $target = $this->target;
+ $admin = $this->admin;
+ $item = $this->item;
+ $note = $this->note;
+ $botname = ($this->settings->webhook_botname) ? $this->settings->webhook_botname : 'Snipe-Bot';
+ $channel = ($this->settings->webhook_channel) ? $this->settings->webhook_channel : '';
+
+ $fields = [
+ trans('general.to') => '<'.$target->present()->viewUrl().'|'.$target->present()->fullName().'>',
+ trans('general.by') => '<'.$admin->present()->viewUrl().'|'.$admin->present()->fullName().'>',
+ ];
+
+ if ($item->location) {
+ $fields[trans('general.location')] = $item->location->name;
+ }
+
+ if ($item->company) {
+ $fields[trans('general.company')] = $item->company->name;
+ }
+
+ return (new SlackMessage)
+ ->content(':arrow_up: :paperclip: Component Checked Out')
+ ->from($botname)
+ ->to($channel)
+ ->attachment(function ($attachment) use ($item, $note, $admin, $fields) {
+ $attachment->title(htmlspecialchars_decode($item->present()->name), $item->present()->viewUrl())
+ ->fields($fields)
+ ->content($note);
+ });
+ }
+ public function toMicrosoftTeams()
+ {
+ $target = $this->target;
+ $admin = $this->admin;
+ $item = $this->item;
+ $note = $this->note;
+
+ if(!Str::contains(Setting::getSettings()->webhook_endpoint, 'workflows')) {
+ return MicrosoftTeamsMessage::create()
+ ->to($this->settings->webhook_endpoint)
+ ->type('success')
+ ->addStartGroupToSection('activityTitle')
+ ->title(trans('mail.Consumable_checkout_notification'))
+ ->addStartGroupToSection('activityText')
+ ->fact(htmlspecialchars_decode($item->present()->name), '', 'activityTitle')
+ ->fact(trans('mail.Component_checkout_notification')." by ", $admin->present()->fullName())
+ ->fact(trans('mail.assigned_to'), $target->present()->fullName())
+ ->fact(trans('admin/consumables/general.remaining'), $item->numRemaining())
+ ->fact(trans('mail.notes'), $note ?: '');
+ }
+
+ $message = trans('mail.Component_checkout_notification');
+ $details = [
+ trans('mail.assigned_to') => $target->present()->fullName(),
+ trans('mail.item') => htmlspecialchars_decode($item->present()->name),
+ trans('mail.Component_checkout_notification').' by' => $admin->present()->fullName(),
+ trans('admin/consumables/general.remaining') => $item->numRemaining(),
+ trans('mail.notes') => $note ?: '',
+ ];
+
+ return array($message, $details);
+ }
+ public function toGoogleChat()
+ {
+ $target = $this->target;
+ $item = $this->item;
+ $note = $this->note;
+
+ return GoogleChatMessage::create()
+ ->to($this->settings->webhook_endpoint)
+ ->card(
+ Card::create()
+ ->header(
+ '<strong>'.trans('mail.Component_checkout_notification').'</strong>' ?: '',
+ htmlspecialchars_decode($item->present()->name) ?: '',
+ )
+ ->section(
+ Section::create(
+ KeyValue::create(
+ trans('mail.assigned_to') ?: '',
+ $target->present()->fullName() ?: '',
+ trans('admin/consumables/general.remaining').': '.$item->numRemaining(),
+ )
+ ->onClick(route('users.show', $target->id))
+ )
+ )
+ );
+
+ }
+}
diff --git a/resources/lang/en-US/mail.php b/resources/lang/en-US/mail.php
index 6878de1561..d0717ea4ff 100644
--- a/resources/lang/en-US/mail.php
+++ b/resources/lang/en-US/mail.php
@@ -13,6 +13,7 @@ return [
'Confirm_consumable_delivery' => 'Consumable delivery confirmation',
'Confirm_license_delivery' => 'License delivery confirmation',
'Consumable_checkout_notification' => 'Consumable checked out',
+ 'Component_checkout_notification' => 'Component checked out',
'Days' => 'Days',
'Expected_Checkin_Date' => 'An asset checked out to you is due to be checked back in on :date',
'Expected_Checkin_Notification' => 'Reminder: :name checkin deadline approaching',
diff --git a/resources/views/components/checkout.blade.php b/resources/views/components/checkout.blade.php
index a55ca4b21e..8e554575f5 100644
--- a/resources/views/components/checkout.blade.php
+++ b/resources/views/components/checkout.blade.php
@@ -41,7 +41,31 @@
</div>
@endif
</div>
+ @if ($component->requireAcceptance() || $component->getEula() || ($snipeSettings->webhook_endpoint!=''))
+ <div class="form-group notification-callout">
+ <div class="col-md-8 col-md-offset-3">
+ <div class="callout callout-info">
+ @if ($component->category->require_acceptance=='1')
+ <i class="far fa-envelope"></i>
+ {{ trans('admin/categories/general.required_acceptance') }}
+ <br>
+ @endif
+
+ @if ($component->getEula())
+ <i class="far fa-envelope"></i>
+ {{ trans('admin/categories/general.required_eula') }}
+ <br>
+ @endif
+
+ @if ($snipeSettings->webhook_endpoint!='')
+ <i class="fab fa-slack"></i>
+ {{ trans('general.webhook_msg_note') }}
+ @endif
+ </div>
+ </div>
+ </div>
+ @endif
<!-- Note -->
<div class="form-group{{ $errors->has('note') ? ' error' : '' }}">