use DbManager for updating the password of a mysql-db
Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
@@ -320,14 +320,9 @@ class Mysqls extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\ResourceEnt
|
|||||||
|
|
||||||
// Begin root-session
|
// Begin root-session
|
||||||
Database::needRoot(true, $result['dbserver']);
|
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) {
|
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)");
|
$dbmgr->getManager()->grantPrivilegesTo($result['databasename'], $password, $mysql_access_host, false, true);
|
||||||
$params = array(
|
|
||||||
"dbname" => $result['databasename'],
|
|
||||||
"host" => $mysql_access_host,
|
|
||||||
"password" => $password
|
|
||||||
);
|
|
||||||
Database::pexecute($stmt, $params, true, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$stmt = Database::prepare("FLUSH PRIVILEGES");
|
$stmt = Database::prepare("FLUSH PRIVILEGES");
|
||||||
|
|||||||
@@ -72,33 +72,37 @@ class DbManagerMySQL
|
|||||||
* @param string $access_host
|
* @param string $access_host
|
||||||
* @param bool $p_encrypted
|
* @param bool $p_encrypted
|
||||||
* optional, whether the password is encrypted or not, default false
|
* 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 (! $update) {
|
||||||
if (version_compare(Database::getAttribute(\PDO::ATTR_SERVER_VERSION), '8.0.11', '>=')) {
|
// mysql8 compatibility
|
||||||
// create user
|
if (version_compare(Database::getAttribute(\PDO::ATTR_SERVER_VERSION), '8.0.11', '>=')) {
|
||||||
$stmt = Database::prepare("
|
// create user
|
||||||
CREATE USER '" . $username . "'@'" . $access_host . "' IDENTIFIED BY 'password'
|
$stmt = Database::prepare("
|
||||||
");
|
CREATE USER '" . $username . "'@'" . $access_host . "' IDENTIFIED BY 'password'
|
||||||
Database::pexecute($stmt);
|
");
|
||||||
// grant privileges
|
Database::pexecute($stmt);
|
||||||
$stmt = Database::prepare("
|
// grant privileges
|
||||||
GRANT ALL ON `" . $username . "`.* TO :username@:host
|
$stmt = Database::prepare("
|
||||||
");
|
GRANT ALL ON `" . $username . "`.* TO :username@:host
|
||||||
Database::pexecute($stmt, array(
|
");
|
||||||
"username" => $username,
|
Database::pexecute($stmt, array(
|
||||||
"host" => $access_host
|
"username" => $username,
|
||||||
));
|
"host" => $access_host
|
||||||
} else {
|
));
|
||||||
// grant privileges
|
} else {
|
||||||
$stmt = Database::prepare("
|
// grant privileges
|
||||||
GRANT ALL PRIVILEGES ON `" . $username . "`.* TO :username@:host IDENTIFIED BY 'password'
|
$stmt = Database::prepare("
|
||||||
");
|
GRANT ALL PRIVILEGES ON `" . $username . "`.* TO :username@:host IDENTIFIED BY 'password'
|
||||||
Database::pexecute($stmt, array(
|
");
|
||||||
"username" => $username,
|
Database::pexecute($stmt, array(
|
||||||
"host" => $access_host
|
"username" => $username,
|
||||||
));
|
"host" => $access_host
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// set passoword
|
// set passoword
|
||||||
if (version_compare(Database::getAttribute(\PDO::ATTR_SERVER_VERSION), '5.7.6', '<')) {
|
if (version_compare(Database::getAttribute(\PDO::ATTR_SERVER_VERSION), '5.7.6', '<')) {
|
||||||
|
|||||||
Reference in New Issue
Block a user