migrate therinaldos.com data
Build & Deploy to DigitalOcean Space / build (push) Failing after 2m38s

This commit is contained in:
2024-05-05 15:50:45 -04:00
commit ef1ff240d4
23182 changed files with 3801898 additions and 0 deletions
@@ -0,0 +1,548 @@
<?php
declare(strict_types=1);
/**
* Circles - Bring cloud-users closer together.
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2021
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Circles\Controller;
use OCA\Circles\Tools\Traits\TDeserialize;
use OCA\Circles\Tools\Traits\TNCLogger;
use Exception;
use OCA\Circles\Exceptions\ContactAddressBookNotFoundException;
use OCA\Circles\Exceptions\ContactFormatException;
use OCA\Circles\Exceptions\ContactNotFoundException;
use OCA\Circles\Exceptions\FederatedUserException;
use OCA\Circles\Exceptions\FederatedUserNotFoundException;
use OCA\Circles\Exceptions\InvalidIdException;
use OCA\Circles\Exceptions\RequestBuilderException;
use OCA\Circles\Exceptions\SingleCircleNotFoundException;
use OCA\Circles\Model\FederatedUser;
use OCA\Circles\Model\Member;
use OCA\Circles\Model\Probes\BasicProbe;
use OCA\Circles\Model\Probes\CircleProbe;
use OCA\Circles\Service\CircleService;
use OCA\Circles\Service\ConfigService;
use OCA\Circles\Service\FederatedUserService;
use OCA\Circles\Service\MemberService;
use OCA\Circles\Service\MembershipService;
use OCA\Circles\Service\SearchService;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\OCSController;
use OCP\IRequest;
use OCP\IUserSession;
/**
* Class AdminController
*
* @package OCA\Circles\Controller
*/
class AdminController extends OCSController {
use TDeserialize;
use TNCLogger;
/** @var IUserSession */
private $userSession;
/** @var FederatedUserService */
private $federatedUserService;
/** @var CircleService */
private $circleService;
/** @var MemberService */
private $memberService;
/** @var MembershipService */
private $membershipService;
/** @var SearchService */
private $searchService;
/** @var ConfigService */
protected $configService;
/**
* LocalController constructor.
*
* @param string $appName
* @param IRequest $request
* @param IUserSession $userSession
* @param FederatedUserService $federatedUserService
* @param CircleService $circleService
* @param MemberService $memberService
* @param MembershipService $membershipService
* @param SearchService $searchService
* @param ConfigService $configService
*/
public function __construct(
string $appName,
IRequest $request,
IUserSession $userSession,
FederatedUserService $federatedUserService,
CircleService $circleService,
MemberService $memberService,
MembershipService $membershipService,
SearchService $searchService,
ConfigService $configService
) {
parent::__construct($appName, $request);
$this->userSession = $userSession;
$this->federatedUserService = $federatedUserService;
$this->circleService = $circleService;
$this->memberService = $memberService;
$this->membershipService = $membershipService;
$this->searchService = $searchService;
$this->configService = $configService;
$this->setup('app', 'circles');
}
/**
* @param string $emulated
* @param string $name
* @param bool $personal
* @param bool $local
*
* @return DataResponse
* @throws OCSException
*/
public function create(
string $emulated,
string $name,
bool $personal = false,
bool $local = false
): DataResponse {
try {
$this->setLocalFederatedUser($emulated);
$circle = $this->circleService->create($name, null, $personal, $local);
return new DataResponse($this->serializeArray($circle));
} catch (Exception $e) {
$this->e(
$e, [
'emulated' => $emulated,
'name' => $name,
'members' => $personal,
'local' => $local
]
);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @param string $emulated
* @param string $circleId
*
* @return DataResponse
* @throws OCSException
*/
public function destroy(string $emulated, string $circleId): DataResponse {
try {
$this->setLocalFederatedUser($emulated);
$circle = $this->circleService->destroy($circleId);
return new DataResponse($this->serializeArray($circle));
} catch (Exception $e) {
$this->e($e, ['emulated' => $emulated, 'circleId' => $circleId]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @param string $emulated
* @param string $circleId
* @param string $userId
* @param int $type
*
* @return DataResponse
* @throws OCSException
*/
public function memberAdd(string $emulated, string $circleId, string $userId, int $type): DataResponse {
try {
$this->setLocalFederatedUser($emulated);
// exception in Contact
if ($type === Member::TYPE_CONTACT) {
$currentUser = $this->federatedUserService->getCurrentUser();
if (!$this->configService->isLocalInstance($currentUser->getInstance())) {
throw new OCSException('works only from local instance', 404);
}
$userId = $currentUser->getUserId() . '/' . $userId;
}
$federatedUser = $this->federatedUserService->generateFederatedUser($userId, $type);
$result = $this->memberService->addMember($circleId, $federatedUser);
return new DataResponse($this->serializeArray($result));
} catch (Exception $e) {
$this->e(
$e, [
'emulated' => $emulated,
'circleId' => $circleId,
'userId' => $userId,
'type' => $type
]
);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @param string $emulated
* @param string $circleId
* @param string $memberId
* @param string|int $level
*
* @return DataResponse
* @throws OCSException
*/
public function memberLevel(string $emulated, string $circleId, string $memberId, $level): DataResponse {
try {
$this->setLocalFederatedUser($emulated);
if (is_int($level)) {
$level = Member::parseLevelInt($level);
} else {
$level = Member::parseLevelString($level);
}
$this->memberService->getMemberById($memberId, $circleId);
$result = $this->memberService->memberLevel($memberId, $level);
return new DataResponse($this->serializeArray($result));
} catch (Exception $e) {
$this->e(
$e,
[
'emulated' => $emulated,
'circleId' => $circleId,
'memberId' => $memberId,
'level' => $level
]
);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @param string $emulated
* @param int $limit
* @param int $offset
* @return DataResponse
* @throws OCSException
*/
public function circles(string $emulated, int $limit = -1, int $offset = 0): DataResponse {
try {
$this->setLocalFederatedUser($emulated);
$probe = new CircleProbe();
$probe->filterHiddenCircles()
->filterBackendCircles()
->addDetail(BasicProbe::DETAILS_POPULATION)
->setItemsLimit($limit)
->setItemsOffset($offset);
return new DataResponse($this->serializeArray($this->circleService->getCircles($probe)));
} catch (Exception $e) {
$this->e($e, ['emulated' => $emulated]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @param string $emulated
* @param string $circleId
*
* @return DataResponse
* @throws OCSException
*/
public function circleDetails(string $emulated, string $circleId): DataResponse {
try {
$this->setLocalFederatedUser($emulated);
$probe = new CircleProbe();
$probe->includeNonVisibleCircles();
return new DataResponse($this->serialize($this->circleService->getCircle($circleId, $probe)));
} catch (Exception $e) {
$this->e($e, ['emulated' => $emulated, 'circleId' => $circleId]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @param string $emulated
* @param string $circleId
*
* @return DataResponse
* @throws OCSException
*/
public function circleJoin(string $emulated, string $circleId): DataResponse {
try {
$this->setLocalFederatedUser($emulated);
$result = $this->circleService->circleJoin($circleId);
return new DataResponse($this->serializeArray($result));
} catch (Exception $e) {
$this->e($e, ['emulated' => $emulated, 'circleId' => $circleId]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @param string $emulated
* @param string $circleId
*
* @return DataResponse
* @throws OCSException
*/
public function circleLeave(string $emulated, string $circleId): DataResponse {
try {
$this->setLocalFederatedUser($emulated);
$result = $this->circleService->circleLeave($circleId);
return new DataResponse($this->serializeArray($result));
} catch (Exception $e) {
$this->e($e, ['emulated' => $emulated, 'circleId' => $circleId]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @param string $emulated
* @param string $circleId
* @param string $memberId
*
* @return DataResponse
* @throws OCSException
*/
public function memberConfirm(string $emulated, string $circleId, string $memberId): DataResponse {
try {
$this->setLocalFederatedUser($emulated);
$member = $this->memberService->getMemberById($memberId, $circleId);
$federatedUser = new FederatedUser();
$federatedUser->importFromIFederatedUser($member);
$result = $this->memberService->addMember($circleId, $federatedUser);
return new DataResponse($this->serializeArray($result));
} catch (Exception $e) {
$this->e($e, ['emulated' => $emulated, 'circleId' => $circleId, 'memberId' => $memberId]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @param string $emulated
* @param string $circleId
* @param string $memberId
*
* @return DataResponse
* @throws OCSException
*/
public function memberRemove(string $emulated, string $circleId, string $memberId): DataResponse {
try {
$this->setLocalFederatedUser($emulated);
$this->memberService->getMemberById($memberId, $circleId);
$result = $this->memberService->removeMember($memberId);
return new DataResponse($this->serializeArray($result));
} catch (Exception $e) {
$this->e($e, ['emulated' => $emulated, 'circleId' => $circleId, 'memberId' => $memberId]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @param string $emulated
* @param string $circleId
*
* @return DataResponse
* @throws OCSException
*/
public function members(string $emulated, string $circleId): DataResponse {
try {
$this->setLocalFederatedUser($emulated);
return new DataResponse($this->serializeArray($this->memberService->getMembers($circleId)));
} catch (Exception $e) {
$this->e($e, ['emulated' => $emulated, 'circleId' => $circleId]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @param string $emulated
* @param string $circleId
* @param string $value
*
* @return DataResponse
* @throws OCSException
*/
public function editName(string $emulated, string $circleId, string $value): DataResponse {
try {
$this->setLocalFederatedUser($emulated);
$outcome = $this->circleService->updateName($circleId, $value);
return new DataResponse($this->serializeArray($outcome));
} catch (Exception $e) {
$this->e($e, ['emulated' => $emulated, 'circleId' => $circleId, 'value' => $value]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @param string $emulated
* @param string $circleId
* @param string $value
*
* @return DataResponse
* @throws OCSException
*/
public function editDescription(string $emulated, string $circleId, string $value): DataResponse {
try {
$this->setLocalFederatedUser($emulated);
$outcome = $this->circleService->updateDescription($circleId, $value);
return new DataResponse($this->serializeArray($outcome));
} catch (Exception $e) {
$this->e($e, ['emulated' => $emulated, 'circleId' => $circleId, 'value' => $value]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @param string $emulated
* @param string $circleId
* @param string $setting
* @param string|null $value
*
* @return DataResponse
* @throws OCSException
*/
public function editSetting(string $emulated, string $circleId, string $setting, ?string $value = null): DataResponse {
try {
$this->setLocalFederatedUser($emulated);
$outcome = $this->circleService->updateSetting($circleId, $setting, $value);
return new DataResponse($this->serializeArray($outcome));
} catch (Exception $e) {
$this->e($e, ['circleId' => $circleId, 'setting' => $setting, 'value' => $value]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @param string $emulated
* @param string $circleId
* @param int $value
*
* @return DataResponse
* @throws OCSException
*/
public function editConfig(string $emulated, string $circleId, int $value): DataResponse {
try {
$this->setLocalFederatedUser($emulated);
$outcome = $this->circleService->updateConfig($circleId, $value);
return new DataResponse($this->serializeArray($outcome));
} catch (Exception $e) {
$this->e($e, ['emulated' => $emulated, 'circleId' => $circleId, 'value' => $value]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @param string $emulated
* @param string $circleId
* @param string $singleId
*
* @return DataResponse
* @throws OCSException
*/
public function link(string $emulated, string $circleId, string $singleId): DataResponse {
try {
$this->setLocalFederatedUser($emulated);
$membership = $this->membershipService->getMembership($circleId, $singleId, true);
return new DataResponse($this->serialize($membership));
} catch (Exception $e) {
$this->e($e, ['emulated' => $emulated, 'circleId' => $circleId, 'singleId' => $singleId]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @param string $emulated
*
* @throws FederatedUserException
* @throws FederatedUserNotFoundException
* @throws InvalidIdException
* @throws RequestBuilderException
* @throws SingleCircleNotFoundException
* @throws ContactAddressBookNotFoundException
* @throws ContactFormatException
* @throws ContactNotFoundException
*/
private function setLocalFederatedUser(string $emulated): void {
$user = $this->userSession->getUser();
$this->federatedUserService->setCurrentPatron($user->getUID());
$this->federatedUserService->setLocalCurrentUserId($emulated);
}
}
@@ -0,0 +1,87 @@
<?php
/**
* Circles - Bring cloud-users closer together.
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@pontapreta.net>
* @copyright 2017
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Circles\Controller;
use Exception;
use OCA\Circles\Model\Circle;
use OCA\Circles\Model\Probes\CircleProbe;
use OCA\Circles\Service\CircleService;
use OCA\Circles\Service\FederatedUserService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\IRequest;
/**
* @deprecated
* re-implemented only to re-enable an old feature until we switch to a better integration.
*/
class DeprecatedController extends Controller {
/** @var FederatedUserService */
private $federatedUserService;
/** @var CircleService */
private $circleService;
public function __construct(
string $appName,
IRequest $request,
FederatedUserService $federatedUserService,
CircleService $circleService
) {
parent::__construct($appName, $request);
$this->federatedUserService = $federatedUserService;
$this->circleService = $circleService;
}
/**
* @NoAdminRequired
* @NoSubAdminRequired
*
* @param string $term
*
* @return DataResponse
*/
public function listing(string $term = ''): DataResponse {
try {
$this->federatedUserService->initCurrentUser();
$probe = new CircleProbe();
$filterCircle = new Circle();
$filterCircle->setName($term)
->setDisplayName($term);
$probe->setFilterCircle($filterCircle);
$probe->filterSystemCircles();
$data = $this->circleService->getCircles($probe);
return new DataResponse(['data' => $data]);
} catch (Exception $e) {
return new DataResponse([]);
}
}
}
@@ -0,0 +1,164 @@
<?php
declare(strict_types=1);
/**
* Circles - Bring cloud-users closer together.
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2017
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Circles\Controller;
use OCA\Circles\AppInfo\Application;
use OCA\Circles\Service\ConfigService;
use OCA\Circles\Service\EventWrapperService;
use OCA\Circles\Service\FederatedEventService;
use OCA\Circles\Service\RemoteDownstreamService;
use OCA\Circles\Service\RemoteUpstreamService;
use OCA\Circles\Tools\Traits\TAsync;
use OCA\Circles\Tools\Traits\TStringTools;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\IRequest;
/**
* Class EventWrapperController
*
* @package OCA\Circles\Controller
*/
class EventWrapperController extends Controller {
use TStringTools;
use TAsync;
/** @var EventWrapperService */
private $eventWrapperService;
/** @var FederatedEventService */
private $federatedEventService;
/** @var RemoteUpstreamService */
private $remoteUpstreamService;
/** @var RemoteDownstreamService */
private $remoteDownstreamService;
/** @var ConfigService */
private $configService;
/**
* EventWrapperController constructor.
*
* @param string $appName
* @param IRequest $request
* @param EventWrapperService $eventWrapperService
* @param FederatedEventService $federatedEventService
* @param RemoteUpstreamService $remoteUpstreamService
* @param RemoteDownstreamService $remoteDownstreamService
* @param ConfigService $configService
*/
public function __construct(
string $appName,
IRequest $request,
EventWrapperService $eventWrapperService,
FederatedEventService $federatedEventService,
RemoteUpstreamService $remoteUpstreamService,
RemoteDownstreamService $remoteDownstreamService,
ConfigService $configService
) {
parent::__construct($appName, $request);
$this->eventWrapperService = $eventWrapperService;
$this->federatedEventService = $federatedEventService;
$this->remoteUpstreamService = $remoteUpstreamService;
$this->remoteDownstreamService = $remoteDownstreamService;
$this->configService = $configService;
$this->setup('app', Application::APP_ID);
$this->setupInt(self::$SETUP_TIME_LIMIT, 900);
}
/**
* Called locally.
*
* Async process and broadcast the event to every instances of GS
* This should be initiated by the instance that owns the Circles.
*
* @PublicPage
* @NoCSRFRequired
*
* @param string $token
*
* @return DataResponse
*/
public function asyncBroadcast(string $token): DataResponse {
$wrappers = $this->remoteUpstreamService->getEventsByToken($token);
if (empty($wrappers) && $token !== 'test-dummy-token') {
return new DataResponse([], Http::STATUS_OK);
}
// closing socket, keep current process running.
$this->async();
foreach ($wrappers as $wrapper) {
$this->eventWrapperService->manageWrapper($wrapper);
}
$this->eventWrapperService->confirmStatus($token);
// so circles:check can check async is fine
if ($token === 'test-dummy-token') {
sleep(4);
}
// exit() or useless log will be generated
exit();
}
// /**
// * Status Event. This is an event to check status of items between instances.
// *
// * @PublicPage
// * @NoCSRFRequired
// *
// * @return DataResponse
// */
// public function status(): DataResponse {
// $data = file_get_contents('php://input');
//
// try {
// $event = new GSEvent();
// $event->importFromJson($data);
// $this->gsDownstreamService->statusEvent($event);
//
// return $this->success(['success' => $event]);
// } catch (Exception $e) {
// return $this->fail(['data' => $data, 'error' => $e->getMessage()]);
// }
// }
}
@@ -0,0 +1,598 @@
<?php
declare(strict_types=1);
/**
* Circles - Bring cloud-users closer together.
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2021
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Circles\Controller;
use Exception;
use OCA\Circles\Exceptions\FederatedUserException;
use OCA\Circles\Exceptions\FederatedUserNotFoundException;
use OCA\Circles\Exceptions\FrontendException;
use OCA\Circles\Exceptions\InvalidIdException;
use OCA\Circles\Exceptions\RequestBuilderException;
use OCA\Circles\Exceptions\SingleCircleNotFoundException;
use OCA\Circles\Model\FederatedUser;
use OCA\Circles\Model\Member;
use OCA\Circles\Model\Probes\BasicProbe;
use OCA\Circles\Model\Probes\CircleProbe;
use OCA\Circles\Service\CircleService;
use OCA\Circles\Service\ConfigService;
use OCA\Circles\Service\FederatedUserService;
use OCA\Circles\Service\MemberService;
use OCA\Circles\Service\MembershipService;
use OCA\Circles\Service\PermissionService;
use OCA\Circles\Service\SearchService;
use OCA\Circles\Tools\Traits\TDeserialize;
use OCA\Circles\Tools\Traits\TNCLogger;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\OCSController;
use OCP\IRequest;
use OCP\IUserSession;
/**
* Class LocalController
*
* @package OCA\Circles\Controller
*/
class LocalController extends OCSController {
use TDeserialize;
use TNCLogger;
/** @var IUserSession */
private $userSession;
/** @var FederatedUserService */
private $federatedUserService;
/** @var CircleService */
private $circleService;
/** @var MemberService */
private $memberService;
/** @var MembershipService */
private $membershipService;
/** @var PermissionService */
private $permissionService;
/** @var SearchService */
private $searchService;
/** @var ConfigService */
protected $configService;
/**
* LocalController constructor.
*
* @param string $appName
* @param IRequest $request
* @param IUserSession $userSession
* @param FederatedUserService $federatedUserService
* @param CircleService $circleService
* @param MemberService $memberService
* @param MembershipService $membershipService
* @param SearchService $searchService
* @param ConfigService $configService
*/
public function __construct(
string $appName,
IRequest $request,
IUserSession $userSession,
FederatedUserService $federatedUserService,
CircleService $circleService,
MemberService $memberService,
MembershipService $membershipService,
PermissionService $permissionService,
SearchService $searchService,
ConfigService $configService
) {
parent::__construct($appName, $request);
$this->userSession = $userSession;
$this->federatedUserService = $federatedUserService;
$this->circleService = $circleService;
$this->memberService = $memberService;
$this->membershipService = $membershipService;
$this->permissionService = $permissionService;
$this->searchService = $searchService;
$this->configService = $configService;
$this->setup('app', 'circles');
}
/**
* @NoAdminRequired
*
* @param string $name
* @param bool $personal
* @param bool $local
*
* @return DataResponse
* @throws OCSException
*/
public function create(string $name, bool $personal = false, bool $local = false): DataResponse {
try {
$this->setCurrentFederatedUser();
$this->permissionService->confirmCircleCreation();
$circle = $this->circleService->create($name, null, $personal, $local);
return new DataResponse($this->serializeArray($circle));
} catch (Exception $e) {
$this->e($e, ['name' => $name, 'members' => $personal, 'local' => $local]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @NoAdminRequired
*
* @param string $circleId
*
* @return DataResponse
* @throws OCSException
*/
public function destroy(string $circleId): DataResponse {
try {
$this->setCurrentFederatedUser();
$circle = $this->circleService->destroy($circleId);
return new DataResponse($this->serializeArray($circle));
} catch (Exception $e) {
$this->e($e, ['circleId' => $circleId]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @NoAdminRequired
*
* @param string $term
*
* @return DataResponse
* @throws OCSException
*/
public function search(string $term): DataResponse {
try {
$this->setCurrentFederatedUser();
return new DataResponse($this->serializeArray($this->searchService->search($term)));
} catch (Exception $e) {
$this->e($e, ['term' => $term]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @NoAdminRequired
*
* @param string $circleId
*
* @return DataResponse
* @throws OCSException
*/
public function circleDetails(string $circleId): DataResponse {
try {
$this->setCurrentFederatedUser();
$probe = new CircleProbe();
$probe->includeNonVisibleCircles();
return new DataResponse($this->serialize($this->circleService->getCircle($circleId, $probe)));
} catch (Exception $e) {
$this->e($e, ['circleId' => $circleId]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @NoAdminRequired
*
* @param string $circleId
* @param string $userId
* @param int $type
*
* @return DataResponse
* @throws OCSException
*/
public function memberAdd(string $circleId, string $userId, int $type): DataResponse {
try {
$this->setCurrentFederatedUser();
// exception in Contact
if ($type === Member::TYPE_CONTACT) {
$currentUser = $this->federatedUserService->getCurrentUser();
if (!$this->configService->isLocalInstance($currentUser->getInstance())) {
throw new OCSException('works only from local instance', 404);
}
$userId = $currentUser->getUserId() . '/' . $userId;
}
$federatedUser = $this->federatedUserService->generateFederatedUser($userId, $type);
$result = $this->memberService->addMember($circleId, $federatedUser);
return new DataResponse($this->serializeArray($result));
} catch (Exception $e) {
$this->e($e, ['circleId' => $circleId, 'userId' => $userId, 'type' => $type]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @NoAdminRequired
*
* @param string $circleId
* @param array $members
*
* @return DataResponse
* @throws OCSException
*/
public function membersAdd(string $circleId, array $members): DataResponse {
try {
$this->setCurrentFederatedUser();
$federatedUsers = [];
foreach ($members as $member) {
// TODO: generate Multiple FederatedUsers using a single SQL request
$federatedUsers[] = $this->federatedUserService->generateFederatedUser(
$this->get('id', $member),
$this->getInt('type', $member)
);
}
$result = $this->memberService->addMembers($circleId, $federatedUsers);
return new DataResponse($this->serializeArray($result));
} catch (Exception $e) {
$this->e($e, ['circleId' => $circleId, 'members' => $members]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @NoAdminRequired
*
* @param string $circleId
*
* @return DataResponse
* @throws OCSException
*/
public function circleJoin(string $circleId): DataResponse {
try {
$this->setCurrentFederatedUser();
$result = $this->circleService->circleJoin($circleId);
return new DataResponse($this->serializeArray($result));
} catch (Exception $e) {
$this->e($e, ['circleId' => $circleId]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @NoAdminRequired
*
* @param string $circleId
*
* @return DataResponse
* @throws OCSException
*/
public function circleLeave(string $circleId): DataResponse {
try {
$this->setCurrentFederatedUser();
$result = $this->circleService->circleLeave($circleId);
return new DataResponse($this->serializeArray($result));
} catch (Exception $e) {
$this->e($e, ['circleId' => $circleId]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @NoAdminRequired
*
* @param string $circleId
* @param string $memberId
* @param string|int $level
*
* @return DataResponse
* @throws OCSException
*/
public function memberLevel(string $circleId, string $memberId, $level): DataResponse {
try {
$this->setCurrentFederatedUser();
if (is_int($level)) {
$level = Member::parseLevelInt($level);
} else {
$level = Member::parseLevelString($level);
}
$this->memberService->getMemberById($memberId, $circleId);
$result = $this->memberService->memberLevel($memberId, $level);
return new DataResponse($this->serializeArray($result));
} catch (Exception $e) {
$this->e($e, ['circleId' => $circleId, 'memberId' => $memberId, 'level' => $level]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @NoAdminRequired
*
* @param string $circleId
* @param string $memberId
*
* @return DataResponse
* @throws OCSException
*/
public function memberConfirm(string $circleId, string $memberId): DataResponse {
try {
$this->setCurrentFederatedUser();
$member = $this->memberService->getMemberById($memberId, $circleId);
$federatedUser = new FederatedUser();
$federatedUser->importFromIFederatedUser($member);
$result = $this->memberService->addMember($circleId, $federatedUser);
return new DataResponse($this->serializeArray($result));
} catch (Exception $e) {
$this->e($e, ['circleId' => $circleId, 'memberId' => $memberId]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @NoAdminRequired
*
* @param string $circleId
* @param string $memberId
*
* @return DataResponse
* @throws OCSException
*/
public function memberRemove(string $circleId, string $memberId): DataResponse {
try {
$this->setCurrentFederatedUser();
$this->memberService->getMemberById($memberId, $circleId);
$result = $this->memberService->removeMember($memberId);
return new DataResponse($this->serializeArray($result));
} catch (Exception $e) {
$this->e($e, ['circleId' => $circleId, 'memberId' => $memberId]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @NoAdminRequired
*
* @param int $limit
* @param int $offset
*
* @return DataResponse
* @throws OCSException
*/
public function circles(int $limit = -1, int $offset = 0): DataResponse {
try {
$this->setCurrentFederatedUser();
$probe = new CircleProbe();
$probe->filterHiddenCircles()
->filterBackendCircles()
->addDetail(BasicProbe::DETAILS_POPULATION)
->setItemsLimit($limit)
->setItemsOffset($offset);
return new DataResponse($this->serializeArray($this->circleService->getCircles($probe)));
} catch (Exception $e) {
$this->e($e);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @NoAdminRequired
*
* @param string $circleId
*
* @return DataResponse
* @throws OCSException
*/
public function members(string $circleId): DataResponse {
try {
$this->setCurrentFederatedUser();
return new DataResponse($this->serializeArray($this->memberService->getMembers($circleId)));
} catch (Exception $e) {
$this->e($e, ['circleId' => $circleId]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @NoAdminRequired
*
* @param string $circleId
* @param string $value
*
* @return DataResponse
* @throws OCSException
*/
public function editName(string $circleId, string $value): DataResponse {
try {
$this->setCurrentFederatedUser();
$outcome = $this->circleService->updateName($circleId, $value);
return new DataResponse($this->serializeArray($outcome));
} catch (Exception $e) {
$this->e($e, ['circleId' => $circleId, 'value' => $value]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @NoAdminRequired
*
* @param string $circleId
* @param string $value
*
* @return DataResponse
* @throws OCSException
*/
public function editDescription(string $circleId, string $value): DataResponse {
try {
$this->setCurrentFederatedUser();
$outcome = $this->circleService->updateDescription($circleId, $value);
return new DataResponse($this->serializeArray($outcome));
} catch (Exception $e) {
$this->e($e, ['circleId' => $circleId, 'value' => $value]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @NoAdminRequired
*
* @param string $circleId
* @param string $setting
* @param string|null $value
*
* @return DataResponse
* @throws OCSException
*/
public function editSetting(string $circleId, string $setting, ?string $value = null): DataResponse {
try {
$this->setCurrentFederatedUser();
$outcome = $this->circleService->updateSetting($circleId, $setting, $value);
return new DataResponse($this->serializeArray($outcome));
} catch (Exception $e) {
$this->e($e, ['circleId' => $circleId, 'setting' => $setting, 'value' => $value]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @NoAdminRequired
*
* @param string $circleId
* @param int $value
*
* @return DataResponse
* @throws OCSException
*/
public function editConfig(string $circleId, int $value): DataResponse {
try {
$this->setCurrentFederatedUser();
$outcome = $this->circleService->updateConfig($circleId, $value);
return new DataResponse($this->serializeArray($outcome));
} catch (Exception $e) {
$this->e($e, ['circleId' => $circleId, 'value' => $value]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @NoAdminRequired
*
* @param string $circleId
* @param string $singleId
*
* @return DataResponse
* @throws OCSException
*/
public function link(string $circleId, string $singleId): DataResponse {
try {
$this->setCurrentFederatedUser();
$membership = $this->membershipService->getMembership($circleId, $singleId, true);
return new DataResponse($this->serialize($membership));
} catch (Exception $e) {
$this->e($e, ['circleId' => $circleId, 'singleId' => $singleId]);
throw new OCSException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @return void
* @throws FederatedUserException
* @throws FederatedUserNotFoundException
* @throws FrontendException
* @throws InvalidIdException
* @throws RequestBuilderException
* @throws SingleCircleNotFoundException
*/
private function setCurrentFederatedUser(): void {
if (!$this->configService->getAppValueBool(ConfigService::FRONTEND_ENABLED)) {
throw new FrontendException('frontend disabled');
}
$user = $this->userSession->getUser();
$this->federatedUserService->setLocalCurrentUser($user);
}
}
@@ -0,0 +1,612 @@
<?php
declare(strict_types=1);
/**
* Circles - Bring cloud-users closer together.
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2020
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Circles\Controller;
use Exception;
use OC\AppFramework\Middleware\Security\Exceptions\NotLoggedInException;
use OCA\Circles\Db\CircleRequest;
use OCA\Circles\Exceptions\FederatedItemException;
use OCA\Circles\Exceptions\FederatedUserException;
use OCA\Circles\Exceptions\FederatedUserNotFoundException;
use OCA\Circles\Exceptions\JsonNotRequestedException;
use OCA\Circles\Exceptions\UnknownInterfaceException;
use OCA\Circles\Model\Circle;
use OCA\Circles\Model\Federated\FederatedEvent;
use OCA\Circles\Model\Federated\RemoteInstance;
use OCA\Circles\Model\FederatedUser;
use OCA\Circles\Model\Member;
use OCA\Circles\Model\Probes\BasicProbe;
use OCA\Circles\Model\Probes\CircleProbe;
use OCA\Circles\Service\CircleService;
use OCA\Circles\Service\ConfigService;
use OCA\Circles\Service\FederatedUserService;
use OCA\Circles\Service\InterfaceService;
use OCA\Circles\Service\MemberService;
use OCA\Circles\Service\MembershipService;
use OCA\Circles\Service\RemoteDownstreamService;
use OCA\Circles\Service\RemoteStreamService;
use OCA\Circles\Tools\Exceptions\InvalidItemException;
use OCA\Circles\Tools\Exceptions\InvalidOriginException;
use OCA\Circles\Tools\Exceptions\ItemNotFoundException;
use OCA\Circles\Tools\Exceptions\MalformedArrayException;
use OCA\Circles\Tools\Exceptions\SignatoryException;
use OCA\Circles\Tools\Exceptions\SignatureException;
use OCA\Circles\Tools\Exceptions\UnknownTypeException;
use OCA\Circles\Tools\Model\NCSignedRequest;
use OCA\Circles\Tools\Model\SimpleDataStore;
use OCA\Circles\Tools\Traits\TDeserialize;
use OCA\Circles\Tools\Traits\TNCLocalSignatory;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\IRequest;
use OCP\IUserSession;
/**
* Class RemoteController
*
* @package OCA\Circles\Controller
*/
class RemoteController extends Controller {
use TNCLocalSignatory;
use TDeserialize;
/** @var CircleRequest */
private $circleRequest;
/** @var RemoteStreamService */
private $remoteStreamService;
/** @var RemoteDownstreamService */
private $remoteDownstreamService;
/** @var FederatedUserService */
private $federatedUserService;
/** @var CircleService */
private $circleService;
/** @var MemberService */
private $memberService;
/** @var MembershipService */
private $membershipService;
/** @var InterfaceService */
private $interfaceService;
/** @var ConfigService */
private $configService;
/** @var IUserSession */
private $userSession;
/**
* RemoteController constructor.
*
* @param string $appName
* @param IRequest $request
* @param CircleRequest $circleRequest
* @param RemoteStreamService $remoteStreamService
* @param RemoteDownstreamService $remoteDownstreamService
* @param FederatedUserService $federatedUserService
* @param CircleService $circleService
* @param MemberService $memberService
* @param MembershipService $membershipService
* @param InterfaceService $interfaceService
* @param ConfigService $configService
*/
public function __construct(
string $appName,
IRequest $request,
CircleRequest $circleRequest,
RemoteStreamService $remoteStreamService,
RemoteDownstreamService $remoteDownstreamService,
FederatedUserService $federatedUserService,
CircleService $circleService,
MemberService $memberService,
MembershipService $membershipService,
InterfaceService $interfaceService,
ConfigService $configService,
IUserSession $userSession
) {
parent::__construct($appName, $request);
$this->circleRequest = $circleRequest;
$this->remoteStreamService = $remoteStreamService;
$this->remoteDownstreamService = $remoteDownstreamService;
$this->federatedUserService = $federatedUserService;
$this->circleService = $circleService;
$this->memberService = $memberService;
$this->membershipService = $membershipService;
$this->interfaceService = $interfaceService;
$this->configService = $configService;
$this->userSession = $userSession;
$this->setup('app', 'circles');
$this->setupArray('enforceSignatureHeaders', ['digest', 'content-length']);
}
/**
* @PublicPage
* @NoCSRFRequired
*
* @param string $test
*
* @return DataResponse
* @throws NotLoggedInException
* @throws SignatoryException
* @throws UnknownInterfaceException
*/
public function appService(string $test = ''): DataResponse {
try {
$this->publicPageJsonLimited();
} catch (JsonNotRequestedException $e) {
return new DataResponse();
}
$this->interfaceService->setCurrentInterfaceFromRequest($this->request, $test);
$signatory = $this->remoteStreamService->getAppSignatory(false, $this->request->getParam('auth', ''));
return new DataResponse($signatory);
}
/**
* @PublicPage
* @NoCSRFRequired
*
* @return DataResponse
*/
public function event(): DataResponse {
try {
$event = $this->extractEventFromRequest();
} catch (Exception $e) {
return $this->exceptionResponse($e, Http::STATUS_UNAUTHORIZED);
}
try {
$this->remoteDownstreamService->requestedEvent($event);
return new DataResponse($event->getOutcome());
} catch (Exception $e) {
$this->e($e, ['event' => $event->getWrapperToken()]);
return $this->exceptionResponse($e);
}
}
/**
* @PublicPage
* @NoCSRFRequired
*
* @return DataResponse
*/
public function incoming(): DataResponse {
try {
$event = $this->extractEventFromRequest();
} catch (Exception $e) {
$this->e($e);
return $this->exceptionResponse($e, Http::STATUS_UNAUTHORIZED);
}
try {
$this->remoteDownstreamService->incomingEvent($event);
return new DataResponse($this->serialize($event->getResult()));
} catch (Exception $e) {
return $this->exceptionResponse($e);
}
}
/**
* @PublicPage
* @NoCSRFRequired
*
* @return DataResponse
*/
public function test(): DataResponse {
try {
$this->interfaceService->setCurrentInterfaceFromRequest($this->request);
$test = $this->remoteStreamService->incomingSignedRequest();
return new DataResponse($this->serialize($test));
} catch (Exception $e) {
$this->e($e);
return $this->exceptionResponse($e, Http::STATUS_UNAUTHORIZED);
}
}
/**
* @PublicPage
* @NoCSRFRequired
*
* @return DataResponse
*/
public function circles(): DataResponse {
try {
$data = $this->extractDataFromFromRequest();
} catch (Exception $e) {
return $this->exceptionResponse($e, Http::STATUS_UNAUTHORIZED);
}
try {
/** @var Circle $filterCircle */
$filterCircle = $data->gObj('filterCircle');
/** @var Member $filterMember */
$filterMember = $data->gObj('filterMember');
$probe = new CircleProbe();
$probe->setFilterCircle($filterCircle)
->setFilterMember($filterMember)
->addDetail(BasicProbe::DETAILS_POPULATION);
$circles = $this->circleService->getCircles($probe);
return new DataResponse($circles);
} catch (Exception $e) {
return $this->exceptionResponse($e);
}
}
/**
* @PublicPage
* @NoCSRFRequired
*
* @param string $circleId
*
* @return DataResponse
*/
public function circle(string $circleId): DataResponse {
try {
$this->extractDataFromFromRequest();
} catch (Exception $e) {
return $this->exceptionResponse($e, Http::STATUS_UNAUTHORIZED);
}
try {
$circle = $this->circleService->getCircle($circleId);
return new DataResponse($this->serialize($circle));
} catch (Exception $e) {
return $this->exceptionResponse($e);
}
}
/**
* @PublicPage
* @NoCSRFRequired
*
* @param string $circleId
*
* @return DataResponse
*/
public function members(string $circleId): DataResponse {
try {
$this->extractDataFromFromRequest();
} catch (Exception $e) {
return $this->exceptionResponse($e, Http::STATUS_UNAUTHORIZED);
}
try {
$members = $this->memberService->getMembers($circleId);
return new DataResponse($members);
} catch (Exception $e) {
return $this->exceptionResponse($e);
}
}
/**
* ?? TODO: rename /member/ to /federatedUser/ ou /federated/ ?
*
* @PublicPage
* @NoCSRFRequired
*
* @param string $type
* @param string $userId
*
* @return DataResponse
*/
public function member(string $type, string $userId): DataResponse {
try {
$this->extractDataFromFromRequest();
} catch (Exception $e) {
$this->e($e);
return $this->exceptionResponse($e, Http::STATUS_UNAUTHORIZED);
}
try {
// FILTER CIRCLE BASED ON THE CONFIG/FEDERATED_8192 !!
if ($type === Member::$TYPE[Member::TYPE_SINGLE]) {
$federatedUser = $this->federatedUserService->getFederatedUser($userId, Member::TYPE_SINGLE);
} elseif ($type === Member::$TYPE[Member::TYPE_CIRCLE]) {
$federatedUser = $this->federatedUserService->getFederatedUser($userId, Member::TYPE_CIRCLE);
} elseif ($type === Member::$TYPE[Member::TYPE_USER]) {
$federatedUser = $this->federatedUserService->getLocalFederatedUser($userId);
} else {
throw new FederatedUserNotFoundException('Entity not found');
}
return new DataResponse($this->serialize($federatedUser));
} catch (Exception $e) {
return $this->exceptionResponse($e);
}
}
/**
* @PublicPage
* @NoCSRFRequired
*
* @param string $circleId
*
* @return DataResponse
*/
public function inherited(string $circleId): DataResponse {
try {
$this->extractDataFromFromRequest();
} catch (Exception $e) {
return $this->exceptionResponse($e, Http::STATUS_UNAUTHORIZED);
}
try {
$circle = $this->circleService->getCircle($circleId);
return new DataResponse($circle->getInheritedMembers());
} catch (Exception $e) {
return $this->exceptionResponse($e);
}
}
/**
* @PublicPage
* @NoCSRFRequired
*
* @param string $circleId
*
* @return DataResponse
*/
public function memberships(string $circleId): DataResponse {
try {
$this->extractDataFromFromRequest();
} catch (Exception $e) {
return $this->exceptionResponse($e, Http::STATUS_UNAUTHORIZED);
}
try {
$circle = $this->circleService->getCircle($circleId);
return new DataResponse($circle->getMemberships());
} catch (Exception $e) {
return $this->exceptionResponse($e);
}
}
/**
* @return FederatedEvent
* @throws InvalidItemException
* @throws InvalidOriginException
* @throws MalformedArrayException
* @throws SignatoryException
* @throws SignatureException
*/
private function extractEventFromRequest(): FederatedEvent {
$signed = $this->remoteStreamService->incomingSignedRequest();
$this->confirmRemoteInstance($signed);
$event = new FederatedEvent();
$event->import(json_decode($signed->getBody(), true));
$event->setSender($signed->getOrigin());
return $event;
}
/**
* @return SimpleDataStore
* @throws FederatedUserException
* @throws InvalidOriginException
* @throws MalformedArrayException
* @throws SignatoryException
* @throws SignatureException
* @throws UnknownTypeException
*/
private function extractDataFromFromRequest(): SimpleDataStore {
$signed = $this->remoteStreamService->incomingSignedRequest();
$remoteInstance = $this->confirmRemoteInstance($signed);
// There should be no need to confirm the need or the origin of the initiator as $remoteInstance
// already helps filtering request to the database.
// initiator here is only used to play with the visibility, on top of the visibility provided to
// the remote instance based on its type.
$this->federatedUserService->setRemoteInstance($remoteInstance);
$data = new SimpleDataStore();
$store = new SimpleDataStore(json_decode($signed->getBody(), true));
try {
/** @var FederatedUser $initiator */
$initiator = $store->gObj('initiator', FederatedUser::class);
$this->federatedUserService->setCurrentUser($initiator);
} catch (InvalidItemException | ItemNotFoundException $e) {
}
try {
/** @var FederatedUser $initiator */
$filterMember = $store->gObj('filterMember', Member::class);
$data->aObj('filterMember', $filterMember);
} catch (InvalidItemException | ItemNotFoundException $e) {
}
try {
/** @var FederatedUser $initiator */
$filterCircle = $store->gObj('filterCircle', Circle::class);
$data->aObj('filterCircle', $filterCircle);
} catch (InvalidItemException | ItemNotFoundException $e) {
}
return $data;
}
/**
* @param NCSignedRequest $signedRequest
*
* @return RemoteInstance
* @throws SignatoryException
*/
private function confirmRemoteInstance(NCSignedRequest $signedRequest): RemoteInstance {
/** @var RemoteInstance $signatory */
$signatory = $signedRequest->getSignatory();
if (!$signatory instanceof RemoteInstance) {
$this->debug('Signatory is not a known RemoteInstance', ['signedRequest' => $signedRequest]);
throw new SignatoryException('Could not confirm identity');
}
if (!$this->configService->isLocalInstance($signedRequest->getOrigin())
&& $signatory->getType() === RemoteInstance::TYPE_UNKNOWN) {
$this->debug('Could not confirm identity', ['signedRequest' => $signedRequest]);
throw new SignatoryException('Could not confirm identity');
}
$this->interfaceService->setCurrentInterface($signatory->getInterface());
return $signatory;
}
/**
* @param Exception $e
* @param int $httpErrorCode
*
* @return DataResponse
*/
public function exceptionResponse(
Exception $e,
int $httpErrorCode = Http::STATUS_BAD_REQUEST
): DataResponse {
if ($e instanceof FederatedItemException) {
return new DataResponse($this->serialize($e), $e->getStatus());
}
return new DataResponse(
[
'message' => $e->getMessage(),
'code' => $e->getCode()
],
($e->getCode() > 0) ? $e->getCode() : $httpErrorCode
);
}
/**
* use this one if a method from a Controller is only PublicPage when remote client asking for Json
*
* try {
* $this->publicPageJsonLimited();
* return new DataResponse(['test' => 42]);
* } catch (JsonNotRequestedException $e) {}
*
*
* @throws NotLoggedInException
* @throws JsonNotRequestedException
*/
private function publicPageJsonLimited(): void {
if (!$this->jsonRequested()) {
if (!$this->userSession->isLoggedIn()) {
throw new NotLoggedInException();
}
throw new JsonNotRequestedException();
}
}
/**
* @return bool
*/
private function jsonRequested(): bool {
return ($this->areWithinAcceptHeader(
[
'application/json',
'application/ld+json',
'application/activity+json'
]
));
}
/**
* @param array $needles
*
* @return bool
*/
private function areWithinAcceptHeader(array $needles): bool {
$accepts = array_map([$this, 'trimHeader'], explode(',', $this->request->getHeader('Accept')));
foreach ($accepts as $accept) {
if (in_array($accept, $needles)) {
return true;
}
}
return false;
}
/**
* @param string $header
*
* @return string
*/
private function trimHeader(string $header): string {
$header = trim($header);
$pos = strpos($header, ';');
if ($pos === false) {
return $header;
}
return substr($header, 0, $pos);
}
}