|
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\Model\Member; |
|
46
|
|
|
use OCA\Circles\Service\ConfigService; |
|
47
|
|
|
use OCA\Circles\Service\FederatedUserService; |
|
48
|
|
|
use OCA\Circles\Service\MemberService; |
|
49
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
50
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
51
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
52
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Class MembersCreate |
|
57
|
|
|
* |
|
58
|
|
|
* @package OCA\Circles\Command |
|
59
|
|
|
*/ |
|
60
|
|
|
class MembersAdd extends Base { |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** @var FederatedUserService */ |
|
64
|
|
|
private $federatedUserService; |
|
65
|
|
|
|
|
66
|
|
|
/** @var CircleRequest */ |
|
67
|
|
|
private $circleRequest; |
|
68
|
|
|
|
|
69
|
|
|
/** @var MemberService */ |
|
70
|
|
|
private $memberService; |
|
71
|
|
|
|
|
72
|
|
|
/** @var ConfigService */ |
|
73
|
|
|
private $configService; |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* MembersCreate constructor. |
|
78
|
|
|
* |
|
79
|
|
|
* @param CircleRequest $circleRequest |
|
80
|
|
|
* @param FederatedUserService $federatedUserService |
|
81
|
|
|
* @param MemberService $memberService |
|
82
|
|
|
* @param ConfigService $configService |
|
83
|
|
|
*/ |
|
84
|
|
|
public function __construct( |
|
85
|
|
|
CircleRequest $circleRequest, FederatedUserService $federatedUserService, |
|
86
|
|
|
MemberService $memberService, |
|
87
|
|
|
ConfigService $configService |
|
88
|
|
|
) { |
|
89
|
|
|
parent::__construct(); |
|
90
|
|
|
$this->federatedUserService = $federatedUserService; |
|
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('initiator', '', InputOption::VALUE_REQUIRED, 'set an initiator to the request', '') |
|
105
|
|
|
->addOption( |
|
106
|
|
|
'type', '', InputOption::VALUE_REQUIRED, 'type of the user', |
|
107
|
|
|
Member::$DEF_TYPE[Member::TYPE_USER] |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param InputInterface $input |
|
114
|
|
|
* @param OutputInterface $output |
|
115
|
|
|
* |
|
116
|
|
|
* @return int |
|
117
|
|
|
* @throws Exception |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int { |
|
120
|
|
|
$circleId = $input->getArgument('circle_id'); |
|
121
|
|
|
$userId = $input->getArgument('user'); |
|
122
|
|
|
|
|
123
|
|
|
$this->federatedUserService->commandLineInitiator($input->getOption('initiator'), $circleId, false); |
|
124
|
|
|
|
|
125
|
|
|
$type = Member::parseTypeString($input->getOption('type')); |
|
126
|
|
|
|
|
127
|
|
|
$federatedUser = $this->federatedUserService->generateFederatedUser($userId, (int)$type); |
|
128
|
|
|
$outcome = $this->memberService->addMember($circleId, $federatedUser); |
|
129
|
|
|
|
|
130
|
|
|
echo json_encode($outcome, JSON_PRETTY_PRINT) . "\n"; |
|
131
|
|
|
|
|
132
|
|
|
return 0; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param string $search |
|
138
|
|
|
* @param string $instance |
|
139
|
|
|
* |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
|
|
private function findUserFromLookup(string $search, string &$instance = ''): string { |
|
|
|
|
|
|
143
|
|
|
$userId = ''; |
|
144
|
|
|
|
|
145
|
|
|
/** @var string $lookup */ |
|
146
|
|
|
try { |
|
147
|
|
|
$lookup = $this->configService->getGSStatus(ConfigService::GS_LOOKUP); |
|
148
|
|
|
} catch (GSStatusException $e) { |
|
149
|
|
|
return ''; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$request = new NC21Request(ConfigService::GS_LOOKUP_USERS, Request::TYPE_GET); |
|
153
|
|
|
$this->configService->configureRequest($request); |
|
154
|
|
|
$request->basedOnUrl($lookup); |
|
155
|
|
|
$request->addParam('search', $search); |
|
156
|
|
|
|
|
157
|
|
|
try { |
|
158
|
|
|
$users = $this->retrieveJson($request); |
|
159
|
|
|
} catch ( |
|
160
|
|
|
RequestContentException | |
|
161
|
|
|
RequestNetworkException | |
|
162
|
|
|
RequestResultSizeException | |
|
163
|
|
|
RequestServerException | |
|
164
|
|
|
RequestResultNotJsonException $e |
|
165
|
|
|
) { |
|
166
|
|
|
return ''; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
$result = []; |
|
170
|
|
|
foreach ($users as $user) { |
|
171
|
|
|
if (!array_key_exists('userid', $user)) { |
|
172
|
|
|
continue; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
list(, $host) = explode('@', $user['federationId']); |
|
176
|
|
|
if (strtolower($user['userid']['value']) === strtolower($search)) { |
|
177
|
|
|
$userId = $user['userid']['value']; |
|
178
|
|
|
$instance = $host; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$result[] = $user['userid']['value'] . ' <info>@' . $host . '</info>'; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
// if ($userId === '') { |
|
185
|
|
|
// foreach($result as $item) { |
|
186
|
|
|
// $output->writeln($item); |
|
187
|
|
|
// } |
|
188
|
|
|
// } |
|
189
|
|
|
|
|
190
|
|
|
return $userId; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
|