From 732c17a28569b6c0586c80748d4c4a71b0137e6b Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Thu, 17 Nov 2022 16:04:03 -0100 Subject: [PATCH] runAsAdmin() Signed-off-by: Maxence Lange --- lib/CirclesManager.php | 130 +++++++++++++++++++++++++------------ lib/CirclesQueryHelper.php | 18 +---- 2 files changed, 91 insertions(+), 57 deletions(-) diff --git a/lib/CirclesManager.php b/lib/CirclesManager.php index 3959d8437..d2df1e8ea 100644 --- a/lib/CirclesManager.php +++ b/lib/CirclesManager.php @@ -71,35 +71,15 @@ * @package OCA\Circles */ class CirclesManager { - /** @var FederatedUserService */ - private $federatedUserService; + private FederatedUserService $federatedUserService; + private CircleService $circleService; + private MemberService $memberService; + private MembershipService $membershipService; + private ConfigService $configService; + private CirclesQueryHelper $circlesQueryHelper; - /** @var CircleService */ - private $circleService; + private bool $runAsAdmin = false; - /** @var MemberService */ - private $memberService; - - /** @var MembershipService */ - private $membershipService; - - /** @var ConfigService */ - private $configService; - - /** @var CirclesQueryHelper */ - private $circlesQueryHelper; - - - /** - * CirclesManager constructor. - * - * @param FederatedUserService $federatedUserService - * @param CircleService $circleService - * @param MemberService $memberService - * @param MembershipService $membershipService - * @param ConfigService $configService - * @param CirclesQueryHelper $circlesQueryHelper - */ public function __construct( FederatedUserService $federatedUserService, CircleService $circleService, @@ -117,6 +97,54 @@ public function __construct( } + /** + * Wrap most of the method of this class to apply runAsAdmin + * + * @param $method + * @param $args + * + * @return false|mixed + * @throws FederatedUserException + */ + public function __call($method, $args) { + $bypassed = false; + $federatedUser = null; + + if ($this->runAsAdmin) { + $federatedUser = $this->federatedUserService->getCurrentEntity(); + $bypassed = $this->federatedUserService->canBypassCurrentUserCondition(); + $this->federatedUserService->unsetCurrentUser(); + $this->federatedUserService->bypassCurrentUserCondition(true); + } + + $result = call_user_func_array(array($this, $method), $args); + + if ($federatedUser !== null) { + $this->federatedUserService->bypassCurrentUserCondition($bypassed); + $this->federatedUserService->setCurrentUser($federatedUser); + } + + return $result; + } + + + + public function setRunAsAdmin(bool $asAdmin = false): void { + $this->runAsAdmin = $asAdmin; + } + + /** + * returns a clone of CirclesManager with an opened super session: + * $circlesManager->runAsAdmin()->getCircle($circleId); + */ + public function runAsAdmin(): self { + $manager = clone $this; + $manager->setRunAsAdmin(true); + + return $manager; + } + + /** * @param string $federatedId * @param int $type @@ -180,6 +208,24 @@ public function startSession(?FederatedUser $federatedUser = null): void { } } + + /** + * @param string $userId + * + * @throws ContactAddressBookNotFoundException + * @throws ContactFormatException + * @throws ContactNotFoundException + * @throws FederatedUserException + * @throws FederatedUserNotFoundException + * @throws InvalidIdException + * @throws RequestBuilderException + * @throws SingleCircleNotFoundException + */ + public function startUserSession(string $userId): void { + $federatedUser = $this->federatedUserService->getLocalFederatedUser($userId, true); + $this->startSession($federatedUser); + } + /** * */ @@ -250,7 +296,7 @@ public function stopSession(): void { /** * @return IFederatedUser */ - public function getCurrentFederatedUser(): IFederatedUser { + protected function getCurrentFederatedUser(): IFederatedUser { return $this->federatedUserService->getCurrentUser(); } @@ -282,7 +328,7 @@ public function getQueryHelper(): CirclesQueryHelper { * @throws RequestBuilderException * @throws UnknownRemoteException */ - public function createCircle( + protected function createCircle( string $name, ?FederatedUser $owner = null, bool $personal = false, @@ -311,7 +357,7 @@ public function createCircle( * @throws RequestBuilderException * @throws UnknownRemoteException */ - public function destroyCircle(string $singleId): void { + protected function destroyCircle(string $singleId): void { $this->circleService->destroy($singleId); } @@ -324,13 +370,13 @@ public function destroyCircle(string $singleId): void { * * returns available Circles to the current session. * - * @see probeCircles() - * * @return Circle[] * @throws InitiatorNotFoundException * @throws RequestBuilderException + * @see probeCircles() + * */ - public function getCircles(?CircleProbe $probe = null, bool $refreshCache = false): array { + protected function getCircles(?CircleProbe $probe = null, bool $refreshCache = false): array { if (is_null($probe)) { $probe = new CircleProbe(); $probe->filterHiddenCircles() @@ -350,7 +396,7 @@ public function getCircles(?CircleProbe $probe = null, bool $refreshCache = fals * @throws InitiatorNotFoundException * @throws RequestBuilderException */ - public function getCircle(string $singleId, ?CircleProbe $probe = null): Circle { + protected function getCircle(string $singleId, ?CircleProbe $probe = null): Circle { return $this->circleService->getCircle($singleId, $probe); } @@ -370,7 +416,7 @@ public function getCircle(string $singleId, ?CircleProbe $probe = null): Circle * @throws RequestBuilderException * @throws UnknownRemoteException */ - public function updateConfig(Circle $circle): void { + protected function updateConfig(Circle $circle): void { $this->circleService->updateConfig($circle->getSingleId(), $circle->getConfig()); } @@ -392,7 +438,7 @@ public function updateConfig(Circle $circle): void { * @throws RequestBuilderException * @throws UnknownRemoteException */ - public function flagAsAppManaged(string $circleId, bool $enabled = true): void { + protected function flagAsAppManaged(string $circleId, bool $enabled = true): void { $this->federatedUserService->confirmSuperSession(); $this->federatedUserService->setOwnerAsCurrentUser($circleId); @@ -439,7 +485,7 @@ public function flagAsAppManaged(string $circleId, bool $enabled = true): void { * @throws SingleCircleNotFoundException * @throws UnknownRemoteException */ - public function addMember(string $circleId, FederatedUser $federatedUser): Member { + protected function addMember(string $circleId, FederatedUser $federatedUser): Member { $outcome = $this->memberService->addMember($circleId, $federatedUser); $member = new Member(); $member->import($outcome); @@ -465,7 +511,7 @@ public function addMember(string $circleId, FederatedUser $federatedUser): Membe * @throws RequestBuilderException * @throws UnknownRemoteException */ - public function levelMember(string $memberId, int $level): Member { + protected function levelMember(string $memberId, int $level): Member { $outcome = $this->memberService->memberLevel($memberId, $level); $member = new Member(); $member->import($outcome); @@ -488,7 +534,7 @@ public function levelMember(string $memberId, int $level): Member { * @throws RequestBuilderException * @throws UnknownRemoteException */ - public function removeMember(string $memberId): void { + protected function removeMember(string $memberId): void { $this->memberService->removeMember($memberId); } @@ -502,7 +548,7 @@ public function removeMember(string $memberId): void { * @throws MembershipNotFoundException * @throws RequestBuilderException */ - public function getLink(string $circleId, string $singleId, bool $detailed = false): Membership { + protected function getLink(string $circleId, string $singleId, bool $detailed = false): Membership { return $this->membershipService->getMembership($circleId, $singleId, $detailed); } @@ -512,7 +558,7 @@ public function getLink(string $circleId, string $singleId, bool $detailed = fal * * @return string */ - public function getDefinition(IEntity $circle): string { + protected function getDefinition(IEntity $circle): string { return $this->circleService->getDefinition($circle); } @@ -531,7 +577,7 @@ public function getDefinition(IEntity $circle): string { * @throws InitiatorNotFoundException * @throws RequestBuilderException */ - public function probeCircles(?CircleProbe $circleProbe = null, ?DataProbe $dataProbe = null): array { + protected function probeCircles(?CircleProbe $circleProbe = null, ?DataProbe $dataProbe = null): array { if (is_null($circleProbe)) { $circleProbe = new CircleProbe(); $circleProbe->filterHiddenCircles() diff --git a/lib/CirclesQueryHelper.php b/lib/CirclesQueryHelper.php index e747402d5..06585c06d 100644 --- a/lib/CirclesQueryHelper.php +++ b/lib/CirclesQueryHelper.php @@ -48,22 +48,10 @@ * @package OCA\Circles */ class CirclesQueryHelper { - /** @var CoreRequestBuilder */ - private $coreRequestBuilder; + private CoreRequestBuilder $coreRequestBuilder; + private CoreQueryBuilder $queryBuilder; + private FederatedUserService $federatedUserService; - /** @var CoreQueryBuilder */ - private $queryBuilder; - - /** @var FederatedUserService */ - private $federatedUserService; - - - /** - * CirclesQueryHelper constructor. - * - * @param CoreRequestBuilder $coreRequestBuilder - * @param FederatedUserService $federatedUserService - */ public function __construct( CoreRequestBuilder $coreRequestBuilder, FederatedUserService $federatedUserService