Skip to content

Commit

Permalink
Merge pull request #38 from fleetbase/dev-v0.3.3
Browse files Browse the repository at this point in the history
v0.3.3
  • Loading branch information
roncodes authored Dec 25, 2023
2 parents e10f6e6 + 6717d78 commit d08e0bd
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 79 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.3.2",
"version": "1.3.3",
"description": "Core Framework and Resources for Fleetbase API",
"keywords": [
"fleetbase",
Expand Down
47 changes: 47 additions & 0 deletions src/Http/Controllers/Api/v1/OrganizationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Fleetbase\Http\Controllers\Api\v1;

use Fleetbase\Http\Controllers\Controller;
use Fleetbase\Http\Resources\Organization;
use Fleetbase\Models\ApiCredential;
use Fleetbase\Models\Company;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class OrganizationController extends Controller
{
public function getCurrent(Request $request)
{
$token = $request->bearerToken();
$isSecretKey = Str::startsWith($token, '$');

// Depending on API key format set the connection to find credential on
$connection = Str::startsWith($token, 'flb_test_') ? 'sandbox' : 'mysql';

// Find the API Credential record
$findApKey = ApiCredential::on($connection)
->where(function ($query) use ($isSecretKey, $token) {
if ($isSecretKey) {
$query->where('secret', $token);
} else {
$query->where('key', $token);
}
})
->with(['company.owner'])
->withoutGlobalScopes();

// Get the api credential model record
$apiCredential = $findApKey->first();

// Handle no api credential found
if (!$apiCredential) {
return response()->error('No API key found to fetch company details with.');
}

// Get the organization owning the API key
$organization = Company::where('uuid', $apiCredential->company_uuid)->first();

return new Organization($organization);
}
}
2 changes: 1 addition & 1 deletion src/Http/Controllers/Internal/v1/OnboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function createAccount(OnboardRequest $request)
'status' => 'success',
'session' => $user->uuid,
'token' => $token->plainTextToken,
'skipVerification' => $user->id === 1,
'skipVerification' => $isAdmin,
]);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Http/Controllers/Internal/v1/SettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,14 @@ public function saveBrandingSettings(Request $request)

if ($iconUuid) {
Setting::configure('branding.icon_uuid', $iconUuid);
} else {
Setting::configure('branding.icon_uuid', null);
}

if ($logoUuid) {
Setting::configure('branding.logo_uuid', $logoUuid);
} else {
Setting::configure('branding.logo_uuid', null);
}

$brandingSettings = Setting::getBranding();
Expand Down
10 changes: 8 additions & 2 deletions src/Http/Filter/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
namespace Fleetbase\Http\Filter;

use Fleetbase\Support\Http;
use Fleetbase\Traits\Expandable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

