38 lines
831 B
PHP
38 lines
831 B
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Console\Command;
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
class AssignRole extends Command
|
|
{
|
|
protected $signature = 'roles:assign {email} {role}';
|
|
|
|
protected $description = 'Assign a role to a user by email';
|
|
|
|
public function handle(): int
|
|
{
|
|
$email = $this->argument('email');
|
|
$roleName = $this->argument('role');
|
|
|
|
$user = User::where('email', $email)->first();
|
|
|
|
if (! $user) {
|
|
$this->error("User not found for email {$email}");
|
|
|
|
return static::FAILURE;
|
|
}
|
|
|
|
$role = Role::firstOrCreate(['name' => $roleName, 'guard_name' => 'web']);
|
|
|
|
$user->assignRole($role);
|
|
|
|
$this->info("Assigned role {$roleName} to {$email}");
|
|
|
|
return static::SUCCESS;
|
|
}
|
|
}
|
|
|