Passed
Push — master ( 43220c...498d3a )
by Christoph
16:41 queued 20s
created
core/Command/User/AddAppPassword.php 1 patch
Indentation   +91 added lines, -91 removed lines patch added patch discarded remove patch
@@ -42,95 +42,95 @@
 block discarded – undo
42 42
 
43 43
 class AddAppPassword extends Command {
44 44
 
45
-	/** @var IUserManager */
46
-	protected $userManager;
47
-
48
-	/** @var IProvider */
49
-	protected $tokenProvider;
50
-
51
-	/** @var ISecureRandom */
52
-	private $random;
53
-
54
-	/** @var IEventDispatcher */
55
-	private $eventDispatcher;
56
-
57
-	public function __construct(IUserManager $userManager,
58
-								IProvider $tokenProvider,
59
-								ISecureRandom $random,
60
-								IEventDispatcher $eventDispatcher) {
61
-		$this->tokenProvider = $tokenProvider;
62
-		$this->userManager = $userManager;
63
-		$this->random = $random;
64
-		$this->eventDispatcher = $eventDispatcher;
65
-		parent::__construct();
66
-	}
67
-
68
-	protected function configure() {
69
-		$this
70
-			->setName('user:add-app-password')
71
-			->setDescription('Add app password for the named user')
72
-			->addArgument(
73
-				'user',
74
-				InputArgument::REQUIRED,
75
-				'Username to add app password for'
76
-			)
77
-			->addOption(
78
-				'password-from-env',
79
-				null,
80
-				InputOption::VALUE_NONE,
81
-				'Read password from environment variable NC_PASS/OC_PASS. Alternatively it will be asked for interactively or an app password without the login password will be created.'
82
-			)
83
-		;
84
-	}
85
-
86
-	protected function execute(InputInterface $input, OutputInterface $output): int {
87
-		$username = $input->getArgument('user');
88
-		$password = null;
89
-
90
-		$user = $this->userManager->get($username);
91
-		if (is_null($user)) {
92
-			$output->writeln('<error>User does not exist</error>');
93
-			return 1;
94
-		}
95
-
96
-		if ($input->getOption('password-from-env')) {
97
-			$password = getenv('NC_PASS') ?? getenv('OC_PASS');
98
-			if (!$password) {
99
-				$output->writeln('<error>--password-from-env given, but NC_PASS is empty!</error>');
100
-				return 1;
101
-			}
102
-		} elseif ($input->isInteractive()) {
103
-			/** @var QuestionHelper $helper */
104
-			$helper = $this->getHelper('question');
105
-
106
-			$question = new Question('Enter the user password: ');
107
-			$question->setHidden(true);
108
-			/** @var null|string $password */
109
-			$password = $helper->ask($input, $output, $question);
110
-		}
111
-
112
-		if ($password === null) {
113
-			$output->writeln('<info>No password provided. The generated app password will therefore have limited capabilities. Any operation that requires the login password will fail.</info>');
114
-		}
115
-
116
-		$token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
117
-		$generatedToken = $this->tokenProvider->generateToken(
118
-			$token,
119
-			$user->getUID(),
120
-			$user->getUID(),
121
-			$password,
122
-			'cli',
123
-			IToken::PERMANENT_TOKEN,
124
-			IToken::DO_NOT_REMEMBER
125
-		);
126
-
127
-		$this->eventDispatcher->dispatchTyped(
128
-			new AppPasswordCreatedEvent($generatedToken)
129
-		);
130
-
131
-		$output->writeln('app password:');
132
-		$output->writeln($token);
133
-
134
-		return 0;
135
-	}
45
+    /** @var IUserManager */
46
+    protected $userManager;
47
+
48
+    /** @var IProvider */
49
+    protected $tokenProvider;
50
+
51
+    /** @var ISecureRandom */
52
+    private $random;
53
+
54
+    /** @var IEventDispatcher */
55
+    private $eventDispatcher;
56
+
57
+    public function __construct(IUserManager $userManager,
58
+                                IProvider $tokenProvider,
59
+                                ISecureRandom $random,
60
+                                IEventDispatcher $eventDispatcher) {
61
+        $this->tokenProvider = $tokenProvider;
62
+        $this->userManager = $userManager;
63
+        $this->random = $random;
64
+        $this->eventDispatcher = $eventDispatcher;
65
+        parent::__construct();
66
+    }
67
+
68
+    protected function configure() {
69
+        $this
70
+            ->setName('user:add-app-password')
71
+            ->setDescription('Add app password for the named user')
72
+            ->addArgument(
73
+                'user',
74
+                InputArgument::REQUIRED,
75
+                'Username to add app password for'
76
+            )
77
+            ->addOption(
78
+                'password-from-env',
79
+                null,
80
+                InputOption::VALUE_NONE,
81
+                'Read password from environment variable NC_PASS/OC_PASS. Alternatively it will be asked for interactively or an app password without the login password will be created.'
82
+            )
83
+        ;
84
+    }
85
+
86
+    protected function execute(InputInterface $input, OutputInterface $output): int {
87
+        $username = $input->getArgument('user');
88
+        $password = null;
89
+
90
+        $user = $this->userManager->get($username);
91
+        if (is_null($user)) {
92
+            $output->writeln('<error>User does not exist</error>');
93
+            return 1;
94
+        }
95
+
96
+        if ($input->getOption('password-from-env')) {
97
+            $password = getenv('NC_PASS') ?? getenv('OC_PASS');
98
+            if (!$password) {
99
+                $output->writeln('<error>--password-from-env given, but NC_PASS is empty!</error>');
100
+                return 1;
101
+            }
102
+        } elseif ($input->isInteractive()) {
103
+            /** @var QuestionHelper $helper */
104
+            $helper = $this->getHelper('question');
105
+
106
+            $question = new Question('Enter the user password: ');
107
+            $question->setHidden(true);
108
+            /** @var null|string $password */
109
+            $password = $helper->ask($input, $output, $question);
110
+        }
111
+
112
+        if ($password === null) {
113
+            $output->writeln('<info>No password provided. The generated app password will therefore have limited capabilities. Any operation that requires the login password will fail.</info>');
114
+        }
115
+
116
+        $token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
117
+        $generatedToken = $this->tokenProvider->generateToken(
118
+            $token,
119
+            $user->getUID(),
120
+            $user->getUID(),
121
+            $password,
122
+            'cli',
123
+            IToken::PERMANENT_TOKEN,
124
+            IToken::DO_NOT_REMEMBER
125
+        );
126
+
127
+        $this->eventDispatcher->dispatchTyped(
128
+            new AppPasswordCreatedEvent($generatedToken)
129
+        );
130
+
131
+        $output->writeln('app password:');
132
+        $output->writeln($token);
133
+
134
+        return 0;
135
+    }
136 136
 }
Please login to merge, or discard this patch.