2014-10-15 05:58:44 -04:00
|
|
|
<?php
|
2021-03-02 14:36:04 -05:00
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2014-10-15 05:58:44 -04:00
|
|
|
/**
|
2024-06-06 13:48:28 -04: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 05:58:44 -04:00
|
|
|
*/
|
|
|
|
|
namespace OCA\Files_Sharing\Middleware;
|
|
|
|
|
|
2017-01-04 07:33:45 -05:00
|
|
|
use OCA\Files_Sharing\Controller\ExternalSharesController;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCA\Files_Sharing\Exceptions\S2SException;
|
2015-01-14 07:56:49 -05:00
|
|
|
use OCP\App\IAppManager;
|
2017-07-26 03:03:04 -04:00
|
|
|
use OCP\AppFramework\Controller;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2015-03-24 06:21:58 -04:00
|
|
|
use OCP\AppFramework\Http\NotFoundResponse;
|
2021-03-02 14:36:04 -05:00
|
|
|
use OCP\AppFramework\Http\Response;
|
2015-01-14 07:56:49 -05:00
|
|
|
use OCP\AppFramework\Middleware;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\AppFramework\Utility\IControllerMethodReflector;
|
2015-09-30 07:32:20 -04:00
|
|
|
use OCP\Files\NotFoundException;
|
2015-01-14 07:56:49 -05:00
|
|
|
use OCP\IConfig;
|
2017-01-04 07:33:45 -05:00
|
|
|
use OCP\IRequest;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\Share\IManager;
|
2014-10-15 05:58:44 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks whether the "sharing check" is enabled
|
|
|
|
|
*
|
|
|
|
|
* @package OCA\Files_Sharing\Middleware
|
|
|
|
|
*/
|
|
|
|
|
class SharingCheckMiddleware extends Middleware {
|
|
|
|
|
|
2024-10-18 06:04:22 -04: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 03:57:33 -04:00
|
|
|
) {
|
2014-10-15 05:58:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if sharing is enabled before the controllers is executed
|
2015-09-30 07:32:20 -04:00
|
|
|
*
|
2017-07-26 03:03:04 -04:00
|
|
|
* @param Controller $controller
|
2015-09-30 07:32:20 -04:00
|
|
|
* @param string $methodName
|
|
|
|
|
* @throws NotFoundException
|
2017-01-04 07:33:45 -05:00
|
|
|
* @throws S2SException
|
2014-10-15 05:58:44 -04:00
|
|
|
*/
|
2021-03-02 14:36:04 -05:00
|
|
|
public function beforeController($controller, $methodName): void {
|
2014-10-15 05:58:44 -04:00
|
|
|
if (!$this->isSharingEnabled()) {
|
2015-09-30 07:32:20 -04:00
|
|
|
throw new NotFoundException('Sharing is disabled.');
|
2014-10-15 05:58:44 -04:00
|
|
|
}
|
2015-09-30 15:35:52 -04:00
|
|
|
|
2017-01-04 07:33:45 -05:00
|
|
|
if ($controller instanceof ExternalSharesController
|
2015-10-02 03:57:33 -04:00
|
|
|
&& !$this->externalSharesChecks()) {
|
|
|
|
|
throw new S2SException('Federated sharing not allowed');
|
2015-09-30 15:35:52 -04:00
|
|
|
}
|
2014-10-15 05:58:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-09-30 07:32:20 -04:00
|
|
|
* Return 404 page in case of a not found exception
|
|
|
|
|
*
|
2017-07-26 03:03:04 -04:00
|
|
|
* @param Controller $controller
|
2014-10-15 05:58:44 -04:00
|
|
|
* @param string $methodName
|
|
|
|
|
* @param \Exception $exception
|
2021-03-02 14:36:04 -05:00
|
|
|
* @return Response
|
2015-09-30 07:32:20 -04:00
|
|
|
* @throws \Exception
|
2014-10-15 05:58:44 -04:00
|
|
|
*/
|
2021-03-02 14:36:04 -05:00
|
|
|
public function afterException($controller, $methodName, \Exception $exception): Response {
|
2018-01-25 17:16:13 -05:00
|
|
|
if (is_a($exception, NotFoundException::class)) {
|
2015-09-30 07:32:20 -04:00
|
|
|
return new NotFoundResponse();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-25 17:16:13 -05:00
|
|
|
if (is_a($exception, S2SException::class)) {
|
2015-10-02 03:57:33 -04:00
|
|
|
return new JSONResponse($exception->getMessage(), 405);
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-30 07:32:20 -04:00
|
|
|
throw $exception;
|
2014-10-15 05:58:44 -04:00
|
|
|
}
|
|
|
|
|
|
2015-10-02 03:57:33 -04:00
|
|
|
/**
|
|
|
|
|
* Checks for externalshares controller
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2021-03-02 14:36:04 -05:00
|
|
|
private function externalSharesChecks(): bool {
|
2015-10-02 03:57:33 -04: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 03:22:29 -04:00
|
|
|
&& $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') !== 'yes') {
|
2015-10-02 03:57:33 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-15 05:58:44 -04:00
|
|
|
/**
|
|
|
|
|
* Check whether sharing is enabled
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2021-03-02 14:36:04 -05:00
|
|
|
private function isSharingEnabled(): bool {
|
2014-10-15 05:58:44 -04: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 07:56:49 -05:00
|
|
|
if (!$this->appManager->isEnabledForUser($this->appName)) {
|
2014-10-15 05:58:44 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-30 15:35:52 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
2014-10-15 05:58:44 -04:00
|
|
|
}
|