diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2015-01-26 13:36:22 +0100 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2015-01-26 13:36:22 +0100 |
commit | 5da4071c4553b5ee64799856e4db58e9484d9839 (patch) | |
tree | a266d0814d94d572e2a0a0d913ca592ca29e9719 /lib/private | |
parent | 810d5a6a675bed9a6a04bf7995a88021642c36e5 (diff) | |
parent | 9ad9d7bfbb85b03bacf0ed1c12d16bae5c8bc6ac (diff) | |
download | nextcloud-server-5da4071c4553b5ee64799856e4db58e9484d9839.tar.gz nextcloud-server-5da4071c4553b5ee64799856e4db58e9484d9839.zip |
Merge pull request #13621 from owncloud/system-config-multiset
Add a method to set/unset multiple config values with one write
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/allconfig.php | 10 | ||||
-rw-r--r-- | lib/private/config.php | 89 | ||||
-rw-r--r-- | lib/private/legacy/config.php | 10 | ||||
-rw-r--r-- | lib/private/setup.php | 17 | ||||
-rw-r--r-- | lib/private/setup/abstractdatabase.php | 34 | ||||
-rw-r--r-- | lib/private/setup/mssql.php | 8 | ||||
-rw-r--r-- | lib/private/setup/mysql.php | 6 | ||||
-rw-r--r-- | lib/private/setup/oci.php | 52 | ||||
-rw-r--r-- | lib/private/setup/postgresql.php | 19 | ||||
-rw-r--r-- | lib/private/systemconfig.php | 10 |
10 files changed, 166 insertions, 89 deletions
diff --git a/lib/private/allconfig.php b/lib/private/allconfig.php index d4b4ed6fb6a..421db566866 100644 --- a/lib/private/allconfig.php +++ b/lib/private/allconfig.php @@ -70,6 +70,16 @@ class AllConfig implements \OCP\IConfig { } /** + * Sets and deletes system wide values + * + * @param array $configs Associative array with `key => value` pairs + * If value is null, the config key will be deleted + */ + public function setSystemValues(array $configs) { + $this->systemConfig->setValues($configs); + } + + /** * Sets a new system wide value * * @param string $key the key of the value, under which will be saved diff --git a/lib/private/config.php b/lib/private/config.php index 8544de34b72..586e8c20587 100644 --- a/lib/private/config.php +++ b/lib/private/config.php @@ -41,10 +41,10 @@ class Config { /** * Lists all available config keys - * @return array an array of key names * - * This function returns all keys saved in config.php. Please note that it - * does not return the values. + * Please note that it does not return the values. + * + * @return array an array of key names */ public function getKeys() { return array_keys($this->cache); @@ -52,12 +52,12 @@ class Config { /** * Gets a value from config.php + * + * If it does not exist, $default will be returned. + * * @param string $key key * @param mixed $default = null default value * @return mixed the value or $default - * - * This function gets the value from config.php. If it does not exist, - * $default will be returned. */ public function getValue($key, $default = null) { if (isset($this->cache[$key])) { @@ -68,36 +68,81 @@ class Config { } /** - * Sets a value - * @param string $key key - * @param mixed $value value - * - * This function sets the value and writes the config.php. + * Sets and deletes values and writes the config.php * + * @param array $configs Associative array with `key => value` pairs + * If value is null, the config key will be deleted */ - public function setValue($key, $value) { - // Add change - $this->cache[$key] = $value; + public function setValues(array $configs) { + $needsUpdate = false; + foreach ($configs as $key => $value) { + if ($value !== null) { + $needsUpdate |= $this->set($key, $value); + } else { + $needsUpdate |= $this->delete($key); + } + } - // Write changes - $this->writeData(); + if ($needsUpdate) { + // Write changes + $this->writeData(); + } } /** - * Removes a key from the config - * @param string $key key + * Sets the value and writes it to config.php if required * - * This function removes a key from the config.php. + * @param string $key key + * @param mixed $value value + */ + public function setValue($key, $value) { + if ($this->set($key, $value)) { + // Write changes + $this->writeData(); + } + } + + /** + * This function sets the value * + * @param string $key key + * @param mixed $value value + * @return bool True if the file needs to be updated, false otherwise + */ + protected function set($key, $value) { + if (!isset($this->cache[$key]) || $this->cache[$key] !== $value) { + // Add change + $this->cache[$key] = $value; + return true; + } + + return false; + } + + /** + * Removes a key from the config and removes it from config.php if required + * @param string $key */ public function deleteKey($key) { + if ($this->delete($key)) { + // Write changes + $this->writeData(); + } + } + + /** + * This function removes a key from the config + * + * @param string $key + * @return bool True if the file needs to be updated, false otherwise + */ + protected function delete($key) { if (isset($this->cache[$key])) { // Delete key from cache unset($this->cache[$key]); - - // Write changes - $this->writeData(); + return true; } + return false; } /** diff --git a/lib/private/legacy/config.php b/lib/private/legacy/config.php index 7b711204256..64d01434b11 100644 --- a/lib/private/legacy/config.php +++ b/lib/private/legacy/config.php @@ -59,6 +59,16 @@ class OC_Config { } /** + * Sets and deletes values and writes the config.php + * + * @param array $configs Associative array with `key => value` pairs + * If value is null, the config key will be deleted + */ + public static function setValues(array $configs) { + self::$object->setValues($configs); + } + + /** * Removes a key from the config * @param string $key key * diff --git a/lib/private/setup.php b/lib/private/setup.php index b9ba8d906c2..e5f84d4c02a 100644 --- a/lib/private/setup.php +++ b/lib/private/setup.php @@ -176,18 +176,19 @@ class OC_Setup { //generate a random salt that is used to salt the local user passwords $salt = \OC::$server->getSecureRandom()->getLowStrengthGenerator()->generate(30); - \OC::$server->getConfig()->setSystemValue('passwordsalt', $salt); - // generate a secret $secret = \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate(48); - \OC::$server->getConfig()->setSystemValue('secret', $secret); //write the config file - \OC::$server->getConfig()->setSystemValue('trusted_domains', $trustedDomains); - \OC::$server->getConfig()->setSystemValue('datadirectory', $dataDir); - \OC::$server->getConfig()->setSystemValue('overwrite.cli.url', \OC_Request::serverProtocol() . '://' . \OC_Request::serverHost() . OC::$WEBROOT); - \OC::$server->getConfig()->setSystemValue('dbtype', $dbType); - \OC::$server->getConfig()->setSystemValue('version', implode('.', OC_Util::getVersion())); + \OC::$server->getConfig()->setSystemValues([ + 'passwordsalt' => $salt, + 'secret' => $secret, + 'trusted_domains' => $trustedDomains, + 'datadirectory' => $dataDir, + 'overwrite.cli.url' => \OC_Request::serverProtocol() . '://' . \OC_Request::serverHost() . OC::$WEBROOT, + 'dbtype' => $dbType, + 'version' => implode('.', OC_Util::getVersion()), + ]); try { $dbSetup->initialize($options); diff --git a/lib/private/setup/abstractdatabase.php b/lib/private/setup/abstractdatabase.php index 84625a217ee..08e295c3fff 100644 --- a/lib/private/setup/abstractdatabase.php +++ b/lib/private/setup/abstractdatabase.php @@ -35,20 +35,24 @@ abstract class AbstractDatabase { } public function initialize($config) { - $dbuser = $config['dbuser']; - $dbpass = $config['dbpass']; - $dbname = $config['dbname']; - $dbhost = !empty($config['dbhost']) ? $config['dbhost'] : 'localhost'; - $dbtableprefix = isset($config['dbtableprefix']) ? $config['dbtableprefix'] : 'oc_'; - - \OC_Config::setValue('dbname', $dbname); - \OC_Config::setValue('dbhost', $dbhost); - \OC_Config::setValue('dbtableprefix', $dbtableprefix); - - $this->dbuser = $dbuser; - $this->dbpassword = $dbpass; - $this->dbname = $dbname; - $this->dbhost = $dbhost; - $this->tableprefix = $dbtableprefix; + $dbUser = $config['dbuser']; + $dbPass = $config['dbpass']; + $dbName = $config['dbname']; + $dbHost = !empty($config['dbhost']) ? $config['dbhost'] : 'localhost'; + $dbTablePrefix = isset($config['dbtableprefix']) ? $config['dbtableprefix'] : 'oc_'; + + \OC_Config::setValues([ + 'dbname' => $dbName, + 'dbhost' => $dbHost, + 'dbtableprefix' => $dbTablePrefix, + ]); + + $this->dbuser = $dbUser; + $this->dbpassword = $dbPass; + $this->dbname = $dbName; + $this->dbhost = $dbHost; + $this->tableprefix = $dbTablePrefix; } + + abstract public function setupDatabase($userName); } diff --git a/lib/private/setup/mssql.php b/lib/private/setup/mssql.php index 5143545b76f..1aa31a678a1 100644 --- a/lib/private/setup/mssql.php +++ b/lib/private/setup/mssql.php @@ -5,7 +5,7 @@ namespace OC\Setup; class MSSQL extends AbstractDatabase { public $dbprettyname = 'MS SQL Server'; - public function setupDatabase() { + public function setupDatabase($username) { //check if the database user has admin right $masterConnectionInfo = array( "Database" => "master", "UID" => $this->dbuser, "PWD" => $this->dbpassword); @@ -21,8 +21,10 @@ class MSSQL extends AbstractDatabase { $this->trans->t('You need to enter either an existing account or the administrator.')); } - \OC_Config::setValue('dbuser', $this->dbuser); - \OC_Config::setValue('dbpassword', $this->dbpassword); + \OC_Config::setValues([ + 'dbuser' => $this->dbuser, + 'dbpassword' => $this->dbpassword, + ]); $this->createDBLogin($masterConnection); diff --git a/lib/private/setup/mysql.php b/lib/private/setup/mysql.php index 8f8d86d388c..97f75e2f676 100644 --- a/lib/private/setup/mysql.php +++ b/lib/private/setup/mysql.php @@ -51,8 +51,10 @@ class MySQL extends AbstractDatabase { } }; - \OC_Config::setValue('dbuser', $this->dbuser); - \OC_Config::setValue('dbpassword', $this->dbpassword); + \OC_Config::setValues([ + 'dbuser' => $this->dbuser, + 'dbpassword' => $this->dbpassword, + ]); } //create the database diff --git a/lib/private/setup/oci.php b/lib/private/setup/oci.php index b75b658bae2..d4f71f18ab4 100644 --- a/lib/private/setup/oci.php +++ b/lib/private/setup/oci.php @@ -16,8 +16,11 @@ class OCI extends AbstractDatabase { } // allow empty hostname for oracle $this->dbhost = $config['dbhost']; - \OC_Config::setValue('dbhost', $this->dbhost); - \OC_Config::setValue('dbtablespace', $this->dbtablespace); + + \OC_Config::setValues([ + 'dbhost' => $this->dbhost, + 'dbtablespace' => $this->dbtablespace, + ]); } public function validate($config) { @@ -72,37 +75,32 @@ class OCI extends AbstractDatabase { $result = oci_execute($stmt); if($result) { $row = oci_fetch_row($stmt); - } - if($result and $row[0] > 0) { - //use the admin login data for the new database user - //add prefix to the oracle user name to prevent collisions - $this->dbuser='oc_'.$username; - //create a new password so we don't need to store the admin config in the config file - $this->dbpassword=\OC_Util::generateRandomBytes(30); + if ($row[0] > 0) { + //use the admin login data for the new database user - //oracle passwords are treated as identifiers: - // must start with alphanumeric char - // needs to be shortened to 30 bytes, as the two " needed to escape the identifier count towards the identifier length. - $this->dbpassword=substr($this->dbpassword, 0, 30); + //add prefix to the oracle user name to prevent collisions + $this->dbuser='oc_'.$username; + //create a new password so we don't need to store the admin config in the config file + $this->dbpassword=\OC_Util::generateRandomBytes(30); - $this->createDBUser($connection); + //oracle passwords are treated as identifiers: + // must start with alphanumeric char + // needs to be shortened to 30 bytes, as the two " needed to escape the identifier count towards the identifier length. + $this->dbpassword=substr($this->dbpassword, 0, 30); - \OC_Config::setValue('dbuser', $this->dbuser); - \OC_Config::setValue('dbname', $this->dbuser); - \OC_Config::setValue('dbpassword', $this->dbpassword); - - //create the database not necessary, oracle implies user = schema - //$this->createDatabase($this->dbname, $this->dbuser, $connection); - } else { + $this->createDBUser($connection); + } + } - \OC_Config::setValue('dbuser', $this->dbuser); - \OC_Config::setValue('dbname', $this->dbname); - \OC_Config::setValue('dbpassword', $this->dbpassword); + \OC_Config::setValues([ + 'dbuser' => $this->dbuser, + 'dbname' => $this->dbname, + 'dbpassword' => $this->dbpassword, + ]); - //create the database not necessary, oracle implies user = schema - //$this->createDatabase($this->dbname, $this->dbuser, $connection); - } + //create the database not necessary, oracle implies user = schema + //$this->createDatabase($this->dbname, $this->dbuser, $connection); //FIXME check tablespace exists: select * from user_tablespaces diff --git a/lib/private/setup/postgresql.php b/lib/private/setup/postgresql.php index 3777d1620bc..5fb6b85fc89 100644 --- a/lib/private/setup/postgresql.php +++ b/lib/private/setup/postgresql.php @@ -43,20 +43,15 @@ class PostgreSQL extends AbstractDatabase { $this->dbpassword=\OC_Util::generateRandomBytes(30); $this->createDBUser($connection); - - \OC_Config::setValue('dbuser', $this->dbuser); - \OC_Config::setValue('dbpassword', $this->dbpassword); - - //create the database - $this->createDatabase($connection); } - else { - \OC_Config::setValue('dbuser', $this->dbuser); - \OC_Config::setValue('dbpassword', $this->dbpassword); - //create the database - $this->createDatabase($connection); - } + \OC_Config::setValues([ + 'dbuser' => $this->dbuser, + 'dbpassword' => $this->dbpassword, + ]); + + //create the database + $this->createDatabase($connection); // the connection to dbname=postgres is not needed anymore pg_close($connection); diff --git a/lib/private/systemconfig.php b/lib/private/systemconfig.php index ce6883e5ab3..36cefdb8499 100644 --- a/lib/private/systemconfig.php +++ b/lib/private/systemconfig.php @@ -28,6 +28,16 @@ class SystemConfig { } /** + * Sets and deletes values and writes the config.php + * + * @param array $configs Associative array with `key => value` pairs + * If value is null, the config key will be deleted + */ + public function setValues(array $configs) { + \OC_Config::setValues($configs); + } + + /** * Looks up a system wide defined value * * @param string $key the key of the value, under which it was saved |