From 2f38de90e585975211c8aa5f8bc1b79b79311178 Mon Sep 17 00:00:00 2001 From: Michael Kaufmann Date: Sun, 24 Feb 2019 08:52:50 +0100 Subject: [PATCH] use DbManager for updating the password of a mysql-db Signed-off-by: Michael Kaufmann --- lib/Froxlor/Api/Commands/Mysqls.php | 9 +--- .../Database/Manager/DbManagerMySQL.php | 54 ++++++++++--------- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/lib/Froxlor/Api/Commands/Mysqls.php b/lib/Froxlor/Api/Commands/Mysqls.php index a2ff23d1..7c10d0e2 100644 --- a/lib/Froxlor/Api/Commands/Mysqls.php +++ b/lib/Froxlor/Api/Commands/Mysqls.php @@ -320,14 +320,9 @@ class Mysqls extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\ResourceEnt // Begin root-session Database::needRoot(true, $result['dbserver']); + $dbmgr = new \Froxlor\Database\DbManager($this->logger()); foreach (array_map('trim', explode(',', Settings::Get('system.mysql_access_host'))) as $mysql_access_host) { - $stmt = Database::prepare("SET PASSWORD FOR :dbname@:host = PASSWORD(:password)"); - $params = array( - "dbname" => $result['databasename'], - "host" => $mysql_access_host, - "password" => $password - ); - Database::pexecute($stmt, $params, true, true); + $dbmgr->getManager()->grantPrivilegesTo($result['databasename'], $password, $mysql_access_host, false, true); } $stmt = Database::prepare("FLUSH PRIVILEGES"); diff --git a/lib/Froxlor/Database/Manager/DbManagerMySQL.php b/lib/Froxlor/Database/Manager/DbManagerMySQL.php index 5919bd72..e98ea86b 100644 --- a/lib/Froxlor/Database/Manager/DbManagerMySQL.php +++ b/lib/Froxlor/Database/Manager/DbManagerMySQL.php @@ -72,33 +72,37 @@ class DbManagerMySQL * @param string $access_host * @param bool $p_encrypted * optional, whether the password is encrypted or not, default false + * @param bool $update + * optional, whether to update the password only (not create user) */ - public function grantPrivilegesTo($username = null, $password = null, $access_host = null, $p_encrypted = false) + public function grantPrivilegesTo($username = null, $password = null, $access_host = null, $p_encrypted = false, $update = false) { - // mysql8 compatibility - if (version_compare(Database::getAttribute(\PDO::ATTR_SERVER_VERSION), '8.0.11', '>=')) { - // create user - $stmt = Database::prepare(" - CREATE USER '" . $username . "'@'" . $access_host . "' IDENTIFIED BY 'password' - "); - Database::pexecute($stmt); - // grant privileges - $stmt = Database::prepare(" - GRANT ALL ON `" . $username . "`.* TO :username@:host - "); - Database::pexecute($stmt, array( - "username" => $username, - "host" => $access_host - )); - } else { - // grant privileges - $stmt = Database::prepare(" - GRANT ALL PRIVILEGES ON `" . $username . "`.* TO :username@:host IDENTIFIED BY 'password' - "); - Database::pexecute($stmt, array( - "username" => $username, - "host" => $access_host - )); + if (! $update) { + // mysql8 compatibility + if (version_compare(Database::getAttribute(\PDO::ATTR_SERVER_VERSION), '8.0.11', '>=')) { + // create user + $stmt = Database::prepare(" + CREATE USER '" . $username . "'@'" . $access_host . "' IDENTIFIED BY 'password' + "); + Database::pexecute($stmt); + // grant privileges + $stmt = Database::prepare(" + GRANT ALL ON `" . $username . "`.* TO :username@:host + "); + Database::pexecute($stmt, array( + "username" => $username, + "host" => $access_host + )); + } else { + // grant privileges + $stmt = Database::prepare(" + GRANT ALL PRIVILEGES ON `" . $username . "`.* TO :username@:host IDENTIFIED BY 'password' + "); + Database::pexecute($stmt, array( + "username" => $username, + "host" => $access_host + )); + } } // set passoword if (version_compare(Database::getAttribute(\PDO::ATTR_SERVER_VERSION), '5.7.6', '<')) {