2014-10-15 11:58:44 +02:00
|
|
|
<?php
|
2021-03-02 20:36:04 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
2014-10-15 11:58:44 +02:00
|
|
|
/**
|
2024-06-06 19:48:28 +02:00
|
|
|
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2014-10-15 11:58:44 +02:00
|
|
|
*/
|
|
|
|
namespace OCA\Files_Sharing\Middleware;
|
|
|
|
|
2017-01-04 13:33:45 +01:00
|
|
|
use OCA\Files_Sharing\Controller\ExternalSharesController;
|
2019-11-22 20:52:10 +01:00
|
|
|
use OCA\Files_Sharing\Exceptions\S2SException;
|
2015-01-14 13:56:49 +01:00
|
|
|
use OCP\App\IAppManager;
|
2017-07-26 09:03:04 +02:00
|
|
|
use OCP\AppFramework\Controller;
|
2019-11-22 20:52:10 +01:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2015-03-24 11:21:58 +01:00
|
|
|
use OCP\AppFramework\Http\NotFoundResponse;
|
2021-03-02 20:36:04 +01:00
|
|
|
use OCP\AppFramework\Http\Response;
|
2015-01-14 13:56:49 +01:00
|
|
|
use OCP\AppFramework\Middleware;
|
2019-11-22 20:52:10 +01:00
|
|
|
use OCP\AppFramework\Utility\IControllerMethodReflector;
|
2015-09-30 13:32:20 +02:00
|
|
|
use OCP\Files\NotFoundException;
|
2015-01-14 13:56:49 +01:00
|
|
|
use OCP\IConfig;
|
2017-01-04 13:33:45 +01:00
|
|
|
use OCP\IRequest;
|
2019-11-22 20:52:10 +01:00
|
|
|
use OCP\Share\IManager;
|
2014-10-15 11:58:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the "sharing check" is enabled
|
|
|
|
*
|
|
|
|
* @package OCA\Files_Sharing\Middleware
|
|
|
|
*/
|
|
|
|
class SharingCheckMiddleware extends Middleware {
|
|
|
|
|
2024-10-18 12:04:22 +02:00
|
|
|
public function __construct(
|
|
|
|
protected string $appName,
|
|
|
|
protected IConfig $config,
|
|
|
|
protected IAppManager $appManager,
|
|
|
|
protected IControllerMethodReflector $reflector,
|
|
|
|
protected IManager $shareManager,
|
|
|
|
protected IRequest $request,
|
2015-10-02 09:57:33 +02:00
|
|
|
) {
|
2014-10-15 11:58:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if sharing is enabled before the controllers is executed
|
2015-09-30 13:32:20 +02:00
|
|
|
*
|
2017-07-26 09:03:04 +02:00
|
|
|
* @param Controller $controller
|
2015-09-30 13:32:20 +02:00
|
|
|
* @param string $methodName
|
|
|
|
* @throws NotFoundException
|
2017-01-04 13:33:45 +01:00
|
|
|
* @throws S2SException
|
2014-10-15 11:58:44 +02:00
|
|
|
*/
|
2021-03-02 20:36:04 +01:00
|
|
|
public function beforeController($controller, $methodName): void {
|
2014-10-15 11:58:44 +02:00
|
|
|
if (!$this->isSharingEnabled()) {
|
2015-09-30 13:32:20 +02:00
|
|
|
throw new NotFoundException('Sharing is disabled.');
|
2014-10-15 11:58:44 +02:00
|
|
|
}
|
2015-09-30 21:35:52 +02:00
|
|
|
|
2017-01-04 13:33:45 +01:00
|
|
|
if ($controller instanceof ExternalSharesController &&
|
2015-10-02 09:57:33 +02:00
|
|
|
!$this->externalSharesChecks()) {
|
|
|
|
throw new S2SException('Federated sharing not allowed');
|
2015-09-30 21:35:52 +02:00
|
|
|
}
|
2014-10-15 11:58:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-30 13:32:20 +02:00
|
|
|
* Return 404 page in case of a not found exception
|
|
|
|
*
|
2017-07-26 09:03:04 +02:00
|
|
|
* @param Controller $controller
|
2014-10-15 11:58:44 +02:00
|
|
|
* @param string $methodName
|
|
|
|
* @param \Exception $exception
|
2021-03-02 20:36:04 +01:00
|
|
|
* @return Response
|
2015-09-30 13:32:20 +02:00
|
|
|
* @throws \Exception
|
2014-10-15 11:58:44 +02:00
|
|
|
*/
|
2021-03-02 20:36:04 +01:00
|
|
|
public function afterException($controller, $methodName, \Exception $exception): Response {
|
2018-01-25 23:16:13 +01:00
|
|
|
if (is_a($exception, NotFoundException::class)) {
|
2015-09-30 13:32:20 +02:00
|
|
|
return new NotFoundResponse();
|
|
|
|
}
|
|
|
|
|
2018-01-25 23:16:13 +01:00
|
|
|
if (is_a($exception, S2SException::class)) {
|
2015-10-02 09:57:33 +02:00
|
|
|
return new JSONResponse($exception->getMessage(), 405);
|
|
|
|
}
|
|
|
|
|
2015-09-30 13:32:20 +02:00
|
|
|
throw $exception;
|
2014-10-15 11:58:44 +02:00
|
|
|
}
|
|
|
|
|
2015-10-02 09:57:33 +02:00
|
|
|
/**
|
|
|
|
* Checks for externalshares controller
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-03-02 20:36:04 +01:00
|
|
|
private function externalSharesChecks(): bool {
|
2015-10-02 09:57:33 +02:00
|
|
|
if (!$this->reflector->hasAnnotation('NoIncomingFederatedSharingRequired') &&
|
|
|
|
$this->config->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes') !== 'yes') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->reflector->hasAnnotation('NoOutgoingFederatedSharingRequired') &&
|
2020-04-09 09:22:29 +02:00
|
|
|
$this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') !== 'yes') {
|
2015-10-02 09:57:33 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-15 11:58:44 +02:00
|
|
|
/**
|
|
|
|
* Check whether sharing is enabled
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-03-02 20:36:04 +01:00
|
|
|
private function isSharingEnabled(): bool {
|
2014-10-15 11:58:44 +02:00
|
|
|
// FIXME: This check is done here since the route is globally defined and not inside the files_sharing app
|
|
|
|
// Check whether the sharing application is enabled
|
2015-01-14 13:56:49 +01:00
|
|
|
if (!$this->appManager->isEnabledForUser($this->appName)) {
|
2014-10-15 11:58:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-30 21:35:52 +02:00
|
|
|
return true;
|
|
|
|
}
|
2014-10-15 11:58:44 +02:00
|
|
|
}
|