This commit is contained in:
Bozhidar 2024-09-13 12:25:57 +03:00
parent 0ab0d12b08
commit e0291b5d8f
4 changed files with 72 additions and 13 deletions

View file

@ -67,10 +67,23 @@ class Database extends Model
PhyreConfig::get('MYSQL_ROOT_PASSWORD'),
);
$universalDatabaseExecutor->fixPasswordPolicy();
// Check main database user exists
$mainDatabaseUser = $universalDatabaseExecutor->getUserByUsername($findHostingSubscription->system_username);
if (!$mainDatabaseUser) {
$createMainDatabaseUser = $universalDatabaseExecutor->createUser($findHostingSubscription->system_username, $findHostingSubscription->system_password);
if (!isset($createMainDatabaseUser['success'])) {
throw new \Exception($createMainDatabaseUser['message']);
}
}
$createDatabase = $universalDatabaseExecutor->createDatabase($databaseName);
if (isset($createDatabase['error'])) {
throw new \Exception($createDatabase['message']);
}
$universalDatabaseExecutor->userGrantPrivilegesToDatabase($findHostingSubscription->system_username, [$databaseName]);
}
return $model;

View file

@ -63,10 +63,15 @@ class DatabaseUser extends Model
PhyreConfig::get('MYSQL_ROOT_PASSWORD'),
$findDatabase->database_name_prefix . $findDatabase->database_name
);
$createDatabase = $universalDatabaseExecutor->createUser($databaseUsername, $model->password);
if (isset($createDatabase['error'])) {
throw new \Exception($createDatabase['message']);
}
$universalDatabaseExecutor->userGrantPrivilegesToDatabase($databaseUsername, [
$findDatabase->database_name_prefix . $findDatabase->database_name
]);
}
});

View file

@ -36,13 +36,14 @@ class UniversalDatabaseExecutor
'driver' => 'pdo_mysql',
];
$connection = DriverManager::getConnection($connectionParams);
$connection->connect();
if (!$connection->isConnected()) {
throw new \Exception('Could not connect to database');
}
return DriverManager::getConnection($connectionParams);
return $connection;
}
public function fixPasswordPolicy()
{
$connection = $this->_getDatabaseConnection();
$connection->executeQuery('SET GLOBAL validate_password.policy = 0');
}
public function createDatabase($databaseName)
@ -87,6 +88,35 @@ class UniversalDatabaseExecutor
}
public function getUserByUsername($username)
{
$connection = $this->_getDatabaseConnection();
$resultSet = $connection->executeQuery('SELECT * FROM mysql.user WHERE User = ?', [
$username
]);
return $resultSet->fetchAssociative();
}
public function userGrantPrivilegesToDatabase($username, $databases = [])
{
$connection = $this->_getDatabaseConnection();
if (!empty($databases)) {
foreach ($databases as $database) {
$connection->executeStatement('GRANT ALL PRIVILEGES ON '.$database.'.* TO ?', [
$username
]);
}
}
$connection->executeQuery('FLUSH PRIVILEGES');
}
public function createUser($username, $password)
{
try {
@ -97,12 +127,6 @@ class UniversalDatabaseExecutor
$password
]);
$connection->executeStatement('GRANT ALL PRIVILEGES ON '.$this->database.'.* TO ?', [
$username
]);
$connection->executeQuery('FLUSH PRIVILEGES');
return [
'success' => true,
'message' => 'User created successfully'
@ -117,7 +141,24 @@ class UniversalDatabaseExecutor
}
public function deleteUser($username)
{
try {
$connection = $this->_getDatabaseConnection();
$resultSet = $connection->executeStatement('DROP USER ?', [
$username
]);
return [
'success' => true,
'message' => 'User deleted successfully'
];
} catch (\Exception $e) {
return [
'error' => true,
'message' => $e->getMessage()
];
}
}
}

View file

@ -47,7 +47,7 @@ if (isset($_GET['token'])) {
$_SESSION['PMA_single_signon_user'] = $response['databaseLoginDetails']['username'];
$_SESSION['PMA_single_signon_password'] = $response['databaseLoginDetails']['password'];
/* Update another field of server configuration */
$_SESSION['PMA_single_signon_cfgupdate'] = array('verbose' => 'PanelOmega');
$_SESSION['PMA_single_signon_cfgupdate'] = array('verbose' => 'PhyrePanel SSO');
$id = session_id();
/* Close that session */
@session_write_close();