User permissions issue

SYSTEM INFORMATION
OS type and version Ubuntu 26.04.1 LTS
Webmin version 2.660
Virtualmin version 8.1.0 GPL
Usermin version 2.560

I’ve created a bash script for users that updates the list of databases for that user when they have created or deleted them within a 3rd party app (Phpmyadmin). Everything works fine when the user triggers the script except Webmin always reverts the user access to “Custom Commands” as disabled after using it once.

Furthermore, if I (as root admin) go into Webmin to adjust user permission and allow them to use Custom Commands again, the file permissions for all sites get misconfigured somehow because FTP stops allowing file access. This forces me to have to go into Virtualmin as root and fix file permissions for all sites.

This repeats anytime that user initiates the script.

Here is the script ( with confidential data changed ) :

# Sync databases with the username prefix created by a 3rd party script for username @ domain_name
# files must be run solely as root to work properly therefore the predefined variables are not working and must be manually adjusted to specifics
#username=$USER;
#dbpassword=$PASS;
#domain_name=$IDNDOM;
username="USERNAME";
dbpassword="DBPASSWORD";
domain_name="DOMAINNAME";
db_pattern="$username%"
count=0
declare -A db_array=()

for db in $(mysql -u "$username" -p"$dbpassword" -e "SHOW DATABASES LIKE '$db_pattern';" -s --skip-column-names | grep -Ev "information_schema|performance_schema|sys|mysql"); do
	virtualmin list-databases --domain "$domain_name" --name-only 2>/dev/null | grep -xFq "$db"
	if [ $? -eq 0 ]; then
		echo "'$username' has access to '$db' via domain '$domain_name'."
	else
		((count++))
		echo -e "\n"
		virtualmin import-database --domain "$domain_name" --type mysql --name "$db"
		echo -e "\n"
	fi
	db_array["$db"]="db_exists_flag"
done

if [ "$count" -eq 0 ]; then
	echo -e "\nNo additional databases were found for '$username' @ '$domain_name'\n"
elif [ "$count" -eq 1 ]; then
		echo -e "\nAn additional database was added to '$username' @ '$domain_name'\n"
else
		echo -e "\nAn additional '$count' databases were added to '$username' @ '$domain_name'\n"
fi
virtualmin list-databases --domain "$domain_name" --type mysql --name-only 2>/dev/null | while read -r db; do
	if [[ ! -v db_array[$db] ]]; then
		echo -e "\n⚠️ Found orphaned database '$db' on domain '$domain_name'. Disconnecting..."
		virtualmin disconnect-database --domain "$domain_name" --type mysql --name "$db"
		echo "✅ Successfully disconnected '$db' from '$domain_name'."
	else
		echo "Detected DB: '$db'"
	fi
done

The script is executed with a button via: bash /path/to/file/file.sh
Within the script itself, if I comment out the lines containing the commands: “virtualmin disconnect-database” & “virtualmin import-database” then the script will not affect any permissions. However, I would like those commands to work without changing other unintended settings.

If I set it up so that a user can use a custom command that I create, then why is it changing other settings including that very permission itself?

To clarify… these things happen in order.
When the script is executed, it changes the user permission to use/view the custom command to false/disabled. At that point the website file permissions are fine and not affected.
When I then use Webmin to give the user back that permission to use the custom commands module, it ends up altering “all” website file permissions on the server.

What is happening here?

I figured out that I need to adjust the module usage from Virtualmin as root as opposed to using the Webmin interface.

As root user:
Navigate to Virtualmin ➔ System Settings ➔ [Server Templates] ➔ Default Settings
From the dropdown select: ➔ Administrators Webmin modules
Checkmark the “custom module” option and save the setting.

This allows the user to use the command without affecting any permissions.
However, I have to run the script as root & can’t use some of the predefined variables which forces me to hard code them.

If running a script as root, how do I access the user variables that initially executed the script?
Such as user name, password, domain name, etc. ?
Currently if someone runs the script it only works to have it run the command as root but I can only access the root variables in from within the script and the p/w is blank. Are there other global variables that carry the values of the user that clicked the button to execute it?
If I enable the setting to run as the user and/or “Use user’s environment” it does not work.