Skip to content

Commit b0ff20b

Browse files
Merge pull request #984 from nextcloud/backport/974/974-stable22
[stable22] limit some feature when Circles is managed by an app
2 parents 0ba262c + f22829f commit b0ff20b

File tree

13 files changed

+368
-71
lines changed

13 files changed

+368
-71
lines changed

lib/CirclesManager.php

Lines changed: 111 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
namespace OCA\Circles;
3333

34-
use OCA\Circles\Tools\Exceptions\InvalidItemException;
3534
use OCA\Circles\Exceptions\CircleNotFoundException;
3635
use OCA\Circles\Exceptions\ContactAddressBookNotFoundException;
3736
use OCA\Circles\Exceptions\ContactFormatException;
@@ -59,10 +58,11 @@
5958
use OCA\Circles\Model\Membership;
6059
use OCA\Circles\Model\Probes\CircleProbe;
6160
use OCA\Circles\Service\CircleService;
61+
use OCA\Circles\Service\ConfigService;
6262
use OCA\Circles\Service\FederatedUserService;
6363
use OCA\Circles\Service\MemberService;
6464
use OCA\Circles\Service\MembershipService;
65-
use OCP\IUserSession;
65+
use OCA\Circles\Tools\Exceptions\InvalidItemException;
6666

6767
/**
6868
* Class CirclesManager
@@ -72,9 +72,6 @@
7272
class CirclesManager {
7373

7474

75-
/** @var CirclesQueryHelper */
76-
private $circlesQueryHelper;
77-
7875
/** @var FederatedUserService */
7976
private $federatedUserService;
8077

