Merge pull request #14066 from nextcloud/feature/noid/casted-system-values

Get typed system values
This commit is contained in:
Joas Schilling 2019-02-22 11:54:20 +01:00 committed by GitHub
commit 2cc4118629
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 92 additions and 27 deletions

View file

@ -78,7 +78,7 @@ class MaintenancePlugin extends ServerPlugin {
* @return bool * @return bool
*/ */
public function checkMaintenanceMode() { public function checkMaintenanceMode() {
if ($this->config->getSystemValue('maintenance', false)) { if ($this->config->getSystemValueBool('maintenance')) {
throw new ServiceUnavailable('System in maintenance mode.'); throw new ServiceUnavailable('System in maintenance mode.');
} }
if (Util::needUpgrade()) { if (Util::needUpgrade()) {

View file

@ -53,7 +53,8 @@ class MaintenancePluginTest extends TestCase {
public function testMaintenanceMode() { public function testMaintenanceMode() {
$this->config $this->config
->expects($this->exactly(1)) ->expects($this->exactly(1))
->method('getSystemValue') ->method('getSystemValueBool')
->with('maintenance')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$this->maintenancePlugin->checkMaintenanceMode(); $this->maintenancePlugin->checkMaintenanceMode();

View file

@ -75,7 +75,7 @@ class Application extends \OCP\AppFramework\App {
* register hooks * register hooks
*/ */
public function registerHooks() { public function registerHooks() {
if (!$this->config->getSystemValue('maintenance', false)) { if (!$this->config->getSystemValueBool('maintenance')) {
$container = $this->getContainer(); $container = $this->getContainer();
$server = $container->getServer(); $server = $container->getServer();

View file

@ -89,7 +89,7 @@ class DecryptAll extends Command {
*/ */
protected function forceMaintenanceAndTrashbin() { protected function forceMaintenanceAndTrashbin() {
$this->wasTrashbinEnabled = $this->appManager->isEnabledForUser('files_trashbin'); $this->wasTrashbinEnabled = $this->appManager->isEnabledForUser('files_trashbin');
$this->wasMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false); $this->wasMaintenanceModeEnabled = $this->config->getSystemValueBool('maintenance');
$this->config->setSystemValue('maintenance', true); $this->config->setSystemValue('maintenance', true);
$this->appManager->disableApp('files_trashbin'); $this->appManager->disableApp('files_trashbin');
} }

View file

@ -78,7 +78,7 @@ class EncryptAll extends Command {
*/ */
protected function forceMaintenanceAndTrashbin() { protected function forceMaintenanceAndTrashbin() {
$this->wasTrashbinEnabled = $this->appManager->isEnabledForUser('files_trashbin'); $this->wasTrashbinEnabled = $this->appManager->isEnabledForUser('files_trashbin');
$this->wasMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false); $this->wasMaintenanceModeEnabled = $this->config->getSystemValueBool('maintenance');
$this->config->setSystemValue('maintenance', true); $this->config->setSystemValue('maintenance', true);
$this->appManager->disableApp('files_trashbin'); $this->appManager->disableApp('files_trashbin');
} }

View file

@ -59,7 +59,7 @@ class Mode extends Command {
} }
protected function execute(InputInterface $input, OutputInterface $output) { protected function execute(InputInterface $input, OutputInterface $output) {
$maintenanceMode = $this->config->getSystemValue('maintenance', false); $maintenanceMode = $this->config->getSystemValueBool('maintenance');
if ($input->getOption('on')) { if ($input->getOption('on')) {
if ($maintenanceMode === false) { if ($maintenanceMode === false) {
$this->config->setSystemValue('maintenance', true); $this->config->setSystemValue('maintenance', true);

View file

@ -106,7 +106,7 @@ class Repair extends Command {
} }
} }
$maintenanceMode = $this->config->getSystemValue('maintenance', false); $maintenanceMode = $this->config->getSystemValueBool('maintenance');
$this->config->setSystemValue('maintenance', true); $this->config->setSystemValue('maintenance', true);
$this->progress = new ProgressBar($output); $this->progress = new ProgressBar($output);

View file

@ -264,7 +264,7 @@ class Upgrade extends Command {
} }
return self::ERROR_SUCCESS; return self::ERROR_SUCCESS;
} else if($this->config->getSystemValue('maintenance', false)) { } else if($this->config->getSystemValueBool('maintenance')) {
//Possible scenario: Nextcloud core is updated but an app failed //Possible scenario: Nextcloud core is updated but an app failed
$output->writeln('<warning>Nextcloud is in maintenance mode</warning>'); $output->writeln('<warning>Nextcloud is in maintenance mode</warning>');
$output->write('<comment>Maybe an upgrade is already in process. Please check the ' $output->write('<comment>Maybe an upgrade is already in process. Please check the '

View file

@ -44,7 +44,7 @@ try {
\OC::$server->getLogger()->debug('Update required, skipping cron', ['app' => 'cron']); \OC::$server->getLogger()->debug('Update required, skipping cron', ['app' => 'cron']);
exit; exit;
} }
if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) { if ((bool) \OC::$server->getSystemConfig()->getValue('maintenance', false)) {
\OC::$server->getLogger()->debug('We are in maintenance mode, skipping cron', ['app' => 'cron']); \OC::$server->getLogger()->debug('We are in maintenance mode, skipping cron', ['app' => 'cron']);
exit; exit;
} }

View file

@ -285,7 +285,7 @@ class OC {
public static function checkMaintenanceMode() { public static function checkMaintenanceMode() {
// Allow ajax update script to execute without being stopped // Allow ajax update script to execute without being stopped
if (\OC::$server->getSystemConfig()->getValue('maintenance', false) && OC::$SUBURI != '/core/ajax/update.php') { if (((bool) \OC::$server->getSystemConfig()->getValue('maintenance', false)) && OC::$SUBURI != '/core/ajax/update.php') {
// send http status 503 // send http status 503
http_response_code(503); http_response_code(503);
header('Retry-After: 120'); header('Retry-After: 120');
@ -938,7 +938,7 @@ class OC {
if (function_exists('opcache_reset')) { if (function_exists('opcache_reset')) {
opcache_reset(); opcache_reset();
} }
if (!$systemConfig->getValue('maintenance', false)) { if (!((bool) $systemConfig->getValue('maintenance', false))) {
self::printUpgradePage($systemConfig); self::printUpgradePage($systemConfig);
exit(); exit();
} }
@ -966,7 +966,7 @@ class OC {
// Load minimum set of apps // Load minimum set of apps
if (!\OCP\Util::needUpgrade() if (!\OCP\Util::needUpgrade()
&& !$systemConfig->getValue('maintenance', false)) { && !((bool) $systemConfig->getValue('maintenance', false))) {
// For logged-in users: Load everything // For logged-in users: Load everything
if(\OC::$server->getUserSession()->isLoggedIn()) { if(\OC::$server->getUserSession()->isLoggedIn()) {
OC_App::loadApps(); OC_App::loadApps();
@ -979,7 +979,7 @@ class OC {
if (!self::$CLI) { if (!self::$CLI) {
try { try {
if (!$systemConfig->getValue('maintenance', false) && !\OCP\Util::needUpgrade()) { if (!((bool) $systemConfig->getValue('maintenance', false)) && !\OCP\Util::needUpgrade()) {
OC_App::loadApps(array('filesystem', 'logging')); OC_App::loadApps(array('filesystem', 'logging'));
OC_App::loadApps(); OC_App::loadApps();
} }

View file

@ -125,6 +125,42 @@ class AllConfig implements \OCP\IConfig {
return $this->systemConfig->getValue($key, $default); return $this->systemConfig->getValue($key, $default);
} }
/**
* Looks up a boolean system wide defined value
*
* @param string $key the key of the value, under which it was saved
* @param mixed $default the default value to be returned if the value isn't set
* @return mixed the value or $default
* @since 16.0.0
*/
public function getSystemValueBool(string $key, bool $default = false): bool {
return (bool) $this->getSystemValue($key, $default);
}
/**
* Looks up an integer system wide defined value
*
* @param string $key the key of the value, under which it was saved
* @param mixed $default the default value to be returned if the value isn't set
* @return mixed the value or $default
* @since 16.0.0
*/
public function getSystemValueInt(string $key, int $default = 0): int {
return (int) $this->getSystemValue($key, $default);
}
/**
* Looks up a string system wide defined value
*
* @param string $key the key of the value, under which it was saved
* @param mixed $default the default value to be returned if the value isn't set
* @return mixed the value or $default
* @since 16.0.0
*/
public function getSystemValueString(string $key, string $default = ''): string {
return (string) $this->getSystemValue($key, $default);
}
/** /**
* Looks up a system wide defined value and filters out sensitive data * Looks up a system wide defined value and filters out sensitive data
* *

View file

@ -119,7 +119,7 @@ class Application {
if ($this->config->getSystemValue('installed', false)) { if ($this->config->getSystemValue('installed', false)) {
if (\OCP\Util::needUpgrade()) { if (\OCP\Util::needUpgrade()) {
throw new NeedsUpdateException(); throw new NeedsUpdateException();
} elseif ($this->config->getSystemValue('maintenance', false)) { } elseif ($this->config->getSystemValueBool('maintenance')) {
$this->writeMaintenanceModeInfo($input, $output); $this->writeMaintenanceModeInfo($input, $output);
} else { } else {
OC_App::loadApps(); OC_App::loadApps();

View file

@ -260,7 +260,7 @@ class Router implements IRouter {
$this->loadRoutes($app); $this->loadRoutes($app);
} else if (substr($url, 0, 6) === '/core/' or substr($url, 0, 10) === '/settings/') { } else if (substr($url, 0, 6) === '/core/' or substr($url, 0, 10) === '/settings/') {
\OC::$REQUESTEDAPP = $url; \OC::$REQUESTEDAPP = $url;
if (!\OC::$server->getConfig()->getSystemValue('maintenance', false) && !Util::needUpgrade()) { if (!\OC::$server->getConfig()->getSystemValueBool('maintenance') && !Util::needUpgrade()) {
\OC_App::loadApps(); \OC_App::loadApps();
} }
$this->loadRoutes('core'); $this->loadRoutes('core');

View file

@ -104,7 +104,7 @@ class Updater extends BasicEmitter {
$this->emit('\OC\Updater', 'setDebugLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]); $this->emit('\OC\Updater', 'setDebugLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]);
$this->config->setSystemValue('loglevel', ILogger::DEBUG); $this->config->setSystemValue('loglevel', ILogger::DEBUG);
$wasMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false); $wasMaintenanceModeEnabled = $this->config->getSystemValueBool('maintenance');
if(!$wasMaintenanceModeEnabled) { if(!$wasMaintenanceModeEnabled) {
$this->config->setSystemValue('maintenance', true); $this->config->setSystemValue('maintenance', true);
@ -614,4 +614,3 @@ class Updater extends BasicEmitter {
} }
} }

View file

@ -106,7 +106,7 @@ class OC_App {
* if $types is set to non-empty array, only apps of those types will be loaded * if $types is set to non-empty array, only apps of those types will be loaded
*/ */
public static function loadApps(array $types = []): bool { public static function loadApps(array $types = []): bool {
if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) { if ((bool) \OC::$server->getSystemConfig()->getValue('maintenance', false)) {
return false; return false;
} }
// Load the enabled apps here // Load the enabled apps here

View file

@ -74,6 +74,36 @@ interface IConfig {
*/ */
public function getSystemValue($key, $default = ''); public function getSystemValue($key, $default = '');
/**
* Looks up a boolean system wide defined value
*
* @param string $key the key of the value, under which it was saved
* @param bool $default the default value to be returned if the value isn't set
* @return bool the value or $default
* @since 16.0.0
*/
public function getSystemValueBool(string $key, bool $default = false): bool;
/**
* Looks up an integer system wide defined value
*
* @param string $key the key of the value, under which it was saved
* @param int $default the default value to be returned if the value isn't set
* @return int the value or $default
* @since 16.0.0
*/
public function getSystemValueInt(string $key, int $default = 0): int;
/**
* Looks up a string system wide defined value
*
* @param string $key the key of the value, under which it was saved
* @param string $default the default value to be returned if the value isn't set
* @return string the value or $default
* @since 16.0.0
*/
public function getSystemValueString(string $key, string $default = ''): string;
/** /**
* Looks up a system wide defined value and filters out sensitive data * Looks up a system wide defined value and filters out sensitive data
* *

View file

@ -33,7 +33,7 @@ require_once __DIR__ . '/../lib/versioncheck.php';
require_once __DIR__ . '/../lib/base.php'; require_once __DIR__ . '/../lib/base.php';
if (\OCP\Util::needUpgrade() if (\OCP\Util::needUpgrade()
|| \OC::$server->getSystemConfig()->getValue('maintenance', false)) { || \OC::$server->getConfig()->getSystemValueBool('maintenance')) {
// since the behavior of apps or remotes are unpredictable during // since the behavior of apps or remotes are unpredictable during
// an upgrade, return a 503 directly // an upgrade, return a 503 directly
http_response_code(503); http_response_code(503);
@ -103,4 +103,3 @@ try {
.' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services.'."\n"; .' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services.'."\n";
OC_API::respond(new \OC\OCS\Result(null, \OCP\API::RESPOND_NOT_FOUND, $txt), $format); OC_API::respond(new \OC\OCS\Result(null, \OCP\API::RESPOND_NOT_FOUND, $txt), $format);
} }

View file

@ -91,7 +91,7 @@ class EncryptAllTest extends TestCase {
$this->appManager->expects($this->once())->method('disableApp')->with('files_trashbin'); $this->appManager->expects($this->once())->method('disableApp')->with('files_trashbin');
// enable single user mode to avoid that other user login during encryption // enable single user mode to avoid that other user login during encryption
// destructor should disable the single user mode again // destructor should disable the single user mode again
$this->config->expects($this->once())->method('getSystemValue')->with('maintenance', false)->willReturn(false); $this->config->expects($this->once())->method('getSystemValueBool')->with('maintenance', false)->willReturn(false);
$this->config->expects($this->at(1))->method('setSystemValue')->with('maintenance', true); $this->config->expects($this->at(1))->method('setSystemValue')->with('maintenance', true);
$this->config->expects($this->at(2))->method('setSystemValue')->with('maintenance', false); $this->config->expects($this->at(2))->method('setSystemValue')->with('maintenance', false);

View file

@ -123,7 +123,7 @@ class ModeTest extends TestCase {
string $expectedOutput string $expectedOutput
) { ) {
$this->config->expects($this->any()) $this->config->expects($this->any())
->method('getSystemValue') ->method('getSystemValueBool')
->willReturn($currentMaintenanceState); ->willReturn($currentMaintenanceState);
if ($expectedMaintenanceState !== null) { if ($expectedMaintenanceState !== null) {