set password directly when adding new mysql user

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2019-07-26 07:34:32 +02:00
parent 5658717653
commit 7bc57ed269
2 changed files with 32 additions and 19 deletions

View File

@@ -82,9 +82,11 @@ class DbManagerMySQL
if (version_compare(Database::getAttribute(\PDO::ATTR_SERVER_VERSION), '8.0.11', '>=')) { if (version_compare(Database::getAttribute(\PDO::ATTR_SERVER_VERSION), '8.0.11', '>=')) {
// create user // create user
$stmt = Database::prepare(" $stmt = Database::prepare("
CREATE USER '" . $username . "'@'" . $access_host . "' IDENTIFIED BY 'password' CREATE USER '" . $username . "'@'" . $access_host . "' IDENTIFIED BY :password
"); ");
Database::pexecute($stmt); Database::pexecute($stmt, array(
"password" => $password
));
// grant privileges // grant privileges
$stmt = Database::prepare(" $stmt = Database::prepare("
GRANT ALL ON `" . $username . "`.* TO :username@:host GRANT ALL ON `" . $username . "`.* TO :username@:host
@@ -96,29 +98,31 @@ class DbManagerMySQL
} else { } else {
// grant privileges // grant privileges
$stmt = Database::prepare(" $stmt = Database::prepare("
GRANT ALL PRIVILEGES ON `" . $username . "`.* TO :username@:host IDENTIFIED BY 'password' GRANT ALL PRIVILEGES ON `" . $username . "`.* TO :username@:host IDENTIFIED BY :password
"); ");
Database::pexecute($stmt, array( Database::pexecute($stmt, array(
"username" => $username, "username" => $username,
"host" => $access_host "host" => $access_host,
"password" => $password
)); ));
} }
}
// set passoword
if (version_compare(Database::getAttribute(\PDO::ATTR_SERVER_VERSION), '5.7.6', '<')) {
if ($p_encrypted) {
$stmt = Database::prepare("SET PASSWORD FOR :username@:host = :password");
} else {
$stmt = Database::prepare("SET PASSWORD FOR :username@:host = PASSWORD(:password)");
}
} else { } else {
$stmt = Database::prepare("ALTER USER :username@:host IDENTIFIED BY :password"); // set passoword
if (version_compare(Database::getAttribute(\PDO::ATTR_SERVER_VERSION), '5.7.6', '<')) {
if ($p_encrypted) {
$stmt = Database::prepare("SET PASSWORD FOR :username@:host = :password");
} else {
$stmt = Database::prepare("SET PASSWORD FOR :username@:host = PASSWORD(:password)");
}
} else {
$stmt = Database::prepare("ALTER USER :username@:host IDENTIFIED BY :password");
}
Database::pexecute($stmt, array(
"username" => $username,
"host" => $access_host,
"password" => $password
));
} }
Database::pexecute($stmt, array(
"username" => $username,
"host" => $access_host,
"password" => $password
));
} }
/** /**

View File

@@ -26,8 +26,9 @@ class MysqlsTest extends TestCase
))->get(); ))->get();
$customer_userdata = json_decode($json_result, true)['data']; $customer_userdata = json_decode($json_result, true)['data'];
$newPwd = \Froxlor\System\Crypt::generatePassword();
$data = [ $data = [
'mysql_password' => \Froxlor\System\Crypt::generatePassword(), 'mysql_password' => $newPwd,
'description' => 'testdb', 'description' => 'testdb',
'sendinfomail' => TRAVIS_CI == 1 ? 0 : 1 'sendinfomail' => TRAVIS_CI == 1 ? 0 : 1
]; ];
@@ -35,6 +36,14 @@ class MysqlsTest extends TestCase
$result = json_decode($json_result, true)['data']; $result = json_decode($json_result, true)['data'];
$this->assertEquals('testdb', $result['description']); $this->assertEquals('testdb', $result['description']);
$this->assertEquals(0, $result['dbserver']); $this->assertEquals(0, $result['dbserver']);
// test connection
try {
$test_conn = new \PDO("mysql:host=localhost", 'test1sql1', $newPwd);
unset($test_conn);
} catch (PDOException $e) {
$this->fail($e->getMessage());
}
} }
/** /**