Completed
Pull Request — master (#551)
by Maxence
03:10 queued 01:14
created

CircleConfig::verify()   B

Complexity

Conditions 8
Paths 24

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.0835
c 0
b 0
f 0
cc 8
nc 24
nop 1
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\FederatedItems;
33
34
35
use OCA\Circles\Db\CircleRequest;
36
use OCA\Circles\Db\MemberRequest;
37
use OCA\Circles\Exceptions\FederatedItemException;
38
use OCA\Circles\IFederatedItem;
39
use OCA\Circles\Model\Circle;
40
use OCA\Circles\Model\Federated\FederatedEvent;
41
use OCA\Circles\Model\Helpers\MemberHelper;
42
use OCA\Circles\Service\ConfigService;
43
44
45
/**
46
 * Class CircleConfig
47
 *
48
 * @package OCA\Circles\FederatedItems
49
 */
50
class CircleConfig implements IFederatedItem {
51
52
53
	/** @var CircleRequest */
54
	private $circleRequest;
55
56
	/** @var MemberRequest */
57
	private $memberRequest;
58
59
	/** @var ConfigService */
60
	private $configService;
61
62
63
	/**
64
	 * CircleConfig constructor.
65
	 *
66
	 * @param CircleRequest $circleRequest
67
	 * @param MemberRequest $memberRequest
68
	 * @param ConfigService $configService
69
	 */
70
	public function __construct(
71
		CircleRequest $circleRequest, MemberRequest $memberRequest, ConfigService $configService
72
	) {
73
		$this->circleRequest = $circleRequest;
74
		$this->memberRequest = $memberRequest;
75
		$this->configService = $configService;
76
	}
77
78
79
	/**
80
	 * @param FederatedEvent $event
81
	 *
82
	 * @throws FederatedItemException
83
	 */
84
	public function verify(FederatedEvent $event): void {
85
		$circle = $event->getCircle();
86
		$config = $event->getData()->gInt('config');
87
88
		$initiatorHelper = new MemberHelper($circle->getInitiator());
89
		$initiatorHelper->mustBeAdmin();
90
91
		$listing = Circle::$DEF_CFG_CORE_FILTER;
92
		if (!$circle->isConfig(Circle::CFG_SYSTEM)) {
93
			$listing = array_merge($listing, Circle::$DEF_CFG_SYSTEM_FILTER);
94
		}
95
96
		$confirmed = true;
97
		foreach ($listing as $item) {
98
			if ($circle->isConfig($item, $config)) {
99
				$confirmed = false;
100
			}
101
		}
102
103
		// if federated, circle is root
104
		if ($circle->isConfig(Circle::CFG_FEDERATED, $config) &&
105
			!$circle->isConfig(Circle::CFG_ROOT, $config)) {
106
			$config += Circle::CFG_ROOT;
107
			// TODO: Check locally that circle is not a member of another circle.
108
			// TODO  in that case, remove the membership (and update the memberships)
109
			$event->getData()->sInt('config', $config);
110
		}
111
112
		if (!$confirmed || $config > Circle::$DEF_CFG_MAX) {
113
			throw new FederatedItemException('Configuration value is not valid');
114
		}
115
116
		$new = clone $circle;
117
		$new->setConfig($config);
118
		$event->setDataOutcome(['circle' => $new]);
119
		$event->setReadingOutcome('Configuration have been updated');
120
	}
121
122
123
	/**
124
	 * @param FederatedEvent $event
125
	 */
126
	public function manage(FederatedEvent $event): void {
127
		$circle = clone $event->getCircle();
128
		$config = $event->getData()->gInt('config');
129
130
		$circle->setConfig($config);
131
		// TODO: Check locally that circle is not un-federated during the process
132
		// TODO: if the circle is managed remotely, remove the circle locally
133
		// TODO: if the circle is managed locally, remove non-local users
134
		$this->circleRequest->updateConfig($circle);
135
	}
136
137
138
	/**
139
	 * @param FederatedEvent[] $events
140
	 */
141
	public function result(array $events): void {
142
	}
143
144
}
145
146