AppEnabledPlugin.php 2.2 KB

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