From eeaf1153517305777253f851693c449e29891136 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 | 72 +++++++++++++++++++ .../Owncloud/OcisPhpSdk/DriveTest.php | 9 +++ 2 files changed, 81 insertions(+) diff --git a/src/Drive.php b/src/Drive.php index 59ff3e17..f64df162 100644 --- a/src/Drive.php +++ b/src/Drive.php @@ -11,6 +11,9 @@ 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\Quota; use Owncloud\OcisPhpSdk\Exception\BadRequestException; @@ -616,6 +619,75 @@ 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 ShareCreated + */ + public function createLink($recipient, string $role, ?string $password = null, ?string $displayName = null, ?bool $quickLink = false, ?\DateTimeImmutable $expiration = null): ShareCreated + { + $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 ShareCreated( + $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/tests/integration/Owncloud/OcisPhpSdk/DriveTest.php b/tests/integration/Owncloud/OcisPhpSdk/DriveTest.php index 97d2f52f..c8028796 100644 --- a/tests/integration/Owncloud/OcisPhpSdk/DriveTest.php +++ b/tests/integration/Owncloud/OcisPhpSdk/DriveTest.php @@ -6,6 +6,7 @@ use Owncloud\OcisPhpSdk\Exception\BadRequestException; use Owncloud\OcisPhpSdk\Exception\NotFoundException; use Owncloud\OcisPhpSdk\Ocis; +use Owncloud\OcisPhpSdk\ShareCreated; use Owncloud\OcisPhpSdk\SharingRole; require_once __DIR__ . '/OcisPhpSdkTestCase.php'; @@ -87,4 +88,12 @@ 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(ShareCreated::class, $share); + } }