setDescription('Set user password in Phraseanet (experimental)') ->addOption('user_id', null, InputOption::VALUE_REQUIRED, 'The id of user.') ->addOption('generate', null, InputOption::VALUE_NONE, 'Generate and set with a random value') ->addOption('password', null, InputOption::VALUE_OPTIONAL, 'Set the user password to the input value') ->addOption('send_renewal_email', null, InputOption::VALUE_NONE, 'Send email link to user for password renewing, work only if --password or --generate are not define') ->addOption('password_hash', null, InputOption::VALUE_OPTIONAL, 'Define a password hashed, work only with password_nonce') ->addOption('password_nonce', null, InputOption::VALUE_OPTIONAL, 'Define a password nonce, work only with password_hash') ->addOption('dump', null, InputOption::VALUE_NONE, 'Return the password hashed and nonce') ->addOption('jsonformat', null, InputOption::VALUE_NONE, 'Output in json format') ->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'); $userRepository = $this->container['repo.users']; $userManipulator = $this->container['manipulator.user']; $user = $userRepository->find($input->getOption('user_id')); $password = $input->getOption('password'); $generate = $input->getOption('generate'); $sendRenewalEmail = $input->getOption('send_renewal_email'); $dump = $input->getOption('dump'); $passwordHash = $input->getOption('password_hash'); $passwordNonce = $input->getOption('password_nonce'); $jsonformat = $input->getOption('jsonformat'); $yes = $input->getOption('yes'); if ($user === null) { $output->writeln('Not found User.'); return 0; } if ($passwordHash && $passwordNonce) { $user->setNonce($passwordNonce); $user->setPassword($passwordHash); $userManipulator->updateUser($user); $output->writeln('password set with hashed pass'); return 0; } if ($dump) { $oldHash = $user->getPassword(); $oldNonce = $user->getNonce(); } if ($generate) { $oldHash = $user->getPassword(); $oldNonce = $user->getNonce(); $password = $this->container['random.medium']->generateString(64); } else { if (!$password && $sendRenewalEmail) { $this->sendPasswordSetupMail($user); $output->writeln('email link sended for password renewing!'); return 0; } elseif (!$password && !$sendRenewalEmail && ! $dump) { $output->writeln('choose one option to set a password!'); return 0; } } 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; } } $oldHash = $user->getPassword(); $oldNonce = $user->getNonce(); $userManipulator->setPassword($user,$password); } if ($dump) { if ($jsonformat) { $hash['password_hash'] = $oldHash; $hash['nonce'] = $oldNonce; echo json_encode($hash); return 0; } else { $output->writeln('password_hash :' . $oldHash); $output->writeln('nonce :' . $oldNonce); return 0; } } if (($password || $generate)) { if ($jsonformat) { $hash['new_password'] = $password; $hash['previous_password_hash'] = $oldHash; $hash['previous_nonce'] = $oldNonce; echo json_encode($hash); } else { $output->writeln('new_password :' . $password); $output->writeln('previous_password_hash :' . $oldHash); $output->writeln('previous_nonce :' . $oldNonce); } } return 0; } /** * Send mail for renew password * @param User $user */ private function sendPasswordSetupMail(User $user) { $this->setDelivererLocator(new LazyLocator($this->container, 'notification.deliverer')); $receiver = Receiver::fromUser($user); $token = $this->container['manipulator.token']->createResetPasswordToken($user); $url = $this->container['url_generator']->generate('login_renew_password', [ 'token' => $token->getValue() ], true); $mail = MailRequestPasswordUpdate::create($this->container, $receiver); $servername = $this->container['conf']->get('servername'); $mail->setButtonUrl($url); $mail->setLogin($user->getLogin()); $mail->setExpiration(new \DateTime('+1 day')); if (($locale = $user->getLocale()) != null) { $mail->setLocale($locale); } $this->deliver($mail); } }