abstract class Filter
{
/**
* Make expandable.
*/
use Expandable;

/**
* The request instance.
*
Expand Down Expand Up @@ -39,7 +45,7 @@ abstract class Filter
public function __construct(Request $request)
{
$this->request = $request;
$this->session = $request->session();
$this->session = $request->hasSession() ? $request->session() : session();
}

/**
Expand Down Expand Up @@ -79,7 +85,7 @@ private function applyFilter($name, $value)
continue;
}

if (method_exists($this, $methodName)) {
if (method_exists($this, $methodName) || static::isExpansion($methodName)) {
call_user_func_array([$this, $methodName], [$value]);
break;
}
Expand Down
5 changes: 4 additions & 1 deletion src/Http/Resources/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Fleetbase\Http\Resources;

use Fleetbase\Models\Setting;
use Fleetbase\Support\Http;

class Organization extends FleetbaseResource
Expand All @@ -25,10 +26,12 @@ public function toArray($request)
'timezone' => $this->timezone,
'logo_url' => $this->logo_url,
'backdrop_url' => $this->backdrop_url,
'branding' => Setting::getBranding(),
'options' => $this->options,
'slug' => $this->slug,
'created_at' => $this->created_at,
'status' => $this->status,
'updated_at' => $this->updated_at,
'created_at' => $this->created_at,
];
}
}
4 changes: 2 additions & 2 deletions src/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public static function lookup(string $key, $defaultValue = null)

public static function getBranding()
{
$brandingSettings = ['id' => 1, 'uuid' => 1];
$brandingSettings = ['id' => 1, 'uuid' => 1, 'icon_url' => null, 'logo_url' => null];
$iconUuid = static::where('key', 'branding.icon_uuid')->value('value');
$logoUuid = static::where('key', 'branding.logo_uuid')->value('value');
$defaultTheme = static::where('key', 'branding.default_theme')->value('value');
Expand All @@ -184,7 +184,7 @@ public static function getBranding()

// set branding settings
$brandingSettings['icon_uuid'] = $iconUuid;
$brandingSettings['logo_uuid'] = $iconUuid;
$brandingSettings['logo_uuid'] = $logoUuid;
$brandingSettings['default_theme'] = $defaultTheme ?? 'dark';

return $brandingSettings;
Expand Down
11 changes: 8 additions & 3 deletions src/Providers/CoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Fleetbase\Providers;

use Fleetbase\Models\Setting;
use Fleetbase\Support\Expansion;
use Fleetbase\Support\NotificationRegistry;
use Fleetbase\Support\Utils;
use Illuminate\Console\Scheduling\Schedule;
Expand Down Expand Up @@ -341,8 +340,14 @@ function ($ns) use ($className) {
continue;
}

$method = $class::$method ?? Expansion::isExpandable($target) ? 'expand' : 'mixin';
$target::$method(new $class());
try {
$target::expand(new $class());
} catch (\Throwable $e) {
try {
$target::mixin(new $class());
} catch (\Throwable $e) {
}
}
}
}

Expand Down
46 changes: 46 additions & 0 deletions src/Support/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2152,4 +2152,50 @@ public static function getDefaultMailFromAddress(?string $default = 'hello@fleet

return $from;
}

/**
* Adds 'www.' to the beginning of a URL if it is not already present.
*
* This function checks if the given URL starts with 'http://' or 'https://'.
* If it does, it parses the URL and adds 'www.' to the host part if it's not already there.
* If the URL does not start with 'http://' or 'https://', it simply checks if 'www.' is
* already present at the start of the URL. If not, it adds 'www.'.
*
* @param string $url The URL to which 'www.' should be added if it's not already present.
*
* @return string The modified URL with 'www.' added if it was absent.
*
* Example usage:
* echo addWwwToUrl("example.com"); // Outputs: www.example.com
* echo addWwwToUrl("https://example.com"); // Outputs: https://www.example.com
*/
public static function addWwwToUrl($url)
{
// Check if the URL already starts with 'http://' or 'https://'
if (strpos($url, 'http://') !== 0 && strpos($url, 'https://') !== 0) {
// Check if the URL starts with 'www.'
if (strpos($url, 'www.') !== 0) {
// Add 'www.' to the start of the URL
$url = 'www.' . $url;
}
} else {
// Parse the URL and extract its components
$parsedUrl = parse_url($url);

// Check if the host is set and does not start with 'www.'
if (isset($parsedUrl['host']) && substr($parsedUrl['host'], 0, 4) !== 'www.') {
// Add 'www.' to the start of the host
$parsedUrl['host'] = 'www.' . $parsedUrl['host'];
}

// Rebuild the URL
$url = (isset($parsedUrl['scheme']) ? $parsedUrl['scheme'] . '://' : '')
. (isset($parsedUrl['host']) ? $parsedUrl['host'] : '')
. (isset($parsedUrl['path']) ? $parsedUrl['path'] : '')
. (isset($parsedUrl['query']) ? '?' . $parsedUrl['query'] : '')
. (isset($parsedUrl['fragment']) ? '#' . $parsedUrl['fragment'] : '');
}

return $url;
}
}
13 changes: 6 additions & 7 deletions src/Traits/Expandable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Fleetbase\Traits;

use Closure;
use Illuminate\Support\Facades\DB;

trait Expandable
Expand All @@ -15,7 +14,7 @@ public static function expand($name, \Closure $closure = null)
return static::expandFromClass($name);
}

$class = get_class(new static());
$class = get_class(app(static::class));

if (!isset(static::$added[$class])) {
static::$added[$class] = [];
Expand Down Expand Up @@ -59,21 +58,21 @@ public static function expandFromClass($class): void

public static function hasExpansion(string $name): bool
{
$class = get_class(new static());
$class = get_class(app(static::class));

return isset(static::$added[$class][$name]);
}

public static function isExpansion(string $name): bool
{
$class = get_class(new static());
$class = get_class(app(static::class));

return static::hasExpansion($name) && static::$added[$class][$name] instanceof \Closure;
}

public static function getExpansionClosure(string $name)
{
$class = get_class(new static());
$class = get_class(app(static::class));

return static::$added[$class][$name];
}
Expand All @@ -87,7 +86,7 @@ public function __call($method, $parameters)
}

if (static::isModel()) {
if (in_array($method, ['increment', 'decrement'])) {
if (method_exists($this, $method)) {
return $this->$method(...$parameters);
}

Expand All @@ -97,7 +96,7 @@ public function __call($method, $parameters)
DB::connection()->getPdo();
} catch (\Exception $e) {
// Connection failed, or other error occurred
return;
return $this->$method(...$parameters);
}

return $this->forwardCallTo($this->newQuery(), $method, $parameters);
Expand Down
Loading

0 comments on commit d08e0bd

Please sign in to comment.