From 8a4891a15cef6240e086b8d10ee2a87febeb1037 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Mon, 10 Jun 2024 13:43:05 +0800 Subject: [PATCH 1/5] improved auth utility methods to fetch an organization/company --- src/Support/Auth.php | 60 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/src/Support/Auth.php b/src/Support/Auth.php index d6eec69..7199856 100644 --- a/src/Support/Auth.php +++ b/src/Support/Auth.php @@ -158,15 +158,65 @@ public static function setSandboxSession($request, $apiCredential = null) } /** - * Get the session company. + * Retrieves a company entity based on session or request parameters. + * + * This method first attempts to fetch the company information from the session. If it is not available, + * it looks for the company identifier either as 'company' or 'company_uuid' in the request parameters. + * The function supports dynamic selection of fields specified by the $select parameter, which can be + * a string or an array of field names. + * + * @param string|array $select the fields to select from the company model, defaults to all (*) + * + * @return Company|null returns the Company object if found, or null if no company is identified + * + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException throws exception if no model is found */ - public static function getCompany($select = '*'): ?Company + public static function getCompany(string|array $select = '*'): ?Company { - if (!session('company')) { - return null; + $company = null; + + if (session()->has('company')) { + $company = Company::select($select)->where('uuid', session('company'))->first(); + } + + if (!$company) { + $companyId = request()->or(['company', 'company_uuid']); + if ($companyId) { + $company = Company::select($select)->where(function ($query) use ($companyId) { + $query->where('uuid', $companyId); + $query->orWhere('public_id', $companyId); + })->first(); + } + } + + return $company; + } + + /** + * Fetches a company entity based on the provided HTTP request. + * + * This method looks for a company identifier in the request using 'company' or 'company_uuid' keys. + * It then retrieves a company from the database where the company's UUID or public ID matches the given identifier. + * The method assumes the request object has a method `or` that fetches the values for specified keys. + * + * @param Request $request the HTTP request object containing potential company identifiers + * + * @return Company returns the Company object if found based on the identifiers + * + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException throws exception if no model is found + */ + public static function getCompanyFromRequest(Request $request): Company + { + $company = null; + $companyId = $request->or(['company', 'company_uuid']); + if ($companyId) { + $company = Company::where(function ($query) use ($companyId) { + $query->where('uuid', $companyId); + $query->orWhere('public_id', $companyId); + })->first(); } - return Company::select($select)->where('uuid', session('company'))->first(); + return $company; } /** From 4c34c40049a1f1f8954d0d37da754724abeebd2f Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Mon, 10 Jun 2024 14:25:44 +0800 Subject: [PATCH 2/5] properly use the user resource for the owner key in organization resource --- src/Http/Resources/Organization.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/Resources/Organization.php b/src/Http/Resources/Organization.php index e5a7492..cc200af 100644 --- a/src/Http/Resources/Organization.php +++ b/src/Http/Resources/Organization.php @@ -30,7 +30,7 @@ public function toArray($request) 'backdrop_url' => $this->backdrop_url, 'branding' => Setting::getBranding(), 'options' => $this->options, - 'owner' => new Author($this->owner), + 'owner' => new User($this->owner), 'slug' => $this->slug, 'status' => $this->status, 'updated_at' => $this->updated_at, From fc070f30d7a1a5496f11ac647f956e1a8a1f6285 Mon Sep 17 00:00:00 2001 From: Doljinsuren Enkhbayar Date: Mon, 24 Jun 2024 17:54:58 +0800 Subject: [PATCH 3/5] fix sign up driver --- src/Http/Resources/ChatParticipant.php | 2 +- src/Models/ChatChannel.php | 2 +- src/Models/ChatParticipant.php | 7 +++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Http/Resources/ChatParticipant.php b/src/Http/Resources/ChatParticipant.php index 1ec4a52..cf03f5a 100644 --- a/src/Http/Resources/ChatParticipant.php +++ b/src/Http/Resources/ChatParticipant.php @@ -21,7 +21,7 @@ public function toArray($request) 'chat_channel_uuid' => $this->when(Http::isInternalRequest(), $this->chat_channel_uuid), 'chat_channel' => $this->when(Http::isPublicRequest(), $this->chatChannel->public_id), 'user_uuid' => $this->when(Http::isInternalRequest(), $this->user_uuid), - 'user' => $this->when(Http::isPublicRequest(), $this->user->public_id), + 'user' => $this->when(Http::isPublicRequest(), $this->user ? $this->user->public_id : null), 'name' => $this->user->name, 'username' => $this->user->username, 'email' => $this->user->email, diff --git a/src/Models/ChatChannel.php b/src/Models/ChatChannel.php index 8a82f68..08aa51a 100644 --- a/src/Models/ChatChannel.php +++ b/src/Models/ChatChannel.php @@ -128,7 +128,7 @@ public function lastMessage() */ public function participants() { - return $this->hasMany(ChatParticipant::class, 'chat_channel_uuid', 'uuid'); + return $this->hasMany(ChatParticipant::class, 'chat_channel_uuid', 'uuid')->whereHas('user'); } /** diff --git a/src/Models/ChatParticipant.php b/src/Models/ChatParticipant.php index a6bf780..2aec625 100644 --- a/src/Models/ChatParticipant.php +++ b/src/Models/ChatParticipant.php @@ -43,6 +43,13 @@ class ChatParticipant extends Model */ protected $appends = ['is_online', 'last_seen_at']; + /** + * The relationships to always load along with the model. + * + * @var array + */ + protected $with = ['user']; + /** * The attributes that should be cast to native types. * From 85ac879fdb867fb352bbd6f7d7f88e3398f24b0f Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Tue, 2 Jul 2024 14:00:18 +0800 Subject: [PATCH 4/5] bettern arg naming on password hash checks --- src/Support/Auth.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Support/Auth.php b/src/Support/Auth.php index 7199856..4b63b01 100644 --- a/src/Support/Auth.php +++ b/src/Support/Auth.php @@ -250,17 +250,17 @@ public static function getUserFromSession() /** * Verifies a password against a hash. */ - public static function checkPassword(string $pw1, string $pw2): bool + public static function checkPassword(string $password, string $hashedPassword): bool { - return Hash::check($pw1, $pw2); + return Hash::check($password, $hashedPassword); } /** * Checks if password is invalid. */ - public static function isInvalidPassword(string $pw1, string $pw2): bool + public static function isInvalidPassword(string $password, string $hashedPassword): bool { - return !static::checkPassword($pw1, $pw2); + return !static::checkPassword($password, $hashedPassword); } /** From 8660b6d80c3ba3ed1013f3ab0ba6265e0ed25e2e Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Tue, 2 Jul 2024 14:02:09 +0800 Subject: [PATCH 5/5] bump version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 950c5d8..b3f962b 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "fleetbase/core-api", - "version": "1.4.25", + "version": "1.4.26", "description": "Core Framework and Resources for Fleetbase API", "keywords": [ "fleetbase",