Add a method to set/unset multiple config values with one writetags/v8.0.0RC1
protected function saveDBInfo(InputInterface $input) { | protected function saveDBInfo(InputInterface $input) { | ||||
$type = $input->getArgument('type'); | $type = $input->getArgument('type'); | ||||
$username = $input->getArgument('username'); | $username = $input->getArgument('username'); | ||||
$dbhost = $input->getArgument('hostname'); | |||||
$dbname = $input->getArgument('database'); | |||||
$dbHost = $input->getArgument('hostname'); | |||||
$dbName = $input->getArgument('database'); | |||||
$password = $input->getOption('password'); | $password = $input->getOption('password'); | ||||
if ($input->getOption('port')) { | if ($input->getOption('port')) { | ||||
$dbhost .= ':'.$input->getOption('port'); | |||||
$dbHost .= ':'.$input->getOption('port'); | |||||
} | } | ||||
$this->config->setSystemValue('dbtype', $type); | |||||
$this->config->setSystemValue('dbname', $dbname); | |||||
$this->config->setSystemValue('dbhost', $dbhost); | |||||
$this->config->setSystemValue('dbuser', $username); | |||||
$this->config->setSystemValue('dbpassword', $password); | |||||
$this->config->setSystemValues([ | |||||
'dbtype' => $type, | |||||
'dbname' => $dbName, | |||||
'dbhost' => $dbHost, | |||||
'dbuser' => $username, | |||||
'dbpassword' => $password, | |||||
]); | |||||
} | } | ||||
} | } |
} | } | ||||
} | } | ||||
/** | |||||
* 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 | * Sets a new system wide value | ||||
* | * |
/** | /** | ||||
* Lists all available config keys | * 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() { | public function getKeys() { | ||||
return array_keys($this->cache); | return array_keys($this->cache); | ||||
/** | /** | ||||
* Gets a value from config.php | * Gets a value from config.php | ||||
* | |||||
* If it does not exist, $default will be returned. | |||||
* | |||||
* @param string $key key | * @param string $key key | ||||
* @param mixed $default = null default value | * @param mixed $default = null default value | ||||
* @return mixed the value or $default | * @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) { | public function getValue($key, $default = null) { | ||||
if (isset($this->cache[$key])) { | if (isset($this->cache[$key])) { | ||||
} | } | ||||
/** | /** | ||||
* 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) { | 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])) { | if (isset($this->cache[$key])) { | ||||
// Delete key from cache | // Delete key from cache | ||||
unset($this->cache[$key]); | unset($this->cache[$key]); | ||||
// Write changes | |||||
$this->writeData(); | |||||
return true; | |||||
} | } | ||||
return false; | |||||
} | } | ||||
/** | /** |
self::$object->setValue($key, $value); | self::$object->setValue($key, $value); | ||||
} | } | ||||
/** | |||||
* 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 | * Removes a key from the config | ||||
* @param string $key key | * @param string $key key |
//generate a random salt that is used to salt the local user passwords | //generate a random salt that is used to salt the local user passwords | ||||
$salt = \OC::$server->getSecureRandom()->getLowStrengthGenerator()->generate(30); | $salt = \OC::$server->getSecureRandom()->getLowStrengthGenerator()->generate(30); | ||||
\OC::$server->getConfig()->setSystemValue('passwordsalt', $salt); | |||||
// generate a secret | // generate a secret | ||||
$secret = \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate(48); | $secret = \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate(48); | ||||
\OC::$server->getConfig()->setSystemValue('secret', $secret); | |||||
//write the config file | //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 { | try { | ||||
$dbSetup->initialize($options); | $dbSetup->initialize($options); |
} | } | ||||
public function initialize($config) { | 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); | |||||
} | } |
class MSSQL extends AbstractDatabase { | class MSSQL extends AbstractDatabase { | ||||
public $dbprettyname = 'MS SQL Server'; | public $dbprettyname = 'MS SQL Server'; | ||||
public function setupDatabase() { | |||||
public function setupDatabase($username) { | |||||
//check if the database user has admin right | //check if the database user has admin right | ||||
$masterConnectionInfo = array( "Database" => "master", "UID" => $this->dbuser, "PWD" => $this->dbpassword); | $masterConnectionInfo = array( "Database" => "master", "UID" => $this->dbuser, "PWD" => $this->dbpassword); | ||||
$this->trans->t('You need to enter either an existing account or the administrator.')); | $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); | $this->createDBLogin($masterConnection); | ||||
} | } | ||||
}; | }; | ||||
\OC_Config::setValue('dbuser', $this->dbuser); | |||||
\OC_Config::setValue('dbpassword', $this->dbpassword); | |||||
\OC_Config::setValues([ | |||||
'dbuser' => $this->dbuser, | |||||
'dbpassword' => $this->dbpassword, | |||||
]); | |||||
} | } | ||||
//create the database | //create the database |
} | } | ||||
// allow empty hostname for oracle | // allow empty hostname for oracle | ||||
$this->dbhost = $config['dbhost']; | $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) { | public function validate($config) { | ||||
$result = oci_execute($stmt); | $result = oci_execute($stmt); | ||||
if($result) { | if($result) { | ||||
$row = oci_fetch_row($stmt); | $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 | //FIXME check tablespace exists: select * from user_tablespaces | ||||
$this->dbpassword=\OC_Util::generateRandomBytes(30); | $this->dbpassword=\OC_Util::generateRandomBytes(30); | ||||
$this->createDBUser($connection); | $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 | // the connection to dbname=postgres is not needed anymore | ||||
pg_close($connection); | pg_close($connection); |
\OC_Config::setValue($key, $value); | \OC_Config::setValue($key, $value); | ||||
} | } | ||||
/** | |||||
* 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 | * Looks up a system wide defined value | ||||
* | * |
* Access to all the configuration options ownCloud offers | * Access to all the configuration options ownCloud offers | ||||
*/ | */ | ||||
interface IConfig { | interface 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); | |||||
/** | /** | ||||
* Sets a new system wide value | * Sets a new system wide value | ||||
* | * |
$mail_smtpport) { | $mail_smtpport) { | ||||
$params = get_defined_vars(); | $params = get_defined_vars(); | ||||
$configs = []; | |||||
foreach($params as $key => $value) { | foreach($params as $key => $value) { | ||||
if(empty($value)) { | |||||
$this->config->deleteSystemValue($key); | |||||
} else { | |||||
$this->config->setSystemValue($key, $value); | |||||
} | |||||
$configs[$key] = (empty($value)) ? null : $value; | |||||
} | } | ||||
// Delete passwords from config in case no auth is specified | // Delete passwords from config in case no auth is specified | ||||
if($params['mail_smtpauth'] !== 1) { | |||||
$this->config->deleteSystemValue('mail_smtpname'); | |||||
$this->config->deleteSystemValue('mail_smtppassword'); | |||||
if ($params['mail_smtpauth'] !== 1) { | |||||
$configs['mail_smtpname'] = null; | |||||
$configs['mail_smtppassword'] = null; | |||||
} | } | ||||
$this->config->setSystemValues($configs); | |||||
return array('data' => | return array('data' => | ||||
array('message' => | array('message' => | ||||
(string) $this->l10n->t('Saved') | (string) $this->l10n->t('Saved') | ||||
* @return array | * @return array | ||||
*/ | */ | ||||
public function storeCredentials($mail_smtpname, $mail_smtppassword) { | public function storeCredentials($mail_smtpname, $mail_smtppassword) { | ||||
$this->config->setSystemValue('mail_smtpname', $mail_smtpname); | |||||
$this->config->setSystemValue('mail_smtppassword', $mail_smtppassword); | |||||
$this->config->setSystemValues([ | |||||
'mail_smtpname' => $mail_smtpname, | |||||
'mail_smtppassword' => $mail_smtppassword, | |||||
]); | |||||
return array('data' => | return array('data' => | ||||
array('message' => | array('message' => |
$this->assertEquals($expected, $content); | $this->assertEquals($expected, $content); | ||||
} | } | ||||
public function testSetValues() { | |||||
$content = file_get_contents($this->configFile); | |||||
$this->assertEquals(self::TESTCONTENT, $content); | |||||
// Changing configs to existing values and deleting non-existing once | |||||
// should not rewrite the config.php | |||||
$this->config->setValues([ | |||||
'foo' => 'bar', | |||||
'not_exists' => null, | |||||
]); | |||||
$this->assertAttributeEquals($this->initialConfig, 'cache', $this->config); | |||||
$content = file_get_contents($this->configFile); | |||||
$this->assertEquals(self::TESTCONTENT, $content); | |||||
$this->config->setValues([ | |||||
'foo' => 'moo', | |||||
'alcohol_free' => null, | |||||
]); | |||||
$expectedConfig = $this->initialConfig; | |||||
$expectedConfig['foo'] = 'moo'; | |||||
unset($expectedConfig['alcohol_free']); | |||||
$this->assertAttributeEquals($expectedConfig, 'cache', $this->config); | |||||
$content = file_get_contents($this->configFile); | |||||
$expected = "<?php\n\$CONFIG = array (\n 'foo' => 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " . | |||||
" 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n);\n"; | |||||
$this->assertEquals($expected, $content); | |||||
} | |||||
public function testDeleteKey() { | public function testDeleteKey() { | ||||
$this->config->deleteKey('foo'); | $this->config->deleteKey('foo'); | ||||
$expectedConfig = $this->initialConfig; | $expectedConfig = $this->initialConfig; |
); | ); | ||||
*/ | */ | ||||
$this->container['Config'] | |||||
->expects($this->exactly(15)) | |||||
->method('setSystemValue'); | |||||
/** @var \PHPUnit_Framework_MockObject_MockObject $config */ | |||||
$config = $this->container['Config']; | |||||
$config->expects($this->exactly(2)) | |||||
->method('setSystemValues'); | |||||
/** | /** | ||||
* FIXME: Use the following block once Jenkins uses PHPUnit >= 4.1 | * FIXME: Use the following block once Jenkins uses PHPUnit >= 4.1 | ||||
*/ | |||||
/* | |||||
$this->container['Config'] | |||||
->expects($this->exactly(3)) | |||||
->method('deleteSystemValue') | |||||
->withConsecutive( | ->withConsecutive( | ||||
array($this->equalTo('mail_smtpauth')), | |||||
array($this->equalTo('mail_smtpname')), | |||||
array($this->equalTo('mail_smtppassword')) | |||||
[[ | |||||
'mail_domain' => 'owncloud.com', | |||||
'mail_from_address' => 'demo@owncloud.com', | |||||
'mail_smtpmode' => 'smtp', | |||||
'mail_smtpsecure' => 'ssl', | |||||
'mail_smtphost' => 'mx.owncloud.org', | |||||
'mail_smtpauthtype' => 'NTLM', | |||||
'mail_smtpauth' => 1, | |||||
'mail_smtpport' => '25', | |||||
]], | |||||
[[ | |||||
'mail_domain' => 'owncloud.com', | |||||
'mail_from_address' => 'demo@owncloud.com', | |||||
'mail_smtpmode' => 'smtp', | |||||
'mail_smtpsecure' => 'ssl', | |||||
'mail_smtphost' => 'mx.owncloud.org', | |||||
'mail_smtpauthtype' => 'NTLM', | |||||
'mail_smtpauth' => null, | |||||
'mail_smtpport' => '25', | |||||
'mail_smtpname' => null, | |||||
'mail_smtppassword' => null, | |||||
]] | |||||
); | ); | ||||
*/ | |||||
$this->container['Config'] | |||||
->expects($this->exactly(3)) | |||||
->method('deleteSystemValue'); | |||||
*/ | |||||
// With authentication | // With authentication | ||||
$response = $this->container['MailSettingsController']->setMailSettings( | $response = $this->container['MailSettingsController']->setMailSettings( | ||||
->method('t') | ->method('t') | ||||
->will($this->returnValue('Saved')); | ->will($this->returnValue('Saved')); | ||||
/** | |||||
* FIXME: Use this block once Jenkins uses PHPUnit >= 4.1 | |||||
*/ | |||||
/* | |||||
$this->container['Config'] | $this->container['Config'] | ||||
->expects($this->exactly(2)) | |||||
->method('setSystemValue') | |||||
->withConsecutive( | |||||
array($this->equalTo('mail_smtpname'), $this->equalTo('UsernameToStore')), | |||||
array($this->equalTo('mail_smtppassword'), $this->equalTo('PasswordToStore')) | |||||
); | |||||
*/ | |||||
$this->container['Config'] | |||||
->expects($this->exactly(2)) | |||||
->method('setSystemValue'); | |||||
->expects($this->once()) | |||||
->method('setSystemValues') | |||||
->with([ | |||||
'mail_smtpname' => 'UsernameToStore', | |||||
'mail_smtppassword' => 'PasswordToStore', | |||||
]); | |||||
$response = $this->container['MailSettingsController']->storeCredentials('UsernameToStore', 'PasswordToStore'); | $response = $this->container['MailSettingsController']->storeCredentials('UsernameToStore', 'PasswordToStore'); | ||||
$expectedResponse = array('data' => array('message' =>'Saved'), 'status' => 'success'); | $expectedResponse = array('data' => array('message' =>'Saved'), 'status' => 'success'); |