Skip to content

Commit 8f9c727

Browse files
committed
change database username
1 parent 260cc51 commit 8f9c727

File tree

4 files changed

+133
-9
lines changed

4 files changed

+133
-9
lines changed

bin/v-change-database-user

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/bin/bash
2+
# info: change database username
3+
# options: USER DATABASE DBUSER [DBPASS]
4+
#
5+
# The function for changing database user. It uses the
6+
7+
8+
#----------------------------------------------------------#
9+
# Variable&Function #
10+
#----------------------------------------------------------#
11+
12+
# Argument defenition
13+
user=$1
14+
database=$2
15+
dbuser="$user"_"$3"
16+
dbpass=$4
17+
18+
# Includes
19+
source $VESTA/func/main.sh
20+
source $VESTA/func/db.sh
21+
source $VESTA/func/rebuild.sh
22+
source $VESTA/conf/vesta.conf
23+
24+
# Hiding password
25+
A4='******'
26+
EVENT="DATE='$DATE' TIME='$TIME' CMD='$SCRIPT' A1='$A1' A2='$A2' A3='$A3'"
27+
EVENT="$EVENT A4='$A4' A5='$A5' A6='$A6' A7='$A7' A8='$A8' A9='$A9'"
28+
29+
30+
#----------------------------------------------------------#
31+
# Verifications #
32+
#----------------------------------------------------------#
33+
34+
check_args '3' "$#" 'USER DATABASE DBUSER [DBPASS]'
35+
validate_format 'user' 'database' 'dbuser'
36+
if [ ! -z "$dbpass" ]; then
37+
validate_format 'dbpass'
38+
fi
39+
is_system_enabled "$DB_SYSTEM" 'DB_SYSTEM'
40+
is_object_valid 'user' 'USER' "$user"
41+
is_object_unsuspended 'user' 'USER' "$user"
42+
is_object_valid 'db' 'DB' "$database"
43+
is_object_unsuspended 'db' 'DB' "$database"
44+
45+
# Compare old and new user
46+
old_dbuser=$(get_object_value 'db' 'DB' "$database" '$DBUSER')
47+
if [ "$old_dbuser" = "$dbuser" ]; then
48+
exit
49+
fi
50+
51+
52+
#----------------------------------------------------------#
53+
# Action #
54+
#----------------------------------------------------------#
55+
56+
# Set new dbuser
57+
update_object_value 'db' 'DB' "$database" '$DBUSER' "$dbuser"
58+
59+
# Get database values
60+
get_database_values
61+
62+
# Rebuild datbase
63+
case $TYPE in
64+
mysql) rebuild_mysql_database ;;
65+
pgsql) rebuild_pgsql_database ;;
66+
esac
67+
68+
# Change password
69+
if [ ! -z "$dbpass" ]; then
70+
case $TYPE in
71+
mysql) change_mysql_password ;;
72+
pgsql) change_pgsql_password ;;
73+
esac
74+
75+
# Update config value
76+
update_object_value 'db' 'DB' "$database" '$MD5' "$md5"
77+
fi
78+
79+
# Remove old user
80+
check_old_dbuser=$(grep "DBUSER='$old_dbuser'" $USER_DATA/db.conf)
81+
if [ -z "$check_old_dbuser" ]; then
82+
case $TYPE in
83+
mysql) delete_mysql_user ;;
84+
pgsql) delete_pgsql_user ;;
85+
esac
86+
fi
87+
88+
89+
#----------------------------------------------------------#
90+
# Vesta #
91+
#----------------------------------------------------------#
92+
93+
# Logging
94+
log_history "changed $database database user to $dbuser"
95+
log_event "$OK" "$EVENT"
96+
97+
exit

web/edit/db/index.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,34 @@
5151
// Action
5252
if (!empty($_POST['save'])) {
5353
$v_username = $user;
54-
// Change password
55-
if (($v_password != $_POST['v_password']) && (empty($_SESSION['error_msg']))) {
54+
55+
// Change database username
56+
if (($v_dbuser != $_POST['v_dbuser']) && (empty($_SESSION['error_msg']))) {
57+
$v_dbuser = preg_replace("/^".$user."_/", "", $_POST['v_dbuser']);
58+
$v_dbuser = escapeshellarg($v_dbuser);
59+
if ($v_password != $_POST['v_password']) {
60+
// Change username and password
61+
$v_password = escapeshellarg($_POST['v_password']);
62+
exec (VESTA_CMD."v-change-database-user ".$v_username." ".$v_database." ".$v_dbuser." ".$v_password, $output, $return_var);
63+
check_return_code($return_var,$output);
64+
unset($output);
65+
$v_dbuser = $user."_".preg_replace("/^".$user."_/", "", $_POST['v_dbuser']);
66+
$v_password = "••••••••";
67+
$v_pw_changed = 'yes';
68+
} else {
69+
// Change only username
70+
exec (VESTA_CMD."v-change-database-user ".$v_username." ".$v_database." ".$v_dbuser, $output, $return_var);
71+
check_return_code($return_var,$output);
72+
unset($output);
73+
$v_dbuser = $user."_".preg_replace("/^".$user."_/", "", $_POST['v_dbuser']);
74+
}
75+
}
76+
77+
// Change only database password
78+
if (($v_password != $_POST['v_password']) && (!isset($v_pw_changed)) && (empty($_SESSION['error_msg']))) {
5679
$v_password = escapeshellarg($_POST['v_password']);
5780
exec (VESTA_CMD."v-change-database-password ".$v_username." ".$v_database." ".$v_password, $output, $return_var);
58-
if ($return_var != 0) {
59-
$error = implode('<br>', $output);
60-
if (empty($error)) $error = __('Error code:',$return_var);
61-
$_SESSION['error_msg'] = $error;
62-
}
81+
check_return_code($return_var,$output);
6382
$v_password = "••••••••";
6483
unset($output);
6584
}

web/inc/main.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,21 @@ function _translate() {
8989
}
9090

9191

92-
function check_error($return_var){
92+
function check_error($return_var) {
9393
if ( $return_var > 0 ) {
9494
header("Location: /error/");
9595
exit;
9696
}
9797
}
9898

99+
function check_return_code($return_var,$output) {
100+
if ($return_var != 0) {
101+
$error = implode('<br>', $output);
102+
if (empty($error)) $error = __('Error code:',$return_var);
103+
$_SESSION['error_msg'] = $error;
104+
}
105+
}
106+
99107
function top_panel($user, $TAB) {
100108
global $panel;
101109
$command = VESTA_CMD."v-list-user '".$user."' 'json'";

web/templates/admin/edit_db.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
</tr>
8484
<tr>
8585
<td>
86-
<input type="text" size="20" class="vst-input" name="v_dbuser" <?php if (!empty($v_dbuser)) echo "value=".$v_dbuser; ?> disabled>
86+
<input type="text" size="20" class="vst-input" name="v_dbuser" <?php if (!empty($v_dbuser)) echo "value=".$v_dbuser; ?>>
8787
<small class="hint"></small>
8888
</td>
8989
</tr>

0 commit comments

Comments
 (0)