entityManager = $entityManager; $this->passwordHasher = $passwordHasher; $this->environment = $environment; } protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); $helper = $this->getHelper('question'); $io->title('Create Super Admin User'); // Ask for email $emailQuestion = new Question('Email address: '); $emailQuestion->setValidator(function ($answer) { if (empty($answer)) { throw new \RuntimeException('Email cannot be empty'); } if (!filter_var($answer, FILTER_VALIDATE_EMAIL)) { throw new \RuntimeException('Invalid email format'); } return $answer; }); $email = $helper->ask($input, $output, $emailQuestion); // Check if user already exists $existingUser = $this->entityManager->getRepository(User::class) ->findOneBy(['email' => $email]); if ($existingUser) { $io->error("A user with email '{$email}' already exists."); return Command::FAILURE; } // Ask for name $nameQuestion = new Question('First name: '); $nameQuestion->setValidator(function ($answer) { if (empty(trim($answer))) { throw new \RuntimeException('First name cannot be empty'); } return trim($answer); }); $name = $helper->ask($input, $output, $nameQuestion); // Ask for surname $surnameQuestion = new Question('Last name: '); $surnameQuestion->setValidator(function ($answer) { if (empty(trim($answer))) { throw new \RuntimeException('Last name cannot be empty'); } return trim($answer); }); $surname = $helper->ask($input, $output, $surnameQuestion); // Ask for password $isDev = $this->environment === 'dev'; $passwordQuestion = new Question('Password: '); $passwordQuestion->setHidden(true); $passwordQuestion->setHiddenFallback(false); $passwordQuestion->setValidator(function ($answer) use ($isDev) { if (empty($answer)) { throw new \RuntimeException('Password cannot be empty'); } if (!$isDev && strlen($answer) < 8) { throw new \RuntimeException('Password must be at least 8 characters long'); } return $answer; }); $password = $helper->ask($input, $output, $passwordQuestion); if ($isDev && strlen($password) < 8) { $io->warning('Development mode: password length requirement bypassed'); } // Confirm password $confirmPasswordQuestion = new Question('Confirm password: '); $confirmPasswordQuestion->setHidden(true); $confirmPasswordQuestion->setHiddenFallback(false); $confirmPassword = $helper->ask($input, $output, $confirmPasswordQuestion); if ($password !== $confirmPassword) { $io->error('Passwords do not match.'); return Command::FAILURE; } // Create user $user = new User(); $user->setEmail($email); $user->setName($name); $user->setSurname($surname); $user->setRoles(['ROLE_SUPER_ADMIN', 'ROLE_ADMIN']); $user->setIsActive(true); $user->setIsDeleted(false); // Hash password $hashedPassword = $this->passwordHasher->hashPassword($user, $password); $user->setPassword($hashedPassword); // Persist user $this->entityManager->persist($user); $this->entityManager->flush(); $io->success([ 'Super Admin user created successfully!', "Email: {$email}", "Name: {$name} {$surname}", 'Roles: ROLE_SUPER_ADMIN, ROLE_ADMIN' ]); return Command::SUCCESS; } }