AppsController.php 3.0 KB

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