AppsController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Tom Needham <tom@owncloud.com>
  11. * @author Kate Döen <kate.doeen@nextcloud.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;
  33. use OCP\AppFramework\Http\DataResponse;
  34. use OCP\AppFramework\OCS\OCSException;
  35. use OCP\AppFramework\OCSController;
  36. use OCP\IRequest;
  37. class AppsController extends OCSController {
  38. /** @var IAppManager */
  39. private $appManager;
  40. public function __construct(
  41. string $appName,
  42. IRequest $request,
  43. IAppManager $appManager
  44. ) {
  45. parent::__construct($appName, $request);
  46. $this->appManager = $appManager;
  47. }
  48. /**
  49. * Get a list of installed apps
  50. *
  51. * @param ?string $filter Filter for enabled or disabled apps
  52. * @return DataResponse<Http::STATUS_OK, array{apps: string[]}, array{}>
  53. * @throws OCSException
  54. *
  55. * 200: Installed apps returned
  56. */
  57. public function getApps(?string $filter = null): DataResponse {
  58. $apps = (new OC_App())->listAllApps();
  59. $list = [];
  60. foreach ($apps as $app) {
  61. $list[] = $app['id'];
  62. }
  63. /** @var string[] $list */
  64. if ($filter) {
  65. switch ($filter) {
  66. case 'enabled':
  67. return new DataResponse(['apps' => \OC_App::getEnabledApps()]);
  68. break;
  69. case 'disabled':
  70. $enabled = OC_App::getEnabledApps();
  71. return new DataResponse(['apps' => array_diff($list, $enabled)]);
  72. break;
  73. default:
  74. // Invalid filter variable
  75. throw new OCSException('', 101);
  76. }
  77. } else {
  78. return new DataResponse(['apps' => $list]);
  79. }
  80. }
  81. /**
  82. * Get the app info for an app
  83. *
  84. * @param string $app ID of the app
  85. * @return DataResponse<Http::STATUS_OK, array<string, ?mixed>, array{}>
  86. * @throws OCSException
  87. *
  88. * 200: App info returned
  89. */
  90. public function getAppInfo(string $app): DataResponse {
  91. $info = $this->appManager->getAppInfo($app);
  92. if (!is_null($info)) {
  93. return new DataResponse($info);
  94. }
  95. throw new OCSException('The request app was not found', OCSController::RESPOND_NOT_FOUND);
  96. }
  97. /**
  98. * @PasswordConfirmationRequired
  99. *
  100. * Enable an app
  101. *
  102. * @param string $app ID of the app
  103. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  104. * @throws OCSException
  105. *
  106. * 200: App enabled successfully
  107. */
  108. public function enable(string $app): DataResponse {
  109. try {
  110. $this->appManager->enableApp($app);
  111. } catch (AppPathNotFoundException $e) {
  112. throw new OCSException('The request app was not found', OCSController::RESPOND_NOT_FOUND);
  113. }
  114. return new DataResponse();
  115. }
  116. /**
  117. * @PasswordConfirmationRequired
  118. *
  119. * Disable an app
  120. *
  121. * @param string $app ID of the app
  122. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  123. *
  124. * 200: App disabled successfully
  125. */
  126. public function disable(string $app): DataResponse {
  127. $this->appManager->disableApp($app);
  128. return new DataResponse();
  129. }
  130. }