Completed
Pull Request — master (#586)
by Maxence
02:32
created

CirclesReport::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 4
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 2021
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\Console\Nextcloud\nc22\InteractiveShell;
35
use daita\MySmallPhpTools\Exceptions\InvalidItemException;
36
use daita\MySmallPhpTools\IInteractiveShellClient;
37
use daita\MySmallPhpTools\Model\SimpleDataStore;
38
use daita\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Deserialize;
39
use daita\MySmallPhpTools\Traits\TArrayTools;
40
use OC\Core\Command\Base;
41
use OCA\Circles\Exceptions\InitiatorNotFoundException;
42
use OCA\Circles\Model\Circle;
43
use OCA\Circles\Model\Federated\RemoteInstance;
44
use OCA\Circles\Model\FederatedUser;
45
use OCA\Circles\Model\Member;
46
use OCA\Circles\Model\Membership;
47
use OCA\Circles\Model\Report;
48
use OCA\Circles\Service\CircleService;
49
use OCA\Circles\Service\ConfigService;
50
use OCA\Circles\Service\FederatedUserService;
51
use OCA\Circles\Service\MemberService;
52
use Symfony\Component\Console\Input\InputInterface;
53
use Symfony\Component\Console\Input\InputOption;
54
use Symfony\Component\Console\Output\ConsoleOutput;
55
use Symfony\Component\Console\Output\OutputInterface;
56
57
58
/**
59
 * Class CirclesReport
60
 *
61
 * @package OCA\Circles\Command
62
 */
63
class CirclesReport extends Base implements IInteractiveShellClient {
64
65
66
	use TNC22Deserialize;
67
	use TArrayTools;
68
69
70
	/** @var FederatedUserService */
71
	private $federatedUserService;
72
73
	/** @var CircleService */
74
	private $circleService;
75
76
	/** @var MemberService */
77
	private $memberService;
78
79
	/** @var ConfigService */
80
	private $configService;
81
82
	/** @var InteractiveShell */
83
	private $interactiveShell;
84
85
86
	/** @var OutputInterface */
87
	private $output;
88
89
90
	/**
91
	 * CirclesReport constructor.
92
	 *
93
	 * @param FederatedUserService $federatedUserService
94
	 * @param CircleService $circleService
95
	 * @param MemberService $memberService
96
	 * @param ConfigService $configService
97
	 */
98 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
		FederatedUserService $federatedUserService,
100
		CircleService $circleService,
101
		MemberService $memberService,
102
		ConfigService $configService
103
	) {
104
		parent::__construct();
105
106
		$this->federatedUserService = $federatedUserService;
107
		$this->circleService = $circleService;
108
		$this->memberService = $memberService;
109
		$this->configService = $configService;
110
	}
111
112
113
	/**
114
	 *
115
	 */
116
	protected function configure() {
117
		parent::configure();
118
		$this->setName('circles:report')
119
			 ->setDescription('Read and write obfuscated report')
120
			 ->addOption('read', '', InputOption::VALUE_REQUIRED, 'File containing the report to read', '');
121
	}
122
123
124
	/**
125
	 * @param InputInterface $input
126
	 * @param OutputInterface $output
127
	 *
128
	 * @return int
129
	 * @throws InvalidItemException
130
	 * @throws InitiatorNotFoundException
131
	 */
132
	protected function execute(InputInterface $input, OutputInterface $output): int {
133
		$filename = $input->getOption('read');
134
		$this->output = $output;
135
136
		if ($filename === '') {
137
			$this->generateReport();
138
		} else {
139
			/** @var Report $report */
140
			$data = file_get_contents($filename);
141
			$report = $this->deserialize(json_decode($data, true), Report::class);
142
			$this->readReport($input, $report);
143
		}
144
145
		return 0;
146
	}
147
148
149
	/**
150
	 * @throws InitiatorNotFoundException
151
	 */
152
	private function generateReport(): void {
153
		$report = new Report();
154
		$report->setSource($this->configService->getFrontalInstance());
155
		$this->federatedUserService->bypassCurrentUserCondition(true);
156
157
		$raw = $this->circleService->getCircles(
158
			null,
159
			null,
160
			new SimpleDataStore(['includeSystemCircles' => true])
161
		);
162
163
		$circles = [];
164
		foreach ($raw as $circle) {
165
			$circle->getMembers();
166
167
			$circles[] = $this->obfuscateCircle($circle);
168
		}
169
170
		$report->setCircles($circles);
171
		$this->output->writeln(json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
172
	}
173
174
175
	/**
176
	 * @param InputInterface $input
177
	 * @param Report $report
178
	 */
179
	private function readReport(InputInterface $input, Report $report) {
180
		$output = new ConsoleOutput();
181
		$this->output = $output->section();
182
183
		$this->interactiveShell = new InteractiveShell($this, $input, $output, $this);
184
		$commands = [
185
			'oui.la.?type_IEntities',
186
			'oui.ok.?type_IEntitiesAccounts',
187
			'remoteInstance'
188
		];
189
190
		$this->interactiveShell->setCommands($commands);
191
		$this->interactiveShell->run(
192
			'Circles Report [<info>' . $report->getSource() . '</info>]:<comment>%PATH%</comment>$'
193
		);
194
	}
195
196
197
	/**
198
	 * @param string $source
199
	 * @param string $field
200
	 *
201
	 * @return string[]
202
	 */
203
	public function fillCommandList(string $source, string $field): array {
204
		echo $source . ' ' . $field . "\n";
205
206
		return ['abcd', 'abdde', 'erfg'];
207
	}
208
209
210
	/**
211
	 * @param string $command
212
	 */
213
	public function manageCommand(string $command): void {
214
//		echo $command . "\n";
215
	}
216
217
218
	/**
219
	 * @param Circle $circle
220
	 *
221
	 * @return Circle
222
	 */
223
	private function obfuscateCircle(Circle $circle): Circle {
224
		$singleId = $this->obfuscateId($circle->getSingleId());
225
		$circle->setSingleId($singleId)
226
			   ->setName($singleId)
227
			   ->setDisplayName($singleId)
228
			   ->setDescription('')
229
			   ->setCreation(0);
230
231
		if ($circle->hasOwner()) {
232
			$circle->setOwner($this->obfuscateMember($circle->getOwner()));
233
		}
234
235
		if ($circle->hasInitiator()) {
236
			$circle->setInitiator($this->obfuscateMember($circle->getInitiator()));
237
		}
238
239
		if ($circle->hasMembers()) {
240
			$members = [];
241
			foreach ($circle->getMembers() as $member) {
242
				$members[] = $this->obfuscateMember($member);
243
			}
244
			$circle->setMembers($members);
245
		}
246
247 View Code Duplication
		if ($circle->hasMemberships()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
248
			$memberships = [];
249
			foreach ($circle->getMemberships() as $membership) {
250
				$memberships[] = $this->obfuscateMembership($membership);
251
			}
252
			$circle->setMemberships($memberships);
253
		}
254
255
256
		return $circle;
257
	}
258
259
260
	/**
261
	 * @param Member $member
262
	 *
263
	 * @return Member
264
	 */
265
	private function obfuscateMember(Member $member): Member {
266
		$memberId = $this->obfuscateId($member->getId());
267
		$singleId = $this->obfuscateId($member->getSingleId());
268
		$circleId = $this->obfuscateId($member->getCircleId());
269
270
		$member->setSingleId($singleId)
271
			   ->setCircleId($circleId)
272
			   ->setId($memberId)
273
			   ->setUserId($singleId)
274
			   ->setDisplayName($singleId)
275
			   ->setDisplayUpdate(0)
276
			   ->setNote('')
277
			   ->setContactId('')
278
			   ->setContactMeta('')
279
			   ->setJoined(0);
280
281
		if ($member->hasCircle()) {
282
			$member->setCircle($this->obfuscateCircle($member->getCircle()));
283
		}
284
285
		if ($member->hasBasedOn()) {
286
			$member->setBasedOn($this->obfuscateCircle($member->getBasedOn()));
287
		}
288
289
		if ($member->hasInheritedBy()) {
290
			$member->setInheritedBy($this->obfuscateFederatedUser($member->getInheritedBy()));
291
		}
292
293
		if ($member->hasInheritanceFrom()) {
294
			$member->setInheritanceFrom($this->obfuscateMember($member->getInheritanceFrom()));
295
		}
296
297
		if ($member->hasRemoteInstance()) {
298
			$member->setRemoteInstance($this->obfuscateRemoteInstance($member->getRemoteInstance()));
299
		}
300
301 View Code Duplication
		if ($member->hasMemberships()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
302
			$memberships = [];
303
			foreach ($member->getMemberships() as $membership) {
304
				$memberships[] = $this->obfuscateMembership($membership);
305
			}
306
			$member->setMemberships($memberships);
307
		}
308
309
		return $member;
310
	}
311
312
313
	/**
314
	 * @param FederatedUser $federatedUser
315
	 *
316
	 * @return FederatedUser
317
	 */
318
	private function obfuscateFederatedUser(FederatedUser $federatedUser): FederatedUser {
319
		$singleId = $this->obfuscateId($federatedUser->getSingleId());
320
		$federatedUser->setSingleId($singleId)
321
					  ->setUserId($singleId);
322
323
		if ($federatedUser->hasBasedOn()) {
324
			$federatedUser->setBasedOn($this->obfuscateCircle($federatedUser->getBasedOn()));
325
		}
326
327
		if ($federatedUser->hasLink()) {
328
			$federatedUser->setLink($this->obfuscateMembership($federatedUser->getLink()));
329
		}
330
331 View Code Duplication
		if ($federatedUser->hasMemberships()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
332
			$memberships = [];
333
			foreach ($federatedUser->getMemberships() as $membership) {
334
				$memberships[] = $this->obfuscateMembership($membership);
335
			}
336
			$federatedUser->setMemberships($memberships);
337
		}
338
339
		return $federatedUser;
340
	}
341
342
343
	/**
344
	 * @param Membership $membership
345
	 *
346
	 * @return Membership
347
	 */
348
	private function obfuscateMembership(Membership $membership): Membership {
349
		$membership->setSingleId($this->obfuscateId($membership->getSingleId()));
350
		$membership->setCircleId($this->obfuscateId($membership->getCircleId()));
351
		$membership->setInheritanceFirst($this->obfuscateId($membership->getInheritanceFirst()));
352
		$membership->setInheritanceLast($this->obfuscateId($membership->getInheritanceLast()));
353
354
		$path = [];
355
		foreach ($membership->getInheritancePath() as $item) {
356
			$path[] = $this->obfuscateId($item);
357
		}
358
		$membership->setInheritancePath($path);
359
360
		return $membership;
361
	}
362
363
364
	/**
365
	 * @param RemoteInstance $remoteInstance
366
	 *
367
	 * @return RemoteInstance
368
	 */
369
	private function obfuscateRemoteInstance(RemoteInstance $remoteInstance): RemoteInstance {
370
		return $remoteInstance;
371
	}
372
373
374
	/**
375
	 * @param string $id
376
	 *
377
	 * @return string
378
	 */
379
	private function obfuscateId(string $id): string {
380
		return substr($id, 0, 5) . '.' . md5(substr($id, 5));
381
	}
382
383
}
384
385