Skip to content

Commit ecfd4fc

Browse files
authored
Merge pull request hestiacp#1104 from hestiacp/fix/1045-database_name-users-changes
Improved DB handling
2 parents 463a58c + 5192355 commit ecfd4fc

File tree

8 files changed

+51
-19
lines changed

8 files changed

+51
-19
lines changed

bin/v-add-database

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ source $HESTIA/func/main.sh
3030
source $HESTIA/func/db.sh
3131
source $HESTIA/conf/hestia.conf
3232

33+
if [ "$type" = "pgsql" ]; then
34+
database=$(echo "$user"_"$2" | tr '[:upper:]' '[:lower:]');
35+
dbuser=$(echo "$user"_"$3" | tr '[:upper:]' '[:lower:]');
36+
fi
37+
3338
#----------------------------------------------------------#
3439
# Verifications #
3540
#----------------------------------------------------------#

bin/v-change-database-owner

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
database=$1
1414
user=$2
1515

16+
1617
# Includes
1718
source $HESTIA/func/main.sh
1819
source $HESTIA/func/db.sh
@@ -51,8 +52,15 @@ if [ -z "$db_data" ]; then
5152
exit $E_NOTEXIST
5253
fi
5354

54-
# Check if datbase name is uniq
55-
new_db=$(echo $database | sed "s/^${owner}_/${user}_/")
55+
#Fix issue #1084 with "Upper case not allowed with PGSQL"
56+
if [ "$TYPE" == "pgsql" ]; then
57+
usersmall=$(echo "$user" | tr '[:upper:]' '[:lower:]');
58+
else
59+
usersmall=$user
60+
fi
61+
62+
# Check if database name is uniqe
63+
new_db=$(echo $database | sed "s/^${owner}_/${usersmall}_/")
5664
check_db=$(grep "DB='$new_db'" $HESTIA/data/users/$user/db.conf)
5765
if [ ! -z "$check_db" ]; then
5866
echo "Error: $new_db database exists"
@@ -84,7 +92,7 @@ case $TYPE in
8492
esac
8593

8694
# Import configuration
87-
db_data=$(echo "$db_data" | sed "s/'${owner}_/'${user}_/g")
95+
db_data=$(echo "$db_data" | sed "s/'${owner}_/'${usersmall}_/g")
8896
echo "$db_data" >> $HESTIA/data/users/$user/db.conf
8997
parse_object_kv_list "$db_data"
9098

bin/v-change-database-user

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ database=$2
1515
dbuser="$user"_"$3"
1616
password=$4; HIDE=4
1717

18+
if [ "$type" = "pgsql" ]; then
19+
dbuser=$(echo "$user"_"$3" | tr '[:upper:]' '[:lower:]');
20+
fi
21+
1822
# Includes
1923
source $HESTIA/func/main.sh
2024
source $HESTIA/func/db.sh

