setDescription('Edit user in Phraseanet')
->addOption('user_id', null, InputOption::VALUE_REQUIRED, 'The id of user.')
->addOption('generatepassword', null, InputOption::VALUE_NONE, 'Generate and set with a random value')
->addOption('password', null, InputOption::VALUE_REQUIRED, 'Set the user password to the input value')
->addOption('login', null, InputOption::VALUE_REQUIRED, 'the user login need to be unique')
->addOption('email', null, InputOption::VALUE_REQUIRED, 'the user email, do not send notification about update change')
->addOption('mailLock', null, InputOption::VALUE_REQUIRED, 'lock the email , true/false')
->addOption('lastName', null, InputOption::VALUE_REQUIRED, 'user lastname')
->addOption('firstName', null, InputOption::VALUE_REQUIRED, 'user firstname')
->addOption('companyName', null, InputOption::VALUE_REQUIRED, 'user company name')
->addOption('yes', 'y', InputOption::VALUE_NONE, 'Answer yes to all questions')
->setHelp('');
return $this;
}
protected function doExecute(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getHelperSet()->get('dialog');
/** @var UserRepository $userRepository */
$userRepository = $this->container['repo.users'];
/** @var UserManipulator $userManipulator */
$userManipulator = $this->container['manipulator.user'];
$userId = $input->getOption('user_id');
$login = $input->getOption('login');
$email = $input->getOption('email');
$mailLock = $input->getOption('mailLock');
$password = $input->getOption('password');
$lastName = $input->getOption('lastName');
$firstName = $input->getOption('firstName');
$companyName = $input->getOption('companyName');
$generatePassword = $input->getOption('generatepassword');
$yes = $input->getOption('yes');
if (empty($userId)) {
$output->writeln('Give the user_id to edit : example --user_id=9999');
return 0;
}
$user = $userRepository->find($userId);
if ($user === null) {
$output->writeln('Not found User.');
return 0;
}
if (!empty($password) && !empty($generatePassword)) {
$output->writeln('Choose only one option to set a password!');
return 0;
}
if ($login) {
try {
$userManipulator->setLogin($user, $login);
$output->writeln('User Login successfully changed !');
} catch(\Exception $e) {
$output->writeln('' . $e->getMessage() . '');
}
}
if ($email) {
try {
$userManipulator->setEmail($user, $email);
$output->writeln('User Email successfully changed !');
} catch(\Exception $e) {
$output->writeln('' . $e->getMessage() . '');
}
}
if ($mailLock) {
if (in_array($mailLock, ['true', 'false'])) {
$mailLock = ($mailLock == 'true') ? true : false;
$user->setMailLocked($mailLock);
$userManipulator->updateUser($user);
$output->writeln('User mailLock status successfully changed !');
} else {
$output->writeln('Bad value for mailLock (true/false)');
}
}
if ($lastName) {
$user->setLastName($lastName);
$userManipulator->updateUser($user);
$output->writeln('User lastname successfully changed !');
}
if ($firstName) {
$user->setFirstName($firstName);
$userManipulator->updateUser($user);
$output->writeln('User firstname successfully changed !');
}
if ($companyName) {
$user->setCompany($companyName);
$userManipulator->updateUser($user);
$output->writeln('User company successfully changed !');
}
if ($generatePassword) {
$pw = $this->container['random.medium']->generateString(64);
$userManipulator->setPassword($user, $pw);
$output->writeln('User password successfully changed !');
}
if ($password) {
if (!$yes) {
do {
$continue = mb_strtolower($dialog->ask($output, 'Do you want really set password to this user? (y/N)', 'N'));
} while (!in_array($continue, ['y', 'n']));
if ($continue !== 'y') {
$output->writeln('Aborting !');
return 0;
}
}
$userManipulator->setPassword($user,$password);
$output->writeln('User password successfully changed !');
}
return 0;
}
}