75 lines
2.5 KiB
PHP
75 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\ManagementTools\Console;
|
|
|
|
use App\User;
|
|
use Illuminate\Console\Command;
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
class AssignExecutiveUsersManualCommand extends Command
|
|
{
|
|
protected $signature = 'managementtools:assign-executive-users-manual
|
|
{business_id}
|
|
{--ceo=}
|
|
{--coo=}
|
|
{--cfo=}
|
|
{--pmo=}
|
|
{--board=*}';
|
|
|
|
protected $description = 'Assign executive roles manually by user IDs';
|
|
|
|
public function handle()
|
|
{
|
|
$businessId = (int) $this->argument('business_id');
|
|
|
|
$roleNames = [
|
|
'ceo' => "CEO#{$businessId}",
|
|
'coo' => "COO#{$businessId}",
|
|
'cfo' => "CFO#{$businessId}",
|
|
'pmo' => "PMO#{$businessId}",
|
|
'board' => "Board#{$businessId}",
|
|
];
|
|
|
|
foreach ($roleNames as $key => $roleName) {
|
|
$role = Role::where('name', $roleName)->where('business_id', $businessId)->first();
|
|
if (! $role) {
|
|
$this->error("Role missing: {$roleName}. Run role matrix command first.");
|
|
return self::FAILURE;
|
|
}
|
|
}
|
|
|
|
$singleAssignments = ['ceo', 'coo', 'cfo', 'pmo'];
|
|
foreach ($singleAssignments as $opt) {
|
|
$id = $this->option($opt);
|
|
if (empty($id)) {
|
|
continue;
|
|
}
|
|
|
|
$user = User::where('business_id', $businessId)->where('id', (int) $id)->first();
|
|
if (! $user) {
|
|
$this->warn("Skipping {$opt}: user #{$id} not found in business #{$businessId}");
|
|
continue;
|
|
}
|
|
|
|
$user->assignRole($roleNames[$opt]);
|
|
$this->info(strtoupper($opt)." assigned to {$user->id} ({$user->username})");
|
|
}
|
|
|
|
$boardIds = array_filter((array) $this->option('board'), fn ($v) => $v !== null && $v !== '');
|
|
foreach ($boardIds as $id) {
|
|
$user = User::where('business_id', $businessId)->where('id', (int) $id)->first();
|
|
if (! $user) {
|
|
$this->warn("Skipping board user #{$id}: not found.");
|
|
continue;
|
|
}
|
|
$user->assignRole($roleNames['board']);
|
|
$this->info("BOARD assigned to {$user->id} ({$user->username})");
|
|
}
|
|
|
|
$this->info("Manual executive assignment completed for business #{$businessId}");
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|
|
|