Skip to content
Merged
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
28 changes: 17 additions & 11 deletions apps/sharing/lib/Controller/ApiV1Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,20 @@ public function __construct(
/**
* Search for recpients that can be added to a share.
*
* @param ?list<class-string<IShareRecipientType>> $recipientTypeClasses Type class of recipients to filter by
* @param ?list<class-string<IShareRecipientType>> $filterRecipientTypeClasses Type classes of recipients to filter by
* @param string $query The query to search for
* @param int<1, 100> $limit The maximum number of participants
* @param non-negative-int $offset The offset of the participants
* @return DataResponse<Http::STATUS_OK, list<SharingRecipient>, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, string, array{}>
* @param ?string $id If provided, recipients that are already part of the share will not be returned.
* @return DataResponse<Http::STATUS_OK, list<SharingRecipient>, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, string, array{}>
*
* 200: Recipients returned
* 400: Invalid recipient search parameters
* 404: Share used for filtering existing recipients does not exist
*/
#[NoAdminRequired]
#[ApiRoute(verb: 'GET', url: '/api/v1/recipients')]
public function searchRecipients(?array $recipientTypeClasses, string $query, int $limit = 10, int $offset = 0): DataResponse {
public function searchRecipients(?array $filterRecipientTypeClasses, string $query, int $limit = 10, int $offset = 0, ?string $id = null): DataResponse {
/** @psalm-suppress DocblockTypeContradiction */
if ($limit < 1) {
return new DataResponse('The limit is too low.', Http::STATUS_BAD_REQUEST);
Expand All @@ -103,10 +105,17 @@ public function searchRecipients(?array $recipientTypeClasses, string $query, in
}

try {
$recipients = $this->manager->searchRecipients($this->accessContext, $recipientTypeClasses, $query, $limit, $offset);
return new DataResponse(ShareRecipient::formatMultiple($this->registry, $this->l10nFactory, $this->urlGenerator, $this->userManager, $recipients));
} catch (ShareInvalidException $shareInvalidException) {
return new DataResponse($shareInvalidException->getHint(), Http::STATUS_BAD_REQUEST);
try {
$this->dbConnection->beginTransaction();
$recipients = $this->manager->searchRecipients($this->accessContext, $filterRecipientTypeClasses, $query, $limit, $offset, $id);
$this->dbConnection->commit();
return new DataResponse(ShareRecipient::formatMultiple($this->registry, $this->l10nFactory, $this->urlGenerator, $this->userManager, $recipients));
} catch (Exception $exception) {
$this->dbConnection->rollBack();
throw $exception;
}
} catch (ShareNotFoundException $shareNotFoundException) {
return new DataResponse($shareNotFoundException->getHint(), Http::STATUS_NOT_FOUND);
}
}

Expand Down Expand Up @@ -519,10 +528,9 @@ public function deleteShare(string $id): DataResponse {
* @param string $id ID of the share
* @param ?string $secret Secret of the share
* @param array<class-string<IShareRecipientType|ISharePropertyTypeFilter>, mixed> $arguments Arguments for accessing the share
* @return DataResponse<Http::STATUS_OK, SharingShare, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, string, array{}>
* @return DataResponse<Http::STATUS_OK, SharingShare, array{}>|DataResponse<Http::STATUS_NOT_FOUND, string, array{}>
*
* 200: Share returned
* 400: Invalid arguments
* 404: Share not found
*/
#[PublicPage]
Expand All @@ -540,8 +548,6 @@ public function getShare(string $id, ?string $secret = null, array $arguments =
$this->dbConnection->rollBack();
throw $exception;
}
} catch (ShareInvalidException $shareInvalidException) {
return new DataResponse($shareInvalidException->getHint(), Http::STATUS_BAD_REQUEST);
} catch (ShareNotFoundException $shareNotFoundException) {
return new DataResponse($shareNotFoundException->getHint(), Http::STATUS_NOT_FOUND);
}
Expand Down
74 changes: 42 additions & 32 deletions apps/sharing/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -618,9 +618,9 @@
],
"parameters": [
{
"name": "recipientTypeClasses[]",
"name": "filterRecipientTypeClasses[]",
"in": "query",
"description": "Type class of recipients to filter by",
"description": "Type classes of recipients to filter by",
"schema": {
"type": "array",
"nullable": true,
Expand Down Expand Up @@ -662,6 +662,16 @@
"minimum": 0
}
},
{
"name": "id",
"in": "query",
"description": "If provided, recipients that are already part of the share will not be returned.",
"schema": {
"type": "string",
"nullable": true,
"default": null
}
},
{
"name": "OCS-APIRequest",
"in": "header",
Expand Down Expand Up @@ -737,6 +747,36 @@
}
}
},
"404": {
"description": "Share used for filtering existing recipients does not exist",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {
"type": "string"
}
}
}
}
}
}
}
},
"401": {
"description": "Current user is not logged in",
"content": {
Expand Down Expand Up @@ -3005,36 +3045,6 @@
}
}
},
"400": {
"description": "Invalid arguments",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {
"type": "string"
}
}
}
}
}
}
}
},
"404": {
"description": "Share not found",
"content": {
Expand Down
14 changes: 11 additions & 3 deletions apps/sharing/tests/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,18 @@ private function runCommand(ShareAccessContext $accessContext, string $class, ar
}

#[Override]
protected function searchRecipients(ShareAccessContext $accessContext, ?array $recipientTypeClasses, string $query, int $limit, int $offset): array {
protected function searchRecipients(ShareAccessContext $accessContext, ?array $filterRecipientTypeClasses, string $query, int $limit, int $offset, ?string $id = null): array {
// We don't have a command for this, so we just call the real manager to make the test pass.
/** @psalm-suppress ArgumentTypeCoercion */
return ShareRecipient::formatMultiple(Server::get(ISharingRegistry::class), Server::get(IFactory::class), Server::get(IURLGenerator::class), Server::get(IUserManager::class), $this->manager->searchRecipients($accessContext, $recipientTypeClasses, $query, $limit, $offset));
try {
$this->dbConnection->beginTransaction();
/** @psalm-suppress ArgumentTypeCoercion */
$shares = ShareRecipient::formatMultiple($this->registry, Server::get(IFactory::class), Server::get(IURLGenerator::class), Server::get(IUserManager::class), $this->manager->searchRecipients($accessContext, $filterRecipientTypeClasses, $query, $limit, $offset, $id));
$this->dbConnection->commit();
return $shares;
} catch (Exception $exception) {
$this->dbConnection->rollBack();
throw $exception;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw $exception;
throw $exception;

}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions apps/sharing/tests/Controller/ApiV1ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ private function executeRequest(ShareAccessContext $accessContext, Closure $clos
}

#[Override]
protected function searchRecipients(ShareAccessContext $accessContext, ?array $recipientTypeClasses, string $query, int $limit, int $offset): array {
protected function searchRecipients(ShareAccessContext $accessContext, ?array $filterRecipientTypeClasses, string $query, int $limit, int $offset, ?string $id = null): array {
/** @psalm-suppress ArgumentTypeCoercion */
return $this->executeRequest($accessContext, fn (ApiV1Controller $controller): DataResponse => $controller->searchRecipients($recipientTypeClasses, $query, $limit, $offset));
return $this->executeRequest($accessContext, fn (ApiV1Controller $controller): DataResponse => $controller->searchRecipients($filterRecipientTypeClasses, $query, $limit, $offset, $id));
}

#[Override]
Expand Down
23 changes: 19 additions & 4 deletions lib/private/Sharing/SharingManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ public function __construct(
}

#[\Override]
public function searchRecipients(ShareAccessContext $accessContext, ?array $recipientTypeClasses, string $query, int $limit, int $offset): array {
public function searchRecipients(ShareAccessContext $accessContext, ?array $filterRecipientTypeClasses, string $query, int $limit, int $offset, ?string $id = null): array {
$recipientTypes = $this->registry->getRecipientTypes();

if ($recipientTypeClasses !== null) {
if ($filterRecipientTypeClasses !== null) {
$filteredRecipientTypes = [];
foreach (array_unique($recipientTypeClasses) as $recipientTypeClass) {
foreach (array_unique($filterRecipientTypeClasses) as $recipientTypeClass) {
if (($recipientType = $recipientTypes[$recipientTypeClass] ?? null) === null) {
throw new RuntimeException('The recipient type is not registered: ' . $recipientTypeClass);
}
Expand All @@ -112,10 +112,25 @@ public function searchRecipients(ShareAccessContext $accessContext, ?array $reci
));
}

return array_merge(...array_map(
$results = array_merge(...array_map(
static fn (IShareRecipientTypeSearch $recipientType): array => $recipientType->searchRecipients($accessContext, $query, $limit, $offset),
$recipientTypes,
));

if ($id !== null) {
// Do not create a new access context with overridden checks, because it could leak the existence of shares and share recipients.
$share = $this->getShare($accessContext, $id);
$recipients = [];
foreach ($share->recipients as $recipient) {
$recipients[$recipient->class] ??= [];
$recipients[$recipient->class][$recipient->instance ?? ''] ??= [];
$recipients[$recipient->class][$recipient->instance ?? ''][$recipient->value] = true;
}

$results = array_values(array_filter($results, static fn (ShareRecipient $recipient): bool => !isset($recipients[$recipient->class][$recipient->instance ?? ''][$recipient->value])));
}

return $results;
}

#[\Override]
Expand Down
6 changes: 4 additions & 2 deletions lib/public/Sharing/ISharingManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ interface ISharingManager {
/**
* Search for recpients that can be added to a share.
*
* @param ?list<class-string<IShareRecipientType>> $recipientTypeClasses
* @param ?list<class-string<IShareRecipientType>> $filterRecipientTypeClasses
* @param positive-int $limit
* @param non-negative-int $offset
* @param ?string $id If provided, recipients that are already part of the share will not be returned.
* @return list<ShareRecipient>
* @throws ShareNotFoundException
* @since 35.0.0
*/
public function searchRecipients(ShareAccessContext $accessContext, ?array $recipientTypeClasses, string $query, int $limit, int $offset): array;
public function searchRecipients(ShareAccessContext $accessContext, ?array $filterRecipientTypeClasses, string $query, int $limit, int $offset, ?string $id = null): array;

/**
* Generate a new secret.
Expand Down
74 changes: 42 additions & 32 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -36514,9 +36514,9 @@
],
"parameters": [
{
"name": "recipientTypeClasses[]",
"name": "filterRecipientTypeClasses[]",
"in": "query",
"description": "Type class of recipients to filter by",
"description": "Type classes of recipients to filter by",
"schema": {
"type": "array",
"nullable": true,
Expand Down Expand Up @@ -36558,6 +36558,16 @@
"minimum": 0
}
},
{
"name": "id",
"in": "query",
"description": "If provided, recipients that are already part of the share will not be returned.",
"schema": {
"type": "string",
"nullable": true,
"default": null
}
},
{
"name": "OCS-APIRequest",
"in": "header",
Expand Down Expand Up @@ -36633,6 +36643,36 @@
}
}
},
"404": {
"description": "Share used for filtering existing recipients does not exist",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {
"type": "string"
}
}
}
}
}
}
}
},
"401": {
"description": "Current user is not logged in",
"content": {
Expand Down Expand Up @@ -38901,36 +38941,6 @@
}
}
},
"400": {
"description": "Invalid arguments",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {
"type": "string"
}
}
}
}
}
}
}
},
"404": {
"description": "Share not found",
"content": {
Expand Down
Loading
Loading