55 lines
1.3 KiB
PHP
55 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use App\Models\User;
|
|
use App\Models\Role;
|
|
|
|
class AssignRoleCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'user:assign-role {email} {role} {--branch=}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Assign a role to a user';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
$email = $this->argument('email');
|
|
$roleName = $this->argument('role');
|
|
$branchCode = $this->option('branch');
|
|
|
|
$user = User::where('email', $email)->first();
|
|
if (!$user) {
|
|
$this->error("User with email {$email} not found.");
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
$role = Role::where('name', $roleName)->first();
|
|
if (!$role) {
|
|
$this->error("Role {$roleName} not found.");
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
// Assign role to user
|
|
$user->assignRole($role, $branchCode);
|
|
|
|
$this->info("Role '{$role->display_name}' assigned to user '{$user->name}'" .
|
|
($branchCode ? " for branch '{$branchCode}'" : '') . ".");
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|