AppsController.php 2.9 KB

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