Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runAsAdmin() #1217

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 88 additions & 42 deletions lib/CirclesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

/**
*
*/
Expand Down Expand Up @@ -250,7 +296,7 @@ public function stopSession(): void {
/**
* @return IFederatedUser
*/
public function getCurrentFederatedUser(): IFederatedUser {
protected function getCurrentFederatedUser(): IFederatedUser {
return $this->federatedUserService->getCurrentUser();
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}

Expand All @@ -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()
Expand All @@ -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);
}

Expand All @@ -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());
}

Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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()
Expand Down
18 changes: 3 additions & 15 deletions lib/CirclesQueryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down