1
0

AppsController.php 3.8 KB

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