diff --git a/src/Command/User/SetPasswordCommand.php b/src/Command/User/SetPasswordCommand.php index 3a738ef8..237c6a7b 100644 --- a/src/Command/User/SetPasswordCommand.php +++ b/src/Command/User/SetPasswordCommand.php @@ -77,7 +77,6 @@ class SetPasswordCommand extends Command ->setDescription('Sets the password of a user') ->setHelp('This password allows you to set the password of a user, without knowing the old password.') ->addArgument('user', InputArgument::REQUIRED, 'The name of the user') - ; } diff --git a/src/Command/User/UserEnableCommand.php b/src/Command/User/UserEnableCommand.php new file mode 100644 index 00000000..847b5f3e --- /dev/null +++ b/src/Command/User/UserEnableCommand.php @@ -0,0 +1,92 @@ +entityManager = $entityManager; + + parent::__construct($name); + } + + protected function configure(): void + { + $this + ->setDescription('Enables/Disable the login of one or more users') + ->setHelp('This allows you to allow or prevent the login of certain user. Use the --disable option to disable the login for the given users') + ->addArgument('users', InputArgument::IS_ARRAY, 'The usernames of the users to use') + ->addOption('all', 'a', InputOption::VALUE_NONE, 'Enable/Disable all users') + ->addOption('disable', 'd', InputOption::VALUE_NONE, 'Disable the login of the given users') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + $usernames = $input->getArgument('users'); + $all_users = $input->getOption('all'); + $disabling = $input->getOption('disable'); + + if(!$all_users && empty($usernames)) { + $io->error('No users given! You have to pass atleast one username or use the --all option to use all users!'); + return self::FAILURE; + } + + $repo = $this->entityManager->getRepository(User::class); + + $users = []; + if($all_users) { //If we requested to change all users at once, then get all users from repo + $users = $repo->findAll(); + } else { //Otherwise, fetch the users from DB + foreach ($usernames as $username) { + $user = $repo->findByEmailOrName($username); + if ($user === null) { + $io->error('No user found with username: '.$username); + return self::FAILURE; + } + $users[] = $user; + } + } + + if ($disabling) { + $io->note('The following users will be disabled:'); + } else { + $io->note('The following users will be enabled:'); + } + $io->table(['Username', 'Enabled/Disabled'], + array_map(function(User $user) { + return [$user->getFullName(true), $user->isDisabled() ? 'Disabled' : 'Enabled']; + }, $users)); + + if(!$io->confirm('Do you want to continue?')) { + $io->warning('Aborting!'); + return self::SUCCESS; + } + + foreach ($users as $user) { + $user->setDisabled($disabling); + } + + //Save the results + $this->entityManager->flush(); + + $io->success('Successfully changed the state of the users!'); + + return self::SUCCESS; + } +} \ No newline at end of file