You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AppsController.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  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 \OCP\App\IAppManager */
  37. private $appManager;
  38. /**
  39. * @param string $appName
  40. * @param IRequest $request
  41. * @param IAppManager $appManager
  42. */
  43. public function __construct(
  44. string $appName,
  45. IRequest $request,
  46. IAppManager $appManager
  47. ) {
  48. parent::__construct($appName, $request);
  49. $this->appManager = $appManager;
  50. }
  51. /**
  52. * @param string $filter
  53. * @return DataResponse
  54. * @throws OCSException
  55. */
  56. public function getApps(string $filter = null): DataResponse {
  57. $apps = (new OC_App())->listAllApps();
  58. $list = [];
  59. foreach($apps as $app) {
  60. $list[] = $app['id'];
  61. }
  62. if($filter){
  63. switch($filter){
  64. case 'enabled':
  65. return new DataResponse(['apps' => \OC_App::getEnabledApps()]);
  66. break;
  67. case 'disabled':
  68. $enabled = OC_App::getEnabledApps();
  69. return new DataResponse(['apps' => array_diff($list, $enabled)]);
  70. break;
  71. default:
  72. // Invalid filter variable
  73. throw new OCSException('', 101);
  74. }
  75. } else {
  76. return new DataResponse(['apps' => $list]);
  77. }
  78. }
  79. /**
  80. * @param string $app
  81. * @return DataResponse
  82. * @throws OCSException
  83. */
  84. public function getAppInfo(string $app): DataResponse {
  85. $info = \OCP\App::getAppInfo($app);
  86. if(!is_null($info)) {
  87. return new DataResponse(OC_App::getAppInfo($app));
  88. }
  89. throw new OCSException('The request app was not found', \OCP\API::RESPOND_NOT_FOUND);
  90. }
  91. /**
  92. * @PasswordConfirmationRequired
  93. * @param string $app
  94. * @return DataResponse
  95. * @throws OCSException
  96. */
  97. public function enable(string $app): DataResponse {
  98. try {
  99. $this->appManager->enableApp($app);
  100. } catch (AppPathNotFoundException $e) {
  101. throw new OCSException('The request app was not found', \OCP\API::RESPOND_NOT_FOUND);
  102. }
  103. return new DataResponse();
  104. }
  105. /**
  106. * @PasswordConfirmationRequired
  107. * @param string $app
  108. * @return DataResponse
  109. */
  110. public function disable(string $app): DataResponse {
  111. $this->appManager->disableApp($app);
  112. return new DataResponse();
  113. }
  114. }