From 57b333f7b3d6047f3b642c3716c95519d2bdca53 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Tue, 29 Mar 2022 11:00:44 -0100 Subject: [PATCH] +runAsSuperSession Signed-off-by: Maxence Lange --- lib/CirclesManager.php | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/CirclesManager.php b/lib/CirclesManager.php index 3959ec0ff..858d02c91 100644 --- a/lib/CirclesManager.php +++ b/lib/CirclesManager.php @@ -31,7 +31,6 @@ namespace OCA\Circles; -use OCA\Circles\Tools\Exceptions\InvalidItemException; use OCA\Circles\Exceptions\CircleNotFoundException; use OCA\Circles\Exceptions\ContactAddressBookNotFoundException; use OCA\Circles\Exceptions\ContactFormatException; @@ -62,7 +61,9 @@ use OCA\Circles\Service\FederatedUserService; use OCA\Circles\Service\MemberService; use OCA\Circles\Service\MembershipService; +use OCA\Circles\Tools\Exceptions\InvalidItemException; use OCP\IUserSession; +use Throwable; /** * Class CirclesManager @@ -161,6 +162,38 @@ public function startSuperSession(): void { } + /** + * Run a single method as Super, then Super level will be removed + * + * @throws Throwable + */ + public function runAsSuperSession(string $method, array $params = []) { + $currentUser = $this->federatedUserService->getCurrentUser(); + $this->startSuperSession(); + + $throwable = null; + try { + $result = call_user_func_array([$this, $method], $params); + } catch (Throwable $t) { + $throwable = $t; + } + + $this->federatedUserService->bypassCurrentUserCondition(false); + if (!is_null($currentUser)) { + try { + $this->federatedUserService->setCurrentUser($currentUser); + } catch (FederatedUserException $e) { + } + } + + if (!is_null($throwable)) { + throw $throwable; + } + + return $result; + } + + /** * $userId - userId to emulate as initiator (can be empty) * $userType - specify if userIs not a singleId