v-change-database-user 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/bin/bash
  2. # info: change database username
  3. # options: USER DATABASE DBUSER [DBPASS]
  4. # labels:
  5. #
  6. # example: v-change-database-user admin my_db joe_user
  7. #
  8. # The function for changing database user. It uses the
  9. #----------------------------------------------------------#
  10. # Variable&Function #
  11. #----------------------------------------------------------#
  12. # Argument definition
  13. user=$1
  14. database=$2
  15. dbuser="$user"_"$3"
  16. password=$4; HIDE=4
  17. # Includes
  18. # shellcheck source=/usr/local/hestia/func/main.sh
  19. source $HESTIA/func/main.sh
  20. # shellcheck source=/usr/local/hestia/func/db.sh
  21. source $HESTIA/func/db.sh
  22. # shellcheck source=/usr/local/hestia/func/rebuild.sh
  23. source $HESTIA/func/rebuild.sh
  24. # shellcheck source=/usr/local/hestia/conf/hestia.conf
  25. source $HESTIA/conf/hestia.conf
  26. #----------------------------------------------------------#
  27. # Verifications #
  28. #----------------------------------------------------------#
  29. check_args '3' "$#" 'USER DATABASE DBUSER [DBPASS]'
  30. is_format_valid 'user' 'database' 'dbuser'
  31. is_system_enabled "$DB_SYSTEM" 'DB_SYSTEM'
  32. is_object_valid 'user' 'USER' "$user"
  33. is_object_unsuspended 'user' 'USER' "$user"
  34. is_object_valid 'db' 'DB' "$database"
  35. is_object_unsuspended 'db' 'DB' "$database"
  36. is_password_valid
  37. dbpass="$password"
  38. # Perform verification if read-only mode is enabled
  39. check_hestia_demo_mode
  40. #----------------------------------------------------------#
  41. # Action #
  42. #----------------------------------------------------------#
  43. # Compare old and new user
  44. old_dbuser=$(get_object_value 'db' 'DB' "$database" '$DBUSER')
  45. if [ "$old_dbuser" = "$dbuser" ]; then
  46. exit
  47. fi
  48. # Set new dbuser
  49. update_object_value 'db' 'DB' "$database" '$DBUSER' "$dbuser"
  50. # Get database values
  51. get_database_values
  52. #Fix issue #1084 with "Upper case not allowed with PGSQL"
  53. if [ "$TYPE" = "pgsql" ]; then
  54. dbuser=$(echo $dbuser | tr '[:upper:]' '[:lower:]');
  55. fi
  56. # Rebuild database
  57. case $TYPE in
  58. mysql) rebuild_mysql_database ;;
  59. pgsql) rebuild_pgsql_database ;;
  60. esac
  61. # Change password
  62. if [ ! -z "$dbpass" ]; then
  63. case $TYPE in
  64. mysql) change_mysql_password ;;
  65. pgsql) change_pgsql_password ;;
  66. esac
  67. # Update config value
  68. update_object_value 'db' 'DB' "$database" '$MD5' "$md5"
  69. fi
  70. # Remove old user
  71. check_old_dbuser=$(grep "DBUSER='$old_dbuser'" $USER_DATA/db.conf)
  72. if [ -z "$check_old_dbuser" ]; then
  73. case $TYPE in
  74. mysql) delete_mysql_user ;;
  75. pgsql) delete_pgsql_user ;;
  76. esac
  77. fi
  78. #----------------------------------------------------------#
  79. # Hestia #
  80. #----------------------------------------------------------#
  81. # Logging
  82. $BIN/v-log-action "$user" "Info" "Database" "Database user changed (Database: $database, User: $dbuser)"
  83. log_event "$OK" "$ARGUMENTS"
  84. exit