Completed
Push — master ( f02e0c...59b9e2 )
by Individual IT
13:32
created

Add::execute()   D

Complexity

Conditions 14
Paths 108

Size

Total Lines 88
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 14
eloc 57
c 1
b 0
f 1
nc 108
nop 2
dl 0
loc 88
rs 4.8132

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 * @author Laurens Post <[email protected]>
5
 * @author Thomas Müller <[email protected]>
6
 *
7
 * @copyright Copyright (c) 2017, ownCloud GmbH
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OC\Core\Command\User;
25
26
use OC\Files\Filesystem;
27
use OCP\IGroupManager;
28
use OCP\IUser;
29
use OCP\IUserManager;
30
use OCP\Mail\IMailer;
31
use Symfony\Component\Console\Command\Command;
32
use Symfony\Component\Console\Input\InputInterface;
33
use Symfony\Component\Console\Input\InputOption;
34
use Symfony\Component\Console\Output\OutputInterface;
35
use Symfony\Component\Console\Input\InputArgument;
36
use Symfony\Component\Console\Question\Question;
37
38
class Add extends Command {
39
	/** @var \OCP\IUserManager */
40
	protected $userManager;
41
42
	/** @var \OCP\IGroupManager */
43
	protected $groupManager;
44
45
	/** @var IMailer  */
46
	protected $mailer;
47
48
	/**
49
	 * @param IUserManager $userManager
50
	 * @param IGroupManager $groupManager
51
	 * @param IMailer $mailer
52
	 */
53
	public function __construct(IUserManager $userManager, IGroupManager $groupManager, IMailer $mailer) {
54
		parent::__construct();
55
		$this->userManager = $userManager;
56
		$this->groupManager = $groupManager;
57
		$this->mailer = $mailer;
58
	}
59
60
	protected function configure() {
61
		$this
62
			->setName('user:add')
63
			->setDescription('adds a user')
64
			->addArgument(
65
				'uid',
66
				InputArgument::REQUIRED,
67
				'User ID used to login (must only contain a-z, A-Z, 0-9, -, _ and @)'
68
			)
69
			->addOption(
70
				'password-from-env',
71
				null,
72
				InputOption::VALUE_NONE,
73
				'read password from environment variable OC_PASS'
74
			)
75
			->addOption(
76
				'display-name',
77
				null,
78
				InputOption::VALUE_OPTIONAL,
79
				'User name used in the web UI (can contain any characters)'
80
			)
81
			->addOption(
82
				'email',
83
				null,
84
				InputOption::VALUE_OPTIONAL,
85
				'Email address for the user'
86
			)
87
			->addOption(
88
				'group',
89
				'g',
90
				InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
91
				'groups the user should be added to (The group will be created if it does not exist)'
92
			);
93
	}
94
95
	protected function execute(InputInterface $input, OutputInterface $output) {
96
		$uid = $input->getArgument('uid');
97
		if ($this->userManager->userExists($uid)) {
98
			$output->writeln('<error>The user "' . $uid . '" already exists.</error>');
99
			return 1;
100
		}
101
102
		// Validate email before we create the user
103
		if ($input->getOption('email')) {
104
			// Validate first
105
			if(!$this->mailer->validateMailAddress($input->getOption('email'))) {
106
				// Invalid! Error
107
				$output->writeln('<error>Invalid email address supplied</error>');
108
				return 1;
109
			} else {
110
				$email = $input->getOption('email');
111
			}
112
		} else {
113
			$email = null;
114
		}
115
116
		if ($input->getOption('password-from-env')) {
117
			$password = getenv('OC_PASS');
118
			if (!$password) {
119
				$output->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>');
120
				return 1;
121
			}
122
		} elseif ($input->isInteractive()) {
123
			/** @var $dialog \Symfony\Component\Console\Helper\QuestionHelper */
124
			$dialog = $this->getHelperSet()->get('question');
125
			$q = new Question('<question>Enter password: </question>',false);
126
			$q->setHidden(true);
127
			$password = $dialog->ask($input, $output, $q);
128
			$q = new Question('<question>Confirm password: </question>',false);
129
			$q->setHidden(true);
130
			$confirm = $dialog->ask($input, $output, $q);
131
132
			if ($password !== $confirm) {
133
				$output->writeln("<error>Passwords did not match!</error>");
134
				return 1;
135
			}
136
		} else {
137
			$output->writeln("<error>Interactive input or --password-from-env is needed for entering a password!</error>");
138
			return 1;
139
		}
140
141
		$user = $this->userManager->createUser(
142
			$input->getArgument('uid'),
143
			$password
144
		);
145
146
		if ($user instanceof IUser) {
147
			$output->writeln('<info>The user "' . $user->getUID() . '" was created successfully</info>');
148
		} else {
149
			$output->writeln('<error>An error occurred while creating the user</error>');
150
			return 1;
151
		}
152
153
		if ($input->getOption('display-name')) {
154
			$user->setDisplayName($input->getOption('display-name'));
155
			$output->writeln('Display name set to "' . $user->getDisplayName() . '"');
156
		}
157
158
		// Set email if supplied & valid
159
		if(!is_null($email)) {
160
			$user->setEMailAddress($email);
161
			$output->writeln('Email address set to "' . $user->getEMailAddress() . '"');
162
		}
163
164
		$groups = $input->getOption('group');
165
166
		if (!empty($groups)) {
167
			// Make sure we init the Filesystem for the user, in case we need to
168
			// init some group shares.
169
			Filesystem::init($user->getUID(), '');
170
		}
171
172
		foreach ($groups as $groupName) {
173
			$group = $this->groupManager->get($groupName);
174
			if (!$group) {
175
				$this->groupManager->createGroup($groupName);
176
				$group = $this->groupManager->get($groupName);
177
				$output->writeln('Created group "' . $group->getGID() . '"');
178
			}
179
			$group->addUser($user);
180
			$output->writeln('User "' . $user->getUID() . '" added to group "' . $group->getGID() . '"');
181
		}
182
	}
183
}
184