mirror of
https://github.com/myvesta/vesta
synced 2025-08-19 21:04:07 -07:00
change database username
This commit is contained in:
parent
260cc5171a
commit
8f9c7272b9
4 changed files with 133 additions and 9 deletions
97
bin/v-change-database-user
Executable file
97
bin/v-change-database-user
Executable file
|
@ -0,0 +1,97 @@
|
||||||
|
#!/bin/bash
|
||||||
|
# info: change database username
|
||||||
|
# options: USER DATABASE DBUSER [DBPASS]
|
||||||
|
#
|
||||||
|
# The function for changing database user. It uses the
|
||||||
|
|
||||||
|
|
||||||
|
#----------------------------------------------------------#
|
||||||
|
# Variable&Function #
|
||||||
|
#----------------------------------------------------------#
|
||||||
|
|
||||||
|
# Argument defenition
|
||||||
|
user=$1
|
||||||
|
database=$2
|
||||||
|
dbuser="$user"_"$3"
|
||||||
|
dbpass=$4
|
||||||
|
|
||||||
|
# Includes
|
||||||
|
source $VESTA/func/main.sh
|
||||||
|
source $VESTA/func/db.sh
|
||||||
|
source $VESTA/func/rebuild.sh
|
||||||
|
source $VESTA/conf/vesta.conf
|
||||||
|
|
||||||
|
# Hiding password
|
||||||
|
A4='******'
|
||||||
|
EVENT="DATE='$DATE' TIME='$TIME' CMD='$SCRIPT' A1='$A1' A2='$A2' A3='$A3'"
|
||||||
|
EVENT="$EVENT A4='$A4' A5='$A5' A6='$A6' A7='$A7' A8='$A8' A9='$A9'"
|
||||||
|
|
||||||
|
|
||||||
|
#----------------------------------------------------------#
|
||||||
|
# Verifications #
|
||||||
|
#----------------------------------------------------------#
|
||||||
|
|
||||||
|
check_args '3' "$#" 'USER DATABASE DBUSER [DBPASS]'
|
||||||
|
validate_format 'user' 'database' 'dbuser'
|
||||||
|
if [ ! -z "$dbpass" ]; then
|
||||||
|
validate_format 'dbpass'
|
||||||
|
fi
|
||||||
|
is_system_enabled "$DB_SYSTEM" 'DB_SYSTEM'
|
||||||
|
is_object_valid 'user' 'USER' "$user"
|
||||||
|
is_object_unsuspended 'user' 'USER' "$user"
|
||||||
|
is_object_valid 'db' 'DB' "$database"
|
||||||
|
is_object_unsuspended 'db' 'DB' "$database"
|
||||||
|
|
||||||
|
# Compare old and new user
|
||||||
|
old_dbuser=$(get_object_value 'db' 'DB' "$database" '$DBUSER')
|
||||||
|
if [ "$old_dbuser" = "$dbuser" ]; then
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
#----------------------------------------------------------#
|
||||||
|
# Action #
|
||||||
|
#----------------------------------------------------------#
|
||||||
|
|
||||||
|
# Set new dbuser
|
||||||
|
update_object_value 'db' 'DB' "$database" '$DBUSER' "$dbuser"
|
||||||
|
|
||||||
|
# Get database values
|
||||||
|
get_database_values
|
||||||
|
|
||||||
|
# Rebuild datbase
|
||||||
|
case $TYPE in
|
||||||
|
mysql) rebuild_mysql_database ;;
|
||||||
|
pgsql) rebuild_pgsql_database ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Change password
|
||||||
|
if [ ! -z "$dbpass" ]; then
|
||||||
|
case $TYPE in
|
||||||
|
mysql) change_mysql_password ;;
|
||||||
|
pgsql) change_pgsql_password ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Update config value
|
||||||
|
update_object_value 'db' 'DB' "$database" '$MD5' "$md5"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Remove old user
|
||||||
|
check_old_dbuser=$(grep "DBUSER='$old_dbuser'" $USER_DATA/db.conf)
|
||||||
|
if [ -z "$check_old_dbuser" ]; then
|
||||||
|
case $TYPE in
|
||||||
|
mysql) delete_mysql_user ;;
|
||||||
|
pgsql) delete_pgsql_user ;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
#----------------------------------------------------------#
|
||||||
|
# Vesta #
|
||||||
|
#----------------------------------------------------------#
|
||||||
|
|
||||||
|
# Logging
|
||||||
|
log_history "changed $database database user to $dbuser"
|
||||||
|
log_event "$OK" "$EVENT"
|
||||||
|
|
||||||
|
exit
|
|
@ -51,15 +51,34 @@ if ($return_var != 0) {
|
||||||
// Action
|
// Action
|
||||||
if (!empty($_POST['save'])) {
|
if (!empty($_POST['save'])) {
|
||||||
$v_username = $user;
|
$v_username = $user;
|
||||||
// Change password
|
|
||||||
if (($v_password != $_POST['v_password']) && (empty($_SESSION['error_msg']))) {
|
// Change database username
|
||||||
|
if (($v_dbuser != $_POST['v_dbuser']) && (empty($_SESSION['error_msg']))) {
|
||||||
|
$v_dbuser = preg_replace("/^".$user."_/", "", $_POST['v_dbuser']);
|
||||||
|
$v_dbuser = escapeshellarg($v_dbuser);
|
||||||
|
if ($v_password != $_POST['v_password']) {
|
||||||
|
// Change username and password
|
||||||
|
$v_password = escapeshellarg($_POST['v_password']);
|
||||||
|
exec (VESTA_CMD."v-change-database-user ".$v_username." ".$v_database." ".$v_dbuser." ".$v_password, $output, $return_var);
|
||||||
|
check_return_code($return_var,$output);
|
||||||
|
unset($output);
|
||||||
|
$v_dbuser = $user."_".preg_replace("/^".$user."_/", "", $_POST['v_dbuser']);
|
||||||
|
$v_password = "••••••••";
|
||||||
|
$v_pw_changed = 'yes';
|
||||||
|
} else {
|
||||||
|
// Change only username
|
||||||
|
exec (VESTA_CMD."v-change-database-user ".$v_username." ".$v_database." ".$v_dbuser, $output, $return_var);
|
||||||
|
check_return_code($return_var,$output);
|
||||||
|
unset($output);
|
||||||
|
$v_dbuser = $user."_".preg_replace("/^".$user."_/", "", $_POST['v_dbuser']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Change only database password
|
||||||
|
if (($v_password != $_POST['v_password']) && (!isset($v_pw_changed)) && (empty($_SESSION['error_msg']))) {
|
||||||
$v_password = escapeshellarg($_POST['v_password']);
|
$v_password = escapeshellarg($_POST['v_password']);
|
||||||
exec (VESTA_CMD."v-change-database-password ".$v_username." ".$v_database." ".$v_password, $output, $return_var);
|
exec (VESTA_CMD."v-change-database-password ".$v_username." ".$v_database." ".$v_password, $output, $return_var);
|
||||||
if ($return_var != 0) {
|
check_return_code($return_var,$output);
|
||||||
$error = implode('<br>', $output);
|
|
||||||
if (empty($error)) $error = __('Error code:',$return_var);
|
|
||||||
$_SESSION['error_msg'] = $error;
|
|
||||||
}
|
|
||||||
$v_password = "••••••••";
|
$v_password = "••••••••";
|
||||||
unset($output);
|
unset($output);
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,6 +96,14 @@ function check_error($return_var){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function check_return_code($return_var,$output) {
|
||||||
|
if ($return_var != 0) {
|
||||||
|
$error = implode('<br>', $output);
|
||||||
|
if (empty($error)) $error = __('Error code:',$return_var);
|
||||||
|
$_SESSION['error_msg'] = $error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function top_panel($user, $TAB) {
|
function top_panel($user, $TAB) {
|
||||||
global $panel;
|
global $panel;
|
||||||
$command = VESTA_CMD."v-list-user '".$user."' 'json'";
|
$command = VESTA_CMD."v-list-user '".$user."' 'json'";
|
||||||
|
|
|
@ -83,7 +83,7 @@
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<input type="text" size="20" class="vst-input" name="v_dbuser" <?php if (!empty($v_dbuser)) echo "value=".$v_dbuser; ?> disabled>
|
<input type="text" size="20" class="vst-input" name="v_dbuser" <?php if (!empty($v_dbuser)) echo "value=".$v_dbuser; ?>>
|
||||||
<small class="hint"></small>
|
<small class="hint"></small>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue