From e73e3f7c4d32ae5b590ee46aaea39be0a9403bda Mon Sep 17 00:00:00 2001 From: Niraj Acharya Date: Wed, 3 Jul 2024 16:49:36 +0545 Subject: [PATCH] adding feature to create link share of a drive using root endpoint --- src/Drive.php | 131 ++++++++++++++++++ src/OcisResource.php | 1 + src/Share.php | 4 + .../Owncloud/OcisPhpSdk/DriveTest.php | 19 +++ .../OcisPhpSdk/OcisPhpSdkTestCase.php | 54 ++++---- 5 files changed, 182 insertions(+), 27 deletions(-) diff --git a/src/Drive.php b/src/Drive.php index 59ff3e17..ab0cd01c 100644 --- a/src/Drive.php +++ b/src/Drive.php @@ -11,7 +11,12 @@ use OpenAPI\Client\Model\Drive as ApiDrive; use OpenAPI\Client\Model\DriveUpdate; use OpenAPI\Client\Model\DriveItem; +use OpenAPI\Client\Model\DriveItemCreateLink; +use OpenAPI\Client\Model\SharingLinkType; +use OpenAPI\Client\Model\DriveRecipient; use OpenAPI\Client\Model\OdataError; +use OpenAPI\Client\Model\Permission; +use OpenAPI\Client\Model\SharingLink as ApiSharingLink; use OpenAPI\Client\Model\Quota; use Owncloud\OcisPhpSdk\Exception\BadRequestException; use Owncloud\OcisPhpSdk\Exception\ExceptionHelper; @@ -616,6 +621,132 @@ public function getRoles(): array return $roles; } + /** + * @param User $recipient + * @param string $role + * @param string|null $password + * @param string|null $displayName + * @param boolean|null $quickLink + * @param \DateTimeImmutable|null $expiration + * @return ShareLink + */ + public function createLink($recipient, string $role, ?string $password = null, ?string $displayName = null, ?bool $quickLink = false, ?\DateTimeImmutable $expiration = null): ShareLink + { + $driveItemCreateLink = []; + $recipientData = []; + + $driveItemCreateLink['type'] = SharingLinkType::from($role); // get from enum + if ($expiration !== null) { + $expirationMutable = \DateTime::createFromImmutable($expiration); + $driveItemCreateLink['expiration_date_time'] = $expirationMutable; + } + if(isset($password)) { + $driveItemCreateLink['password'] = $password; + } + if(isset($displayName)) { + $driveItemCreateLink['display_name'] = $displayName; + } + $driveItemCreateLink['at_libre_graph_quick_link'] = $quickLink; + + $recipientData['object_id'] = $recipient->getId(); + $driveItemCreateLink['recipients'][] = new DriveRecipient($recipientData); + + if (array_key_exists('drivesRootApi', $this->connectionConfig)) { + $apiInstance = $this->connectionConfig['drivesRootApi']; + } else { + $guzzle = new Client( + Ocis::createGuzzleConfig($this->connectionConfig, $this->accessToken) + ); + $apiInstance = new DrivesRootApi( + $guzzle, + $this->graphApiConfig + ); + } + + $inviteData = new DriveItemCreateLink($driveItemCreateLink); + try { + $permissions = $apiInstance->createLinkSpaceRoot($this->getId(), $inviteData); + } catch (ApiException $e) { + throw ExceptionHelper::getHttpErrorException($e); + } + if ($permissions instanceof OdataError) { + throw new InvalidResponseException( + "invite returned an OdataError - " . $permissions->getError() + ); + } + if(isset($password)) { + $permissions->setHasPassword(true); + } else { + $permissions->setHasPassword(false); + } + + return new ShareLink( + $permissions, + $this->getId(), + $this->getId(), + $this->connectionConfig, + $this->serviceUrl, + $this->accessToken + ); + } + + /** + * @param ShareLink $recipient + * @param string $role + * @param string|null $password + * @param string|null $displayName + * @param boolean|null $quickLink + * @param \DateTimeImmutable|null $expiration + * @return ShareLink + */ + public function updateLink($share, string $type, ?string $displayName = null, ?bool $quickLink = false, ?\DateTimeImmutable $expiration = null): ShareLink + { + $shareLink=new ApiSharingLink(); + $permissions=new Permission(); + + // $permissions->set; + $shareLink->setType(SharingLinkType::EDIT); + + $permissions->setLink($shareLink); + + if (array_key_exists('drivesRootApi', $this->connectionConfig)) { + $apiInstance = $this->connectionConfig['drivesRootApi']; + } else { + $guzzle = new Client( + Ocis::createGuzzleConfig($this->connectionConfig, $this->accessToken) + ); + $apiInstance = new DrivesRootApi( + $guzzle, + $this->graphApiConfig + ); + } + + var_dump($this->accessToken); + try { + $permissions = $apiInstance->updatePermissionSpaceRoot($this->getId(), $share->getPermissionId(),$permissions); + } catch (ApiException $e) { + throw ExceptionHelper::getHttpErrorException($e); + } + if ($permissions instanceof OdataError) { + throw new InvalidResponseException( + "invite returned an OdataError - " . $permissions->getError() + ); + } + if(isset($password)) { + $permissions->setHasPassword(true); + } else { + $permissions->setHasPassword(false); + } + return new ShareLink( + $permissions, + $this->getId(), + $this->getId(), + $this->connectionConfig, + $this->serviceUrl, + $this->accessToken + ); + } + /** * @param array $tags * @todo This function is not implemented yet! Place, name and signature of the function might change! diff --git a/src/OcisResource.php b/src/OcisResource.php index f4e6fa4d..ea5ea260 100644 --- a/src/OcisResource.php +++ b/src/OcisResource.php @@ -5,6 +5,7 @@ use GuzzleHttp\Client; use OpenAPI\Client\Api\DrivesApi; // @phan-suppress-current-line PhanUnreferencedUseNormal it's used in a comment use OpenAPI\Client\Api\DrivesPermissionsApi; +use OpenAPI\Client\Api\DrivesRootApi; use OpenAPI\Client\ApiException; use OpenAPI\Client\Configuration; use OpenAPI\Client\Model\DriveItemCreateLink; diff --git a/src/Share.php b/src/Share.php index 7193ba7a..0a8b8bbb 100644 --- a/src/Share.php +++ b/src/Share.php @@ -177,4 +177,8 @@ public function setExpiration(?\DateTimeImmutable $expiration): bool $this->apiPermission = $apiPermission; return true; } + + function getPermission() : ApiPermission { + return $this->apiPermission; + } } diff --git a/tests/integration/Owncloud/OcisPhpSdk/DriveTest.php b/tests/integration/Owncloud/OcisPhpSdk/DriveTest.php index 97d2f52f..9cb6a244 100644 --- a/tests/integration/Owncloud/OcisPhpSdk/DriveTest.php +++ b/tests/integration/Owncloud/OcisPhpSdk/DriveTest.php @@ -6,6 +6,8 @@ use Owncloud\OcisPhpSdk\Exception\BadRequestException; use Owncloud\OcisPhpSdk\Exception\NotFoundException; use Owncloud\OcisPhpSdk\Ocis; +use Owncloud\OcisPhpSdk\ShareCreated; +use Owncloud\OcisPhpSdk\ShareLink; use Owncloud\OcisPhpSdk\SharingRole; require_once __DIR__ . '/OcisPhpSdkTestCase.php'; @@ -87,4 +89,21 @@ public function testGetDriveRole(): void "Array contains not only 'SharingRole' items" ); } + + public function testbesta(): void + { + $einsteinOcis = $this->initUser('einstein', 'relativity'); + $einstein = $einsteinOcis->getUsers('einstein')[0]; + $share = $this->drive->createLink($einstein, 'view', 'P@$$w0rd', 'user'); + $this->assertInstanceOf(ShareLink::class, $share); + } + + public function testbestb(): void + { + $einsteinOcis = $this->initUser('einstein', 'relativity'); + $einstein = $einsteinOcis->getUsers('einstein')[0]; + $share = $this->drive->createLink($einstein, 'view', 'P@$$w0rd', 'user'); + $shareU = $this->drive->updateLink($share, 'edit'); + $this->assertInstanceOf(ShareLink::class, $shareU); + } } diff --git a/tests/integration/Owncloud/OcisPhpSdk/OcisPhpSdkTestCase.php b/tests/integration/Owncloud/OcisPhpSdk/OcisPhpSdkTestCase.php index 74cd9c09..bc26a441 100644 --- a/tests/integration/Owncloud/OcisPhpSdk/OcisPhpSdkTestCase.php +++ b/tests/integration/Owncloud/OcisPhpSdk/OcisPhpSdkTestCase.php @@ -53,35 +53,35 @@ public function setUp(): void $this->tokenUrl = $openIdConfiguration['token_endpoint']; } - public function tearDown(): void - { - $ocis = $this->getOcis('admin', 'admin'); - foreach ($this->createdDrives as $driveId) { - try { - $drive = $ocis->getDriveById($driveId); - $drive->disable(); - $drive->delete(); - } catch (NotFoundException) { - // ignore, we don't care if the drive was already deleted - } + // public function tearDown(): void + // { + // $ocis = $this->getOcis('admin', 'admin'); + // foreach ($this->createdDrives as $driveId) { + // try { + // $drive = $ocis->getDriveById($driveId); + // $drive->disable(); + // $drive->delete(); + // } catch (NotFoundException) { + // // ignore, we don't care if the drive was already deleted + // } - } - foreach ($this->createdGroups as $group) { - $group->delete(); - } - $this->createdGroups = []; + // } + // foreach ($this->createdGroups as $group) { + // $group->delete(); + // } + // $this->createdGroups = []; - foreach ($this->createdResources as $driveId => $resources) { - $drive = $ocis->getDriveById($driveId); - foreach ($resources as $resource) { - try { - $drive->deleteResource($resource); - } catch (NotFoundException) { - // ignore, we don't care if the resource was already deleted - } - } - } - } + // foreach ($this->createdResources as $driveId => $resources) { + // $drive = $ocis->getDriveById($driveId); + // foreach ($resources as $resource) { + // try { + // $drive->deleteResource($resource); + // } catch (NotFoundException) { + // // ignore, we don't care if the resource was already deleted + // } + // } + // } + // } protected function getGuzzleClient(): Client {