Skip to content

A free open source IT asset/license management system

Apply pint to Jobs and Listeners directory

snipe317b1a462e07
diff --git a/app/Listeners/CheckoutableListener.php b/app/Listeners/CheckoutableListener.php
index 54177a7448..0c064dbab6 100644
--- a/app/Listeners/CheckoutableListener.php
+++ b/app/Listeners/CheckoutableListener.php
@@ -4,11 +4,11 @@ namespace App\Listeners;
use App\Events\CheckoutableCheckedOut;
use App\Mail\CheckinAccessoryMail;
+use App\Mail\CheckinAssetMail;
use App\Mail\CheckinComponentMail;
use App\Mail\CheckinLicenseMail;
use App\Mail\CheckoutAccessoryMail;
use App\Mail\CheckoutAssetMail;
-use App\Mail\CheckinAssetMail;
use App\Mail\CheckoutComponentMail;
use App\Mail\CheckoutConsumableMail;
use App\Mail\CheckoutLicenseMail;
@@ -31,20 +31,20 @@ use App\Notifications\CheckoutAssetNotification;
use App\Notifications\CheckoutComponentNotification;
use App\Notifications\CheckoutConsumableNotification;
use App\Notifications\CheckoutLicenseSeatNotification;
+use Exception;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Context;
+use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;
-use Exception;
-use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Osama\LaravelTeamsNotification\TeamsNotification;
class CheckoutableListener
{
private array $skipNotificationsFor = [
-// Component::class,
+ // Component::class,
];
/**
@@ -60,7 +60,7 @@ class CheckoutableListener
);
$events->listen(
- \App\Events\CheckoutableCheckedOut::class,
+ CheckoutableCheckedOut::class,
'App\Listeners\CheckoutableListener@onCheckedOut'
);
}
@@ -81,7 +81,7 @@ class CheckoutableListener
$shouldSendEmailToAlertAddress = $this->shouldSendEmailToAlertAddress($acceptance);
$shouldSendWebhookNotification = $this->shouldSendWebhookNotification();
- if (!$shouldSendEmailToUser && !$shouldSendEmailToAlertAddress && !$shouldSendWebhookNotification) {
+ if (! $shouldSendEmailToUser && ! $shouldSendEmailToAlertAddress && ! $shouldSendWebhookNotification) {
return;
}
@@ -95,25 +95,25 @@ class CheckoutableListener
[$to, $cc] = $this->generateEmailRecipients($shouldSendEmailToUser, $shouldSendEmailToAlertAddress, $notifiable);
- if (!empty($to)) {
+ if (! empty($to)) {
try {
$toMail = (clone $mailable)->locale($notifiable->locale);
Mail::to(array_flatten($to))->send($toMail);
Log::info('Checkout Mail sent to checkout target');
} catch (ClientException $e) {
- Log::debug("Exception caught during checkout email: " . $e->getMessage());
+ Log::debug('Exception caught during checkout email: '.$e->getMessage());
} catch (Exception $e) {
- Log::debug("Exception caught during checkout email: " . $e->getMessage());
+ Log::debug('Exception caught during checkout email: '.$e->getMessage());
}
}
- if (!empty($cc)) {
+ if (! empty($cc)) {
try {
$ccMail = (clone $mailable)->locale(Setting::getSettings()->locale);
Mail::to(array_flatten($cc))->send($ccMail);
} catch (ClientException $e) {
- Log::debug("Exception caught during checkout email: " . $e->getMessage());
+ Log::debug('Exception caught during checkout email: '.$e->getMessage());
} catch (Exception $e) {
- Log::debug("Exception caught during checkout email: " . $e->getMessage());
+ Log::debug('Exception caught during checkout email: '.$e->getMessage());
}
}
}
@@ -132,24 +132,28 @@ class CheckoutableListener
$status = $e->getResponse()->getStatusCode();
if (strpos($e->getMessage(), 'channel_not_found') !== false) {
- Log::warning(Setting::getSettings()->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'));
+ Log::warning(Setting::getSettings()->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'));
} else {
if ($status >= 500 || $status === null) {
- Log::error(Setting::getSettings()->webhook_selected . " notification failed: " . $e->getMessage());
+ Log::error(Setting::getSettings()->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'));
+ 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(Setting::getSettings()->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'));
} catch (Exception $e) {
- Log::warning(ucfirst(Setting::getSettings()->webhook_selected) . ' webhook notification failed:', [
+ Log::warning(ucfirst(Setting::getSettings()->webhook_selected).' webhook notification failed:', [
'error' => $e->getMessage(),
'webhook_endpoint' => Setting::getSettings()->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(Setting::getSettings()->webhook_selected).trans('admin/settings/message.webhook.webhook_fail'));
}
}
}
@@ -168,7 +172,7 @@ class CheckoutableListener
$shouldSendEmailToUser = $this->checkoutableCategoryShouldSendEmail($event->checkoutable);
$shouldSendEmailToAlertAddress = $this->shouldSendEmailToAlertAddress();
$shouldSendWebhookNotification = $this->shouldSendWebhookNotification();
- if (!$shouldSendEmailToUser && !$shouldSendEmailToAlertAddress && !$shouldSendWebhookNotification) {
+ if (! $shouldSendEmailToUser && ! $shouldSendEmailToAlertAddress && ! $shouldSendWebhookNotification) {
return;
}
@@ -197,25 +201,25 @@ class CheckoutableListener
[$to, $cc] = $this->generateEmailRecipients($shouldSendEmailToUser, $shouldSendEmailToAlertAddress, $notifiable);
- if (!empty($to)) {
+ if (! empty($to)) {
try {
$toMail = (clone $mailable)->locale($notifiable->locale);
Mail::to(array_flatten($to))->send($toMail);
Log::info('Checkin Mail sent to checkin target');
} catch (ClientException $e) {
- Log::debug("Exception caught during checkin email: " . $e->getMessage());
+ Log::debug('Exception caught during checkin email: '.$e->getMessage());
} catch (Exception $e) {
- Log::debug("Exception caught during checkin email: " . $e->getMessage());
+ Log::debug('Exception caught during checkin email: '.$e->getMessage());
}
}
- if (!empty($cc)) {
+ if (! empty($cc)) {
try {
$ccMail = (clone $mailable)->locale(Setting::getSettings()->locale);
Mail::to(array_flatten($cc))->send($ccMail);
} catch (ClientException $e) {
- Log::debug("Exception caught during checkin email: " . $e->getMessage());
+ Log::debug('Exception caught during checkin email: '.$e->getMessage());
} catch (Exception $e) {
- Log::debug("Exception caught during checkin email: " . $e->getMessage());
+ Log::debug('Exception caught during checkin email: '.$e->getMessage());
}
}
}
@@ -235,30 +239,34 @@ class CheckoutableListener
$status = $e->getResponse()->getStatusCode();
if (strpos($e->getMessage(), 'channel_not_found') !== false) {
- Log::warning(Setting::getSettings()->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'));
+ Log::warning(Setting::getSettings()->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'));
} else {
if ($status >= 500 || $status === null) {
- Log::error(Setting::getSettings()->webhook_selected . " notification failed: " . $e->getMessage());
+ Log::error(Setting::getSettings()->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'));
+ 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'));
}
}
} catch (Exception $e) {
- Log::warning(ucfirst(Setting::getSettings()->webhook_selected) . ' webhook notification failed:', [
+ Log::warning(ucfirst(Setting::getSettings()->webhook_selected).' webhook notification failed:', [
'error' => $e->getMessage(),
'webhook_endpoint' => Setting::getSettings()->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(Setting::getSettings()->webhook_selected).trans('admin/settings/message.webhook.webhook_fail'));
}
}
}
/**
* Generates a checkout acceptance
- * @param Event $event
+ *
+ * @param Event $event
* @return mixed
*/
private function getCheckoutAcceptance($event)
@@ -268,7 +276,7 @@ class CheckoutableListener
return null;
}
- if (!$event->checkoutable->requireAcceptance()) {
+ if (! $event->checkoutable->requireAcceptance()) {
return null;
}
@@ -287,7 +295,7 @@ class CheckoutableListener
if ($category?->alert_on_response) {
$acceptance->alert_on_response_id = auth()->id();
}
-
+
$acceptance->save();
return $acceptance;
@@ -323,11 +331,12 @@ class CheckoutableListener
return new $notificationClass($event->checkoutable, $event->checkedOutTo, $event->checkedInBy, $event->note);
}
+
/**
* Get the appropriate notification for the event
- *
- * @param CheckoutableCheckedOut $event
- * @param CheckoutAcceptance|null $acceptance
+ *
+ * @param CheckoutableCheckedOut $event
+ * @param CheckoutAcceptance|null $acceptance
* @return Notification
*/
private function getCheckoutNotification($event, $acceptance = null)
@@ -349,13 +358,14 @@ class CheckoutableListener
break;
case Component::class:
$notificationClass = CheckoutComponentNotification::class;
- break;
+ break;
}
-
return new $notificationClass($event->checkoutable, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note);
}
- private function getCheckoutMailType($event, $acceptance){
+
+ private function getCheckoutMailType($event, $acceptance)
+ {
$lookup = [
Accessory::class => CheckoutAccessoryMail::class,
Asset::class => CheckoutAssetMail::class,
@@ -363,20 +373,21 @@ class CheckoutableListener
Consumable::class => CheckoutConsumableMail::class,
Component::class => CheckoutComponentMail::class,
];
- $mailable= $lookup[get_class($event->checkoutable)];
+ $mailable = $lookup[get_class($event->checkoutable)];
return new $mailable($event->checkoutable, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note);
}
- private function getCheckinMailType($event){
+ private function getCheckinMailType($event)
+ {
$lookup = [
Accessory::class => CheckinAccessoryMail::class,
Asset::class => CheckinAssetMail::class,
LicenseSeat::class => CheckinLicenseMail::class,
Component::class => CheckinComponentMail::class,
];
- $mailable= $lookup[get_class($event->checkoutable)];
+ $mailable = $lookup[get_class($event->checkoutable)];
return new $mailable($event->checkoutable, $event->checkedOutTo, $event->checkedInBy, $event->note);
@@ -386,30 +397,32 @@ class CheckoutableListener
* This gets the recipient objects based on the type of checkoutable.
* The 'name' property for users is set in the boot method in the User model.
*
- * @see \App\Models\User::boot()
- * @param $event
+ * @see User::boot()
+ *
* @return mixed
*/
private function getNotifiableUser($event)
{
// If it's assigned to an asset, get that asset's assignedTo object
- if ($event->checkedOutTo instanceof Asset){
+ if ($event->checkedOutTo instanceof Asset) {
$event->checkedOutTo->load('assignedTo');
+
return $event->checkedOutTo->assignedto;
- // If it's assigned to a location, get that location's manager object
+ // If it's assigned to a location, get that location's manager object
} elseif ($event->checkedOutTo instanceof Location) {
return $event->checkedOutTo->manager;
- // Otherwise just return the assigned to object
+ // Otherwise just return the assigned to object
} else {
return $event->checkedOutTo;
}
}
- private function webhookSelected(){
- if(Setting::getSettings()->webhook_selected === 'slack' || Setting::getSettings()->webhook_selected === 'general'){
+ private function webhookSelected()
+ {
+ if (Setting::getSettings()->webhook_selected === 'slack' || Setting::getSettings()->webhook_selected === 'general') {
return 'slack';
}
@@ -431,7 +444,8 @@ class CheckoutableListener
if ($checkoutable instanceof LicenseSeat) {
return $checkoutable->license->checkin_email();
}
- return (method_exists($checkoutable, 'checkin_email') && $checkoutable->checkin_email());
+
+ return method_exists($checkoutable, 'checkin_email') && $checkoutable->checkin_email();
}
private function newMicrosoftTeamsWebhookEnabled(): bool
@@ -448,7 +462,6 @@ class CheckoutableListener
* 2. The item has a EULA
* 3. The item should send an email at check-in/check-out
*/
-
if (Context::get('action') === 'bulk_asset_checkout') {
return false;
}
@@ -476,11 +489,11 @@ class CheckoutableListener
$setting = Setting::getSettings();
- if (!$setting) {
+ if (! $setting) {
return false;
}
- if (is_null($acceptance) && !$setting->admin_cc_always) {
+ if (is_null($acceptance) && ! $setting->admin_cc_always) {
return false;
}
@@ -513,16 +526,16 @@ class CheckoutableListener
}
// if user && no cc: to user
- if ($shouldSendEmailToUser && !$shouldSendEmailToAlertAddress) {
+ if ($shouldSendEmailToUser && ! $shouldSendEmailToAlertAddress) {
$to[] = $notifiable;
}
// if no user && cc: to admin
- if (!$shouldSendEmailToUser && $shouldSendEmailToAlertAddress) {
+ if (! $shouldSendEmailToUser && $shouldSendEmailToAlertAddress) {
$to[] = $this->getFormattedAlertAddresses();
}
- return array($to, $cc);
+ return [$to, $cc];
}
private function getCategoryFromCheckoutable(Model $checkoutable): ?Category
@@ -530,8 +543,8 @@ class CheckoutableListener
return match (true) {
$checkoutable instanceof Asset => $checkoutable->model->category,
$checkoutable instanceof Accessory,
- $checkoutable instanceof Consumable,
- $checkoutable instanceof Component => $checkoutable->category,
+ $checkoutable instanceof Consumable,
+ $checkoutable instanceof Component => $checkoutable->category,
$checkoutable instanceof LicenseSeat => $checkoutable->license->category,
};
}
diff --git a/app/Listeners/CheckoutablesCheckedOutInBulkListener.php b/app/Listeners/CheckoutablesCheckedOutInBulkListener.php
index c1ff57d911..492a1c6498 100644
--- a/app/Listeners/CheckoutablesCheckedOutInBulkListener.php
+++ b/app/Listeners/CheckoutablesCheckedOutInBulkListener.php
@@ -16,7 +16,6 @@ use Illuminate\Support\Facades\Mail;
class CheckoutablesCheckedOutInBulkListener
{
-
public function subscribe($events)
{
$events->listen(
@@ -45,7 +44,7 @@ class CheckoutablesCheckedOutInBulkListener
Log::info('BulkAssetCheckoutMail sent to checkout target');
} catch (Exception $e) {
- Log::debug("Exception caught during BulkAssetCheckoutMail to target: " . $e->getMessage());
+ Log::debug('Exception caught during BulkAssetCheckoutMail to target: '.$e->getMessage());
}
}
@@ -62,14 +61,14 @@ class CheckoutablesCheckedOutInBulkListener
Log::info('BulkAssetCheckoutMail sent to admin_cc_email');
} catch (Exception $e) {
- Log::debug("Exception caught during BulkAssetCheckoutMail to admin_cc_email: " . $e->getMessage());
+ Log::debug('Exception caught during BulkAssetCheckoutMail to admin_cc_email: '.$e->getMessage());
}
}
}
private function shouldSendCheckoutEmailToUser(?User $user, Collection $assets): bool
{
- if (!$user?->email) {
+ if (! $user?->email) {
return false;
}
@@ -88,7 +87,7 @@ class CheckoutablesCheckedOutInBulkListener
{
$setting = Setting::getSettings();
- if (!$setting) {
+ if (! $setting) {
return false;
}
@@ -96,7 +95,7 @@ class CheckoutablesCheckedOutInBulkListener
return true;
}
- if (!$this->hasAssetThatRequiresAcceptance($assets)) {
+ if (! $this->hasAssetThatRequiresAcceptance($assets)) {
return false;
}
@@ -142,6 +141,7 @@ class CheckoutablesCheckedOutInBulkListener
if ($target instanceof Asset) {
$target->load('assignedTo');
+
return $target->assignedto;
}
diff --git a/app/Listeners/LogFailedLogin.php b/app/Listeners/LogFailedLogin.php
index e26251edce..91dc0aaaef 100644
--- a/app/Listeners/LogFailedLogin.php
+++ b/app/Listeners/LogFailedLogin.php
@@ -3,8 +3,8 @@
namespace App\Listeners;
use Carbon\Carbon;
-use Illuminate\Support\Facades\DB;
use Illuminate\Auth\Events\Failed;
+use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class LogFailedLogin
@@ -22,12 +22,11 @@ class LogFailedLogin
/**
* Handle the event.
*
- * @param \Illuminate\Auth\Events\Failed $event
* @return void
*/
public function handle(Failed $event)
{
- $now = new Carbon();
+ $now = new Carbon;
try {
DB::table('login_attempts')->insert(
[
diff --git a/app/Listeners/LogListener.php b/app/Listeners/LogListener.php
index e4d35aa62d..c2de69d888 100644
--- a/app/Listeners/LogListener.php
+++ b/app/Listeners/LogListener.php
@@ -2,25 +2,14 @@
namespace App\Listeners;
-use App\Events\AccessoryCheckedIn;
-use App\Events\AccessoryCheckedOut;
-use App\Events\AssetCheckedIn;
-use App\Events\AssetCheckedOut;
use App\Events\CheckoutableCheckedIn;
use App\Events\CheckoutableCheckedOut;
use App\Events\CheckoutAccepted;
use App\Events\CheckoutDeclined;
-use App\Events\ComponentCheckedIn;
-use App\Events\ComponentCheckedOut;
-use App\Events\ConsumableCheckedOut;
-use App\Events\ItemAccepted;
-use App\Events\ItemDeclined;
-use App\Events\LicenseCheckedIn;
-use App\Events\LicenseCheckedOut;
+use App\Events\UserMerged;
use App\Models\Actionlog;
-use App\Models\User;
use App\Models\LicenseSeat;
-use App\Events\UserMerged;
+use App\Models\User;
use Illuminate\Support\Facades\Log;
class LogListener
@@ -28,9 +17,8 @@ class LogListener
/**
* These onBlah methods are used by the subscribe() method further down in this file.
* This one creates an action_logs entry for the checkin
- * @param CheckoutableCheckedIn $event
- * @return void
*
+ * @return void
*/
public function onCheckoutableCheckedIn(CheckoutableCheckedIn $event)
{
@@ -41,9 +29,7 @@ class LogListener
* These onBlah methods are used by the subscribe() method further down in this file.
* This one creates an action_logs entry for the checkout
*
- * @param CheckoutableCheckedOut $event
* @return void
- *
*/
public function onCheckoutableCheckedOut(CheckoutableCheckedOut $event)
{
@@ -64,7 +50,7 @@ class LogListener
{
Log::debug('event passed to the onCheckoutAccepted listener:');
- $logaction = new Actionlog();
+ $logaction = new Actionlog;
$logaction->item()->associate($event->acceptance->checkoutable);
$logaction->target()->associate($event->acceptance->assignedTo);
$logaction->accept_signature = $event->acceptance->signature_filename;
@@ -75,7 +61,6 @@ class LogListener
$logaction->quantity = $event->acceptance->qty ?? 1;
$logaction->created_by = auth()->user()->id;
-
// TODO: log the actual license seat that was checked out
if ($event->acceptance->checkoutable instanceof LicenseSeat) {
$logaction->item()->associate($event->acceptance->checkoutable->license);
@@ -86,7 +71,7 @@ class LogListener
public function onCheckoutDeclined(CheckoutDeclined $event)
{
- $logaction = new Actionlog();
+ $logaction = new Actionlog;
$logaction->item()->associate($event->acceptance->checkoutable);
$logaction->target()->associate($event->acceptance->assignedTo);
$logaction->accept_signature = $event->acceptance->signature_filename;
@@ -104,7 +89,6 @@ class LogListener
$logaction->save();
}
-
public function onUserMerged(UserMerged $event)
{
@@ -116,8 +100,8 @@ class LogListener
];
// Add a record to the users being merged FROM
- Log::debug('Users merged: '.$event->merged_from->id .' ('.$event->merged_from->username.') merged into '. $event->merged_to->id. ' ('.$event->merged_to->username.')');
- $logaction = new Actionlog();
+ Log::debug('Users merged: '.$event->merged_from->id.' ('.$event->merged_from->username.') merged into '.$event->merged_to->id.' ('.$event->merged_to->username.')');
+ $logaction = new Actionlog;
$logaction->item_id = $event->merged_from->id;
$logaction->item_type = User::class;
$logaction->target_id = $event->merged_to->id;
@@ -128,7 +112,7 @@ class LogListener
$logaction->save();
// Add a record to the users being merged TO
- $logaction = new Actionlog();
+ $logaction = new Actionlog;
$logaction->target_id = $event->merged_from->id;
$logaction->target_type = User::class;
$logaction->item_id = $event->merged_to->id;
@@ -138,7 +122,6 @@ class LogListener
$logaction->created_by = $event->admin->id ?? null;
$logaction->save();
-
}
/**
@@ -164,6 +147,4 @@ class LogListener
);
}
}
-
-
}
diff --git a/app/Listeners/LogSuccessfulLogin.php b/app/Listeners/LogSuccessfulLogin.php
index d6d9c618f2..3de4cd854d 100644
--- a/app/Listeners/LogSuccessfulLogin.php
+++ b/app/Listeners/LogSuccessfulLogin.php
@@ -3,9 +3,10 @@
namespace App\Listeners;
use Carbon\Carbon;
-use Illuminate\Support\Facades\DB;
use Illuminate\Auth\Events\Login;
+use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
+
class LogSuccessfulLogin
{
/**
@@ -21,12 +22,11 @@ class LogSuccessfulLogin
/**
* Handle the event.
*
- * @param Login $event
* @return void
*/
public function handle(Login $event)
{
- $now = new Carbon();
+ $now = new Carbon;
try {
DB::table('login_attempts')->insert(