Skip to content

Commit

Permalink
Merge pull request #18 from fleetbase/feature-universe-powered-extens…
Browse files Browse the repository at this point in the history
…ions

Feature universe powered extensions
  • Loading branch information
roncodes authored Jul 23, 2023
2 parents 39768d8 + 5d003c0 commit c4ff317
Show file tree
Hide file tree
Showing 13 changed files with 363 additions and 32 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fleetbase/core-api",
"version": "1.1.4-alpha",
"version": "1.1.5-alpha",
"description": "Core Framework and Resources for Fleetbase API",
"keywords": [
"fleetbase",
Expand Down Expand Up @@ -32,7 +32,6 @@
"illuminate/routing": "^7.0|^8.0|^9.0",
"illuminate/support": "^7.0|^8.0|^9.0",
"jdorn/sql-formatter": "^1.2",
"laravel/cashier": "^13.15",
"laravel/sanctum": "^2.15",
"maatwebsite/excel": "^3.1",
"phpoffice/phpspreadsheet": "^1.28",
Expand Down
2 changes: 2 additions & 0 deletions migrations/2023_04_25_094311_create_policies_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public function up()
*/
public function down()
{
Schema::disableForeignKeyConstraints();
Schema::dropIfExists('policies');
Schema::dropIfExists('model_has_policies');
Schema::enableForeignKeyConstraints();
}
};
20 changes: 10 additions & 10 deletions seeds/PermissionSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public function run()
try {
$administratorPolicy->givePermissionTo($permission);
} catch (\Spatie\Permission\Exceptions\GuardDoesNotMatch $e) {
dd($e->getMessage(), $guard, $permission, $administratorPolicy);
dd($e->getMessage());
}

// output message for permissions creation
$this->output('Created (' . $guard . ') permission: ' . $permission->name);
// $this->output('Created (' . $guard . ') permission: ' . $permission->name);

// check if schema has direct permissions to add
if (is_array($permissions)) {
Expand All @@ -79,11 +79,11 @@ public function run()
try {
$administratorPolicy->givePermissionTo($permission);
} catch (\Spatie\Permission\Exceptions\GuardDoesNotMatch $e) {
dd($e->getMessage(), $guard, $permission, $administratorPolicy);
dd($e->getMessage());
}

// output message for permissions creation
$this->output('Created (' . $guard . ') permission: ' . $permission->name);
// $this->output('Created (' . $guard . ') permission: ' . $permission->name);
}
}

Expand Down Expand Up @@ -156,16 +156,16 @@ public function run()
try {
$fullAccessPolicy->givePermissionTo($permission);
} catch (\Spatie\Permission\Exceptions\GuardDoesNotMatch $e) {
dd($e->getMessage(), $guard, $permission, $fullAccessPolicy);
dd($e->getMessage());
}
try {
$resourceFullAccessPolicy->givePermissionTo($permission);
} catch (\Spatie\Permission\Exceptions\GuardDoesNotMatch $e) {
dd($e->getMessage(), $guard, $permission, $resourceFullAccessPolicy);
dd($e->getMessage());
}

// output message for permissions creation
$this->output('Created (' . $guard . ') permission: ' . $permission->name);
// $this->output('Created (' . $guard . ') permission: ' . $permission->name);

// create action permissions
$resourceActions = array_merge($actions, data_get($resource, 'actions', []));
Expand Down Expand Up @@ -197,17 +197,17 @@ public function run()
try {
$readOnlyPolicy->givePermissionTo($permission);
} catch (\Spatie\Permission\Exceptions\GuardDoesNotMatch $e) {
dd($e->getMessage(), $guard, $permission, $readOnlyPolicy);
dd($e->getMessage());
}
try {
$resourceReadOnlyPolicy->givePermissionTo($permission);
} catch (\Spatie\Permission\Exceptions\GuardDoesNotMatch $e) {
dd($e->getMessage(), $guard, $permission, $resourceReadOnlyPolicy);
dd($e->getMessage());
}
}

