From 45afe725a1f039dddd87537e16470963684f0711 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 9 Jul 2020 20:03:47 -0700 Subject: [PATCH 1/3] =?UTF-8?q?Only=20try=20to=20get=20the=20company=20if?= =?UTF-8?q?=20there=20is=20an=20auth=E2=80=99d=20user?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (Needed for command line tools, where no Auth::user() is present) --- app/Models/Company.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/Models/Company.php b/app/Models/Company.php index bb3467c38c38..ea92f2595236 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -128,9 +128,12 @@ public static function isCurrentUserHasAccess($companyable) } elseif (!static::isFullMultipleCompanySupportEnabled()) { return true; } else { - $current_user_company_id = Auth::user()->company_id; - $companyable_company_id = $companyable->company_id; - return ($current_user_company_id == null || $current_user_company_id == $companyable_company_id || Auth::user()->isSuperUser()); + if (Auth::user()) { + $current_user_company_id = Auth::user()->company_id; + $companyable_company_id = $companyable->company_id; + return ($current_user_company_id == null || $current_user_company_id == $companyable_company_id || Auth::user()->isSuperUser()); + } + } } From e2570ada6f158dfc9acead583a0b2fa7fae17ca6 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 9 Jul 2020 20:04:05 -0700 Subject: [PATCH 2/3] CLI tool to checkout a license to ALL users --- .../Commands/CheckoutLicenseToAllUsers.php | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 app/Console/Commands/CheckoutLicenseToAllUsers.php diff --git a/app/Console/Commands/CheckoutLicenseToAllUsers.php b/app/Console/Commands/CheckoutLicenseToAllUsers.php new file mode 100644 index 000000000000..f87a9cfa2875 --- /dev/null +++ b/app/Console/Commands/CheckoutLicenseToAllUsers.php @@ -0,0 +1,103 @@ +option('license_id'); + $notify = $this->option('notify'); + + if (!$license_id) { + $this->error('ERROR: License ID is required.'); + return false; + } + + + if (!$license = License::find($license_id)->first()) { + $this->error('Invalid license ID'); + return false; + } + $users = User::whereNull('deleted_at')->get(); + + if ($users->count() > $license->getAvailSeatsCountAttribute()) { + $this->info('You do not have enough free seats to complete this task, so we will check out as many as we can. '); + } + + $this->info('Checking out '.$users->count().' of '.$license->getAvailSeatsCountAttribute().' seats for '.$license->name); + + if (!$notify) { + $this->info('No mail will be sent.'); + } + + foreach ($users as $user) { + // If the license is valid, check that there is an available seat + if ($license->getAvailSeatsCountAttribute() < 1) { + $this->error('ERROR: No available seats'); + return false; + } + + // Get the seat ID + $next = $license->freeSeat(); + if (!$licenseSeat = LicenseSeat::where('id', '=', $next->id)->first()) { + $this->error('ERROR: No available seats'); + return false; + } + + // Update the seat with checkout info, + $licenseSeat->assigned_to = $user->id; + if ($licenseSeat->save()) { + + // Temporarily null the user's email address so we don't send mail if we're not supposed to + if (!$notify) { + $user->email = null; + } + + // Log the checkout + $licenseSeat->logCheckout('Checked out via cli tool', $user); + $this->info('License '.$license_id.' checked out to '.$user->username); + } + + } + + + + } +} From 89d433b41aa0de862cb60142c8d6ef80f339a958 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 9 Jul 2020 20:26:02 -0700 Subject: [PATCH 3/3] Removed duplicate seat call --- app/Console/Commands/CheckoutLicenseToAllUsers.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/app/Console/Commands/CheckoutLicenseToAllUsers.php b/app/Console/Commands/CheckoutLicenseToAllUsers.php index f87a9cfa2875..bf8941233b97 100644 --- a/app/Console/Commands/CheckoutLicenseToAllUsers.php +++ b/app/Console/Commands/CheckoutLicenseToAllUsers.php @@ -75,11 +75,8 @@ public function handle() } // Get the seat ID - $next = $license->freeSeat(); - if (!$licenseSeat = LicenseSeat::where('id', '=', $next->id)->first()) { - $this->error('ERROR: No available seats'); - return false; - } + $licenseSeat = $license->freeSeat(); + // Update the seat with checkout info, $licenseSeat->assigned_to = $user->id;