Car-Repairs-Shop/app/Console/Commands/AssignRoleCommand.php
sackey e839d40a99
Some checks are pending
linter / quality (push) Waiting to run
tests / ci (push) Waiting to run
Initial commit
2025-07-30 17:15:50 +00:00

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;
}
}