Skip to content

Commit

Permalink
Merge pull request #225 from owncloud/add-get-roles-root-endpoint
Browse files Browse the repository at this point in the history
Add support for get roles of drive using root endpoint
  • Loading branch information
phil-davis authored Jul 3, 2024
2 parents 6d2647c + 6c242a4 commit f4f6787
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions .drone.star
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def phpIntegrationTest(ctx, phpVersions, coverage):
"OCIS_URL": "https://ocis:9200",
"OCISWRAPPER_URL": "http://ocis:5200",
"COMPOSER_HOME": "%s/.cache/composer" % dir["base"],
"OCIS_VERSION": branch,
},
"commands": [
installPhpXdebugCommand(phpVersion),
Expand Down
44 changes: 44 additions & 0 deletions src/Drive.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DateTime;
use GuzzleHttp\Client;
use OpenAPI\Client\Api\DrivesApi;
use OpenAPI\Client\Api\DrivesRootApi;
use OpenAPI\Client\ApiException;
use OpenAPI\Client\Configuration;
use OpenAPI\Client\Model\Drive as ApiDrive;
Expand Down Expand Up @@ -572,6 +573,49 @@ public function emptyTrashbin(): bool
return true;
}

/**
* Gets all possible roles for the drive
* @return array<SharingRole>
* @throws BadRequestException
* @throws ForbiddenException
* @throws HttpException
* @throws NotFoundException
* @throws UnauthorizedException
* @throws InvalidResponseException
* @throws InternalServerErrorException
*/
public function getRoles(): array
{
$guzzle = new Client(
Ocis::createGuzzleConfig($this->connectionConfig, $this->accessToken)
);

if (array_key_exists('drivesRootApi', $this->connectionConfig)) {
$apiInstance = $this->connectionConfig['drivesRootApi'];
} else {
$apiInstance = new DrivesRootApi(
$guzzle,
$this->graphApiConfig
);
}
try {
$collectionOfPermissions = $apiInstance->listPermissionsSpaceRoot($this->getId());
} catch (ApiException $e) {
throw ExceptionHelper::getHttpErrorException($e);
}
if ($collectionOfPermissions instanceof OdataError) {
throw new InvalidResponseException(
"listPermissions returned an OdataError - " . $collectionOfPermissions->getError()
);
}
$apiRoles = $collectionOfPermissions->getAtLibreGraphPermissionsRolesAllowedValues() ?? [];
$roles = [];
foreach ($apiRoles as $role) {
$roles[] = new SharingRole($role);
}
return $roles;
}

/**
* @param array<string> $tags
* @todo This function is not implemented yet! Place, name and signature of the function might change!
Expand Down
10 changes: 9 additions & 1 deletion src/Ocis.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use OpenAPI\Client\Api\DrivesApi;
use OpenAPI\Client\Api\DrivesGetDrivesApi;
use OpenAPI\Client\Api\DrivesPermissionsApi;
use OpenAPI\Client\Api\DrivesRootApi;
use OpenAPI\Client\Api\GroupApi;
use OpenAPI\Client\Api\MeDriveApi;
use OpenAPI\Client\Api\MeDrivesApi;
Expand Down Expand Up @@ -43,7 +44,8 @@
* 'guzzle'?:Client,
* 'drivesApi'?:DrivesApi,
* 'drivesGetDrivesApi'?:DrivesGetDrivesApi,
* 'drivesPermissionsApi'?:DrivesPermissionsApi
* 'drivesPermissionsApi'?:DrivesPermissionsApi,
* 'drivesRootApi'?:DrivesRootApi
* }
*/
class Ocis
Expand Down Expand Up @@ -155,6 +157,11 @@ public static function isDrivesGetDrivesApi(mixed $api): bool
return $api instanceof DrivesGetDrivesApi;
}

public static function isDrivesRootApi(mixed $api): bool
{
return $api instanceof DrivesRootApi;
}

/**
* @param array<mixed> $connectionConfig
* @ignore This function is used for internal purposes only and should not be shown in the documentation.
Expand All @@ -170,6 +177,7 @@ public static function isConnectionConfigValid(array $connectionConfig): bool
'drivesPermissionsApi' => self::class . '::isDrivesPermissionsApi',
'drivesApi' => self::class . '::isDrivesApi',
'drivesGetDrivesApi' => self::class . '::isDrivesGetDrivesApi',
'drivesRootApi' => self::class . '::isDrivesRootApi',
'proxy' => 'is_array',
];
foreach ($connectionConfig as $key => $check) {
Expand Down
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,7 @@
use Owncloud\OcisPhpSdk\Exception\BadRequestException;
use Owncloud\OcisPhpSdk\Exception\NotFoundException;
use Owncloud\OcisPhpSdk\Ocis;
use Owncloud\OcisPhpSdk\SharingRole;

require_once __DIR__ . '/OcisPhpSdkTestCase.php';

Expand Down Expand Up @@ -68,4 +69,22 @@ public function testDeleteEnabledDrive(): void
$this->expectExceptionMessage('invalidRequest - error: bad request: can\'t purge enabled space');
$this->drive->delete();
}

public function testGetDriveRole(): void
{
// At the time of writing, "stable" is major version 5 of ocis.
// This functionality works with major version 6.
// When ocis major version 6 has been released as "stable" then remove this test skip.
if (getenv('OCIS_VERSION') === "stable") {
$this->markTestSkipped(
'This test is skipped because root endpoint for drive share is not applicable for version 5 of OCIS.'
);
};
$role = $this->drive->getRoles();
$this->assertContainsOnlyInstancesOf(
SharingRole::class,
$role,
"Array contains not only 'SharingRole' items"
);
}
}

0 comments on commit f4f6787

Please sign in to comment.