2014-12-22 08:54:50 -05:00
|
|
|
<?php
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2018-02-27 09:47:59 -05:00
|
|
|
declare(strict_types=1);
|
2014-12-22 08:54:50 -05:00
|
|
|
/**
|
2024-06-02 09:26:54 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2014-12-22 08:54:50 -05:00
|
|
|
*/
|
2016-08-12 04:27:08 -04:00
|
|
|
namespace OCA\Provisioning_API\Controller;
|
2014-12-22 08:54:50 -05:00
|
|
|
|
2019-11-22 14:52:10 -05:00
|
|
|
use OC_App;
|
2017-03-20 05:30:46 -04:00
|
|
|
use OCP\App\AppPathNotFoundException;
|
2016-08-12 04:27:08 -04:00
|
|
|
use OCP\App\IAppManager;
|
2023-02-15 13:27:04 -05:00
|
|
|
use OCP\AppFramework\Http;
|
2024-07-25 07:14:49 -04:00
|
|
|
use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
|
2016-08-12 04:27:08 -04:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
|
use OCP\AppFramework\OCS\OCSException;
|
|
|
|
|
use OCP\AppFramework\OCSController;
|
|
|
|
|
use OCP\IRequest;
|
2014-12-22 08:54:50 -05:00
|
|
|
|
2016-08-12 04:27:08 -04:00
|
|
|
class AppsController extends OCSController {
|
|
|
|
|
public function __construct(
|
2018-02-27 09:47:59 -05:00
|
|
|
string $appName,
|
2016-08-12 04:27:08 -04:00
|
|
|
IRequest $request,
|
2024-10-18 06:04:22 -04:00
|
|
|
private IAppManager $appManager,
|
2016-08-12 04:27:08 -04:00
|
|
|
) {
|
|
|
|
|
parent::__construct($appName, $request);
|
2015-07-25 15:14:43 -04:00
|
|
|
}
|
|
|
|
|
|
2025-02-17 12:06:45 -05:00
|
|
|
/**
|
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
|
*/
|
|
|
|
|
protected function verifyAppId(string $app): string {
|
|
|
|
|
$cleanId = $this->appManager->cleanAppId($app);
|
|
|
|
|
if ($cleanId !== $app) {
|
|
|
|
|
throw new \InvalidArgumentException('Invalid app id given');
|
|
|
|
|
}
|
|
|
|
|
return $cleanId;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-08 08:02:30 -04:00
|
|
|
/**
|
2023-02-15 13:27:04 -05:00
|
|
|
* Get a list of installed apps
|
|
|
|
|
*
|
|
|
|
|
* @param ?string $filter Filter for enabled or disabled apps
|
2024-09-24 09:53:13 -04:00
|
|
|
* @return DataResponse<Http::STATUS_OK, array{apps: list<string>}, array{}>
|
2016-08-12 04:27:08 -04:00
|
|
|
* @throws OCSException
|
2023-09-19 08:12:17 -04:00
|
|
|
*
|
|
|
|
|
* 200: Installed apps returned
|
2015-09-08 08:02:30 -04:00
|
|
|
*/
|
2023-02-15 13:27:04 -05:00
|
|
|
public function getApps(?string $filter = null): DataResponse {
|
2016-10-27 11:41:15 -04:00
|
|
|
$apps = (new OC_App())->listAllApps();
|
2024-09-24 09:53:13 -04:00
|
|
|
/** @var list<string> $list */
|
2015-09-08 08:02:30 -04:00
|
|
|
$list = [];
|
2020-04-10 08:19:56 -04:00
|
|
|
foreach ($apps as $app) {
|
2014-12-22 08:54:50 -05:00
|
|
|
$list[] = $app['id'];
|
|
|
|
|
}
|
2020-04-10 08:19:56 -04:00
|
|
|
if ($filter) {
|
|
|
|
|
switch ($filter) {
|
2014-12-22 08:54:50 -05:00
|
|
|
case 'enabled':
|
2016-08-12 04:27:08 -04:00
|
|
|
return new DataResponse(['apps' => \OC_App::getEnabledApps()]);
|
2014-12-22 08:54:50 -05:00
|
|
|
break;
|
|
|
|
|
case 'disabled':
|
|
|
|
|
$enabled = OC_App::getEnabledApps();
|
2024-09-24 09:53:13 -04:00
|
|
|
return new DataResponse(['apps' => array_values(array_diff($list, $enabled))]);
|
2014-12-22 08:54:50 -05:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// Invalid filter variable
|
2016-08-12 04:27:08 -04:00
|
|
|
throw new OCSException('', 101);
|
2014-12-22 08:54:50 -05:00
|
|
|
}
|
|
|
|
|
} else {
|
2016-08-12 04:27:08 -04:00
|
|
|
return new DataResponse(['apps' => $list]);
|
2014-12-22 08:54:50 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-08 08:02:30 -04:00
|
|
|
/**
|
2023-02-15 13:27:04 -05:00
|
|
|
* Get the app info for an app
|
|
|
|
|
*
|
|
|
|
|
* @param string $app ID of the app
|
2024-02-27 12:06:42 -05:00
|
|
|
* @return DataResponse<Http::STATUS_OK, array<string, ?mixed>, array{}>
|
2016-12-05 05:55:21 -05:00
|
|
|
* @throws OCSException
|
2023-09-19 08:12:17 -04:00
|
|
|
*
|
|
|
|
|
* 200: App info returned
|
2015-09-08 08:02:30 -04:00
|
|
|
*/
|
2018-02-27 09:47:59 -05:00
|
|
|
public function getAppInfo(string $app): DataResponse {
|
2025-02-17 12:06:45 -05:00
|
|
|
try {
|
|
|
|
|
$app = $this->verifyAppId($app);
|
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
|
throw new OCSException($e->getMessage(), OCSController::RESPOND_UNAUTHORISED);
|
|
|
|
|
}
|
2021-02-16 09:51:25 -05:00
|
|
|
$info = $this->appManager->getAppInfo($app);
|
2020-04-10 08:19:56 -04:00
|
|
|
if (!is_null($info)) {
|
2021-02-16 09:51:25 -05:00
|
|
|
return new DataResponse($info);
|
2014-12-22 08:54:50 -05:00
|
|
|
}
|
2017-07-24 01:44:09 -04:00
|
|
|
|
2021-03-03 09:10:42 -05:00
|
|
|
throw new OCSException('The request app was not found', OCSController::RESPOND_NOT_FOUND);
|
2014-12-22 08:54:50 -05:00
|
|
|
}
|
|
|
|
|
|
2015-09-08 08:02:30 -04:00
|
|
|
/**
|
2023-02-15 13:27:04 -05:00
|
|
|
* Enable an app
|
|
|
|
|
*
|
|
|
|
|
* @param string $app ID of the app
|
2024-09-24 09:53:13 -04:00
|
|
|
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
|
2017-03-20 05:30:46 -04:00
|
|
|
* @throws OCSException
|
2023-09-19 08:12:17 -04:00
|
|
|
*
|
|
|
|
|
* 200: App enabled successfully
|
2015-09-08 08:02:30 -04:00
|
|
|
*/
|
2024-07-25 07:14:49 -04:00
|
|
|
#[PasswordConfirmationRequired]
|
2018-02-27 09:47:59 -05:00
|
|
|
public function enable(string $app): DataResponse {
|
2017-03-20 05:30:46 -04:00
|
|
|
try {
|
2025-02-17 12:06:45 -05:00
|
|
|
$app = $this->verifyAppId($app);
|
2017-03-20 05:30:46 -04:00
|
|
|
$this->appManager->enableApp($app);
|
2025-02-17 12:06:45 -05:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
|
throw new OCSException($e->getMessage(), OCSController::RESPOND_UNAUTHORISED);
|
2017-03-20 05:30:46 -04:00
|
|
|
} catch (AppPathNotFoundException $e) {
|
2021-03-03 09:10:42 -05:00
|
|
|
throw new OCSException('The request app was not found', OCSController::RESPOND_NOT_FOUND);
|
2017-03-20 05:30:46 -04:00
|
|
|
}
|
2016-08-12 04:27:08 -04:00
|
|
|
return new DataResponse();
|
2014-12-22 08:54:50 -05:00
|
|
|
}
|
|
|
|
|
|
2015-09-08 08:02:30 -04:00
|
|
|
/**
|
2023-02-15 13:27:04 -05:00
|
|
|
* Disable an app
|
|
|
|
|
*
|
|
|
|
|
* @param string $app ID of the app
|
2024-09-24 09:53:13 -04:00
|
|
|
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
|
2025-02-17 12:06:45 -05:00
|
|
|
* @throws OCSException
|
2023-09-19 08:12:17 -04:00
|
|
|
*
|
|
|
|
|
* 200: App disabled successfully
|
2015-09-08 08:02:30 -04:00
|
|
|
*/
|
2024-07-25 07:14:49 -04:00
|
|
|
#[PasswordConfirmationRequired]
|
2018-02-27 09:47:59 -05:00
|
|
|
public function disable(string $app): DataResponse {
|
2025-02-17 12:06:45 -05:00
|
|
|
try {
|
|
|
|
|
$app = $this->verifyAppId($app);
|
|
|
|
|
$this->appManager->disableApp($app);
|
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
|
throw new OCSException($e->getMessage(), OCSController::RESPOND_UNAUTHORISED);
|
|
|
|
|
}
|
2016-08-12 04:27:08 -04:00
|
|
|
return new DataResponse();
|
2014-12-22 08:54:50 -05:00
|
|
|
}
|
|
|
|
|
}
|