|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace MultiFlexi\Cli\Command\UserCompany; |
| 6 | + |
| 7 | +use MultiFlexi\Cli\Command\MultiFlexiCommand; |
| 8 | +use MultiFlexi\Company; |
| 9 | +use MultiFlexi\User; |
| 10 | +use Symfony\Component\Console\Input\InputInterface; |
| 11 | +use Symfony\Component\Console\Input\InputOption; |
| 12 | +use Symfony\Component\Console\Output\OutputInterface; |
| 13 | + |
| 14 | +class AssignCommand extends MultiFlexiCommand |
| 15 | +{ |
| 16 | + protected static $defaultName = 'user-company:assign'; |
| 17 | + |
| 18 | + protected function configure(): void |
| 19 | + { |
| 20 | + $this |
| 21 | + ->setName('user-company:assign') |
| 22 | + ->setDescription('Assign a user to a company') |
| 23 | + ->addOption('format', 'f', InputOption::VALUE_OPTIONAL, 'Output format: text or json', 'text') |
| 24 | + ->addOption('company_id', null, InputOption::VALUE_REQUIRED, 'Company ID') |
| 25 | + ->addOption('user_id', null, InputOption::VALUE_REQUIRED, 'User ID') |
| 26 | + ->addOption('login', null, InputOption::VALUE_REQUIRED, 'User login (alternative to --user_id)') |
| 27 | + ->addOption('email', null, InputOption::VALUE_REQUIRED, 'User email (alternative to --user_id)') |
| 28 | + ->addOption('role', null, InputOption::VALUE_OPTIONAL, 'Assignment role in company_user table', 'viewer'); |
| 29 | + } |
| 30 | + |
| 31 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 32 | + { |
| 33 | + $format = strtolower((string) $input->getOption('format')); |
| 34 | + $companyId = (int) $input->getOption('company_id'); |
| 35 | + $role = (string) $input->getOption('role'); |
| 36 | + |
| 37 | + if ($companyId <= 0) { |
| 38 | + $msg = 'Missing or invalid --company_id'; |
| 39 | + $format === 'json' ? $this->jsonError($output, $msg) : $output->writeln("<error>{$msg}</error>"); |
| 40 | + |
| 41 | + return self::FAILURE; |
| 42 | + } |
| 43 | + |
| 44 | + $company = new Company($companyId); |
| 45 | + |
| 46 | + if (empty($company->getData())) { |
| 47 | + $msg = "Company #{$companyId} not found"; |
| 48 | + $format === 'json' ? $this->jsonError($output, $msg) : $output->writeln("<error>{$msg}</error>"); |
| 49 | + |
| 50 | + return self::FAILURE; |
| 51 | + } |
| 52 | + |
| 53 | + $userId = $this->resolveUserId($input); |
| 54 | + |
| 55 | + if ($userId <= 0) { |
| 56 | + $msg = 'Provide --user_id or --login or --email'; |
| 57 | + $format === 'json' ? $this->jsonError($output, $msg) : $output->writeln("<error>{$msg}</error>"); |
| 58 | + |
| 59 | + return self::FAILURE; |
| 60 | + } |
| 61 | + |
| 62 | + $user = new User($userId); |
| 63 | + |
| 64 | + if (empty($user->getData())) { |
| 65 | + $msg = "User #{$userId} not found"; |
| 66 | + $format === 'json' ? $this->jsonError($output, $msg) : $output->writeln("<error>{$msg}</error>"); |
| 67 | + |
| 68 | + return self::FAILURE; |
| 69 | + } |
| 70 | + |
| 71 | + $pdo = $this->connectPdo(); |
| 72 | + |
| 73 | + if (!$this->tableExists($pdo, 'company_user')) { |
| 74 | + $msg = 'Table company_user does not exist. Run database migrations first.'; |
| 75 | + $format === 'json' ? $this->jsonError($output, $msg) : $output->writeln("<error>{$msg}</error>"); |
| 76 | + |
| 77 | + return self::FAILURE; |
| 78 | + } |
| 79 | + |
| 80 | + $sql = 'INSERT INTO company_user (company_id, user_id, role) VALUES (?, ?, ?) ' |
| 81 | + .'ON DUPLICATE KEY UPDATE role = VALUES(role)'; |
| 82 | + $ok = $pdo->prepare($sql)->execute([$companyId, $userId, $role]); |
| 83 | + |
| 84 | + if (!$ok) { |
| 85 | + $msg = 'Failed to assign user to company'; |
| 86 | + $format === 'json' ? $this->jsonError($output, $msg) : $output->writeln("<error>{$msg}</error>"); |
| 87 | + |
| 88 | + return self::FAILURE; |
| 89 | + } |
| 90 | + |
| 91 | + if ($format === 'json') { |
| 92 | + $this->jsonSuccess($output, 'User assigned to company', [ |
| 93 | + 'company_id' => $companyId, |
| 94 | + 'user_id' => $userId, |
| 95 | + 'role' => $role, |
| 96 | + ]); |
| 97 | + } else { |
| 98 | + $output->writeln("User #{$userId} assigned to company #{$companyId} with role '{$role}'"); |
| 99 | + } |
| 100 | + |
| 101 | + return self::SUCCESS; |
| 102 | + } |
| 103 | + |
| 104 | + private function resolveUserId(InputInterface $input): int |
| 105 | + { |
| 106 | + $userId = (int) $input->getOption('user_id'); |
| 107 | + |
| 108 | + if ($userId > 0) { |
| 109 | + return $userId; |
| 110 | + } |
| 111 | + |
| 112 | + $login = trim((string) $input->getOption('login')); |
| 113 | + $email = trim((string) $input->getOption('email')); |
| 114 | + |
| 115 | + if ($login !== '') { |
| 116 | + $found = (new User())->listingQuery()->where(['login' => $login])->fetch(); |
| 117 | + |
| 118 | + return $found ? (int) $found['id'] : 0; |
| 119 | + } |
| 120 | + |
| 121 | + if ($email !== '') { |
| 122 | + $found = (new User())->listingQuery()->where(['email' => $email])->fetch(); |
| 123 | + |
| 124 | + return $found ? (int) $found['id'] : 0; |
| 125 | + } |
| 126 | + |
| 127 | + return 0; |
| 128 | + } |
| 129 | + |
| 130 | + private function connectPdo(): \PDO |
| 131 | + { |
| 132 | + return new \PDO( |
| 133 | + \Ease\Shared::cfg('DB_CONNECTION').':host='.\Ease\Shared::cfg('DB_HOST').';port='.(string) \Ease\Shared::cfg('DB_PORT', 3306).';dbname='.\Ease\Shared::cfg('DB_DATABASE').';charset=utf8mb4', |
| 134 | + \Ease\Shared::cfg('DB_USERNAME'), |
| 135 | + \Ease\Shared::cfg('DB_PASSWORD'), |
| 136 | + [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION], |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + private function tableExists(\PDO $pdo, string $table): bool |
| 141 | + { |
| 142 | + $stmt = $pdo->prepare('SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = ? AND table_name = ?'); |
| 143 | + $stmt->execute([\Ease\Shared::cfg('DB_DATABASE'), $table]); |
| 144 | + |
| 145 | + return (int) $stmt->fetchColumn() > 0; |
| 146 | + } |
| 147 | +} |
0 commit comments