Skip to content

Commit 317b4d0

Browse files
author
mcramer
committed
Fixed: Some improvements and fixes for the database/user separation
1 parent 5c80fff commit 317b4d0

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

interface/lib/plugins/sites_web_database_user_plugin.inc.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ function sites_web_database_user_edit($event_name, $page_form) {
3030
// also make sure that the user can not delete domain created by a admin
3131
if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($page_form->dataRecord["client_group_id"])) {
3232
$client_group_id = intval($page_form->dataRecord["client_group_id"]);
33-
$app->db->query("UPDATE web_database_user SET sys_groupid = $client_group_id, sys_perm_group = 'ru' WHERE domain_id = ".$page_form->id);
33+
$app->db->query("UPDATE web_database_user SET sys_groupid = $client_group_id, sys_perm_group = 'ru' WHERE database_user_id = ".$page_form->id);
3434
}
3535
if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($page_form->dataRecord["client_group_id"])) {
3636
$client_group_id = intval($page_form->dataRecord["client_group_id"]);
37-
$app->db->query("UPDATE web_database_user SET sys_groupid = $client_group_id, sys_perm_group = 'riud' WHERE domain_id = ".$page_form->id);
37+
$app->db->query("UPDATE web_database_user SET sys_groupid = $client_group_id, sys_perm_group = 'riud' WHERE database_user_id = ".$page_form->id);
3838
}
39+
$app->db->query("UPDATE web_database_user SET server_id = '" . intval($conf['server_id']) . "' WHERE database_user_id = ".$page_form->id);
3940
}
4041
}

interface/web/sites/database_edit.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ function onUpdateSave($sql) {
309309
global $app;
310310
if(!empty($sql) && !$app->tform->isReadonlyTab($app->tform->getCurrentTab(),$this->id)) {
311311

312+
$app->uses('sites_database_plugin');
312313
$app->sites_database_plugin->processDatabaseUpdate($this);
313314

314315
$app->db->query($sql);

interface/web/sites/database_user_edit.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ function onBeforeUpdate() {
145145
$this->dataRecord['database_user'] = substr($dbuser_prefix . $this->dataRecord['database_user'], 0, 16);
146146
}
147147

148+
$this->dataRecord['server_id'] = $conf['server_id'];
149+
148150
parent::onBeforeUpdate();
149151
}
150152

@@ -173,6 +175,8 @@ function onBeforeInsert() {
173175
$this->dataRecord['database_user'] = substr($dbuser_prefix . $this->dataRecord['database_user'], 0, 16);
174176
}
175177

178+
$this->dataRecord['server_id'] = $conf['server_id'];
179+
176180
parent::onBeforeInsert();
177181
}
178182

@@ -200,7 +204,16 @@ function onAfterUpdate() {
200204
$client_group_id = intval($this->dataRecord["client_group_id"]);
201205
$app->db->query("UPDATE web_database_user SET sys_groupid = $client_group_id, sys_perm_group = 'riud' WHERE database_user_id = ".$this->id);
202206
}
203-
207+
208+
$old_rec = $app->db->queryOneRecord("SELECT * FROM web_database_user WHERE database_user_id = '".$this->id."'");
209+
210+
$records = $app->db->queryAllRecords("SELECT DISTINCT server_id FROM web_database WHERE database_user_id = '".intval($this->id)."' UNION SELECT DISTINCT server_id FROM web_database WHERE database_ro_user_id = '".intval($this->id)."'");
211+
foreach($records as $rec) {
212+
$new_rec = $this->dataRecord;
213+
$new_rec['server_id'] = $rec['server_id'];
214+
$app->db->datalogSave('web_database_user', 'UPDATE', 'database_user_id', $this->id, $old_rec, $new_rec);
215+
}
216+
unset($new_rec);
204217
}
205218

206219
}

interface/web/sites/form/database_user.tform.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@
6363
##################################
6464
# Begin Datatable fields
6565
##################################
66+
'server_id' => array (
67+
'datatype' => 'INTEGER',
68+
'formtype' => 'SELECT',
69+
'default' => '',
70+
'datasource' => array ( 'type' => 'SQL',
71+
'querystring' => 'SELECT server_id,server_name FROM server WHERE mirror_server_id = 0 AND {AUTHSQL} AND db_server = 1 ORDER BY server_name',
72+
'keyfield'=> 'server_id',
73+
'valuefield'=> 'server_name'
74+
),
75+
'value' => ''
76+
),
6677
'database_user' => array (
6778
'datatype' => 'VARCHAR',
6879
'formtype' => 'TEXT',

server/plugins-available/mysql_clientdb_plugin.inc.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ function process_host_list($action, $database_name, $database_user, $database_pa
8888
foreach($host_list as $db_host) {
8989
$db_host = trim($db_host);
9090

91+
$app->log($action . ' for user ' . $database_user . ' at host ' . $db_host, LOGLEVEL_DEBUG);
92+
9193
// check if entry is valid ip address
9294
$valid = true;
93-
if($db_host == '%') {
95+
if($db_host == '%' || $db_host == 'localhost') {
9496
$valid = true;
9597
} elseif(preg_match("/^[0-9]{1,3}(\.)[0-9]{1,3}(\.)[0-9]{1,3}(\.)[0-9]{1,3}$/", $db_host)) {
9698
$groups = explode('.', $db_host);
@@ -106,6 +108,7 @@ function process_host_list($action, $database_name, $database_user, $database_pa
106108

107109
if($action == 'GRANT') {
108110
if(!$link->query("GRANT " . ($user_read_only ? "SELECT" : "ALL") . " ON ".$link->escape_string($database_name).".* TO '".$link->escape_string($database_user)."'@'$db_host' IDENTIFIED BY PASSWORD '".$link->escape_string($database_password)."';")) $success = false;
111+
$app->log("GRANT " . ($user_read_only ? "SELECT" : "ALL") . " ON ".$link->escape_string($database_name).".* TO '".$link->escape_string($database_user)."'@'$db_host' IDENTIFIED BY PASSWORD '".$link->escape_string($database_password)."'; success? " . ($success ? 'yes' : 'no'), LOGLEVEL_DEBUG);
109112
} elseif($action == 'REVOKE') {
110113
if(!$link->query("REVOKE ALL PRIVILEGES ON ".$link->escape_string($database_name).".* FROM '".$link->escape_string($database_user)."'@'$db_host' IDENTIFIED BY PASSWORD '".$link->escape_string($database_password)."';")) $success = false;
111114
} elseif($action == 'DROP') {
@@ -388,9 +391,8 @@ function db_user_update($event_name,$data) {
388391
}
389392

390393
if($data['new']['database_password'] != $data['old']['database_password']) {
391-
$db_host = 'localhost';
392-
$link->query("SET PASSWORD FOR '".$link->escape_string($data['new']['database_user'])."'@'$db_host' = '".$link->escape_string($data['new']['database_password'])."';");
393-
$app->log('Changing MySQL user password for: '.$data['new']['database_user'],LOGLEVEL_DEBUG);
394+
$link->query("SET PASSWORD FOR '".$link->escape_string($data['new']['database_user'])."'@'$db_host' = PASSWORD('".$link->escape_string($data['new']['database_password'])."');"); // is contained in clear text so PASSWORD() func is needed
395+
$app->log('Changing MySQL user password for: '.$data['new']['database_user'].'@'.$db_host,LOGLEVEL_DEBUG);
394396
}
395397
}
396398

0 commit comments

Comments
 (0)