func/main.sh

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -724,9 +724,14 @@ is_common_format_valid() {
724724

725725
# Database format validator
726726
is_database_format_valid() {
727-
exclude="[!|@|#|$|^|&|*|(|)|+|=|{|}|:|,|<|>|?|/|\|\"|'|;|%|\`| ]"
728-
if [[ "$1" =~ $exclude ]] || [ 65 -le ${#1} ]; then
729-
check_result $E_INVALID "invalid $2 format :: $1"
727+
if [ "$3" == "pgsql" ]; then
728+
if ! [[ "$1" =~ ^[0-9a-z_]{1,63}$ ]]; then
729+
check_result $E_INVALID "invalid $2 format :: $1"
730+
fi
731+
else
732+
if ! [[ "$1" =~ ^[0-9a-zA-Z_]{1,64}$ ]]; then
733+
check_result $E_INVALID "invalid $2 format :: $1"
734+
fi
730735
fi
731736
}
732737

@@ -739,12 +744,17 @@ is_date_format_valid() {
739744

740745
# Database user validator
741746
is_dbuser_format_valid() {
742-
exclude="[!|@|#|$|^|&|*|(|)|+|=|{|}|:|,|<|>|?|/|\|\"|'|;|%|\`| ]"
743-
if [ 31 -le ${#1} ]; then
744-
check_result $E_INVALID "mysql username can be up to 30 characters long"
745-
fi
746-
if [[ "$1" =~ $exclude ]]; then
747-
check_result $E_INVALID "invalid $2 format :: $1"
747+
if [ "$3" == "pgsql" ]; then
748+
if ! [[ "$1" =~ ^[0-9a-z_]{1,63}$ ]]; then
749+
check_result $E_INVALID "invalid $2 format :: $1"
750+
fi
751+
else
752+
if [ 33 -le ${#1} ]; then
753+
check_result $E_INVALID "mysql username can be up to 32 characters long"
754+
fi
755+
if ! [[ "$1" =~ ^[0-9a-zA-Z_]{1,64}$ ]]; then
756+
check_result $E_INVALID "invalid $2 format :: $1"
757+
fi
748758
fi
749759
}
750760

@@ -931,10 +941,10 @@ is_format_valid() {
931941
charset) is_object_format_valid "$arg" "$arg_name" ;;
932942
charsets) is_common_format_valid "$arg" 'charsets' ;;
933943
comment) is_object_format_valid "$arg" 'comment' ;;
934-
database) is_database_format_valid "$arg" 'database';;
944+
database) is_database_format_valid "$arg" 'database' $type;;
935945
day) is_cron_format_valid "$arg" $arg_name ;;
936946
dbpass) is_password_format_valid "$arg" ;;
937-
dbuser) is_dbuser_format_valid "$arg" 'dbuser';;
947+
dbuser) is_dbuser_format_valid "$arg" 'dbuser' $type;;
938948
dkim) is_boolean_format_valid "$arg" 'dkim' ;;
939949
dkim_size) is_int_format_valid "$arg" ;;
940950
domain) is_domain_format_valid "$arg" ;;

web/add/db/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
if (!filter_var($_POST['v_db_email'], FILTER_VALIDATE_EMAIL)) {
3939
$_SESSION['error_msg'] = __('Please enter valid email address.');
4040
}
41-
}
41+
}
4242

4343
// Check password length
4444
if (empty($_SESSION['error_msg'])) {

web/edit/db/index.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
5050
header('location: /login/');
5151
exit();
52-
}
53-
52+
}
53+
5454
// Change database user
5555
if (($v_dbuser != $_POST['v_dbuser']) && (empty($_SESSION['error_msg']))) {
5656
$v_dbuser = preg_replace("/^".$user."_/", "", $_POST['v_dbuser']);

web/templates/admin/add_db.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
<?php
7272
print __('User');
7373
// if (is_it_mysql_or_mariadb()=='mysql')
74-
echo "&nbsp;&nbsp;&nbsp;&nbsp;<em><small>(".__('maximum characters length, including prefix', 16).")</small></em>";
74+
echo "&nbsp;&nbsp;&nbsp;&nbsp;<em><small>(".__('maximum characters length, including prefix', 32).")</small></em>";
7575
?>
7676
</td>
7777
</tr>

web/templates/admin/edit_db.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<tr>
6969
<td class="vst-text input-label">
7070
<?php print __('User');?>
71+
<?php echo "&nbsp;&nbsp;&nbsp;&nbsp;<em><small>(".__('maximum characters length, including prefix', 32).")</small></em>"; ?>
7172
</td>
7273
</tr>
7374
<tr>
@@ -134,8 +135,12 @@
134135
</table>
135136
</form>
136137
</div>
137-
138+
<?php if ($v_type == 'pgsql'){
139+
$user=strtolower($user);
140+
}
141+
?>
138142
<script>
139143
GLOBAL.DB_USER_PREFIX = <?=json_encode($user.'_')?>;
140144
GLOBAL.DB_DBNAME_PREFIX = <?=json_encode($user.'_')?>;
141145
</script>
146+

0 commit comments

Comments
 (0)