Move to AppFramework Controller

Signed-off-by: Julius Härtl <jus@bitgrid.net>

Move app management ajax code to AppSettingsController

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2018-01-18 10:02:08 +01:00
Родитель 88f657b71c
Коммит 8594fdc493
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4C614C6ED2CDE6DF
8 изменённых файлов: 153 добавлений и 232 удалений

Просмотреть файл

@ -100,6 +100,7 @@ interface IAppManager {
*
* @param string $appId
* @param \OCP\IGroup[] $groups
* @throws \Exception
* @since 8.0.0
*/
public function enableAppForGroups($appId, $groups);

Просмотреть файл

@ -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);
}
}

Просмотреть файл

@ -1,44 +0,0 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Georg Ehrke <oc.list@georgehrke.com>
* @author Kamil Domanski <kdomanski@kdemail.net>
* @author Lukas Reschke <lukas@statuscode.ch>
*
* @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 <http://www.gnu.org/licenses/>
*
*/
\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();

Просмотреть файл

@ -1,61 +0,0 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Bart Visscher <bartv@thisnet.nl>
* @author Christopher Schäpers <kondou@ts.unde.re>
* @author Kamil Domanski <kdomanski@kdemail.net>
* @author Lukas Reschke <lukas@statuscode.ch>
* @author Robin Appelman <robin@icewind.nl>
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* @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 <http://www.gnu.org/licenses/>
*
*/
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()) ));
}

Просмотреть файл

@ -1,55 +0,0 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Georg Ehrke <oc.list@georgehrke.com>
* @author Lukas Reschke <lukas@statuscode.ch>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @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 <http://www.gnu.org/licenses/>
*
*/
\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.") )));
}

Просмотреть файл

@ -1,58 +0,0 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Christopher Schäpers <kondou@ts.unde.re>
* @author Frank Karlitschek <frank@karlitschek.de>
* @author Georg Ehrke <oc.list@georgehrke.com>
* @author Lukas Reschke <lukas@statuscode.ch>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* @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 <http://www.gnu.org/licenses/>
*
*/
\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.") )));
}

Просмотреть файл

@ -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);

Просмотреть файл

@ -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');