Skip to content

adding feature to create link share of a drive using root endpoint #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions src/Drive.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string> $tags
* @todo This function is not implemented yet! Place, name and signature of the function might change!
Expand Down
1 change: 1 addition & 0 deletions src/OcisResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/Share.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,8 @@ public function setExpiration(?\DateTimeImmutable $expiration): bool
$this->apiPermission = $apiPermission;
return true;
}

function getPermission() : ApiPermission {
return $this->apiPermission;
}
}
19 changes: 19 additions & 0 deletions tests/integration/Owncloud/OcisPhpSdk/DriveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
}
54 changes: 27 additions & 27 deletions tests/integration/Owncloud/OcisPhpSdk/OcisPhpSdkTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down