75 lines
2.3 KiB
PHP
75 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace Modules\ManagementTools\Console;
|
|
|
|
use App\User;
|
|
use Illuminate\Console\Command;
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
class AssignExecutiveUsersCommand extends Command
|
|
{
|
|
protected $signature = 'managementtools:assign-executive-users {business_id}';
|
|
|
|
protected $description = 'Assign executive users to CEO/COO/CFO/PMO/Board roles';
|
|
|
|
public function handle()
|
|
{
|
|
$businessId = (int) $this->argument('business_id');
|
|
|
|
$adminRoleName = "Admin#{$businessId}";
|
|
$adminRole = Role::where('name', $adminRoleName)->where('business_id', $businessId)->first();
|
|
|
|
$userQuery = User::where('business_id', $businessId)->where('allow_login', 1);
|
|
if ($adminRole) {
|
|
$userQuery->whereHas('roles', function ($q) use ($adminRoleName) {
|
|
$q->where('name', $adminRoleName);
|
|
});
|
|
}
|
|
|
|
$users = $userQuery->orderBy('id')->get();
|
|
if ($users->isEmpty()) {
|
|
$this->error("No eligible users found for business #{$businessId}");
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$roleNames = [
|
|
'CEO' => "CEO#{$businessId}",
|
|
'COO' => "COO#{$businessId}",
|
|
'CFO' => "CFO#{$businessId}",
|
|
'PMO' => "PMO#{$businessId}",
|
|
'Board' => "Board#{$businessId}",
|
|
];
|
|
|
|
foreach ($roleNames as $base => $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;
|
|
}
|
|
}
|
|
|
|
$primaryMap = [
|
|
'CEO' => $users->get(0),
|
|
'COO' => $users->get(1),
|
|
'CFO' => $users->get(2),
|
|
'PMO' => $users->get(3),
|
|
];
|
|
|
|
foreach ($primaryMap as $title => $user) {
|
|
if (! $user) {
|
|
continue;
|
|
}
|
|
$user->assignRole($roleNames[$title]);
|
|
$this->info("Assigned {$title} => {$user->id} ({$user->username})");
|
|
}
|
|
|
|
foreach ($users as $user) {
|
|
$user->assignRole($roleNames['Board']);
|
|
}
|
|
$this->info("Assigned Board role to {$users->count()} user(s)");
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|
|
|