From 8594fdc493a0bb16bfe37a7cde6c5b8f37a9f4e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Thu, 18 Jan 2018 10:02:08 +0100 Subject: [PATCH] Move to AppFramework Controller MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl Move app management ajax code to AppSettingsController Signed-off-by: Julius Härtl --- lib/public/App/IAppManager.php | 1 + settings/Controller/AppSettingsController.php | 135 +++++++++++++++++- settings/ajax/disableapp.php | 44 ------ settings/ajax/enableapp.php | 61 -------- settings/ajax/uninstallapp.php | 55 ------- settings/ajax/updateapp.php | 58 -------- settings/js/apps.js | 12 +- settings/routes.php | 19 ++- 8 files changed, 153 insertions(+), 232 deletions(-) delete mode 100644 settings/ajax/disableapp.php delete mode 100644 settings/ajax/enableapp.php delete mode 100644 settings/ajax/uninstallapp.php delete mode 100644 settings/ajax/updateapp.php diff --git a/lib/public/App/IAppManager.php b/lib/public/App/IAppManager.php index 4840d71d75..b0d04500f3 100644 --- a/lib/public/App/IAppManager.php +++ b/lib/public/App/IAppManager.php @@ -100,6 +100,7 @@ interface IAppManager { * * @param string $appId * @param \OCP\IGroup[] $groups + * @throws \Exception * @since 8.0.0 */ public function enableAppForGroups($appId, $groups); diff --git a/settings/Controller/AppSettingsController.php b/settings/Controller/AppSettingsController.php index 325b6a0daf..234001e189 100644 --- a/settings/Controller/AppSettingsController.php +++ b/settings/Controller/AppSettingsController.php @@ -38,11 +38,14 @@ use OC\App\AppStore\Version\VersionParser; use OC\App\DependencyAnalyzer; use OC\App\Platform; use OC\Installer; +use OC_App; use OCP\App\IAppManager; use \OCP\AppFramework\Controller; +use OCP\AppFramework\Http; use OCP\AppFramework\Http\ContentSecurityPolicy; use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\TemplateResponse; +use OCP\ILogger; use OCP\INavigationManager; use OCP\IRequest; use OCP\IL10N; @@ -80,6 +83,8 @@ class AppSettingsController extends Controller { private $installer; /** @var IURLGenerator */ private $urlGenerator; + /** @var ILogger */ + private $logger; /** * @param string $appName @@ -94,6 +99,7 @@ class AppSettingsController extends Controller { * @param BundleFetcher $bundleFetcher * @param Installer $installer * @param IURLGenerator $urlGenerator + * @param ILogger $logger */ public function __construct(string $appName, IRequest $request, @@ -106,7 +112,8 @@ class AppSettingsController extends Controller { IFactory $l10nFactory, BundleFetcher $bundleFetcher, Installer $installer, - IURLGenerator $urlGenerator) { + IURLGenerator $urlGenerator, + ILogger $logger) { parent::__construct($appName, $request); $this->l10n = $l10n; $this->config = $config; @@ -118,6 +125,7 @@ class AppSettingsController extends Controller { $this->bundleFetcher = $bundleFetcher; $this->installer = $installer; $this->urlGenerator = $urlGenerator; + $this->logger = $logger; } /** @@ -280,6 +288,7 @@ class AppSettingsController extends Controller { 'needsDownload' => !$existsLocally, 'groups' => $groups, 'fromAppStore' => true, + 'appstoreData' => $app ]; @@ -295,10 +304,13 @@ class AppSettingsController extends Controller { private function getAppsWithUpdates() { $appClass = new \OC_App(); $apps = $appClass->listAllApps(); + /** @var \OC\App\AppStore\Manager $manager */ + $manager = \OC::$server->query(\OC\App\AppStore\Manager::class); foreach($apps as $key => $app) { $newVersion = $this->installer->isUpdateAvailable($app['id']); if($newVersion !== false) { $apps[$key]['update'] = $newVersion; + $apps[$key]['appstoreData'] = $manager->getApp($app['id']); } else { unset($apps[$key]); } @@ -471,4 +483,125 @@ class AppSettingsController extends Controller { return new JSONResponse(['apps' => $apps, 'status' => 'success']); } + + private function getGroupList(array $groups) { + $groupManager = \OC::$server->getGroupManager(); + $groupsList = []; + foreach ($groups as $group) { + $groupItem = $groupManager->get($group); + if ($groupItem instanceof \OCP\IGroup) { + $groupsList[] = $groupManager->get($group); + } + } + return $groupsList; + } + + /** + * @PasswordConfirmationRequired + * + * @param string $appId + * @return JSONResponse + */ + public function enableApp(string $appId, array $groups): JSONResponse { + return $this->enableApps([$appId], $groups); + } + + /** + * Enable one or more apps + * + * apps will be enabled for specific groups only if $groups is defined + * + * @PasswordConfirmationRequired + * @param array $appIds + * @param array $groups + * @return JSONResponse + */ + public function enableApps(array $appIds, array $groups = []): JSONResponse { + try { + $updateRequired = false; + + foreach ($appIds as $appId) { + $appId = OC_App::cleanAppId($appId); + if (count($groups) > 0) { + $this->appManager->enableAppForGroups($appId, $this->getGroupList($groups)); + } else { + $this->appManager->enableApp($appId); + } + if (\OC_App::shouldUpgrade($appId)) { + $updateRequired = true; + } + } + return new JSONResponse(['data' => ['update_required' => $updateRequired]]); + + } catch (\Exception $e) { + $this->logger->logException($e); + return new JSONResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_INTERNAL_SERVER_ERROR); + } + } + + /** + * @PasswordConfirmationRequired + * + * @param string $appId + * @return JSONResponse + */ + public function disableApp(string $appId): JSONResponse { + return $this->disableApps([$appId]); + } + + /** + * @PasswordConfirmationRequired + * + * @param array $appIds + * @return JSONResponse + */ + public function disableApps(array $appIds): JSONResponse { + try { + foreach ($appIds as $appId) { + $appId = OC_App::cleanAppId($appId); + $this->appManager->disableApp($appId); + } + return new JSONResponse([]); + } catch (\Exception $e) { + $this->logger->logException($e); + return new JSONResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_INTERNAL_SERVER_ERROR); + } + } + + /** + * @PasswordConfirmationRequired + * + * @param string $appId + * @return JSONResponse + */ + public function uninstallApp(string $appId): JSONResponse { + $appId = OC_App::cleanAppId($appId); + $result = OC_App::removeApp($appId); + if($result !== false) { + // FIXME: Clear the cache - move that into some sane helper method + \OC::$server->getMemCacheFactory()->createDistributed('settings')->remove('listApps-0'); + \OC::$server->getMemCacheFactory()->createDistributed('settings')->remove('listApps-1'); + return new JSONResponse(['data' => ['appid' => $appId]]); + } + return new JSONResponse(['data' => ['message' => $this->l10n->t('Couldn\'t remove app.')]], Http::STATUS_INTERNAL_SERVER_ERROR); + } + + public function updateApp(string $appId) { + $appId = OC_App::cleanAppId($appId); + + $this->config->setSystemValue('maintenance', true); + try { + $installer = \OC::$server->query(\OC\Installer::class); + $result = $installer->updateAppstoreApp($appId); + $this->config->setSystemValue('maintenance', false); + } catch (\Exception $ex) { + $this->config->setSystemValue('maintenance', false); + return new JSONResponse(['data' => ['message' => $ex->getMessage()]], Http::STATUS_INTERNAL_SERVER_ERROR); + } + + if ($result !== false) { + return new JSONResponse(['data' => ['appid' => $appId]]); + } + return new JSONResponse(['data' => ['message' => $this->l10n->t('Couldn\'t update app.')]], Http::STATUS_INTERNAL_SERVER_ERROR); + } } diff --git a/settings/ajax/disableapp.php b/settings/ajax/disableapp.php deleted file mode 100644 index d719c3e9f2..0000000000 --- a/settings/ajax/disableapp.php +++ /dev/null @@ -1,44 +0,0 @@ - - * @author Kamil Domanski - * @author Lukas Reschke - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ -\OC_JSON::checkAdminUser(); -\OC_JSON::callCheck(); - -$lastConfirm = (int) \OC::$server->getSession()->get('last-password-confirm'); -if ($lastConfirm < (time() - 30 * 60 + 15)) { // allow 15 seconds delay - $l = \OC::$server->getL10N('core'); - OC_JSON::error(array( 'data' => array( 'message' => $l->t('Password confirmation is required')))); - exit(); -} - -if (!array_key_exists('appid', $_POST)) { - OC_JSON::error(); - exit; -} - -$appIds = (array)$_POST['appid']; -foreach($appIds as $appId) { - $appId = OC_App::cleanAppId($appId); - \OC::$server->getAppManager()->disableApp($appId); -} -OC_JSON::success(); diff --git a/settings/ajax/enableapp.php b/settings/ajax/enableapp.php deleted file mode 100644 index 0ecc001a24..0000000000 --- a/settings/ajax/enableapp.php +++ /dev/null @@ -1,61 +0,0 @@ - - * @author Christopher Schäpers - * @author Kamil Domanski - * @author Lukas Reschke - * @author Robin Appelman - * @author Thomas Müller - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -use OCP\ILogger; - -OC_JSON::checkAdminUser(); -\OC_JSON::callCheck(); - -$lastConfirm = (int) \OC::$server->getSession()->get('last-password-confirm'); -if ($lastConfirm < (time() - 30 * 60 + 15)) { // allow 15 seconds delay - $l = \OC::$server->getL10N('core'); - OC_JSON::error(array( 'data' => array( 'message' => $l->t('Password confirmation is required')))); - exit(); -} - -$groups = isset($_POST['groups']) ? (array)$_POST['groups'] : []; -$appIds = isset($_POST['appIds']) ? (array)$_POST['appIds'] : []; - -try { - $updateRequired = false; - foreach($appIds as $appId) { - $app = new OC_App(); - $appId = OC_App::cleanAppId($appId); - $app->enable($appId, $groups); - if(\OC_App::shouldUpgrade($appId)) { - $updateRequired = true; - } - } - - OC_JSON::success(['data' => ['update_required' => $updateRequired]]); -} catch (Exception $e) { - \OC::$server->getLogger()->logException($e, [ - 'level' => ILogger::DEBUG, - 'app' => 'core', - ]); - OC_JSON::error(array("data" => array("message" => $e->getMessage()) )); -} diff --git a/settings/ajax/uninstallapp.php b/settings/ajax/uninstallapp.php deleted file mode 100644 index 26100d3b06..0000000000 --- a/settings/ajax/uninstallapp.php +++ /dev/null @@ -1,55 +0,0 @@ - - * @author Lukas Reschke - * @author Robin Appelman - * @author Roeland Jago Douma - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ -\OC_JSON::checkAdminUser(); -\OC_JSON::callCheck(); - -$lastConfirm = (int) \OC::$server->getSession()->get('last-password-confirm'); -if ($lastConfirm < (time() - 30 * 60 + 15)) { // allow 15 seconds delay - $l = \OC::$server->getL10N('core'); - OC_JSON::error(array( 'data' => array( 'message' => $l->t('Password confirmation is required')))); - exit(); -} - -if (!array_key_exists('appid', $_POST)) { - OC_JSON::error(); - exit; -} - -$appId = (string)$_POST['appid']; -$appId = OC_App::cleanAppId($appId); - -// FIXME: move to controller -/** @var \OC\Installer $installer */ -$installer = \OC::$server->query(\OC\Installer::class); -$result = $installer->removeApp($app); -if($result !== false) { - // FIXME: Clear the cache - move that into some sane helper method - \OC::$server->getMemCacheFactory()->createDistributed('settings')->remove('listApps-0'); - \OC::$server->getMemCacheFactory()->createDistributed('settings')->remove('listApps-1'); - OC_JSON::success(array('data' => array('appid' => $appId))); -} else { - $l = \OC::$server->getL10N('settings'); - OC_JSON::error(array('data' => array( 'message' => $l->t("Couldn't remove app.") ))); -} diff --git a/settings/ajax/updateapp.php b/settings/ajax/updateapp.php deleted file mode 100644 index d37e1cfcae..0000000000 --- a/settings/ajax/updateapp.php +++ /dev/null @@ -1,58 +0,0 @@ - - * @author Frank Karlitschek - * @author Georg Ehrke - * @author Lukas Reschke - * @author Robin Appelman - * @author Roeland Jago Douma - * @author Thomas Müller - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ -\OC_JSON::checkAdminUser(); -\OC_JSON::callCheck(); - -if (!array_key_exists('appid', $_POST)) { - \OC_JSON::error(array( - 'message' => 'No AppId given!' - )); - return; -} - -$appId = (string)$_POST['appid']; -$appId = OC_App::cleanAppId($appId); - -$config = \OC::$server->getConfig(); -$config->setSystemValue('maintenance', true); -try { - $installer = \OC::$server->query(\OC\Installer::class); - $result = $installer->updateAppstoreApp($appId); - $config->setSystemValue('maintenance', false); -} catch(Exception $ex) { - $config->setSystemValue('maintenance', false); - OC_JSON::error(array('data' => array( 'message' => $ex->getMessage() ))); - return; -} - -if($result !== false) { - OC_JSON::success(array('data' => array('appid' => $appId))); -} else { - $l = \OC::$server->getL10N('settings'); - OC_JSON::error(array('data' => array( 'message' => $l->t("Couldn't update app.") ))); -} diff --git a/settings/js/apps.js b/settings/js/apps.js index 4fca853956..9776b6e33c 100644 --- a/settings/js/apps.js +++ b/settings/js/apps.js @@ -377,7 +377,13 @@ OC.Settings.Apps = OC.Settings.Apps || { elements.forEach(function(element) { element.val(t('settings','Disabling app …')); }); - $.post(OC.filePath('settings','ajax','disableapp.php'),{appid:appId},function(result) { + $.post(OC.generateUrl('/settings/apps/enable/' + appId)) + .done(function(result) { + console.log(result); + }).fail(function(xhr, status, error) { + console.log(error); + }); + /* if(!result || result.status !== 'success') { if (result.data && result.data.message) { OC.Settings.Apps.showErrorMessage(appId, result.data.message); @@ -415,7 +421,7 @@ OC.Settings.Apps = OC.Settings.Apps || { }); OC.Settings.Apps.State.apps[appId].active = false; } - },'json'); + },'json');*/ } else { // TODO: display message to admin to not refresh the page! // TODO: lock UI to prevent further operations @@ -429,7 +435,7 @@ OC.Settings.Apps = OC.Settings.Apps || { } else { appIdArray = appId; } - $.post(OC.filePath('settings','ajax','enableapp.php'),{appIds: appIdArray, groups: groups},function(result) { + $.post(OC.generateUrl('/settings/apps/enable'),{appIds: appIdArray, groups: groups},function(result) { if(!result || result.status !== 'success') { if (result.data && result.data.message) { OC.Settings.Apps.showErrorMessage(appId, result.data.message); diff --git a/settings/routes.php b/settings/routes.php index a3a5812bf4..a21a0f0789 100644 --- a/settings/routes.php +++ b/settings/routes.php @@ -49,6 +49,14 @@ $application->registerRoutes($this, [ ['name' => 'AppSettings#listCategories', 'url' => '/settings/apps/categories', 'verb' => 'GET'], ['name' => 'AppSettings#viewApps', 'url' => '/settings/apps', 'verb' => 'GET'], ['name' => 'AppSettings#listApps', 'url' => '/settings/apps/list', 'verb' => 'GET'], + ['name' => 'AppSettings#enableApp', 'url' => '/settings/apps/enable/{appId}', 'verb' => 'GET'], + ['name' => 'AppSettings#enableApps', 'url' => '/settings/apps/enable', 'verb' => 'POST'], + ['name' => 'AppSettings#disableApp', 'url' => '/settings/apps/disable/{appId}', 'verb' => 'GET'], + ['name' => 'AppSettings#disableApps', 'url' => '/settings/apps/disable', 'verb' => 'POST'], + ['name' => 'AppSettings#updateApp', 'url' => '/settings/apps/update/{appId}', 'verb' => 'GET'], + ['name' => 'AppSettings#uninstallApp', 'url' => '/settings/apps/uninstall/{appId}', 'verb' => 'GET'], + ['name' => 'Users#setDisplayName', 'url' => '/settings/users/{username}/displayName', 'verb' => 'POST'], + ['name' => 'Users#setEMailAddress', 'url' => '/settings/users/{id}/mailAddress', 'verb' => 'PUT'], ['name' => 'Users#setUserSettings', 'url' => '/settings/users/{username}/settings', 'verb' => 'PUT'], ['name' => 'Users#getVerificationCode', 'url' => '/settings/users/{account}/verify', 'verb' => 'GET'], ['name' => 'Users#usersList', 'url' => '/settings/users', 'verb' => 'GET'], @@ -76,13 +84,4 @@ $application->registerRoutes($this, [ // Settings pages $this->create('settings_help', '/settings/help') ->actionInclude('settings/help.php'); -// Settings ajax actions -// apps -$this->create('settings_ajax_enableapp', '/settings/ajax/enableapp.php') - ->actionInclude('settings/ajax/enableapp.php'); -$this->create('settings_ajax_disableapp', '/settings/ajax/disableapp.php') - ->actionInclude('settings/ajax/disableapp.php'); -$this->create('settings_ajax_updateapp', '/settings/ajax/updateapp.php') - ->actionInclude('settings/ajax/updateapp.php'); -$this->create('settings_ajax_uninstallapp', '/settings/ajax/uninstallapp.php') - ->actionInclude('settings/ajax/uninstallapp.php'); +