1
0

sharingcheckmiddleware.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Thomas Müller <thomas.mueller@tmit.eu>
  6. *
  7. * @copyright Copyright (c) 2015, ownCloud, Inc.
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\Files_Sharing\Middleware;
  24. use OCP\App\IAppManager;
  25. use OCP\AppFramework\Http\NotFoundResponse;
  26. use OCP\AppFramework\Middleware;
  27. use OCP\AppFramework\Http\TemplateResponse;
  28. use OCP\IConfig;
  29. /**
  30. * Checks whether the "sharing check" is enabled
  31. *
  32. * @package OCA\Files_Sharing\Middleware
  33. */
  34. class SharingCheckMiddleware extends Middleware {
  35. /** @var string */
  36. protected $appName;
  37. /** @var IConfig */
  38. protected $config;
  39. /** @var IAppManager */
  40. protected $appManager;
  41. /***
  42. * @param string $appName
  43. * @param IConfig $config
  44. * @param IAppManager $appManager
  45. */
  46. public function __construct($appName,
  47. IConfig $config,
  48. IAppManager $appManager) {
  49. $this->appName = $appName;
  50. $this->config = $config;
  51. $this->appManager = $appManager;
  52. }
  53. /**
  54. * Check if sharing is enabled before the controllers is executed
  55. */
  56. public function beforeController($controller, $methodName) {
  57. if(!$this->isSharingEnabled()) {
  58. throw new \Exception('Sharing is disabled.');
  59. }
  60. }
  61. /**
  62. * Return 404 page in case of an exception
  63. * @param \OCP\AppFramework\Controller $controller
  64. * @param string $methodName
  65. * @param \Exception $exception
  66. * @return TemplateResponse
  67. */
  68. public function afterException($controller, $methodName, \Exception $exception){
  69. return new NotFoundResponse();
  70. }
  71. /**
  72. * Check whether sharing is enabled
  73. * @return bool
  74. */
  75. private function isSharingEnabled() {
  76. // FIXME: This check is done here since the route is globally defined and not inside the files_sharing app
  77. // Check whether the sharing application is enabled
  78. if(!$this->appManager->isEnabledForUser($this->appName)) {
  79. return false;
  80. }
  81. // Check whether public sharing is enabled
  82. if($this->config->getAppValue('core', 'shareapi_allow_links', 'yes') !== 'yes') {
  83. return false;
  84. }
  85. return true;
  86. }
  87. }