// output message for permissions creation
$this->output('Created (' . $guard . ') permission: ' . $permission->name);
// $this->output('Created (' . $guard . ') permission: ' . $permission->name);
}
}
}
Expand Down
72 changes: 68 additions & 4 deletions src/Console/Commands/MigrateSandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ public function handle()
$command = $refresh ? 'migrate:refresh' : 'migrate';

// only run core and fleetops migrations
$paths = [
'vendor/fleetbase/core-api/migrations',
'vendor/fleetbase/fleetops-api/migrations'
];
$paths = ['vendor/fleetbase/core-api/migrations'];
$migrationDirectories = $this->getExtensionsMigrationPaths();

if (is_array($migrationDirectories)) {
$paths = array_merge($paths, $migrationDirectories);
}

foreach ($paths as $path) {
$this->call($command, [
Expand All @@ -59,4 +61,66 @@ public function handle()
]);
}
}

/**
* Returns the relative paths to the migration directories of all installed Fleetbase extensions.
*
* This function retrieves all installed Fleetbase extensions, and then for each extension,
* it checks if sandbox migrations are disabled. If not, it gets the migration directory
* for the extension. All collected paths are then converted to relative paths and returned.
*
* @return array An array of the relative paths to the migration directories of all installed Fleetbase extensions.
*/
private function getExtensionsMigrationPaths(): array
{
$packages = Utils::getInstalledFleetbaseExtensions();
$paths = [];

foreach ($packages as $packageName => $package) {
// check if migrations is disabled for sandbox
$sandboxMigrations = Utils::getFleetbaseExtensionProperty($packageName, 'sandbox-migrations');

if ($sandboxMigrations === false || $sandboxMigrations === 'false' || $sandboxMigrations === 0 || $sandboxMigrations === '0') {
continue;
}

$path = Utils::getMigrationDirectoryForExtension($packageName);

if ($path) {
$paths[] = $path;
}
}

return $this->makePathsRelative($paths);
}

/**
* Converts an array of absolute paths to relative paths.
*
* This function maps over an array of paths and for each path, it creates a substring
* from the position of 'vendor' to the end of the string, effectively creating a relative path.
* The trailing slash is also removed from each path. If the provided input is not an array,
* the function will return an empty array.
*
* @param array|null $paths An array of absolute paths that will be converted to relative paths.
* @return array An array of relative paths.
*/
private function makePathsRelative(?array $paths = []): array
{
if (!is_array($paths)) {
return [];
}

$relativePaths = array_map(function ($path) {
// Find the position of "vendor" in the string
$startPosition = strpos($path, 'vendor');
// Create a substring from the position of "vendor" to the end of the string
$relativePath = substr($path, $startPosition);
// Remove the trailing slash
$relativePath = rtrim($relativePath, '/');
return $relativePath;
}, $paths);

return $relativePaths;
}
}
16 changes: 15 additions & 1 deletion src/Console/Commands/SeedDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Fleetbase\Console\Commands;

use Fleetbase\Support\Utils;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

Expand All @@ -12,7 +13,7 @@ class SeedDatabase extends Command
*
* @var string
*/
protected $signature = 'fleetbase:seed {--class=FleetbaseSeeder}';
protected $signature = 'fleetbase:seed {--class}';

/**
* The console command description.
Expand Down Expand Up @@ -45,6 +46,19 @@ public function handle()
'--class' => 'Fleetbase\\Seeds\\FleetbaseSeeder',
]
);

// seed for extensions
$extensionSeeders = Utils::getSeedersFromExtensions();

foreach ($extensionSeeders as $seeder) {
// Manually include the seeder file
require_once $seeder['path'];

// Instantiate the seeder class and run it
$seederInstance = new $seeder['class']();
$seederInstance->run();
}

$this->info('Fleetbase Seeders were run Successfully!');
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/Exceptions/FleetbaseRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

namespace Fleetbase\Exceptions;

use Throwable;
use Exception;

class FleetbaseRequestValidationException extends Exception implements Throwable
class FleetbaseRequestException extends \Exception implements \Throwable
{
protected string $message = 'Invalid request';
protected array $errors = [];

public function __construct($errors = [], $message = 'Invalid request', $code = 0, Throwable $previous = null)
public function __construct($errors = [], $message = 'Invalid request', $code = 0, \Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$this->errors = $errors;
Expand Down
20 changes: 20 additions & 0 deletions src/Exceptions/FleetbaseRequestValidationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Fleetbase\Exceptions;

class FleetbaseRequestValidationException extends \Exception implements \Throwable
{
protected string $message = 'Invalid request';
protected array $errors = [];

public function __construct($errors = [], $message = 'Invalid request', $code = 0, \Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$this->errors = $errors;
}

public function getErrors()
{
return (array) $this->errors;
}
}
2 changes: 2 additions & 0 deletions src/Expansions/PendingResourceRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static function target()
public function setRouter()
{
return function (Router $router) {
/** @var \Illuminate\Routing\PendingResourceRegistration $this */
$this->router = $router;

