AppsController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Provisioning_API\Controller;
  28. use OC_App;
  29. use OCP\App\AppPathNotFoundException;
  30. use OCP\App\IAppManager;
  31. use OCP\AppFramework\Http\DataResponse;
  32. use OCP\AppFramework\OCS\OCSException;
  33. use OCP\AppFramework\OCSController;
  34. use OCP\IRequest;
  35. class AppsController extends OCSController {
  36. /** @var IAppManager */
  37. private $appManager;
  38. public function __construct(
  39. string $appName,
  40. IRequest $request,
  41. IAppManager $appManager
  42. ) {
  43. parent::__construct($appName, $request);
  44. $this->appManager = $appManager;
  45. }
  46. /**
  47. * @param string|null $filter
  48. * @return DataResponse
  49. * @throws OCSException
  50. */
  51. public function getApps(string $filter = null): DataResponse {
  52. $apps = (new OC_App())->listAllApps();
  53. $list = [];
  54. foreach ($apps as $app) {
  55. $list[] = $app['id'];
  56. }
  57. if ($filter) {
  58. switch ($filter) {
  59. case 'enabled':
  60. return new DataResponse(['apps' => \OC_App::getEnabledApps()]);
  61. break;
  62. case 'disabled':
  63. $enabled = OC_App::getEnabledApps();
  64. return new DataResponse(['apps' => array_diff($list, $enabled)]);
  65. break;
  66. default:
  67. // Invalid filter variable
  68. throw new OCSException('', 101);
  69. }
  70. } else {
  71. return new DataResponse(['apps' => $list]);
  72. }
  73. }
  74. /**
  75. * @param string $app
  76. * @return DataResponse
  77. * @throws OCSException
  78. */
  79. public function getAppInfo(string $app): DataResponse {
  80. $info = $this->appManager->getAppInfo($app);
  81. if (!is_null($info)) {
  82. return new DataResponse($info);
  83. }
  84. throw new OCSException('The request app was not found', OCSController::RESPOND_NOT_FOUND);
  85. }
  86. /**
  87. * @PasswordConfirmationRequired
  88. * @param string $app
  89. * @return DataResponse
  90. * @throws OCSException
  91. */
  92. public function enable(string $app): DataResponse {
  93. try {
  94. $this->appManager->enableApp($app);
  95. } catch (AppPathNotFoundException $e) {
  96. throw new OCSException('The request app was not found', OCSController::RESPOND_NOT_FOUND);
  97. }
  98. return new DataResponse();
  99. }
  100. /**
  101. * @PasswordConfirmationRequired
  102. * @param string $app
  103. * @return DataResponse
  104. */
  105. public function disable(string $app): DataResponse {
  106. $this->appManager->disableApp($app);
  107. return new DataResponse();
  108. }
  109. }