AppsController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Tom Needham <tom@owncloud.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 OCP\App\AppPathNotFoundException;
  31. use OCP\App\IAppManager;
  32. use OCP\AppFramework\Http\DataResponse;
  33. use OCP\AppFramework\OCS\OCSException;
  34. use OCP\AppFramework\OCSController;
  35. use OCP\IRequest;
  36. class AppsController extends OCSController {
  37. /** @var \OCP\App\IAppManager */
  38. private $appManager;
  39. /**
  40. * @param string $appName
  41. * @param IRequest $request
  42. * @param IAppManager $appManager
  43. */
  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. * @param string $filter
  54. * @return DataResponse
  55. * @throws OCSException
  56. */
  57. public function getApps(string $filter = null): DataResponse {
  58. $apps = (new OC_App())->listAllApps();
  59. $list = [];
  60. foreach ($apps as $app) {
  61. $list[] = $app['id'];
  62. }
  63. if ($filter) {
  64. switch ($filter) {
  65. case 'enabled':
  66. return new DataResponse(['apps' => \OC_App::getEnabledApps()]);
  67. break;
  68. case 'disabled':
  69. $enabled = OC_App::getEnabledApps();
  70. return new DataResponse(['apps' => array_diff($list, $enabled)]);
  71. break;
  72. default:
  73. // Invalid filter variable
  74. throw new OCSException('', 101);
  75. }
  76. } else {
  77. return new DataResponse(['apps' => $list]);
  78. }
  79. }
  80. /**
  81. * @param string $app
  82. * @return DataResponse
  83. * @throws OCSException
  84. */
  85. public function getAppInfo(string $app): DataResponse {
  86. $info = \OCP\App::getAppInfo($app);
  87. if (!is_null($info)) {
  88. return new DataResponse(OC_App::getAppInfo($app));
  89. }
  90. throw new OCSException('The request app was not found', \OCP\API::RESPOND_NOT_FOUND);
  91. }
  92. /**
  93. * @PasswordConfirmationRequired
  94. * @param string $app
  95. * @return DataResponse
  96. * @throws OCSException
  97. */
  98. public function enable(string $app): DataResponse {
  99. try {
  100. $this->appManager->enableApp($app);
  101. } catch (AppPathNotFoundException $e) {
  102. throw new OCSException('The request app was not found', \OCP\API::RESPOND_NOT_FOUND);
  103. }
  104. return new DataResponse();
  105. }
  106. /**
  107. * @PasswordConfirmationRequired
  108. * @param string $app
  109. * @return DataResponse
  110. */
  111. public function disable(string $app): DataResponse {
  112. $this->appManager->disableApp($app);
  113. return new DataResponse();
  114. }
  115. }