Skip to content

Commit

Permalink
adding feature to create link share of a drive using root endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
nirajacharya2 committed Jul 4, 2024
1 parent f4f6787 commit eeaf115
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/Drive.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string> $tags
* @todo This function is not implemented yet! Place, name and signature of the function might change!
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/Owncloud/OcisPhpSdk/DriveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
}

0 comments on commit eeaf115

Please sign in to comment.