@@ -39,96 +39,96 @@ |
||
| 39 | 39 | |
| 40 | 40 | class AddAppPassword extends Command { |
| 41 | 41 | |
| 42 | - /** @var IUserManager */ |
|
| 43 | - protected $userManager; |
|
| 44 | - /** @var IProvider */ |
|
| 45 | - protected $tokenProvider; |
|
| 46 | - /** @var ISecureRandom */ |
|
| 47 | - private $random; |
|
| 48 | - /** @var IEventDispatcher */ |
|
| 49 | - private $eventDispatcher; |
|
| 42 | + /** @var IUserManager */ |
|
| 43 | + protected $userManager; |
|
| 44 | + /** @var IProvider */ |
|
| 45 | + protected $tokenProvider; |
|
| 46 | + /** @var ISecureRandom */ |
|
| 47 | + private $random; |
|
| 48 | + /** @var IEventDispatcher */ |
|
| 49 | + private $eventDispatcher; |
|
| 50 | 50 | |
| 51 | - public function __construct(IUserManager $userManager, |
|
| 52 | - IProvider $tokenProvider, |
|
| 53 | - ISecureRandom $random, |
|
| 54 | - IEventDispatcher $eventDispatcher) { |
|
| 55 | - $this->tokenProvider = $tokenProvider; |
|
| 56 | - $this->userManager = $userManager; |
|
| 57 | - $this->random = $random; |
|
| 58 | - $this->eventDispatcher = $eventDispatcher; |
|
| 59 | - parent::__construct(); |
|
| 60 | - } |
|
| 51 | + public function __construct(IUserManager $userManager, |
|
| 52 | + IProvider $tokenProvider, |
|
| 53 | + ISecureRandom $random, |
|
| 54 | + IEventDispatcher $eventDispatcher) { |
|
| 55 | + $this->tokenProvider = $tokenProvider; |
|
| 56 | + $this->userManager = $userManager; |
|
| 57 | + $this->random = $random; |
|
| 58 | + $this->eventDispatcher = $eventDispatcher; |
|
| 59 | + parent::__construct(); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - protected function configure() { |
|
| 63 | - $this |
|
| 64 | - ->setName('user:add-app-password') |
|
| 65 | - ->setDescription('Add app password for the named user') |
|
| 66 | - ->addArgument( |
|
| 67 | - 'user', |
|
| 68 | - InputArgument::REQUIRED, |
|
| 69 | - 'Username to add app password for' |
|
| 70 | - ) |
|
| 71 | - ->addOption( |
|
| 72 | - 'password-from-env', |
|
| 73 | - null, |
|
| 74 | - InputOption::VALUE_NONE, |
|
| 75 | - 'read password from environment variable NC_PASS/OC_PASS' |
|
| 76 | - ) |
|
| 77 | - ; |
|
| 78 | - } |
|
| 62 | + protected function configure() { |
|
| 63 | + $this |
|
| 64 | + ->setName('user:add-app-password') |
|
| 65 | + ->setDescription('Add app password for the named user') |
|
| 66 | + ->addArgument( |
|
| 67 | + 'user', |
|
| 68 | + InputArgument::REQUIRED, |
|
| 69 | + 'Username to add app password for' |
|
| 70 | + ) |
|
| 71 | + ->addOption( |
|
| 72 | + 'password-from-env', |
|
| 73 | + null, |
|
| 74 | + InputOption::VALUE_NONE, |
|
| 75 | + 'read password from environment variable NC_PASS/OC_PASS' |
|
| 76 | + ) |
|
| 77 | + ; |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 81 | - $username = $input->getArgument('user'); |
|
| 80 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 81 | + $username = $input->getArgument('user'); |
|
| 82 | 82 | |
| 83 | - $user = $this->userManager->get($username); |
|
| 84 | - if (is_null($user)) { |
|
| 85 | - $output->writeln('<error>User does not exist</error>'); |
|
| 86 | - return 1; |
|
| 87 | - } |
|
| 83 | + $user = $this->userManager->get($username); |
|
| 84 | + if (is_null($user)) { |
|
| 85 | + $output->writeln('<error>User does not exist</error>'); |
|
| 86 | + return 1; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - if ($input->getOption('password-from-env')) { |
|
| 90 | - $password = getenv('NC_PASS') ?? getenv('OC_PASS'); |
|
| 91 | - if (!$password) { |
|
| 92 | - $output->writeln('<error>--password-from-env given, but NC_PASS is empty!</error>'); |
|
| 93 | - return 1; |
|
| 94 | - } |
|
| 95 | - } elseif ($input->isInteractive()) { |
|
| 96 | - /** @var QuestionHelper $helper */ |
|
| 97 | - $helper = $this->getHelper('question'); |
|
| 89 | + if ($input->getOption('password-from-env')) { |
|
| 90 | + $password = getenv('NC_PASS') ?? getenv('OC_PASS'); |
|
| 91 | + if (!$password) { |
|
| 92 | + $output->writeln('<error>--password-from-env given, but NC_PASS is empty!</error>'); |
|
| 93 | + return 1; |
|
| 94 | + } |
|
| 95 | + } elseif ($input->isInteractive()) { |
|
| 96 | + /** @var QuestionHelper $helper */ |
|
| 97 | + $helper = $this->getHelper('question'); |
|
| 98 | 98 | |
| 99 | - $question = new Question('Enter the user password: '); |
|
| 100 | - $question->setHidden(true); |
|
| 101 | - $password = $helper->ask($input, $output, $question); |
|
| 99 | + $question = new Question('Enter the user password: '); |
|
| 100 | + $question->setHidden(true); |
|
| 101 | + $password = $helper->ask($input, $output, $question); |
|
| 102 | 102 | |
| 103 | - if ($password === null) { |
|
| 104 | - $output->writeln("<error>Password cannot be empty!</error>"); |
|
| 105 | - return 1; |
|
| 106 | - } |
|
| 107 | - } else { |
|
| 108 | - $output->writeln("<error>Interactive input or --password-from-env is needed for entering a new password!</error>"); |
|
| 109 | - return 1; |
|
| 110 | - } |
|
| 103 | + if ($password === null) { |
|
| 104 | + $output->writeln("<error>Password cannot be empty!</error>"); |
|
| 105 | + return 1; |
|
| 106 | + } |
|
| 107 | + } else { |
|
| 108 | + $output->writeln("<error>Interactive input or --password-from-env is needed for entering a new password!</error>"); |
|
| 109 | + return 1; |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - $output->writeln('<comment>The password has not been validated, some features might not work as intended.</comment>'); |
|
| 112 | + $output->writeln('<comment>The password has not been validated, some features might not work as intended.</comment>'); |
|
| 113 | 113 | |
| 114 | - $token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS); |
|
| 115 | - $generatedToken = $this->tokenProvider->generateToken( |
|
| 116 | - $token, |
|
| 117 | - $user->getUID(), |
|
| 118 | - $user->getUID(), |
|
| 119 | - $password, |
|
| 120 | - 'cli', |
|
| 121 | - IToken::PERMANENT_TOKEN, |
|
| 122 | - IToken::DO_NOT_REMEMBER |
|
| 123 | - ); |
|
| 114 | + $token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS); |
|
| 115 | + $generatedToken = $this->tokenProvider->generateToken( |
|
| 116 | + $token, |
|
| 117 | + $user->getUID(), |
|
| 118 | + $user->getUID(), |
|
| 119 | + $password, |
|
| 120 | + 'cli', |
|
| 121 | + IToken::PERMANENT_TOKEN, |
|
| 122 | + IToken::DO_NOT_REMEMBER |
|
| 123 | + ); |
|
| 124 | 124 | |
| 125 | - $this->eventDispatcher->dispatchTyped( |
|
| 126 | - new AppPasswordCreatedEvent($generatedToken) |
|
| 127 | - ); |
|
| 125 | + $this->eventDispatcher->dispatchTyped( |
|
| 126 | + new AppPasswordCreatedEvent($generatedToken) |
|
| 127 | + ); |
|
| 128 | 128 | |
| 129 | - $output->writeln('app password:'); |
|
| 130 | - $output->writeln($token); |
|
| 129 | + $output->writeln('app password:'); |
|
| 130 | + $output->writeln($token); |
|
| 131 | 131 | |
| 132 | - return 0; |
|
| 133 | - } |
|
| 132 | + return 0; |
|
| 133 | + } |
|
| 134 | 134 | } |