From 9d48e8800d7c65e8f088cc8c7f4d88038b31d950 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Fri, 16 Feb 2024 16:12:57 +0700 Subject: [PATCH 1/4] hotfix user onboard registration bug --- composer.json | 2 +- .../Internal/v1/UserController.php | 1 + src/Models/User.php | 15 +++++---- src/Observers/UserObserver.php | 32 ------------------- 4 files changed, 11 insertions(+), 39 deletions(-) diff --git a/composer.json b/composer.json index 16a4dcc..950e915 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "fleetbase/core-api", - "version": "1.4.4", + "version": "1.4.5", "description": "Core Framework and Resources for Fleetbase API", "keywords": [ "fleetbase", diff --git a/src/Http/Controllers/Internal/v1/UserController.php b/src/Http/Controllers/Internal/v1/UserController.php index d8fcf24..c7f6a78 100644 --- a/src/Http/Controllers/Internal/v1/UserController.php +++ b/src/Http/Controllers/Internal/v1/UserController.php @@ -17,6 +17,7 @@ use Fleetbase\Models\Invite; use Fleetbase\Models\Setting; use Fleetbase\Models\User; +use Fleetbase\Notifications\UserCreated; use Fleetbase\Notifications\UserAcceptedCompanyInvite; use Fleetbase\Notifications\UserInvited; use Fleetbase\Support\Auth; diff --git a/src/Models/User.php b/src/Models/User.php index 2bc76eb..0e46c92 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -3,7 +3,9 @@ namespace Fleetbase\Models; use Fleetbase\Casts\Json; +use Fleetbase\Notifications\UserCreated; use Fleetbase\Notifications\UserInvited; +use Fleetbase\Support\NotificationRegistry; use Fleetbase\Support\Utils; use Fleetbase\Traits\Expandable; use Fleetbase\Traits\Filterable; @@ -214,6 +216,12 @@ public function assignCompany(Company $company) CompanyUser::create(['company_uuid' => $company->uuid, 'user_uuid' => $this->uuid, 'status' => $this->status]); } + // Invite user to join company + $this->sendInviteFromCompany($company); + + // Notify the company owner a user has been created + NotificationRegistry::notify(UserCreated::class, $this, $company); + $this->save(); } @@ -582,12 +590,7 @@ public function sendInviteFromCompany(Company $company = null): bool } // make sure user isn't already invited - $isAlreadyInvited = Invite::where([ - 'company_uuid' => $company->uuid, - 'subject_uuid' => $company->uuid, - 'protocol' => 'email', - 'reason' => 'join_company', - ])->whereJsonContains('recipients', $this->email)->exists(); + $isAlreadyInvited = Invite::isAlreadySentToJoinCompany($this, $company); if ($isAlreadyInvited) { return false; } diff --git a/src/Observers/UserObserver.php b/src/Observers/UserObserver.php index 039ea27..d708e12 100644 --- a/src/Observers/UserObserver.php +++ b/src/Observers/UserObserver.php @@ -4,40 +4,8 @@ use Fleetbase\Models\CompanyUser; use Fleetbase\Models\User; -use Fleetbase\Notifications\UserCreated; -use Fleetbase\Support\NotificationRegistry; - class UserObserver { - /** - * Handle the User "created" event. - * - * @return void - */ - public function created(User $user) - { - // Make sure we have company - $company = $user->getCompany(); - - // If no company delete user and throw exception - if (!$company) { - $user->deleteQuietly(); - throw new \Exception('Unable to assign user to company.'); - } - - if (CompanyUser::where(['company_uuid' => $company->uuid, 'user_uuid' => $user->uuid])->doesntExist()) { - CompanyUser::create(['company_uuid' => $company->uuid, 'user_uuid' => $user->uuid, 'status' => $user->status]); - } - - // invite user to join company - $user->sendInviteFromCompany($company); - - // Notify the company owner a user has been created - if ($company) { - NotificationRegistry::notify(UserCreated::class, $user, $company); - } - } - /** * Handle the User "deleted" event. * From b43c8465e6ce6c492d0ed5c9d1ec72cf32b65bfc Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Fri, 16 Feb 2024 16:16:31 +0700 Subject: [PATCH 2/4] ran linter --- src/Http/Controllers/Internal/v1/UserController.php | 1 - src/Observers/UserObserver.php | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/Controllers/Internal/v1/UserController.php b/src/Http/Controllers/Internal/v1/UserController.php index c7f6a78..d8fcf24 100644 --- a/src/Http/Controllers/Internal/v1/UserController.php +++ b/src/Http/Controllers/Internal/v1/UserController.php @@ -17,7 +17,6 @@ use Fleetbase\Models\Invite; use Fleetbase\Models\Setting; use Fleetbase\Models\User; -use Fleetbase\Notifications\UserCreated; use Fleetbase\Notifications\UserAcceptedCompanyInvite; use Fleetbase\Notifications\UserInvited; use Fleetbase\Support\Auth; diff --git a/src/Observers/UserObserver.php b/src/Observers/UserObserver.php index d708e12..958c6ba 100644 --- a/src/Observers/UserObserver.php +++ b/src/Observers/UserObserver.php @@ -4,6 +4,7 @@ use Fleetbase\Models\CompanyUser; use Fleetbase\Models\User; + class UserObserver { /** From e2e1950e52423e6cb9135f74cf0c95521566a757 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Fri, 16 Feb 2024 17:30:06 +0700 Subject: [PATCH 3/4] hotfix patch webhook request log callback event listeners --- src/Http/Filter/CompanyFilter.php | 7 +++++ src/Http/Requests/AdminRequest.php | 3 +-- src/Http/Resources/Organization.php | 33 +++++++++++++----------- src/Listeners/LogFailedWebhook.php | 18 ++++++++++--- src/Listeners/LogFinalWebhookAttempt.php | 18 ++++++++++--- src/Listeners/LogSuccessfulWebhook.php | 19 +++++++++++--- src/Models/Company.php | 8 ++++++ src/Models/User.php | 11 +++++--- 8 files changed, 85 insertions(+), 32 deletions(-) diff --git a/src/Http/Filter/CompanyFilter.php b/src/Http/Filter/CompanyFilter.php index 475f5a1..6864957 100644 --- a/src/Http/Filter/CompanyFilter.php +++ b/src/Http/Filter/CompanyFilter.php @@ -6,6 +6,13 @@ class CompanyFilter extends Filter { public function queryForInternal() { + // If admin query then do not filter + $isAdminQuery = $this->request->input('view') === 'admin' && $this->request->user()->isAdmin(); + if ($isAdminQuery) { + return; + } + + // Otherwise filter so that user only see's their own companies $this->builder->where( function ($query) { $query diff --git a/src/Http/Requests/AdminRequest.php b/src/Http/Requests/AdminRequest.php index 04bb3cf..4a8ab39 100644 --- a/src/Http/Requests/AdminRequest.php +++ b/src/Http/Requests/AdminRequest.php @@ -12,8 +12,7 @@ class AdminRequest extends FleetbaseRequest public function authorize() { $user = $this->user(); - - if (!$user === null) { + if (!$user) { return false; } diff --git a/src/Http/Resources/Organization.php b/src/Http/Resources/Organization.php index e24e99e..e5a7492 100644 --- a/src/Http/Resources/Organization.php +++ b/src/Http/Resources/Organization.php @@ -17,21 +17,24 @@ class Organization extends FleetbaseResource public function toArray($request) { return [ - 'id' => $this->when(Http::isInternalRequest(), $this->id, $this->public_id), - 'uuid' => $this->when(Http::isInternalRequest(), $this->uuid), - 'public_id' => $this->when(Http::isInternalRequest(), $this->public_id), - 'name' => $this->name, - 'description' => $this->description, - 'phone' => $this->phone, - 'timezone' => $this->timezone, - 'logo_url' => $this->logo_url, - 'backdrop_url' => $this->backdrop_url, - 'branding' => Setting::getBranding(), - 'options' => $this->options, - 'slug' => $this->slug, - 'status' => $this->status, - 'updated_at' => $this->updated_at, - 'created_at' => $this->created_at, + 'id' => $this->when(Http::isInternalRequest(), $this->id, $this->public_id), + 'uuid' => $this->when(Http::isInternalRequest(), $this->uuid), + 'public_id' => $this->when(Http::isInternalRequest(), $this->public_id), + 'name' => $this->name, + 'description' => $this->description, + 'phone' => $this->phone, + 'type' => $this->when(Http::isInternalRequest(), $this->type), + 'users_count' => $this->when(Http::isInternalRequest(), $this->companyUsers()->count()), + 'timezone' => $this->timezone, + 'logo_url' => $this->logo_url, + 'backdrop_url' => $this->backdrop_url, + 'branding' => Setting::getBranding(), + 'options' => $this->options, + 'owner' => new Author($this->owner), + 'slug' => $this->slug, + 'status' => $this->status, + 'updated_at' => $this->updated_at, + 'created_at' => $this->created_at, ]; } } diff --git a/src/Listeners/LogFailedWebhook.php b/src/Listeners/LogFailedWebhook.php index 048cb56..72b2d6a 100644 --- a/src/Listeners/LogFailedWebhook.php +++ b/src/Listeners/LogFailedWebhook.php @@ -4,6 +4,7 @@ use Fleetbase\Models\WebhookRequestLog; use Fleetbase\Webhook\Events\WebhookCallFailedEvent; +use Illuminate\Support\Str; class LogFailedWebhook { @@ -23,8 +24,11 @@ public function handle(WebhookCallFailedEvent $event) /** @var string $connection The db connection the webhook was called on */ $connection = (bool) data_get($event, 'meta.is_sandbox') ? 'sandbox' : 'mysql'; - // Log webhook callback event - WebhookRequestLog::on($connection)->create([ + // Get API credential + $apiCredentialUuid = data_get($event, 'meta.api_credential_uuid'); + + // Prepare insert array + $data = [ '_key' => data_get($event, 'meta.api_key'), 'company_uuid' => data_get($event, 'meta.company_uuid'), 'api_credential_uuid' => data_get($event, 'meta.api_credential_uuid'), @@ -41,6 +45,14 @@ public function handle(WebhookCallFailedEvent $event) 'headers' => $event->headers, 'meta' => $event->meta, 'sent_at' => data_get($event, 'meta.sent_at'), - ]); + ]; + + // Set api credential uuid + if ($apiCredentialUuid && Str::isUuuid($apiCredentialUuid)) { + $data['api_credential_uuid'] = $apiCredentialUuid; + } + + // Log webhook callback event + WebhookRequestLog::on($connection)->create($data); } } diff --git a/src/Listeners/LogFinalWebhookAttempt.php b/src/Listeners/LogFinalWebhookAttempt.php index 646b146..d41bf71 100644 --- a/src/Listeners/LogFinalWebhookAttempt.php +++ b/src/Listeners/LogFinalWebhookAttempt.php @@ -26,11 +26,13 @@ public function handle(FinalWebhookCallFailedEvent $event) /** @var int $statusCode The response status code */ $statusCode = $response ? $response->getStatusCode() : 500; - // log webhook event - WebhookRequestLog::on($connection)->create([ + // Get API credential + $apiCredentialUuid = data_get($event, 'meta.api_credential_uuid'); + + // Prepare insert array + $data = [ '_key' => data_get($event, 'meta.api_key'), 'company_uuid' => data_get($event, 'meta.company_uuid'), - 'api_credential_uuid' => data_get($event, 'meta.api_credential_uuid'), 'webhook_uuid' => data_get($event, 'meta.webhook_uuid'), 'api_event_uuid' => data_get($event, 'meta.api_event_uuid'), 'method' => $event->httpVerb, @@ -44,6 +46,14 @@ public function handle(FinalWebhookCallFailedEvent $event) 'headers' => $event->headers, 'meta' => $event->meta, 'sent_at' => data_get($event, 'meta.sent_at'), - ]); + ]; + + // Set api credential uuid + if ($apiCredentialUuid && Str::isUuuid($apiCredentialUuid)) { + $data['api_credential_uuid'] = $apiCredentialUuid; + } + + // log webhook event + WebhookRequestLog::on($connection)->create($data); } } diff --git a/src/Listeners/LogSuccessfulWebhook.php b/src/Listeners/LogSuccessfulWebhook.php index 2a154d7..06741d8 100644 --- a/src/Listeners/LogSuccessfulWebhook.php +++ b/src/Listeners/LogSuccessfulWebhook.php @@ -4,6 +4,7 @@ use Fleetbase\Models\WebhookRequestLog; use Fleetbase\Webhook\Events\WebhookCallSucceededEvent; +use Illuminate\Support\Str; class LogSuccessfulWebhook { @@ -23,11 +24,13 @@ public function handle(WebhookCallSucceededEvent $event) /** @var string $connection The db connection the webhook was called on */ $connection = (bool) data_get($event, 'meta.is_sandbox') ? 'sandbox' : 'mysql'; - // Log webhook callback event - WebhookRequestLog::on($connection)->create([ + // Get API credential + $apiCredentialUuid = data_get($event, 'meta.api_credential_uuid'); + + // Prepare insert array + $data = [ '_key' => data_get($event, 'meta.api_key'), 'company_uuid' => data_get($event, 'meta.company_uuid'), - 'api_credential_uuid' => data_get($event, 'meta.api_credential_uuid'), 'webhook_uuid' => data_get($event, 'meta.webhook_uuid'), 'api_event_uuid' => data_get($event, 'meta.api_event_uuid'), 'method' => $event->httpVerb, @@ -41,6 +44,14 @@ public function handle(WebhookCallSucceededEvent $event) 'headers' => $event->headers, 'meta' => $event->meta, 'sent_at' => data_get($event, 'meta.sent_at'), - ]); + ]; + + // Set api credential uuid + if ($apiCredentialUuid && Str::isUuuid($apiCredentialUuid)) { + $data['api_credential_uuid'] = $apiCredentialUuid; + } + + // Log webhook callback event + WebhookRequestLog::on($connection)->create($data); } } diff --git a/src/Models/Company.php b/src/Models/Company.php index 745ff46..86bea05 100644 --- a/src/Models/Company.php +++ b/src/Models/Company.php @@ -163,6 +163,14 @@ public function users() return $this->belongsToMany(User::class, 'company_users'); } + /** + * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany + */ + public function companyUsers() + { + return $this->hasManyThrough(User::class, CompanyUser::class, 'company_uuid', 'uuid', 'uuid', 'user_uuid'); + } + /** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ diff --git a/src/Models/User.php b/src/Models/User.php index 0e46c92..398e0a8 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -216,11 +216,14 @@ public function assignCompany(Company $company) CompanyUser::create(['company_uuid' => $company->uuid, 'user_uuid' => $this->uuid, 'status' => $this->status]); } - // Invite user to join company - $this->sendInviteFromCompany($company); + // Determine if user should receive invite to join company + if ($user->isNotAdmin()) { + // Invite user to join company + $this->sendInviteFromCompany($company); - // Notify the company owner a user has been created - NotificationRegistry::notify(UserCreated::class, $this, $company); + // Notify the company owner a user has been created + NotificationRegistry::notify(UserCreated::class, $this, $company); + } $this->save(); } From 2e2b966a463cead32f2e14efe836c1f37e5c66de Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Sat, 17 Feb 2024 19:00:42 +0800 Subject: [PATCH 4/4] fix webhook event logging --- src/Listeners/LogFailedWebhook.php | 2 +- src/Listeners/LogFinalWebhookAttempt.php | 2 +- src/Listeners/LogSuccessfulWebhook.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Listeners/LogFailedWebhook.php b/src/Listeners/LogFailedWebhook.php index 72b2d6a..dc51e35 100644 --- a/src/Listeners/LogFailedWebhook.php +++ b/src/Listeners/LogFailedWebhook.php @@ -48,7 +48,7 @@ public function handle(WebhookCallFailedEvent $event) ]; // Set api credential uuid - if ($apiCredentialUuid && Str::isUuuid($apiCredentialUuid)) { + if ($apiCredentialUuid && Str::isUuid($apiCredentialUuid)) { $data['api_credential_uuid'] = $apiCredentialUuid; } diff --git a/src/Listeners/LogFinalWebhookAttempt.php b/src/Listeners/LogFinalWebhookAttempt.php index d41bf71..6a3e51f 100644 --- a/src/Listeners/LogFinalWebhookAttempt.php +++ b/src/Listeners/LogFinalWebhookAttempt.php @@ -49,7 +49,7 @@ public function handle(FinalWebhookCallFailedEvent $event) ]; // Set api credential uuid - if ($apiCredentialUuid && Str::isUuuid($apiCredentialUuid)) { + if ($apiCredentialUuid && Str::isUuid($apiCredentialUuid)) { $data['api_credential_uuid'] = $apiCredentialUuid; } diff --git a/src/Listeners/LogSuccessfulWebhook.php b/src/Listeners/LogSuccessfulWebhook.php index 06741d8..0183fc7 100644 --- a/src/Listeners/LogSuccessfulWebhook.php +++ b/src/Listeners/LogSuccessfulWebhook.php @@ -47,7 +47,7 @@ public function handle(WebhookCallSucceededEvent $event) ]; // Set api credential uuid - if ($apiCredentialUuid && Str::isUuuid($apiCredentialUuid)) { + if ($apiCredentialUuid && Str::isUuid($apiCredentialUuid)) { $data['api_credential_uuid'] = $apiCredentialUuid; }