Completed
Pull Request — master (#362)
by Maxence
02:14
created

MembersCreate::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
4
/**
5
 * Circles - Bring cloud-users closer together.
6
 *
7
 * This file is licensed under the Affero General Public License version 3 or
8
 * later. See the COPYING file.
9
 *
10
 * @author Maxence Lange <[email protected]>
11
 * @copyright 2017
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
30
namespace OCA\Circles\Command;
31
32
use daita\MySmallPhpTools\Exceptions\RequestContentException;
33
use daita\MySmallPhpTools\Exceptions\RequestNetworkException;
34
use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException;
35
use daita\MySmallPhpTools\Exceptions\RequestResultSizeException;
36
use daita\MySmallPhpTools\Exceptions\RequestServerException;
37
use daita\MySmallPhpTools\Model\Request;
38
use daita\MySmallPhpTools\Traits\TRequest;
39
use Exception;
40
use OC\Core\Command\Base;
41
use OC\User\NoUserException;
42
use OCA\Circles\Db\MembersRequest;
43
use OCA\Circles\Exceptions\GSStatusException;
44
use OCA\Circles\Model\Member;
45
use OCA\Circles\Service\ConfigService;
46
use OCA\Circles\Service\MembersService;
47
use OCP\IL10N;
48
use OCP\IUserManager;
49
use Symfony\Component\Console\Input\InputArgument;
50
use Symfony\Component\Console\Input\InputInterface;
51
use Symfony\Component\Console\Output\OutputInterface;
52
53
54
/**
55
 * Class MembersCreate
56
 *
57
 * @package OCA\Circles\Command
58
 */
59
class MembersCreate extends Base {
60
61
62
	use TRequest;
63
64
65
	/** @var IL10N */
66
	private $l10n;
67
68
	/** @var IUserManager */
69
	private $userManager;
70
71
	/** @var MembersService */
72
	private $membersService;
73
74
	/** @var MembersRequest */
75
	private $membersRequest;
76
77
	/** @var ConfigService */
78
	private $configService;
79
80
81
	/**
82
	 * MembersCreate constructor.
83
	 *
84
	 * @param IL10N $l10n
85
	 * @param IUserManager $userManager
86
	 * @param MembersService $membersService
87
	 * @param MembersRequest $membersRequest
88
	 * @param ConfigService $configService
89
	 */
90
	public function __construct(
91
		IL10N $l10n, IUserManager $userManager, MembersService $membersService,
92
		MembersRequest $membersRequest, ConfigService $configService
93
	) {
94
		parent::__construct();
95
		$this->l10n = $l10n;
96
		$this->userManager = $userManager;
97
		$this->membersService = $membersService;
98
		$this->membersRequest = $membersRequest;
99
		$this->configService = $configService;
100
	}
101
102
103
	protected function configure() {
104
		parent::configure();
105
		$this->setName('circles:members:create')
106
			 ->setDescription('create a new member')
107
			 ->addArgument('circle_id', InputArgument::REQUIRED, 'ID of the circle')
108
			 ->addArgument('user', InputArgument::REQUIRED, 'username of the member')
109
			 ->addArgument('level', InputArgument::OPTIONAL, 'level of the member', 'member');
110
	}
111
112
113
	/**
114
	 * @param InputInterface $input
115
	 * @param OutputInterface $output
116
	 *
117
	 * @return int
118
	 * @throws NoUserException
119
	 * @throws Exception
120
	 */
121
	protected function execute(InputInterface $input, OutputInterface $output): int {
122
		$circleId = $input->getArgument('circle_id');
123
		$userId = $input->getArgument('user');
124
		$level = $input->getArgument('level');
125
126
		$instance = '';
127
		$user = $this->userManager->get($userId);
128
		if ($user === null) {
129
			$userId = $this->findUserFromLookup($userId, $instance);
130
		} else {
131
			$userId = $user->getUID();
132
		}
133
134
		if ($userId === '') {
135
			throw new NoUserException('user does not exist');
136
		}
137
138
		$levels = [
139
			'member'    => Member::LEVEL_MEMBER,
140
			'moderator' => Member::LEVEL_MODERATOR,
141
			'admin'     => Member::LEVEL_ADMIN,
142
			'owner'     => Member::LEVEL_OWNER
143
		];
144
145
		if (!key_exists(strtolower($level), $levels)) {
146
			throw new Exception('unknown level: ' . json_encode(array_keys($levels)));
147
		}
148
149
		$level = $levels[strtolower($level)];
150
151
		$this->membersService->addMember($circleId, $userId, Member::TYPE_USER, $instance, true);
152
		$this->membersService->levelMember($circleId, $userId, Member::TYPE_USER, $instance, $level, true);
153
154
		$member = $this->membersRequest->forceGetMember($circleId, $userId, Member::TYPE_USER, $instance);
155
		echo json_encode($member, JSON_PRETTY_PRINT) . "\n";
156
157
		return 0;
158
	}
159
160
161
	/**
162
	 * @param string $search
163
	 * @param string $instance
164
	 *
165
	 * @return string
166
	 */
167
	private function findUserFromLookup(string $search, string &$instance = ''): string {
168
		$userId = '';
169
170
		/** @var string $lookup */
171
		try {
172
			$lookup = $this->configService->getGSStatus(ConfigService::GS_LOOKUP);
173
		} catch (GSStatusException $e) {
174
			return '';
175
		}
176
177
		$request = new Request('/users', Request::TYPE_GET);
178
		$request->setProtocols(['https', 'http']);
0 ignored issues
show
Bug introduced by
The method setProtocols() does not exist on daita\MySmallPhpTools\Model\Request. Did you maybe mean setProtocol()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
179
		$request->addData('search', $search);
180
		$request->setAddressFromUrl($lookup);
181
182
		try {
183
			$users = $this->retrieveJson($request);
184
		} catch (
185
		RequestContentException |
186
		RequestNetworkException |
187
		RequestResultSizeException |
188
		RequestServerException |
189
		RequestResultNotJsonException $e
190
		) {
191
			return '';
192
		}
193
194
		$result = [];
195
		foreach ($users as $user) {
196
			if (!array_key_exists('userid', $user)) {
197
				continue;
198
			}
199
200
			list(, $host) = explode('@', $user['federationId']);
201
			if (strtolower($user['userid']['value']) === strtolower($search)) {
202
				$userId = $user['userid']['value'];
203
				$instance = $host;
204
			}
205
206
			$result[] = $user['userid']['value'] . ' <info>@' . $host . '</info>';
207
		}
208
209
//		if ($userId === '') {
210
//			foreach($result as $item) {
211
//				$output->writeln($item);
212
//			}
213
//		}
214
215
		return $userId;
216
	}
217
218
}
219
220