AppsController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCA\Provisioning_API\Controller;
  9. use OC_App;
  10. use OCP\App\AppPathNotFoundException;
  11. use OCP\App\IAppManager;
  12. use OCP\AppFramework\Http;
  13. use OCP\AppFramework\Http\DataResponse;
  14. use OCP\AppFramework\OCS\OCSException;
  15. use OCP\AppFramework\OCSController;
  16. use OCP\IRequest;
  17. class AppsController extends OCSController {
  18. /** @var IAppManager */
  19. private $appManager;
  20. public function __construct(
  21. string $appName,
  22. IRequest $request,
  23. IAppManager $appManager
  24. ) {
  25. parent::__construct($appName, $request);
  26. $this->appManager = $appManager;
  27. }
  28. /**
  29. * Get a list of installed apps
  30. *
  31. * @param ?string $filter Filter for enabled or disabled apps
  32. * @return DataResponse<Http::STATUS_OK, array{apps: string[]}, array{}>
  33. * @throws OCSException
  34. *
  35. * 200: Installed apps returned
  36. */
  37. public function getApps(?string $filter = null): DataResponse {
  38. $apps = (new OC_App())->listAllApps();
  39. $list = [];
  40. foreach ($apps as $app) {
  41. $list[] = $app['id'];
  42. }
  43. /** @var string[] $list */
  44. if ($filter) {
  45. switch ($filter) {
  46. case 'enabled':
  47. return new DataResponse(['apps' => \OC_App::getEnabledApps()]);
  48. break;
  49. case 'disabled':
  50. $enabled = OC_App::getEnabledApps();
  51. return new DataResponse(['apps' => array_diff($list, $enabled)]);
  52. break;
  53. default:
  54. // Invalid filter variable
  55. throw new OCSException('', 101);
  56. }
  57. } else {
  58. return new DataResponse(['apps' => $list]);
  59. }
  60. }
  61. /**
  62. * Get the app info for an app
  63. *
  64. * @param string $app ID of the app
  65. * @return DataResponse<Http::STATUS_OK, array<string, ?mixed>, array{}>
  66. * @throws OCSException
  67. *
  68. * 200: App info returned
  69. */
  70. public function getAppInfo(string $app): DataResponse {
  71. $info = $this->appManager->getAppInfo($app);
  72. if (!is_null($info)) {
  73. return new DataResponse($info);
  74. }
  75. throw new OCSException('The request app was not found', OCSController::RESPOND_NOT_FOUND);
  76. }
  77. /**
  78. * @PasswordConfirmationRequired
  79. *
  80. * Enable an app
  81. *
  82. * @param string $app ID of the app
  83. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  84. * @throws OCSException
  85. *
  86. * 200: App enabled successfully
  87. */
  88. public function enable(string $app): DataResponse {
  89. try {
  90. $this->appManager->enableApp($app);
  91. } catch (AppPathNotFoundException $e) {
  92. throw new OCSException('The request app was not found', OCSController::RESPOND_NOT_FOUND);
  93. }
  94. return new DataResponse();
  95. }
  96. /**
  97. * @PasswordConfirmationRequired
  98. *
  99. * Disable an app
  100. *
  101. * @param string $app ID of the app
  102. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  103. *
  104. * 200: App disabled successfully
  105. */
  106. public function disable(string $app): DataResponse {
  107. $this->appManager->disableApp($app);
  108. return new DataResponse();
  109. }
  110. }