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 Vincent Petry <pvince81@owncloud.com>
  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 OC\Connector\Sabre;
  24. use OCP\App\IAppManager;
  25. use Sabre\DAV\Exception\Forbidden;
  26. use Sabre\DAV\ServerPlugin;
  27. /**
  28. * Plugin to check if an app is enabled for the current user
  29. */
  30. class AppEnabledPlugin extends ServerPlugin {
  31. /**
  32. * Reference to main server object
  33. *
  34. * @var \Sabre\DAV\Server
  35. */
  36. private $server;
  37. /**
  38. * @var string
  39. */
  40. private $app;
  41. /**
  42. * @var \OCP\App\IAppManager
  43. */
  44. private $appManager;
  45. /**
  46. * @param string $app
  47. * @param \OCP\App\IAppManager $appManager
  48. */
  49. public function __construct($app, IAppManager $appManager) {
  50. $this->app = $app;
  51. $this->appManager = $appManager;
  52. }
  53. /**
  54. * This initializes the plugin.
  55. *
  56. * This function is called by \Sabre\DAV\Server, after
  57. * addPlugin is called.
  58. *
  59. * This method should set up the required event subscriptions.
  60. *
  61. * @param \Sabre\DAV\Server $server
  62. * @return void
  63. */
  64. public function initialize(\Sabre\DAV\Server $server) {
  65. $this->server = $server;
  66. $this->server->on('beforeMethod', array($this, 'checkAppEnabled'), 30);
  67. }
  68. /**
  69. * This method is called before any HTTP after auth and checks if the user has access to the app
  70. *
  71. * @throws \Sabre\DAV\Exception\Forbidden
  72. * @return bool
  73. */
  74. public function checkAppEnabled() {
  75. if (!$this->appManager->isEnabledForUser($this->app)) {
  76. throw new Forbidden();
  77. }
  78. }
  79. }