Skip to content

Commit

Permalink
getOwner()
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Jun 2, 2022
1 parent cdf995f commit 2a8c4d0
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 51 deletions.
3 changes: 2 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@
['name' => 'Sync#syncItem', 'url' => '/sync/item', 'verb' => 'GET'],
['name' => 'Sync#updateSyncedItem', 'url' => '/sync/item', 'verb' => 'PUT'],
// ['name' => 'Remote#syncItem', 'url' => '/sync/item/{singleId}', 'verb' => 'GET'],
['name' => 'Sync#syncShare', 'url' => '/sync/share', 'verb' => 'POST'],
['name' => 'Sync#syncShare', 'url' => '/sync/share', 'verb' => 'GET'],
['name' => 'Sync#createSyncedShare', 'url' => '/sync/share', 'verb' => 'PUT'],
['name' => 'Debug#debugDaemon', 'url' => '/debug', 'verb' => 'POST']

]
Expand Down
28 changes: 13 additions & 15 deletions lib/CircleSharesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

use Exception;
use OCA\Circles\Exceptions\CircleSharesManagerException;
use OCA\Circles\Model\Probes\CircleProbe;
use OCA\Circles\Model\SyncedItemLock;
use OCA\Circles\Service\CircleService;
use OCA\Circles\Service\ConfigService;
Expand Down Expand Up @@ -145,12 +144,12 @@ public function createShare(
try {
$this->mustHaveOrigin();

// TODO: verify rules that apply when sharing to a circle
$probe = new CircleProbe();
$probe->includeSystemCircles()
->mustBeMember();

$circle = $this->circleService->getCircle($circleId, $probe);
// TODO: do we need this here as we are also checking federatedUser during isShareCreatable()
// $probe = new CircleProbe();
// $probe->includeSystemCircles()
// ->mustBeMember();
//
// $circle = $this->circleService->getCircle($circleId, $probe);

// get valid SyncedItem based on appId, itemType, itemId
$syncedItem = $this->federatedSyncItemService->initSyncedItem(
Expand All @@ -163,20 +162,19 @@ public function createShare(
$this->debugService->info(
'initiating the process of sharing {syncedItem.singleId} to {circle.id}',
$circleId, [
'circle' => $circle,
'circleId' => $circleId,
'syncedItem' => $syncedItem,
'extraData' => $extraData,
'isLocal' => $syncedItem->isLocal()
]
);

// confirm item is local
if (!$syncedItem->isLocal()) {
// TODO: sharing a remote item
return;
}

$this->federatedSyncShareService->createShare($syncedItem, $circle, $extraData);
$this->federatedSyncShareService->requestSyncedShareCreation(
$this->federatedUserService->getCurrentEntity(),
$syncedItem,
$circleId,
$extraData
);
} catch (Exception $e) {
$this->debugService->exception($e, $circleId);
throw $e;
Expand Down
10 changes: 10 additions & 0 deletions lib/CirclesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
use OCA\Circles\Model\Probes\CircleProbe;
use OCA\Circles\Service\CircleService;
use OCA\Circles\Service\ConfigService;
use OCA\Circles\Service\DebugService;
use OCA\Circles\Service\FederatedUserService;
use OCA\Circles\Service\MemberService;
use OCA\Circles\Service\MembershipService;
Expand All @@ -74,6 +75,7 @@ class CirclesManager {
private MembershipService $membershipService;
private ConfigService $configService;
private CirclesQueryHelper $circlesQueryHelper;
private DebugService $debugService;


/**
Expand All @@ -94,6 +96,7 @@ public function __construct(
MemberService $memberService,
MembershipService $membershipService,
ConfigService $configService,
DebugService $debugService,
CirclesQueryHelper $circlesQueryHelper
) {
$this->circleSharesManager = $circleSharesManager;
Expand All @@ -102,6 +105,7 @@ public function __construct(
$this->memberService = $memberService;
$this->membershipService = $membershipService;
$this->configService = $configService;
$this->debugService = $debugService;
$this->circlesQueryHelper = $circlesQueryHelper;
}

Expand Down Expand Up @@ -269,6 +273,12 @@ public function getQueryHelper(): CirclesQueryHelper {
return $this->circlesQueryHelper;
}

/**
* @return DebugService
*/
public function getDebugService(): DebugService {
return $this->debugService;
}

/**
* @param string $name
Expand Down
64 changes: 64 additions & 0 deletions lib/Controller/SyncController.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,68 @@ public function updateSyncedItem(): DataResponse {
exit();
}


/**
* @PublicPage
* @NoCSRFRequired
*
* @return DataResponse
*/
public function createSyncedShare(): DataResponse {
try {
/** @var SyncedWrapper $wrapper */
$wrapper = $this->signedControllerService->extractObjectFromRequest(
SyncedWrapper::class,
$signed
);

$this->debugService->info(
'{instance} is requesting the creation of a new SyncedShare on SyncedItem {syncedWrapper.syncedShare.singleId}',
'',
[
'instance' => $signed->getOrigin(),
'syncedWrapper' => $wrapper
]
);

if (!$wrapper->hasShare() || !$wrapper->hasFederatedUser()) {
throw new FederatedItemBadRequestException();
}

$syncedShare = $wrapper->getshare();
$local = $this->federatedSyncItemService->getLocalSyncedItem($syncedShare->getSingleId());

// confirm that remote is in a circle with a share on the item
$this->federatedSyncShareService->confirmRemoteInstanceAccess(
$syncedShare->getSingleId(),
$signed->getOrigin()
);

$this->debugService->info(
'SyncedItem exists, is local, and {instance} have access to the SyncedItem.', '',
[
'instance' => $signed->getOrigin(),
'local' => $local,
]
);

$this->asyncService->setSplittable(true);
$this->federatedSyncShareService->requestSyncedShareCreation(
$wrapper->getFederatedUser(),
$local,
$wrapper->getShare()->getCircleId(),
$wrapper->getExtraData(),
);

if (!$this->asyncService->isAsynced()) {
return new DataResponse(['success' => true]);
}
} catch (Exception $e) {
$this->e($e);

return $this->signedControllerService->exceptionResponse($e, Http::STATUS_UNAUTHORIZED);
}

exit();
}
}
2 changes: 1 addition & 1 deletion lib/Db/SyncedItemLockRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function save(SyncedItemLock $lock): void {
->setValue('update_type_id', $qb->createNamedParameter($lock->getUpdateTypeId()))
->setValue('time', $qb->createNamedParameter(time()));

$qb->executeQuery();
$qb->executeStatement();
}


Expand Down
25 changes: 24 additions & 1 deletion lib/FederatedItems/FederatedSync/ShareCreation.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@
use OCA\Circles\IFederatedItem;
use OCA\Circles\IFederatedItemAsyncProcess;
use OCA\Circles\IFederatedItemHighSeverity;
use OCA\Circles\IFederatedItemInitiatorCheckNotRequired;
use OCA\Circles\IFederatedItemLimitedToInstanceWithMember;
use OCA\Circles\IFederatedItemSyncedItem;
use OCA\Circles\Model\Federated\FederatedEvent;
use OCA\Circles\Model\FederatedUser;
use OCA\Circles\Service\ConfigService;
use OCA\Circles\Service\DebugService;
use OCA\Circles\Service\FederatedSyncItemService;
Expand All @@ -52,6 +54,7 @@ class ShareCreation implements
IFederatedItemLimitedToInstanceWithMember,
IFederatedItemHighSeverity,
IFederatedItemAsyncProcess,
IFederatedItemInitiatorCheckNotRequired,
IFederatedItemSyncedItem {
use TDeserialize;

Expand Down Expand Up @@ -113,14 +116,34 @@ public function manage(FederatedEvent $event): void {
}

$circle = $event->getCircle();
if (!$circle->hasInitiator()) {
$this->debugService->info(
'{?Initiator not available}', '',
['circle' => $circle]
);

return;
}

// note that we have no confirmation about the origin and/or memberships of FederatedUser at
// this point as this class implements IFederatedItemInitiatorCheckNotRequired
// the only use of this IFederatedItem is to broadcast an eventual new share which needs
// to be confirmed with a direct request to the instance that owns the shared item
$federatedUser = new FederatedUser();
$federatedUser->importFromIFederatedUser($circle->getInitiator());
$syncedItem = $event->getSyncedItem();

$this->federatedSyncItemService->compareWithKnownItem($syncedItem, true);

$extraData = $event->getParams()->gArray('extraData');

$this->federatedSyncItemService->updateSyncedItem($syncedItem);
$this->federatedSyncShareService->syncShareCreation($syncedItem, $circle, $extraData);
$this->federatedSyncShareService->syncShareCreation(
$federatedUser,
$syncedItem,
$circle->getSingleId(),
$extraData
);
}


Expand Down
11 changes: 10 additions & 1 deletion lib/IFederatedSyncManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ public function syncItem(string $itemId, array $serializedData): void;
*/
public function itemExists(string $itemId): bool;

/**
* Method is called when Circles needs to know the owner of an item. Must returns a singleId.
*
* @param string $itemId
*
* @return mixed
*/
public function getOwner(string $itemId): string;


/**
* Your app returns details about a share.
* Method will be called to re-sync a share on a remote instance
Expand Down Expand Up @@ -324,5 +334,4 @@ public function onItemModification(
IFederatedUser $federatedUser
): void;


}
3 changes: 2 additions & 1 deletion lib/Model/SyncedShare.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ public function getCircleId(): string {
* @throws InvalidItemException
*/
public function import(array $data): IDeserializable {
if ($this->getInt('singleId', $data) === 0) {
if ($this->get('singleId', $data) === ''
|| $this->get('circleId', $data) === '') {
throw new InvalidItemException();
}

Expand Down
11 changes: 5 additions & 6 deletions lib/Service/FederatedSyncItemService.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public function requestSyncedItemUpdate(
);

return;
} else if (is_null($remoteSum)) {
} else if (is_null($remoteSum)) { // this means that the request is not coming from remote location
$this->requestSyncedItemUpdateRemote($federatedUser, $syncedItem, $syncedLock, $extraData);

return;
Expand Down Expand Up @@ -389,7 +389,7 @@ private function requestSyncedItemUpdateRemote(
SyncedItem $syncedItem,
SyncedItemLock $syncedLock,
array $extraData = []
): array {
): void {
$wrapper = new SyncedWrapper($federatedUser, $syncedItem, $syncedLock, null, $extraData);
$this->interfaceService->setCurrentInterfaceFromInstance($syncedItem->getInstance());
$data = $this->remoteStreamService->resultRequestRemoteInstance(
Expand Down Expand Up @@ -420,11 +420,9 @@ private function requestSyncedItemUpdateRemote(
// update item based on current (old) checksum to confirm item was not already updated (race condition)
// do not update checksum
// $this->updateChecksum($syncedItem->getSingleId(), $data);

return $data;
}
//
//


// private function updateChecksum(string $syncedItemId, ?array $data = null): void {
// $currSum = '';
// if (is_null($data)) {
Expand Down Expand Up @@ -579,6 +577,7 @@ private function isItemModifiable(
SyncedItemLock $syncedLock,
array $extraData = []
): bool {
// $federatedUser
$syncManager = $this->federatedSyncService->initSyncManager($syncedItem);
$this->debugService->info(
'sharing of SyncedItem {syncedItem.singleId} looks doable, calling {`isShareCreatable()} on {syncManager.class} for confirmation',
Expand Down
Loading

0 comments on commit 2a8c4d0

Please sign in to comment.