Initial commit
This commit is contained in:
37
app/Console/Commands/AssignRole.php
Normal file
37
app/Console/Commands/AssignRole.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user