-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
1,235 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
migrations/2023_07_04_173018_make_roles_multi_tenant_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
$tableNames = config('permission.table_names'); | ||
$columnNames = config('permission.column_names'); | ||
|
||
if (empty($tableNames)) { | ||
throw new \Exception('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again.'); | ||
} | ||
|
||
Schema::table($tableNames['roles'], function (Blueprint $table) { | ||
$table->uuid('company_uuid')->nullable()->after('id')->index(); | ||
$table->foreign('company_uuid')->references('uuid')->on('companies')->onDelete('cascade'); | ||
$table->string('description')->nullable()->after('guard_name'); | ||
$table->softDeletes(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
$tableNames = config('permission.table_names'); | ||
|
||
if (empty($tableNames)) { | ||
throw new \Exception('Error: config/permission.php not found and defaults could not be merged. Please publish the package configuration before proceeding, or drop the tables manually.'); | ||
} | ||
|
||
Schema::table($tableNames['roles'], function (Blueprint $table) { | ||
$table->dropColumn('description'); | ||
$table->dropForeign(['company_uuid']); | ||
$table->dropColumn('company_uuid'); | ||
$table->dropColumn('deleted_at'); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Fleetbase\Seeds; | ||
|
||
use Illuminate\Database\Seeder; | ||
|
||
class FleetbaseSeeder extends Seeder | ||
{ | ||
/** | ||
* Run the database seeds. | ||
* | ||
* @return void | ||
*/ | ||
public function run() | ||
{ | ||
$this->call(ExtensionSeeder::class); | ||
$this->call(PermissionSeeder::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
<?php | ||
|
||
namespace Fleetbase\Seeds; | ||
|
||
use Fleetbase\Models\Permission; | ||
use Fleetbase\Models\Policy; | ||
use Fleetbase\Support\Utils; | ||
use Illuminate\Database\Seeder; | ||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Support\Str; | ||
|
||
class PermissionSeeder extends Seeder | ||
{ | ||
/** | ||
* Run the database seeds. | ||
* | ||
* @return void | ||
*/ | ||
public function run() | ||
{ | ||
Schema::disableForeignKeyConstraints(); | ||
Permission::truncate(); | ||
Policy::truncate(); | ||
|
||
$actions = ['create', 'update', 'delete', 'view', 'list']; | ||
$schemas = Utils::getAuthSchemas(); | ||
|
||
foreach ($schemas as $schema) { | ||
$service = $schema->name; | ||
$resources = $schema->resources ?? []; | ||
$permissions = $schema->permissions ?? null; | ||
$guard = 'web'; | ||
|
||
// first create a wilcard permission for the entire schema | ||
$administratorPolicy = Policy::firstOrCreate( | ||
[ | ||
'name' => 'AdministratorAccess', | ||
'guard_name' => $guard, | ||
'description' => 'Provides full access to Fleetbase extensions and resources.', | ||
] | ||
); | ||
|
||
$permission = Permission::firstOrCreate( | ||
[ | ||
'name' => $service . ' *', | ||
'guard_name' => $guard | ||
], | ||
[ | ||
'name' => $service . ' *', | ||
'guard_name' => $guard | ||
] | ||
); | ||
|
||
// add wildcard permissions to administrator access policy | ||
try { | ||
$administratorPolicy->givePermissionTo($permission); | ||
} catch (\Spatie\Permission\Exceptions\GuardDoesNotMatch $e) { | ||
dd($e->getMessage(), $guard, $permission, $administratorPolicy); | ||
} | ||
|
||
// output message for permissions creation | ||
$this->output('Created (' . $guard . ') permission: ' . $permission->name); | ||
|
||
// check if schema has direct permissions to add | ||
if (is_array($permissions)) { | ||
foreach ($permissions as $action) { | ||
$permission = Permission::firstOrCreate( | ||
[ | ||
'name' => $service . ' ' . $action, | ||
'guard_name' => $guard | ||
], | ||
[ | ||
'name' => $service . ' ' . $action, | ||
'guard_name' => $guard | ||
] | ||
); | ||
|
||
// add wildcard permissions to administrator access policy | ||
try { | ||
$administratorPolicy->givePermissionTo($permission); | ||
} catch (\Spatie\Permission\Exceptions\GuardDoesNotMatch $e) { | ||
dd($e->getMessage(), $guard, $permission, $administratorPolicy); | ||
} | ||
|
||
// output message for permissions creation | ||
$this->output('Created (' . $guard . ') permission: ' . $permission->name); | ||
} | ||
} | ||
|
||
// create a resource policy for full access | ||
$fullAccessPolicy = Policy::firstOrCreate( | ||
[ | ||
'name' => Str::studly(data_get($schema, 'policyName')) . 'FullAccess', | ||
'guard_name' => $guard | ||
], | ||
[ | ||
'name' => Str::studly(data_get($schema, 'policyName')) . 'FullAccess', | ||
'description' => 'Provides full access to ' . Str::studly(data_get($schema, 'policyName')) . '.', | ||
'guard_name' => $guard | ||
] | ||
); | ||
|
||
// create a resource policy for read-only access | ||
$readOnlyPolicy = Policy::firstOrCreate( | ||
[ | ||
'name' => Str::studly(data_get($schema, 'policyName')) . 'FullAccess', | ||
'guard_name' => $guard | ||
], | ||
[ | ||
'name' => Str::studly(data_get($schema, 'policyName')) . 'FullAccess', | ||
'description' => 'Provides read-only access to ' . Str::studly(data_get($schema, 'policyName')) . '.', | ||
'guard_name' => $guard | ||
] | ||
); | ||
|
||
// create wilcard permission for service and all resources | ||
foreach ($resources as $resource) { | ||
// create a resource policy for full access | ||
$resourceFullAccessPolicy = Policy::firstOrCreate( | ||
[ | ||
'name' => Str::studly(data_get($schema, 'policyName')) . Str::studly(data_get($resource, 'name')) . 'FullAccess', | ||
'guard_name' => $guard | ||
], | ||
[ | ||
'name' => Str::studly(data_get($schema, 'policyName')) . Str::studly(data_get($resource, 'name')) . 'FullAccess', | ||
'description' => 'Provides full access to ' . Str::studly(data_get($schema, 'policyName')) . ' ' . Str::plural(data_get($resource, 'name')) . '.', | ||
'guard_name' => $guard | ||
] | ||
); | ||
|
||
// create a resource policy for read-only access | ||
$resourceReadOnlyPolicy = Policy::firstOrCreate( | ||
[ | ||
'name' => Str::studly(data_get($schema, 'policyName')) . Str::studly(data_get($resource, 'name')) . 'FullAccess', | ||
'guard_name' => $guard | ||
], | ||
[ | ||
'name' => Str::studly(data_get($schema, 'policyName')) . Str::studly(data_get($resource, 'name')) . 'FullAccess', | ||
'description' => 'Provides read-only access to ' . Str::studly(data_get($schema, 'policyName')) . ' ' . Str::plural(data_get($resource, 'name')) . '.', | ||
'guard_name' => $guard | ||
] | ||
); | ||
|
||
$permission = Permission::firstOrCreate( | ||
[ | ||
'name' => $service . ' * ' . data_get($resource, 'name'), | ||
'guard_name' => $guard | ||
], | ||
[ | ||
'name' => $service . ' * ' . data_get($resource, 'name'), | ||
'guard_name' => $guard | ||
] | ||
); | ||
|
||
// add wildcard permissions to full access policy | ||
try { | ||
$fullAccessPolicy->givePermissionTo($permission); | ||
} catch (\Spatie\Permission\Exceptions\GuardDoesNotMatch $e) { | ||
dd($e->getMessage(), $guard, $permission, $fullAccessPolicy); | ||
} | ||
try { | ||
$resourceFullAccessPolicy->givePermissionTo($permission); | ||
} catch (\Spatie\Permission\Exceptions\GuardDoesNotMatch $e) { | ||
dd($e->getMessage(), $guard, $permission, $resourceFullAccessPolicy); | ||
} | ||
|
||
// output message for permissions creation | ||
$this->output('Created (' . $guard . ') permission: ' . $permission->name); | ||
|
||
// create action permissions | ||
$resourceActions = array_merge($actions, data_get($resource, 'actions', [])); | ||
|
||
// if some actions should be excluded | ||
if (is_array(data_get($resource, 'remove_actions', null))) { | ||
foreach (data_get($resource, 'remove_actions') as $remove) { | ||
if (($key = array_search($remove, $actions)) !== false) { | ||
unset($actions[$key]); | ||
} | ||
} | ||
} | ||
|
||
// create action permissions | ||
foreach ($resourceActions as $action) { | ||
$permission = Permission::firstOrCreate( | ||
[ | ||
'name' => $service . ' ' . $action . ' ' . data_get($resource, 'name'), | ||
'guard_name' => $guard | ||
], | ||
[ | ||
'name' => $service . ' ' . $action . ' ' . data_get($resource, 'name'), | ||
'guard_name' => $guard | ||
] | ||
); | ||
|
||
// add the permission to the read only policy | ||
if ($action === 'view' || $action === 'list') { | ||
try { | ||
$readOnlyPolicy->givePermissionTo($permission); | ||
} catch (\Spatie\Permission\Exceptions\GuardDoesNotMatch $e) { | ||
dd($e->getMessage(), $guard, $permission, $readOnlyPolicy); | ||
} | ||
try { | ||
$resourceReadOnlyPolicy->givePermissionTo($permission); | ||
} catch (\Spatie\Permission\Exceptions\GuardDoesNotMatch $e) { | ||
dd($e->getMessage(), $guard, $permission, $resourceReadOnlyPolicy); | ||
} | ||
} | ||
|
||
// output message for permissions creation | ||
$this->output('Created (' . $guard . ') permission: ' . $permission->name); | ||
} | ||
} | ||
} | ||
|
||
Schema::enableForeignKeyConstraints(); | ||
} | ||
|
||
/** | ||
* Simple echo to output to CLI | ||
* | ||
* @param string $line | ||
* @return void | ||
*/ | ||
public function output(string $line = ''): void | ||
{ | ||
echo $line . PHP_EOL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Fleetbase\Seeds; | ||
|
||
use Illuminate\Database\Seeder; | ||
|
||
class RolesSeeder extends Seeder | ||
{ | ||
/** | ||
* Run the database seeds. | ||
* | ||
* @return void | ||
*/ | ||
public function run() | ||
{ | ||
|
||
} | ||
} |
Oops, something went wrong.