|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace HiEvents\Console\Commands; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use HiEvents\DomainObjects\Enums\Role; |
| 7 | +use HiEvents\Repository\Interfaces\AccountUserRepositoryInterface; |
| 8 | +use HiEvents\Repository\Interfaces\UserRepositoryInterface; |
| 9 | +use Illuminate\Console\Command; |
| 10 | +use Psr\Log\LoggerInterface; |
| 11 | + |
| 12 | +class AssignSuperAdminCommand extends Command |
| 13 | +{ |
| 14 | + protected $signature = 'user:make-superadmin {userId : The ID of the user to make a superadmin}'; |
| 15 | + |
| 16 | + protected $description = 'Assign SUPERADMIN role to a user. WARNING: This grants complete system access.'; |
| 17 | + |
| 18 | + public function __construct( |
| 19 | + private readonly UserRepositoryInterface $userRepository, |
| 20 | + private readonly AccountUserRepositoryInterface $accountUserRepository, |
| 21 | + private readonly LoggerInterface $logger, |
| 22 | + ) |
| 23 | + { |
| 24 | + parent::__construct(); |
| 25 | + } |
| 26 | + |
| 27 | + public function handle(): int |
| 28 | + { |
| 29 | + $userId = $this->argument('userId'); |
| 30 | + |
| 31 | + $this->warn('⚠️ WARNING: This command will grant COMPLETE SYSTEM ACCESS to the user.'); |
| 32 | + $this->warn('⚠️ SUPERADMIN users have unrestricted access to all accounts and data.'); |
| 33 | + $this->newLine(); |
| 34 | + |
| 35 | + if (!$this->confirm('Are you sure you want to proceed?', false)) { |
| 36 | + $this->info('Operation cancelled.'); |
| 37 | + return self::FAILURE; |
| 38 | + } |
| 39 | + |
| 40 | + try { |
| 41 | + $user = $this->userRepository->findById((int)$userId); |
| 42 | + } catch (Exception $exception) { |
| 43 | + $this->error("Error finding user with ID: $userId" . " Message: " . $exception->getMessage()); |
| 44 | + return self::FAILURE; |
| 45 | + } |
| 46 | + |
| 47 | + $this->info("Found user: {$user->getFullName()} ({$user->getEmail()})"); |
| 48 | + $this->newLine(); |
| 49 | + |
| 50 | + if (!$this->confirm('Confirm assigning SUPERADMIN role to this user?', false)) { |
| 51 | + $this->info('Operation cancelled.'); |
| 52 | + return self::FAILURE; |
| 53 | + } |
| 54 | + |
| 55 | + $accountUsers = $this->accountUserRepository->findWhere([ |
| 56 | + 'user_id' => $userId, |
| 57 | + ]); |
| 58 | + |
| 59 | + if ($accountUsers->isEmpty()) { |
| 60 | + $this->error('User is not associated with any accounts.'); |
| 61 | + return self::FAILURE; |
| 62 | + } |
| 63 | + |
| 64 | + $updatedCount = 0; |
| 65 | + foreach ($accountUsers as $accountUser) { |
| 66 | + if ($accountUser->getRole() === Role::SUPERADMIN->name) { |
| 67 | + $this->comment("User already has SUPERADMIN role for account ID: {$accountUser->getAccountId()}"); |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + $this->accountUserRepository->updateWhere( |
| 72 | + attributes: [ |
| 73 | + 'role' => Role::SUPERADMIN->name, |
| 74 | + ], |
| 75 | + where: [ |
| 76 | + 'id' => $accountUser->getId(), |
| 77 | + ] |
| 78 | + ); |
| 79 | + |
| 80 | + $updatedCount++; |
| 81 | + |
| 82 | + $this->logger->critical('SUPERADMIN role assigned via console command', [ |
| 83 | + 'user_id' => $userId, |
| 84 | + 'user_email' => $user->getEmail(), |
| 85 | + 'account_id' => $accountUser->getAccountId(), |
| 86 | + 'previous_role' => $accountUser->getRole(), |
| 87 | + 'command' => $this->signature, |
| 88 | + ]); |
| 89 | + } |
| 90 | + |
| 91 | + $this->newLine(); |
| 92 | + $this->info("✓ Successfully assigned SUPERADMIN role to user across $updatedCount account(s)."); |
| 93 | + $this->warn("⚠️ User {$user->getFullName()} now has COMPLETE SYSTEM ACCESS."); |
| 94 | + |
| 95 | + $this->logger->critical('SUPERADMIN role assignment completed', [ |
| 96 | + 'user_id' => $userId, |
| 97 | + 'accounts_updated' => $updatedCount, |
| 98 | + ]); |
| 99 | + |
| 100 | + return self::SUCCESS; |
| 101 | + } |
| 102 | +} |
0 commit comments