@@ -87,27 +84,36 @@ class CirclesManager {
8784
/** @var MembershipService */
8885
private $membershipService;
8986

87+
/** @var ConfigService */
88+
private $configService;
89+
90+
/** @var CirclesQueryHelper */
91+
private $circlesQueryHelper;
92+
9093

9194
/**
9295
* CirclesManager constructor.
9396
*
94-
* @param IUserSession $userSession
9597
* @param FederatedUserService $federatedUserService
9698
* @param CircleService $circleService
9799
* @param MemberService $memberService
100+
* @param MembershipService $membershipService
101+
* @param ConfigService $configService
98102
* @param CirclesQueryHelper $circlesQueryHelper
99103
*/
100104
public function __construct(
101105
FederatedUserService $federatedUserService,
102106
CircleService $circleService,
103107
MemberService $memberService,
104108
MembershipService $membershipService,
109+
ConfigService $configService,
105110
CirclesQueryHelper $circlesQueryHelper
106111
) {
107112
$this->federatedUserService = $federatedUserService;
108113
$this->circleService = $circleService;
109114
$this->memberService = $memberService;
110115
$this->membershipService = $membershipService;
116+
$this->configService = $configService;
111117
$this->circlesQueryHelper = $circlesQueryHelper;
112118
}
113119

@@ -136,6 +142,29 @@ public function getFederatedUser(string $federatedId, int $type = Member::TYPE_S
136142
return $this->federatedUserService->getFederatedUser($federatedId, $type);
137143
}
138144

145+
/**
146+
* @param string $userId
147+
*
148+
* @return FederatedUser
149+
* @throws CircleNotFoundException
150+
* @throws FederatedItemException
151+
* @throws FederatedUserException
152+
* @throws FederatedUserNotFoundException
153+
* @throws InvalidIdException
154+
* @throws MemberNotFoundException
155+
* @throws OwnerNotFoundException
156+
* @throws RemoteInstanceException
157+
* @throws RemoteNotFoundException
158+
* @throws RemoteResourceNotFoundException
159+
* @throws RequestBuilderException
160+
* @throws SingleCircleNotFoundException
161+
* @throws UnknownRemoteException
162+
* @throws UserTypeNotFoundException
163+
*/
164+
public function getLocalFederatedUser(string $userId): FederatedUser {
165+
return $this->getFederatedUser($userId, Member::TYPE_USER);
166+
}
167+
139168

140169
/**
141170
* @throws FederatedUserNotFoundException
@@ -161,6 +190,22 @@ public function startSuperSession(): void {
161190
}
162191

163192

193+
/**
194+
* @param string $appId
195+
* @param int $appSerial
196+
*
197+
* @throws ContactAddressBookNotFoundException
198+
* @throws ContactFormatException
199+
* @throws ContactNotFoundException
200+
* @throws FederatedUserException
201+
* @throws InvalidIdException
202+
* @throws RequestBuilderException
203+
* @throws SingleCircleNotFoundException
204+
*/
205+
public function startAppSession(string $appId, int $appSerial = Member::APP_DEFAULT): void {
206+
$this->federatedUserService->setLocalCurrentApp($appId, $appSerial);
207+
}
208+
164209
/**
165210
* $userId - userId to emulate as initiator (can be empty)
166211
* $userType - specify if userIs not a singleId
@@ -304,6 +349,66 @@ public function getCircle(string $singleId, ?CircleProbe $probe = null): Circle
304349
}
305350

306351

352+
/**
353+
* @param Circle $circle
354+
*
355+
* @throws CircleNotFoundException
356+
* @throws FederatedEventException
357+
* @throws FederatedItemException
358+
* @throws InitiatorNotConfirmedException
359+
* @throws InitiatorNotFoundException
360+
* @throws OwnerNotFoundException
361+
* @throws RemoteInstanceException
362+
* @throws RemoteNotFoundException
363+
* @throws RemoteResourceNotFoundException
364+
* @throws RequestBuilderException
365+
* @throws UnknownRemoteException
366+
*/
367+
public function updateConfig(Circle $circle): void {
368+
$this->circleService->updateConfig($circle->getSingleId(), $circle->getConfig());
369+
}
370+
371+
372+
/**
373+
* @param string $circleId
374+
* @param bool $enabled
375+
*
376+
* @throws CircleNotFoundException
377+
* @throws FederatedEventException
378+
* @throws FederatedItemException
379+
* @throws FederatedUserException
380+
* @throws InitiatorNotConfirmedException
381+
* @throws InitiatorNotFoundException
382+
* @throws OwnerNotFoundException
383+
* @throws RemoteInstanceException
384+
* @throws RemoteNotFoundException
385+
* @throws RemoteResourceNotFoundException
386+
* @throws RequestBuilderException
387+
* @throws UnknownRemoteException
388+
*/
389+
public function flagAsAppManaged(string $circleId, bool $enabled = true): void {
390+
$this->federatedUserService->confirmSuperSession();
391+
$this->federatedUserService->setOwnerAsCurrentUser($circleId);
392+
393+
$probe = new CircleProbe();
394+
$probe->includeSystemCircles();
395+
396+
$localCircle = $this->circleService->getCircle($circleId, $probe);
397+
if (!$this->configService->isLocalInstance($localCircle->getInstance())) {
398+
throw new CircleNotFoundException('This Circle is not managed from this instance');
399+
}
400+
401+
$config = $localCircle->getConfig();
402+
if ($enabled) {
403+
$config |= Circle::CFG_APP;
404+
} else {
405+
$config &= ~Circle::CFG_APP;
406+
}
407+
408+
$this->circleService->updateConfig($circleId, $config);
409+
}
410+
411+
307412
/**
308413
* @param string $circleId
309414
* @param FederatedUser $federatedUser

lib/Command/CirclesConfig.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ protected function configure() {
102102
)
103103
->addOption('initiator', '', InputOption::VALUE_REQUIRED, 'set an initiator to the request', '')
104104
->addOption('initiator-type', '', InputOption::VALUE_REQUIRED, 'set initiator type', '0')
105+
->addOption(
106+
'super-session', '',
107+
InputOption::VALUE_NONE, 'use super session to bypass some condition'
108+
)
105109
->addOption('status-code', '', InputOption::VALUE_NONE, 'display status code on exception');
106110
}
107111

@@ -133,12 +137,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
133137
$circleId = (string)$input->getArgument('circle_id');
134138

135139
try {
136-
$this->federatedUserService->commandLineInitiator(
137-
$input->getOption('initiator'),
138-
Member::parseTypeString($input->getOption('initiator-type')),
139-
$circleId,
140-
false
141-
);
140+
if ($input->getArgument('super-session')) {
141+
$this->federatedUserService->bypassCurrentUserCondition(true);
142+
} else {
143+
$this->federatedUserService->commandLineInitiator(
144+
$input->getOption('initiator'),
145+
Member::parseTypeString($input->getOption('initiator-type')),
146+
$circleId,
147+
false
148+
);
149+
}
142150

143151
$circle = $this->circleService->getCircle($circleId);
144152

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 <maxence@artificial-owl.com>
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+
namespace OCA\Circles\Exceptions;
32+
33+
class RemoteCircleException extends FederatedItemBadRequestException {
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 <maxence@artificial-owl.com>
13+
* @copyright 2022
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+
namespace OCA\Circles\Exceptions;
32+
33+
class SuperSessionException extends FederatedItemUnauthorizedException {
34+
}

lib/FederatedItems/CircleConfig.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
namespace OCA\Circles\FederatedItems;
3333

34-
use OCA\Circles\Tools\Traits\TDeserialize;
3534
use OCA\Circles\Db\CircleRequest;
3635
use OCA\Circles\Exceptions\FederatedItemBadRequestException;
3736
use OCA\Circles\Exceptions\FederatedItemException;
@@ -41,6 +40,7 @@
4140
use OCA\Circles\Model\Federated\FederatedEvent;
4241
use OCA\Circles\Model\Helpers\MemberHelper;
4342
use OCA\Circles\Service\ConfigService;
43+
use OCA\Circles\Tools\Traits\TDeserialize;
4444

4545
/**
4646
* Class CircleConfig
@@ -89,6 +89,15 @@ public function verify(FederatedEvent $event): void {
8989
$listing = array_merge($listing, Circle::$DEF_CFG_SYSTEM_FILTER);
9090
}
9191

92+
// filtering config values when not using Super Session
93+
if (!$event->getParams()->gBool('superSession')) {
94+
if ($circle->isConfig(Circle::CFG_APP)) {
95+
$config |= Circle::CFG_APP;
96+
} else {
97+
$config &= ~Circle::CFG_APP;
98+
}
99+
}
100+
92101
$confirmed = true;
93102
foreach ($listing as $item) {
94103
if ($circle->isConfig($item, $config)) {

lib/FederatedItems/CircleDestroy.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,22 @@
3131

3232
namespace OCA\Circles\FederatedItems;
3333

34-
use OCA\Circles\Tools\Traits\TDeserialize;
3534
use OCA\Circles\Db\CircleRequest;
3635
use OCA\Circles\Db\MemberRequest;
36+
use OCA\Circles\Exceptions\FederatedItemBadRequestException;
3737
use OCA\Circles\Exceptions\RequestBuilderException;
3838
use OCA\Circles\IFederatedItem;
3939
use OCA\Circles\IFederatedItemAsyncProcess;
4040
use OCA\Circles\IFederatedItemHighSeverity;
4141
use OCA\Circles\IFederatedItemMemberEmpty;
42+
use OCA\Circles\Model\Circle;
4243
use OCA\Circles\Model\Federated\FederatedEvent;
4344
use OCA\Circles\Model\Helpers\MemberHelper;
4445
use OCA\Circles\Service\EventService;
4546
use OCA\Circles\Service\MembershipService;
47+
use OCA\Circles\StatusCode;
48+
use OCA\Circles\Tools\Traits\TDeserialize;
49+
use OCA\Circles\Tools\Traits\TStringTools;
4650

4751
/**
4852
* Class CircleDestroy
@@ -54,6 +58,7 @@ class CircleDestroy implements
5458
IFederatedItemHighSeverity,
5559
IFederatedItemAsyncProcess,
5660
IFederatedItemMemberEmpty {
61+
use TStringTools;
5762
use TDeserialize;
5863

5964

@@ -91,9 +96,18 @@ public function __construct(
9196

9297
/**
9398
* @param FederatedEvent $event
99+
*
100+
* @throws FederatedItemBadRequestException
94101
*/
95102
public function verify(FederatedEvent $event): void {
96103
$circle = $event->getCircle();
104+
if ($circle->isConfig(Circle::CFG_APP)) {
105+
throw new FederatedItemBadRequestException(
106+
StatusCode::$CIRCLE_DESTROY[120],
107+
120
108+
);
109+
}
110+
97111
$initiator = $circle->getInitiator();
98112

99113
$initiatorHelper = new MemberHelper($initiator);

0 commit comments

Comments
 (0)