Completed
Pull Request — master (#551)
by Maxence
01:54
created

MembersAdd::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @copyright 2017
14
 * @license GNU AGPL version 3 or any later version
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 */
30
31
32
namespace OCA\Circles\Command;
33
34
use daita\MySmallPhpTools\Exceptions\RequestContentException;
35
use daita\MySmallPhpTools\Exceptions\RequestNetworkException;
36
use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException;
37
use daita\MySmallPhpTools\Exceptions\RequestResultSizeException;
38
use daita\MySmallPhpTools\Exceptions\RequestServerException;
39
use daita\MySmallPhpTools\Model\Nextcloud\nc21\NC21Request;
40
use daita\MySmallPhpTools\Model\Request;
41
use Exception;
42
use OC\Core\Command\Base;
43
use OCA\Circles\Db\CircleRequest;
44
use OCA\Circles\Exceptions\GSStatusException;
45
use OCA\Circles\IMember;
46
use OCA\Circles\Model\Member;
47
use OCA\Circles\Service\ConfigService;
48
use OCA\Circles\Service\CurrentUserService;
49
use OCA\Circles\Service\MemberService;
50
use Symfony\Component\Console\Input\InputArgument;
51
use Symfony\Component\Console\Input\InputInterface;
52
use Symfony\Component\Console\Input\InputOption;
53
use Symfony\Component\Console\Output\OutputInterface;
54
55
56
/**
57
 * Class MembersCreate
58
 *
59
 * @package OCA\Circles\Command
60
 */
61
class MembersAdd extends Base {
62
63
64
	/** @var CurrentUserService */
65
	private $currentUserService;
66
67
	/** @var CircleRequest */
68
	private $circleRequest;
69
70
	/** @var MemberService */
71
	private $memberService;
72
73
	/** @var ConfigService */
74
	private $configService;
75
76
77
	/**
78
	 * MembersCreate constructor.
79
	 *
80
	 * @param CircleRequest $circleRequest
81
	 * @param CurrentUserService $currentUserService
82
	 * @param MemberService $memberService
83
	 * @param ConfigService $configService
84
	 */
85
	public function __construct(
86
		CircleRequest $circleRequest, CurrentUserService $currentUserService, MemberService $memberService,
87
		ConfigService $configService
88
	) {
89
		parent::__construct();
90
		$this->currentUserService = $currentUserService;
91
		$this->circleRequest = $circleRequest;
92
93
		$this->memberService = $memberService;
94
		$this->configService = $configService;
95
	}
96
97
98
	protected function configure() {
99
		parent::configure();
100
		$this->setName('circles:members:add')
101
			 ->setDescription('Add a member to a Circle')
102
			 ->addArgument('circle_id', InputArgument::REQUIRED, 'ID of the circle')
103
			 ->addArgument('user', InputArgument::REQUIRED, 'username of the member')
104
			 ->addOption('viewer', '', InputOption::VALUE_REQUIRED, 'set a viewer', '')
105
			 ->addOption('type', '', InputOption::VALUE_REQUIRED, 'type of the user', Member::TYPE_USER);
106
	}
107
108
109
	/**
110
	 * @param InputInterface $input
111
	 * @param OutputInterface $output
112
	 *
113
	 * @return int
114
	 * @throws Exception
115
	 */
116
	protected function execute(InputInterface $input, OutputInterface $output): int {
117
		$circleId = $input->getArgument('circle_id');
118
		$viewerId = $input->getOption('viewer');
119
		$userId = $input->getArgument('user');
120
		$userType = $input->getOption('type');
121
122
		if ($viewerId !== '') {
123
			$this->currentUserService->setLocalViewer($viewerId);
124
		} else {
125
			$localCircle = $this->circleRequest->getCircle($circleId);
126
			if (!$this->configService->isLocalInstance($localCircle->getInstance())) {
127
				throw new Exception('the Circle is not managed from this instance, please use --viewer');
128
			}
129
130
			// TODO: manage NO_OWNER circles
131
			$owner = $localCircle->getOwner();
132
			$this->currentUserService->setCurrentUser($owner);
133
		}
134
135
		$member = $this->currentUserService->createCurrentUser($userId, (int)$userType);
136
		$this->memberService->addMember($circleId, $member);
137
138
////		$this->membersService->levelMember($circleId, $userId, DeprecatedMember::TYPE_USER, $instance, $level, true);
139
////
140
////		$member = $this->membersRequest->forceGetMember($circleId, $userId, DeprecatedMember::TYPE_USER, $instance);
141
////		echo json_encode($member, JSON_PRETTY_PRINT) . "\n";
142
143
		return 0;
144
	}
145
146
147
	/**
148
	 * @param string $search
149
	 * @param string $instance
150
	 *
151
	 * @return string
152
	 */
153
	private function findUserFromLookup(string $search, string &$instance = ''): string {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
154
		$userId = '';
155
156
		/** @var string $lookup */
157
		try {
158
			$lookup = $this->configService->getGSStatus(ConfigService::GS_LOOKUP);
159
		} catch (GSStatusException $e) {
160
			return '';
161
		}
162
163
		$request = new NC21Request(ConfigService::GS_LOOKUP_USERS, Request::TYPE_GET);
164
		$this->configService->configureRequest($request);
165
		$request->basedOnUrl($lookup);
166
		$request->addParam('search', $search);
167
168
		try {
169
			$users = $this->retrieveJson($request);
170
		} catch (
171
		RequestContentException |
172
		RequestNetworkException |
173
		RequestResultSizeException |
174
		RequestServerException |
175
		RequestResultNotJsonException $e
176
		) {
177
			return '';
178
		}
179
180
		$result = [];
181
		foreach ($users as $user) {
182
			if (!array_key_exists('userid', $user)) {
183
				continue;
184
			}
185
186
			list(, $host) = explode('@', $user['federationId']);
187
			if (strtolower($user['userid']['value']) === strtolower($search)) {
188
				$userId = $user['userid']['value'];
189
				$instance = $host;
190
			}
191
192
			$result[] = $user['userid']['value'] . ' <info>@' . $host . '</info>';
193
		}
194
195
//		if ($userId === '') {
196
//			foreach($result as $item) {
197
//				$output->writeln($item);
198
//			}
199
//		}
200
201
		return $userId;
202
	}
203
204
}
205
206