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.

AppsController.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Tom Needham <tom@owncloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Provisioning_API\Controller;
  29. use OC_App;
  30. use OCP\App\AppPathNotFoundException;
  31. use OCP\App\IAppManager;
  32. use OCP\AppFramework\Http\DataResponse;
  33. use OCP\AppFramework\OCS\OCSException;
  34. use OCP\AppFramework\OCSController;
  35. use OCP\IRequest;
  36. class AppsController extends OCSController {
  37. /** @var IAppManager */
  38. private $appManager;
  39. public function __construct(
  40. string $appName,
  41. IRequest $request,
  42. IAppManager $appManager
  43. ) {
  44. parent::__construct($appName, $request);
  45. $this->appManager = $appManager;
  46. }
  47. /**
  48. * @param string|null $filter
  49. * @return DataResponse
  50. * @throws OCSException
  51. */
  52. public function getApps(string $filter = null): DataResponse {
  53. $apps = (new OC_App())->listAllApps();
  54. $list = [];
  55. foreach ($apps as $app) {
  56. $list[] = $app['id'];
  57. }
  58. if ($filter) {
  59. switch ($filter) {
  60. case 'enabled':
  61. return new DataResponse(['apps' => \OC_App::getEnabledApps()]);
  62. break;
  63. case 'disabled':
  64. $enabled = OC_App::getEnabledApps();
  65. return new DataResponse(['apps' => array_diff($list, $enabled)]);
  66. break;
  67. default:
  68. // Invalid filter variable
  69. throw new OCSException('', 101);
  70. }
  71. } else {
  72. return new DataResponse(['apps' => $list]);
  73. }
  74. }
  75. /**
  76. * @param string $app
  77. * @return DataResponse
  78. * @throws OCSException
  79. */
  80. public function getAppInfo(string $app): DataResponse {
  81. $info = $this->appManager->getAppInfo($app);
  82. if (!is_null($info)) {
  83. return new DataResponse($info);
  84. }
  85. throw new OCSException('The request app was not found', OCSController::RESPOND_NOT_FOUND);
  86. }
  87. /**
  88. * @PasswordConfirmationRequired
  89. * @param string $app
  90. * @return DataResponse
  91. * @throws OCSException
  92. */
  93. public function enable(string $app): DataResponse {
  94. try {
  95. $this->appManager->enableApp($app);
  96. } catch (AppPathNotFoundException $e) {
  97. throw new OCSException('The request app was not found', OCSController::RESPOND_NOT_FOUND);
  98. }
  99. return new DataResponse();
  100. }
  101. /**
  102. * @PasswordConfirmationRequired
  103. * @param string $app
  104. * @return DataResponse
  105. */
  106. public function disable(string $app): DataResponse {
  107. $this->appManager->disableApp($app);
  108. return new DataResponse();
  109. }
  110. }