Skip to content

Commit

Permalink
Merge pull request #98 from fleetbase/utility-methods-to-get-company
Browse files Browse the repository at this point in the history
improved auth utility methods to fetch an organization/company
  • Loading branch information
roncodes authored Jul 2, 2024
2 parents 8069d7c + 8660b6d commit 05e9272
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 13 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Resources/ChatParticipant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Resources/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/Models/ChatChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/Models/ChatParticipant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
68 changes: 59 additions & 9 deletions src/Support/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -200,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);
}

/**
Expand Down

0 comments on commit 05e9272

Please sign in to comment.