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('type', '', InputOption::VALUE_REQUIRED, 'type of the user', '0'); |
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
|
|
|
$userId = $input->getArgument('user'); |
119
|
|
|
$type = $input->getOption('type'); |
120
|
|
|
if ($type !== '0') { |
121
|
|
|
$type = Member::parseTypeString($type); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$this->federatedUserService->commandLineInitiator($input->getOption('initiator'), $circleId, false); |
125
|
|
|
$federatedUser = $this->federatedUserService->generateFederatedUser($userId, (int)$type); |
126
|
|
|
$outcome = $this->memberService->addMember($circleId, $federatedUser); |
127
|
|
|
|
128
|
|
|
echo json_encode($outcome, JSON_PRETTY_PRINT) . "\n"; |
129
|
|
|
|
130
|
|
|
return 0; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $search |
136
|
|
|
* @param string $instance |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
private function findUserFromLookup(string $search, string &$instance = ''): string { |
|
|
|
|
141
|
|
|
$userId = ''; |
142
|
|
|
|
143
|
|
|
/** @var string $lookup */ |
144
|
|
|
try { |
145
|
|
|
$lookup = $this->configService->getGSStatus(ConfigService::GS_LOOKUP); |
146
|
|
|
} catch (GSStatusException $e) { |
147
|
|
|
return ''; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$request = new NC21Request(ConfigService::GS_LOOKUP_USERS, Request::TYPE_GET); |
151
|
|
|
$this->configService->configureRequest($request); |
152
|
|
|
$request->basedOnUrl($lookup); |
153
|
|
|
$request->addParam('search', $search); |
154
|
|
|
|
155
|
|
|
try { |
156
|
|
|
$users = $this->retrieveJson($request); |
157
|
|
|
} catch ( |
158
|
|
|
RequestContentException | |
159
|
|
|
RequestNetworkException | |
160
|
|
|
RequestResultSizeException | |
161
|
|
|
RequestServerException | |
162
|
|
|
RequestResultNotJsonException $e |
163
|
|
|
) { |
164
|
|
|
return ''; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$result = []; |
168
|
|
|
foreach ($users as $user) { |
169
|
|
|
if (!array_key_exists('userid', $user)) { |
170
|
|
|
continue; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
list(, $host) = explode('@', $user['federationId']); |
174
|
|
|
if (strtolower($user['userid']['value']) === strtolower($search)) { |
175
|
|
|
$userId = $user['userid']['value']; |
176
|
|
|
$instance = $host; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$result[] = $user['userid']['value'] . ' <info>@' . $host . '</info>'; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
// if ($userId === '') { |
183
|
|
|
// foreach($result as $item) { |
184
|
|
|
// $output->writeln($item); |
185
|
|
|
// } |
186
|
|
|
// } |
187
|
|
|
|
188
|
|
|
return $userId; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|