feat(permissions): add dashboard:sync-permissions artisan command
Auto-discovers all dashboard permissions from resource/prefix matrix, creates missing ones in DB, syncs to super_admin and admin roles. Run on production after deploy or when adding new menu sections.
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Ship\Commands;
|
||||
|
||||
use App\Ship\Abstracts\Commands\ConsoleCommand as AbstractConsoleCommand;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
use Spatie\Permission\Models\Role;
|
||||
|
||||
class SyncDashboardPermissions extends AbstractConsoleCommand
|
||||
{
|
||||
protected $signature = 'dashboard:sync-permissions {--role=super_admin,admin : Roles to sync all permissions to}';
|
||||
|
||||
protected $description = 'Create missing dashboard permissions and sync to roles';
|
||||
|
||||
private const RESOURCES = [
|
||||
'post', 'category', 'department', 'faculty', 'slider',
|
||||
'user', 'role', 'page', 'news', 'menu', 'widget',
|
||||
'contactwidget', 'additional_education', 'admission_campaign',
|
||||
'schedule', 'academic_journal', 'main_section', 'sub_section',
|
||||
'direction_study', 'educational_program', 'admission_plan',
|
||||
'educational_group', 'division',
|
||||
];
|
||||
|
||||
private const PREFIXES = [
|
||||
'view_any', 'create', 'update', 'delete', 'restore', 'force_delete',
|
||||
];
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$created = 0;
|
||||
|
||||
foreach (self::RESOURCES as $resource) {
|
||||
foreach (self::PREFIXES as $prefix) {
|
||||
$name = "{$prefix}_{$resource}";
|
||||
|
||||
Permission::firstOrCreate(
|
||||
['name' => $name, 'guard_name' => 'web'],
|
||||
);
|
||||
|
||||
$created++;
|
||||
}
|
||||
}
|
||||
|
||||
// Also create view_any_contact_widget (with underscore) if menuConfig uses it
|
||||
Permission::firstOrCreate(
|
||||
['name' => 'view_any_contact_widget', 'guard_name' => 'web'],
|
||||
);
|
||||
|
||||
$roleNames = explode(',', $this->option('role'));
|
||||
$allPerms = Permission::where('guard_name', 'web')->get();
|
||||
|
||||
foreach ($roleNames as $roleName) {
|
||||
$roleName = trim($roleName);
|
||||
$role = Role::firstOrCreate(
|
||||
['name' => $roleName, 'guard_name' => 'web'],
|
||||
);
|
||||
$role->syncPermissions($allPerms);
|
||||
$this->info("Role [{$roleName}] synced with {$allPerms->count()} permissions");
|
||||
}
|
||||
|
||||
$this->info("Done. Total permissions: {$allPerms->count()}");
|
||||
|
||||
return static::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ use AlxDorosenco\PortoForLaravel\Loaders\CommandsLoader;
|
||||
use AlxDorosenco\PortoForLaravel\Loaders\RoutesLoader;
|
||||
use App\Containers\Dashboard\Commands\FetchEmailNewsCommand;
|
||||
use App\Ship\Commands\InitRoles;
|
||||
use App\Ship\Commands\SyncDashboardPermissions;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Foundation\Console\Kernel as LaravelConsoleKernel;
|
||||
|
||||
@@ -34,6 +35,7 @@ class ConsoleKernel extends LaravelConsoleKernel
|
||||
protected $commands = [
|
||||
InitRoles::class,
|
||||
FetchEmailNewsCommand::class,
|
||||
SyncDashboardPermissions::class,
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user