return $this;
Expand All @@ -25,6 +26,7 @@ public function setRouter()
public function extend()
{
return function (?Closure $callback = null) {
/** @var \Illuminate\Routing\PendingResourceRegistration $this */
if ($this->router instanceof Router && is_callable($callback)) {
$callback($this->router);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Expansions/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public function error()
if ($error instanceof MessageBag) {
$error = $error->all();
}


/** @var \Illuminate\Support\Facades\Response $this */
return static::json(
[
'errors' => is_array($error) ? $error : [$error],
Expand Down
26 changes: 22 additions & 4 deletions src/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,31 @@ public static function configure($key, $value = null)
);
}

/**
* lookup a setting and return the value.
*
* @param string $key
* @param mixed $defaultValue
* @return mixed|null
*/
public static function lookup(string $key, $defaultValue = null)
{
$setting = static::where('key', $key)->first();

if (!$setting) {
return $defaultValue;
}

return data_get($setting, 'value', $defaultValue);
}

public static function getBranding()
{
$brandingSettings = ['id' => 1, 'uuid' => 1];
$iconUrl = static::where('key', 'branding.icon_url')->first();
$logoUrl = static::where('key', 'branding.logo_url')->first();
$brandingSettings['icon_url'] = $iconUrl ? $iconUrl->value : '/images/icon.png';
$brandingSettings['logo_url'] = $logoUrl ? $logoUrl->value : '/images/fleetbase-logo-svg.svg';
$iconUrl = static::where('key', 'branding.icon_url')->first();
$logoUrl = static::where('key', 'branding.logo_url')->first();
$brandingSettings['icon_url'] = $iconUrl ? $iconUrl->value : '/images/icon.png';
$brandingSettings['logo_url'] = $logoUrl ? $logoUrl->value : '/images/fleetbase-logo-svg.svg';

return $brandingSettings;
}
Expand Down
14 changes: 11 additions & 3 deletions src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Fleetbase\Traits\HasCacheableAttributes;
use Fleetbase\Traits\HasMetaAttributes;
use Illuminate\Database\Eloquent\Concerns\HasTimestamps;
use Laravel\Cashier\Billable;

class User extends Authenticatable
{
Expand All @@ -44,8 +43,7 @@ class User extends Authenticatable
CausesActivity,
SoftDeletes,
Expandable,
Filterable,
Billable;
Filterable;

/**
* The database connection to use.
Expand Down Expand Up @@ -358,6 +356,16 @@ public function isAdmin()
return $this->type === 'admin';
}

/**
* Checks if the user is NOT admin
*
* @return boolean
*/
public function isNotAdmin()
{
return $this->type !== 'admin';
}

/**
* Adds a boolean dynamic property to check if user is an admin.
*
Expand Down
2 changes: 0 additions & 2 deletions src/Providers/CoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Fleetbase\Models\Setting;
use Fleetbase\Support\Expansion;
use Fleetbase\Support\Utils;
use Laravel\Cashier\Cashier;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Schema;
Expand Down Expand Up @@ -65,7 +64,6 @@ class CoreServiceProvider extends ServiceProvider
public function boot()
{
JsonResource::withoutWrapping();
Cashier::ignoreMigrations();

$this->registerCommands();
$this->registerObservers();
Expand Down
Loading

0 comments on commit c4ff317

Please sign in to comment.