AppEnabledPlugin.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * @author Morris Jobke <hey@morrisjobke.de>
  4. * @author Robin Appelman <icewind@owncloud.com>
  5. * @author Thomas Müller <thomas.mueller@tmit.eu>
  6. * @author Vincent Petry <pvince81@owncloud.com>
  7. *
  8. * @copyright Copyright (c) 2016, ownCloud, Inc.
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\DAV\Connector\Sabre;
  25. use OCP\App\IAppManager;
  26. use Sabre\DAV\Exception\Forbidden;
  27. use Sabre\DAV\ServerPlugin;
  28. /**
  29. * Plugin to check if an app is enabled for the current user
  30. */
  31. class AppEnabledPlugin extends ServerPlugin {
  32. /**
  33. * Reference to main server object
  34. *
  35. * @var \Sabre\DAV\Server
  36. */
  37. private $server;
  38. /**
  39. * @var string
  40. */
  41. private $app;
  42. /**
  43. * @var \OCP\App\IAppManager
  44. */
  45. private $appManager;
  46. /**
  47. * @param string $app
  48. * @param \OCP\App\IAppManager $appManager
  49. */
  50. public function __construct($app, IAppManager $appManager) {
  51. $this->app = $app;
  52. $this->appManager = $appManager;
  53. }
  54. /**
  55. * This initializes the plugin.
  56. *
  57. * This function is called by \Sabre\DAV\Server, after
  58. * addPlugin is called.
  59. *
  60. * This method should set up the required event subscriptions.
  61. *
  62. * @param \Sabre\DAV\Server $server
  63. * @return void
  64. */
  65. public function initialize(\Sabre\DAV\Server $server) {
  66. $this->server = $server;
  67. $this->server->on('beforeMethod', array($this, 'checkAppEnabled'), 30);
  68. }
  69. /**
  70. * This method is called before any HTTP after auth and checks if the user has access to the app
  71. *
  72. * @throws \Sabre\DAV\Exception\Forbidden
  73. * @return bool
  74. */
  75. public function checkAppEnabled() {
  76. if (!$this->appManager->isEnabledForUser($this->app)) {
  77. throw new Forbidden();
  78. }
  79. }
  80. }