2018-10-16 17:48:16 +02:00
|
|
|
<?php
|
2019-12-03 19:57:53 +01:00
|
|
|
|
2018-10-16 17:48:16 +02:00
|
|
|
declare(strict_types=1);
|
2019-12-03 19:57:53 +01:00
|
|
|
|
2018-10-16 17:48:16 +02:00
|
|
|
/**
|
2024-05-27 10:08:53 +02:00
|
|
|
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2018-10-16 17:48:16 +02:00
|
|
|
*/
|
2023-06-04 23:20:35 +03:30
|
|
|
|
2018-10-16 17:48:16 +02:00
|
|
|
namespace OC\Core\Controller;
|
|
|
|
|
2019-03-28 09:26:38 +01:00
|
|
|
use Exception;
|
2024-05-26 12:51:24 +02:00
|
|
|
use OC\Core\ResponseDefinitions;
|
2018-10-16 17:48:16 +02:00
|
|
|
use OCP\AppFramework\Http;
|
2024-01-10 12:35:44 +01:00
|
|
|
use OCP\AppFramework\Http\Attribute\ApiRoute;
|
2024-07-25 13:24:59 +02:00
|
|
|
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
|
2018-10-16 17:48:16 +02:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
2019-11-22 20:52:10 +01:00
|
|
|
use OCP\AppFramework\OCSController;
|
2018-10-16 17:48:16 +02:00
|
|
|
use OCP\Collaboration\Resources\CollectionException;
|
|
|
|
use OCP\Collaboration\Resources\ICollection;
|
|
|
|
use OCP\Collaboration\Resources\IManager;
|
|
|
|
use OCP\Collaboration\Resources\IResource;
|
|
|
|
use OCP\Collaboration\Resources\ResourceException;
|
|
|
|
use OCP\IRequest;
|
2018-10-17 20:03:44 +02:00
|
|
|
use OCP\IUserSession;
|
2022-04-12 17:55:01 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
2018-10-16 17:48:16 +02:00
|
|
|
|
2023-03-15 17:29:32 +01:00
|
|
|
/**
|
2023-09-14 12:26:14 +02:00
|
|
|
* @psalm-import-type CoreResource from ResponseDefinitions
|
2023-03-15 17:29:32 +01:00
|
|
|
* @psalm-import-type CoreCollection from ResponseDefinitions
|
|
|
|
*/
|
2018-10-16 17:48:16 +02:00
|
|
|
class CollaborationResourcesController extends OCSController {
|
2023-06-05 16:45:21 +03:30
|
|
|
public function __construct(
|
|
|
|
string $appName,
|
|
|
|
IRequest $request,
|
|
|
|
private IManager $manager,
|
|
|
|
private IUserSession $userSession,
|
|
|
|
private LoggerInterface $logger,
|
|
|
|
) {
|
2018-10-16 17:48:16 +02:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $collectionId
|
|
|
|
* @return ICollection
|
|
|
|
* @throws CollectionException when the collection was not found for the user
|
|
|
|
*/
|
|
|
|
protected function getCollection(int $collectionId): ICollection {
|
2019-02-11 12:57:38 +01:00
|
|
|
$collection = $this->manager->getCollectionForUser($collectionId, $this->userSession->getUser());
|
2018-10-16 17:48:16 +02:00
|
|
|
|
2018-10-17 20:03:44 +02:00
|
|
|
if (!$collection->canAccess($this->userSession->getUser())) {
|
2018-10-16 17:48:16 +02:00
|
|
|
throw new CollectionException('Not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $collection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-03-15 17:29:32 +01:00
|
|
|
* Get a collection
|
|
|
|
*
|
|
|
|
* @param int $collectionId ID of the collection
|
2024-09-24 15:53:13 +02:00
|
|
|
* @return DataResponse<Http::STATUS_OK, CoreCollection, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, list<empty>, array{}>
|
2023-03-15 17:29:32 +01:00
|
|
|
*
|
|
|
|
* 200: Collection returned
|
|
|
|
* 404: Collection not found
|
2018-10-16 17:48:16 +02:00
|
|
|
*/
|
2024-07-25 13:24:59 +02:00
|
|
|
#[NoAdminRequired]
|
2024-01-10 12:35:44 +01:00
|
|
|
#[ApiRoute(verb: 'GET', url: '/resources/collections/{collectionId}', root: '/collaboration')]
|
2018-10-16 17:48:16 +02:00
|
|
|
public function listCollection(int $collectionId): DataResponse {
|
|
|
|
try {
|
|
|
|
$collection = $this->getCollection($collectionId);
|
|
|
|
} catch (CollectionException $e) {
|
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
2019-03-28 09:26:38 +01:00
|
|
|
return $this->respondCollection($collection);
|
2018-10-16 17:48:16 +02:00
|
|
|
}
|
|
|
|
|
2019-01-29 12:23:14 +01:00
|
|
|
/**
|
2023-03-15 17:29:32 +01:00
|
|
|
* Search for collections
|
|
|
|
*
|
|
|
|
* @param string $filter Filter collections
|
2024-09-24 15:53:13 +02:00
|
|
|
* @return DataResponse<Http::STATUS_OK, list<CoreCollection>, array{}>|DataResponse<Http::STATUS_NOT_FOUND, list<empty>, array{}>
|
2023-03-15 17:29:32 +01:00
|
|
|
*
|
|
|
|
* 200: Collections returned
|
|
|
|
* 404: Collection not found
|
2019-01-29 12:23:14 +01:00
|
|
|
*/
|
2024-07-25 13:24:59 +02:00
|
|
|
#[NoAdminRequired]
|
2024-01-10 12:35:44 +01:00
|
|
|
#[ApiRoute(verb: 'GET', url: '/resources/collections/search/{filter}', root: '/collaboration')]
|
2019-01-29 12:23:14 +01:00
|
|
|
public function searchCollections(string $filter): DataResponse {
|
|
|
|
try {
|
|
|
|
$collections = $this->manager->searchCollections($this->userSession->getUser(), $filter);
|
|
|
|
} catch (CollectionException $e) {
|
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
2019-03-28 09:26:38 +01:00
|
|
|
return new DataResponse($this->prepareCollections($collections));
|
2019-01-29 12:23:14 +01:00
|
|
|
}
|
|
|
|
|
2018-10-16 17:48:16 +02:00
|
|
|
/**
|
2023-03-15 17:29:32 +01:00
|
|
|
* Add a resource to a collection
|
|
|
|
*
|
|
|
|
* @param int $collectionId ID of the collection
|
|
|
|
* @param string $resourceType Name of the resource
|
|
|
|
* @param string $resourceId ID of the resource
|
2024-09-24 15:53:13 +02:00
|
|
|
* @return DataResponse<Http::STATUS_OK, CoreCollection, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, list<empty>, array{}>
|
2023-03-15 17:29:32 +01:00
|
|
|
*
|
|
|
|
* 200: Collection returned
|
|
|
|
* 404: Collection not found or resource inaccessible
|
2018-10-16 17:48:16 +02:00
|
|
|
*/
|
2024-07-25 13:24:59 +02:00
|
|
|
#[NoAdminRequired]
|
2024-01-10 12:35:44 +01:00
|
|
|
#[ApiRoute(verb: 'POST', url: '/resources/collections/{collectionId}', root: '/collaboration')]
|
2018-10-16 17:48:16 +02:00
|
|
|
public function addResource(int $collectionId, string $resourceType, string $resourceId): DataResponse {
|
|
|
|
try {
|
|
|
|
$collection = $this->getCollection($collectionId);
|
|
|
|
} catch (CollectionException $e) {
|
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
2019-02-11 12:57:38 +01:00
|
|
|
$resource = $this->manager->createResource($resourceType, $resourceId);
|
|
|
|
|
|
|
|
if (!$resource->canAccess($this->userSession->getUser())) {
|
2018-10-16 17:48:16 +02:00
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$collection->addResource($resource);
|
|
|
|
} catch (ResourceException $e) {
|
|
|
|
}
|
|
|
|
|
2019-03-28 09:26:38 +01:00
|
|
|
return $this->respondCollection($collection);
|
2018-10-16 17:48:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-03-15 17:29:32 +01:00
|
|
|
* Remove a resource from a collection
|
|
|
|
*
|
|
|
|
* @param int $collectionId ID of the collection
|
|
|
|
* @param string $resourceType Name of the resource
|
|
|
|
* @param string $resourceId ID of the resource
|
2024-09-24 15:53:13 +02:00
|
|
|
* @return DataResponse<Http::STATUS_OK, CoreCollection, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, list<empty>, array{}>
|
2023-03-15 17:29:32 +01:00
|
|
|
*
|
|
|
|
* 200: Collection returned
|
|
|
|
* 404: Collection or resource not found
|
2018-10-16 17:48:16 +02:00
|
|
|
*/
|
2024-07-25 13:24:59 +02:00
|
|
|
#[NoAdminRequired]
|
2024-01-10 12:35:44 +01:00
|
|
|
#[ApiRoute(verb: 'DELETE', url: '/resources/collections/{collectionId}', root: '/collaboration')]
|
2018-10-16 17:48:16 +02:00
|
|
|
public function removeResource(int $collectionId, string $resourceType, string $resourceId): DataResponse {
|
|
|
|
try {
|
|
|
|
$collection = $this->getCollection($collectionId);
|
|
|
|
} catch (CollectionException $e) {
|
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-02-11 12:57:38 +01:00
|
|
|
$resource = $this->manager->getResourceForUser($resourceType, $resourceId, $this->userSession->getUser());
|
2018-10-16 17:48:16 +02:00
|
|
|
} catch (CollectionException $e) {
|
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
$collection->removeResource($resource);
|
|
|
|
|
2019-03-28 09:26:38 +01:00
|
|
|
return $this->respondCollection($collection);
|
2018-10-16 17:48:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-03-15 17:29:32 +01:00
|
|
|
* Get collections by resource
|
|
|
|
*
|
|
|
|
* @param string $resourceType Type of the resource
|
|
|
|
* @param string $resourceId ID of the resource
|
2024-09-24 15:53:13 +02:00
|
|
|
* @return DataResponse<Http::STATUS_OK, list<CoreCollection>, array{}>|DataResponse<Http::STATUS_NOT_FOUND, list<empty>, array{}>
|
2023-03-15 17:29:32 +01:00
|
|
|
*
|
|
|
|
* 200: Collections returned
|
|
|
|
* 404: Resource not accessible
|
2018-10-16 17:48:16 +02:00
|
|
|
*/
|
2024-07-25 13:24:59 +02:00
|
|
|
#[NoAdminRequired]
|
2024-01-10 12:35:44 +01:00
|
|
|
#[ApiRoute(verb: 'GET', url: '/resources/{resourceType}/{resourceId}', root: '/collaboration')]
|
2018-10-16 17:48:16 +02:00
|
|
|
public function getCollectionsByResource(string $resourceType, string $resourceId): DataResponse {
|
|
|
|
try {
|
2019-02-11 12:57:38 +01:00
|
|
|
$resource = $this->manager->getResourceForUser($resourceType, $resourceId, $this->userSession->getUser());
|
|
|
|
} catch (ResourceException $e) {
|
2019-03-18 15:13:24 +01:00
|
|
|
$resource = $this->manager->createResource($resourceType, $resourceId);
|
2018-10-16 17:48:16 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 20:03:44 +02:00
|
|
|
if (!$resource->canAccess($this->userSession->getUser())) {
|
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
2019-03-28 09:26:38 +01:00
|
|
|
return new DataResponse($this->prepareCollections($resource->getCollections()));
|
2018-10-16 17:48:16 +02:00
|
|
|
}
|
|
|
|
|
2018-10-18 13:00:25 +02:00
|
|
|
/**
|
2023-03-15 17:29:32 +01:00
|
|
|
* Create a collection for a resource
|
|
|
|
*
|
|
|
|
* @param string $baseResourceType Type of the base resource
|
|
|
|
* @param string $baseResourceId ID of the base resource
|
|
|
|
* @param string $name Name of the collection
|
2024-09-24 15:53:13 +02:00
|
|
|
* @return DataResponse<Http::STATUS_OK, CoreCollection, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, list<empty>, array{}>
|
2023-03-15 17:29:32 +01:00
|
|
|
*
|
|
|
|
* 200: Collection returned
|
|
|
|
* 400: Creating collection is not possible
|
|
|
|
* 404: Resource inaccessible
|
2018-10-18 13:00:25 +02:00
|
|
|
*/
|
2024-07-25 13:24:59 +02:00
|
|
|
#[NoAdminRequired]
|
2024-01-10 12:35:44 +01:00
|
|
|
#[ApiRoute(verb: 'POST', url: '/resources/{baseResourceType}/{baseResourceId}', root: '/collaboration')]
|
2019-01-22 13:11:49 +01:00
|
|
|
public function createCollectionOnResource(string $baseResourceType, string $baseResourceId, string $name): DataResponse {
|
2018-10-24 14:25:35 +02:00
|
|
|
if (!isset($name[0]) || isset($name[64])) {
|
|
|
|
return new DataResponse([], Http::STATUS_BAD_REQUEST);
|
|
|
|
}
|
|
|
|
|
2018-10-18 13:00:25 +02:00
|
|
|
try {
|
2019-02-11 12:57:38 +01:00
|
|
|
$resource = $this->manager->createResource($baseResourceType, $baseResourceId);
|
2018-10-18 13:00:25 +02:00
|
|
|
} catch (CollectionException $e) {
|
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
2018-10-24 14:25:35 +02:00
|
|
|
if (!$resource->canAccess($this->userSession->getUser())) {
|
2018-10-18 13:00:25 +02:00
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
2018-10-24 14:25:35 +02:00
|
|
|
$collection = $this->manager->newCollection($name);
|
2018-10-18 13:00:25 +02:00
|
|
|
$collection->addResource($resource);
|
|
|
|
|
2019-03-28 09:26:38 +01:00
|
|
|
return $this->respondCollection($collection);
|
2018-10-18 13:00:25 +02:00
|
|
|
}
|
|
|
|
|
2019-01-29 12:23:14 +01:00
|
|
|
/**
|
2023-03-15 17:29:32 +01:00
|
|
|
* Rename a collection
|
|
|
|
*
|
|
|
|
* @param int $collectionId ID of the collection
|
|
|
|
* @param string $collectionName New name
|
2024-09-24 15:53:13 +02:00
|
|
|
* @return DataResponse<Http::STATUS_OK, CoreCollection, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, list<empty>, array{}>
|
2023-03-15 17:29:32 +01:00
|
|
|
*
|
|
|
|
* 200: Collection returned
|
|
|
|
* 404: Collection not found
|
2019-01-29 12:23:14 +01:00
|
|
|
*/
|
2024-07-25 13:24:59 +02:00
|
|
|
#[NoAdminRequired]
|
2024-01-10 12:35:44 +01:00
|
|
|
#[ApiRoute(verb: 'PUT', url: '/resources/collections/{collectionId}', root: '/collaboration')]
|
2019-01-29 12:23:14 +01:00
|
|
|
public function renameCollection(int $collectionId, string $collectionName): DataResponse {
|
|
|
|
try {
|
2019-02-11 12:57:38 +01:00
|
|
|
$collection = $this->getCollection($collectionId);
|
2019-01-29 12:23:14 +01:00
|
|
|
} catch (CollectionException $exception) {
|
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
2019-02-11 12:57:38 +01:00
|
|
|
$collection->setName($collectionName);
|
|
|
|
|
2019-03-28 09:26:38 +01:00
|
|
|
return $this->respondCollection($collection);
|
|
|
|
}
|
|
|
|
|
2023-03-15 17:29:32 +01:00
|
|
|
/**
|
2024-09-24 15:53:13 +02:00
|
|
|
* @return DataResponse<Http::STATUS_OK, CoreCollection, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, list<empty>, array{}>
|
2023-03-15 17:29:32 +01:00
|
|
|
*/
|
2019-03-28 09:26:38 +01:00
|
|
|
protected function respondCollection(ICollection $collection): DataResponse {
|
|
|
|
try {
|
|
|
|
return new DataResponse($this->prepareCollection($collection));
|
|
|
|
} catch (CollectionException $e) {
|
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
} catch (Exception $e) {
|
2022-04-12 17:55:01 +02:00
|
|
|
$this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => 'core']);
|
2019-03-28 09:26:38 +01:00
|
|
|
return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-15 17:29:32 +01:00
|
|
|
/**
|
2024-09-24 15:53:13 +02:00
|
|
|
* @return list<CoreCollection>
|
2023-03-15 17:29:32 +01:00
|
|
|
*/
|
2019-03-28 09:26:38 +01:00
|
|
|
protected function prepareCollections(array $collections): array {
|
|
|
|
$result = [];
|
|
|
|
|
|
|
|
foreach ($collections as $collection) {
|
|
|
|
try {
|
|
|
|
$result[] = $this->prepareCollection($collection);
|
|
|
|
} catch (CollectionException $e) {
|
|
|
|
} catch (Exception $e) {
|
2022-04-12 17:55:01 +02:00
|
|
|
$this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => 'core']);
|
2019-03-28 09:26:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2019-01-29 12:23:14 +01:00
|
|
|
}
|
|
|
|
|
2023-03-15 17:29:32 +01:00
|
|
|
/**
|
|
|
|
* @return CoreCollection
|
|
|
|
*/
|
2018-10-16 17:48:16 +02:00
|
|
|
protected function prepareCollection(ICollection $collection): array {
|
2019-02-22 09:47:36 +01:00
|
|
|
if (!$collection->canAccess($this->userSession->getUser())) {
|
2019-03-28 09:26:38 +01:00
|
|
|
throw new CollectionException('Can not access collection');
|
2019-02-22 09:47:36 +01:00
|
|
|
}
|
|
|
|
|
2018-10-18 13:00:25 +02:00
|
|
|
return [
|
|
|
|
'id' => $collection->getId(),
|
2018-10-24 14:25:35 +02:00
|
|
|
'name' => $collection->getName(),
|
2019-03-28 09:26:38 +01:00
|
|
|
'resources' => $this->prepareResources($collection->getResources()),
|
2018-10-18 13:00:25 +02:00
|
|
|
];
|
2018-10-16 17:48:16 +02:00
|
|
|
}
|
|
|
|
|
2023-03-15 17:29:32 +01:00
|
|
|
/**
|
2024-09-24 15:53:13 +02:00
|
|
|
* @return list<CoreResource>
|
2023-03-15 17:29:32 +01:00
|
|
|
*/
|
|
|
|
protected function prepareResources(array $resources): array {
|
2019-03-28 09:26:38 +01:00
|
|
|
$result = [];
|
|
|
|
|
|
|
|
foreach ($resources as $resource) {
|
|
|
|
try {
|
|
|
|
$result[] = $this->prepareResource($resource);
|
|
|
|
} catch (ResourceException $e) {
|
|
|
|
} catch (Exception $e) {
|
2022-04-12 17:55:01 +02:00
|
|
|
$this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => 'core']);
|
2019-03-28 09:26:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2023-03-15 17:29:32 +01:00
|
|
|
/**
|
2023-09-14 12:26:14 +02:00
|
|
|
* @return CoreResource
|
2023-03-15 17:29:32 +01:00
|
|
|
*/
|
2019-03-28 09:26:38 +01:00
|
|
|
protected function prepareResource(IResource $resource): array {
|
2019-01-23 16:35:35 +01:00
|
|
|
if (!$resource->canAccess($this->userSession->getUser())) {
|
2019-03-28 09:26:38 +01:00
|
|
|
throw new ResourceException('Can not access resource');
|
2019-01-23 16:35:35 +01:00
|
|
|
}
|
|
|
|
|
2019-03-14 12:30:34 +01:00
|
|
|
return $resource->getRichObject();
|
2018-10-16 17:48:16 +02:00
|
|
|
}
|
|
|
|
}
|