From f1a6308002caa865fe1a9b17b91d34fbfdd94a75 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 14 Aug 2020 14:43:07 -0700 Subject: [PATCH 1/5] Check for Auth::user before trying to log id (for cli) --- app/Models/Loggable.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/Models/Loggable.php b/app/Models/Loggable.php index 71a4e298c194..ea374dd4868c 100644 --- a/app/Models/Loggable.php +++ b/app/Models/Loggable.php @@ -144,7 +144,9 @@ public function logCheckin($target, $note) $log->location_id = null; $log->note = $note; - $log->user_id = Auth::user()->id; + if(Auth::user()) + $log->user_id = Auth::user()->id; + $log->logaction('checkin from'); $params = [ From e0f6f9b83972ef9fde79dbc342555580a0574591 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 14 Aug 2020 14:43:37 -0700 Subject: [PATCH 2/5] Artisan command to check in licenses from all users --- .../Commands/CheckinLicensesFromAllUsers.php | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 app/Console/Commands/CheckinLicensesFromAllUsers.php diff --git a/app/Console/Commands/CheckinLicensesFromAllUsers.php b/app/Console/Commands/CheckinLicensesFromAllUsers.php new file mode 100644 index 000000000000..fac3a92afaaa --- /dev/null +++ b/app/Console/Commands/CheckinLicensesFromAllUsers.php @@ -0,0 +1,94 @@ +option('license_id'); + $notify = $this->option('notify'); + + if (!$license_id) { + $this->error('ERROR: License ID is required.'); + return false; + } + + + if (!$license = License::where('id','=',$license_id)->first()) { + $this->error('Invalid license ID'); + return false; + } + + $this->info('Checking in ALL seats for '.$license->name); + + + $licenseSeats = LicenseSeat::where('license_id', '=', $license_id) + ->whereNotNull('assigned_to') + ->with('user') + ->get(); + + $this->info(' There are ' .$licenseSeats->count(). ' seats checked out: '); + + if (!$notify) { + $this->info('No mail will be sent.'); + } + + foreach ($licenseSeats as $seat) { + $this->info($seat->user->username .' has a license seat for '.$license->name); + + if (!$notify) { + $seat->user->email = null; + } + + $seat->assigned_to = null; + + if ($seat->save()) { + // Log the checkin + $seat->logCheckin($license, 'Checked in via cli tool'); + } + + + + + } + + + } +} From 02913235020d242e959f274fec588d9ebf8e39fa Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 14 Aug 2020 14:57:58 -0700 Subject: [PATCH 3/5] Use the user as the target --- app/Console/Commands/CheckinLicensesFromAllUsers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Console/Commands/CheckinLicensesFromAllUsers.php b/app/Console/Commands/CheckinLicensesFromAllUsers.php index fac3a92afaaa..a4e74778172c 100644 --- a/app/Console/Commands/CheckinLicensesFromAllUsers.php +++ b/app/Console/Commands/CheckinLicensesFromAllUsers.php @@ -81,7 +81,7 @@ public function handle() if ($seat->save()) { // Log the checkin - $seat->logCheckin($license, 'Checked in via cli tool'); + $seat->logCheckin($seat->user, 'Checked in via cli tool'); } From 134e8e6fb9958e71b8fa960de53c041324bd9e1c Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 14 Aug 2020 15:25:07 -0700 Subject: [PATCH 4/5] Moved user email nulling until after the save --- app/Console/Commands/CheckinLicensesFromAllUsers.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/Console/Commands/CheckinLicensesFromAllUsers.php b/app/Console/Commands/CheckinLicensesFromAllUsers.php index a4e74778172c..bcd8583eafa4 100644 --- a/app/Console/Commands/CheckinLicensesFromAllUsers.php +++ b/app/Console/Commands/CheckinLicensesFromAllUsers.php @@ -72,14 +72,15 @@ public function handle() foreach ($licenseSeats as $seat) { $this->info($seat->user->username .' has a license seat for '.$license->name); - - if (!$notify) { - $seat->user->email = null; - } - $seat->assigned_to = null; if ($seat->save()) { + + // Override the email address so we don't notify on checkin + if (!$notify) { + $seat->user->email = null; + } + // Log the checkin $seat->logCheckin($seat->user, 'Checked in via cli tool'); } From 29f3a5c48f9b9fc4fcfb19cc6eebb1ce1e0e5a91 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 14 Aug 2020 15:27:40 -0700 Subject: [PATCH 5/5] Use more verbose annotation for Auth::user if/else --- app/Models/Loggable.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/Models/Loggable.php b/app/Models/Loggable.php index ea374dd4868c..612e4f65cd0c 100644 --- a/app/Models/Loggable.php +++ b/app/Models/Loggable.php @@ -41,8 +41,9 @@ public function logCheckout($note, $target /* What are we checking out to? */) $settings = Setting::getSettings(); $log = new Actionlog; $log = $this->determineLogItemType($log); - if(Auth::user()) + if (Auth::user()) { $log->user_id = Auth::user()->id; + } if (!isset($target)) { throw new \Exception('All checkout logs require a target.'); @@ -144,9 +145,11 @@ public function logCheckin($target, $note) $log->location_id = null; $log->note = $note; - if(Auth::user()) - $log->user_id = Auth::user()->id; + if (Auth::user()) { + $log->user_id = Auth::user()->id; + } + $log->logaction('checkin from'); $params = [