You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							110 lines
						
					
					
						
							3.0 KiB
						
					
					
				
			
		
		
	
	
							110 lines
						
					
					
						
							3.0 KiB
						
					
					
				#!/bin/bash
 | 
						|
# info: change database username
 | 
						|
# options: USER DATABASE DBUSER [DBPASS]
 | 
						|
#
 | 
						|
# example: v-change-database-user admin my_db joe_user
 | 
						|
#
 | 
						|
# This function for changing database user. It uses the
 | 
						|
 | 
						|
#----------------------------------------------------------#
 | 
						|
#                Variables & Functions                     #
 | 
						|
#----------------------------------------------------------#
 | 
						|
 | 
						|
# Argument definition
 | 
						|
user=$1
 | 
						|
database=$2
 | 
						|
dbuser="$user"_"$3"
 | 
						|
password=$4
 | 
						|
HIDE=4
 | 
						|
 | 
						|
# Includes
 | 
						|
# shellcheck source=/etc/hestiacp/hestia.conf
 | 
						|
source /etc/hestiacp/hestia.conf
 | 
						|
# shellcheck source=/usr/local/hestia/func/main.sh
 | 
						|
source $HESTIA/func/main.sh
 | 
						|
# shellcheck source=/usr/local/hestia/func/db.sh
 | 
						|
source $HESTIA/func/db.sh
 | 
						|
# shellcheck source=/usr/local/hestia/func/rebuild.sh
 | 
						|
source $HESTIA/func/rebuild.sh
 | 
						|
# load config file
 | 
						|
source_conf "$HESTIA/conf/hestia.conf"
 | 
						|
 | 
						|
#----------------------------------------------------------#
 | 
						|
#                    Verifications                         #
 | 
						|
#----------------------------------------------------------#
 | 
						|
 | 
						|
check_args '3' "$#" 'USER DATABASE DBUSER [DBPASS]'
 | 
						|
is_format_valid 'user' 'database' 'dbuser'
 | 
						|
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_new 'db' 'DBUSER' "$dbuser"
 | 
						|
is_object_unsuspended 'db' 'DB' "$database"
 | 
						|
is_password_valid
 | 
						|
dbpass="$password"
 | 
						|
 | 
						|
# Perform verification if read-only mode is enabled
 | 
						|
check_hestia_demo_mode
 | 
						|
 | 
						|
#----------------------------------------------------------#
 | 
						|
#                       Action                             #
 | 
						|
#----------------------------------------------------------#
 | 
						|
 | 
						|
# Compare old and new user
 | 
						|
old_dbuser=$(get_object_value 'db' 'DB' "$database" '$DBUSER')
 | 
						|
if [ "$old_dbuser" = "$dbuser" ]; then
 | 
						|
	exit
 | 
						|
fi
 | 
						|
 | 
						|
# Set new dbuser
 | 
						|
update_object_value 'db' 'DB' "$database" '$DBUSER' "$dbuser"
 | 
						|
 | 
						|
# Get database values
 | 
						|
get_database_values
 | 
						|
 | 
						|
#Fix issue #1084 with "Upper case not allowed with PGSQL"
 | 
						|
if [ "$TYPE" = "pgsql" ]; then
 | 
						|
	dbuser=$(echo $dbuser | tr '[:upper:]' '[:lower:]')
 | 
						|
	exclude="-"
 | 
						|
	if [[ "$dbuser" =~ $exclude ]]; then
 | 
						|
		check_result "$E_INVALID" "invalid database user format"
 | 
						|
	fi
 | 
						|
fi
 | 
						|
 | 
						|
# Rebuild database
 | 
						|
case $TYPE in
 | 
						|
	mysql) rebuild_mysql_database ;;
 | 
						|
	pgsql) rebuild_pgsql_database ;;
 | 
						|
esac
 | 
						|
 | 
						|
# Change password
 | 
						|
if [ -n "$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
 | 
						|
 | 
						|
#----------------------------------------------------------#
 | 
						|
#                       Hestia                             #
 | 
						|
#----------------------------------------------------------#
 | 
						|
 | 
						|
# Logging
 | 
						|
$BIN/v-log-action "$user" "Info" "Database" "Database user changed (Database: $database, User: $dbuser)"
 | 
						|
log_event "$OK" "$ARGUMENTS"
 | 
						|
 | 
						|
exit
 |