43 lines
1.1 KiB
PHP
43 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Console\Command;
|
|
|
|
class SyncUserPermissions extends Command
|
|
{
|
|
protected $signature = 'users:sync-permissions';
|
|
protected $description = 'Sync all users permissions with MODULES, adding missing keys with default 0';
|
|
|
|
public function handle(): int
|
|
{
|
|
$count = 0;
|
|
|
|
foreach (User::all() as $user) {
|
|
$permissions = $user->permissions ?? [];
|
|
$changed = false;
|
|
|
|
foreach (User::MODULES as $module) {
|
|
if (!array_key_exists($module, $permissions)) {
|
|
$permissions[$module] = User::LEVEL_NONE;
|
|
$changed = true;
|
|
}
|
|
}
|
|
|
|
if ($changed) {
|
|
$user->permissions = $permissions;
|
|
$user->save();
|
|
$this->info("Synced permissions for user ID {$user->id} ({$user->email})");
|
|
$count++;
|
|
}
|
|
}
|
|
|
|
$this->info("Done. {$count} users updated.");
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|