You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AppEnabledPlugin.php 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Georg Ehrke <oc.list@georgehrke.com>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  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:*', [$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. }