Skip to content

Commit cad1405

Browse files
committed
API for user favourites
1 parent 15a3d94 commit cad1405

File tree

4 files changed

+326
-1
lines changed

4 files changed

+326
-1
lines changed

bin/v-add-user-favourites

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/bin/bash
2+
# info: adding user favourites
3+
# options: USER SYSTEM OBJECT
4+
#
5+
# The function adds object to users favourites
6+
7+
8+
#----------------------------------------------------------#
9+
# Variable&Function #
10+
#----------------------------------------------------------#
11+
12+
# Argument defenition
13+
user=$1
14+
system=$(echo "$2" |tr '[:lower:]' '[:upper:]')
15+
object=$3
16+
17+
# Includes
18+
source $VESTA/func/main.sh
19+
source $VESTA/conf/vesta.conf
20+
21+
22+
#----------------------------------------------------------#
23+
# Verifications #
24+
#----------------------------------------------------------#
25+
26+
check_args '3' "$#" 'USER SYSTEM OBJECT'
27+
validate_format 'user' 'system' 'object'
28+
is_object_valid 'user' 'USER' "$user"
29+
is_object_unsuspended 'user' 'USER' "$user"
30+
31+
# Checking system
32+
case $system in
33+
USER) check='ok' ;;
34+
WEB) check='ok' ;;
35+
DNS) check='ok' ;;
36+
MAIL) check='ok' ;;
37+
DB) check='ok' ;;
38+
CRON) check='ok' ;;
39+
BACKUP) check='ok' ;;
40+
IP) check='ok' ;;
41+
PACKAGE) check='ok' ;;
42+
FIREWALL) check='ok' ;;
43+
*) check_args '2' '0' 'USER SYSTEM OBJECT'
44+
esac
45+
46+
47+
#----------------------------------------------------------#
48+
# Action #
49+
#----------------------------------------------------------#
50+
51+
# Flushing vars
52+
USER=''
53+
WEB=''
54+
DNS=''
55+
MAIL=''
56+
DB=''
57+
CRON=''
58+
BACKUP=''
59+
IP=''
60+
PACKAGE=''
61+
FIREWALL=''
62+
63+
# Creating config just in case
64+
touch $USER_DATA/favourites.conf
65+
66+
# Reading current values
67+
source $USER_DATA/favourites.conf
68+
69+
# Assigning current system value
70+
eval value=\$$system
71+
72+
# Checking if object is new
73+
check_fav=$(echo "$value" |tr ',' '\n'| grep "^$object$")
74+
if [ ! -z "$check_fav" ]; then
75+
exit 0
76+
fi
77+
78+
# Adding object to favorites
79+
if [ -z "$value" ]; then
80+
value="$object"
81+
else
82+
value="$value,$object"
83+
fi
84+
85+
# Updating sytem
86+
eval $system=$value
87+
88+
# Updating user favorites
89+
echo "USER='$USER'
90+
WEB='$WEB'
91+
DNS='$DNS'
92+
MAIL='$MAIL'
93+
DB='$DB'
94+
CRON='$CRON'
95+
BACKUP='$BACKUP'
96+
IP='$IP'
97+
PACKAGE='$PACKAGE'
98+
FIREWALL='$FIREWALL'" > $USER_DATA/favourites.conf
99+
100+
# Changing file permission
101+
chmod 640 $USER_DATA/favourites.conf
102+
103+
#----------------------------------------------------------#
104+
# Vesta #
105+
#----------------------------------------------------------#
106+
107+
# Logging
108+
log_history "added starred $object in $system listing"
109+
log_event "$OK" "$EVENT"
110+
111+
exit

bin/v-delete-user-favourites

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/bin/bash
2+
# info: deleting user favourites
3+
# options: USER SYSTEM OBJECT
4+
#
5+
# The function deletes object from users favourites
6+
7+
8+
#----------------------------------------------------------#
9+
# Variable&Function #
10+
#----------------------------------------------------------#
11+
12+
# Argument defenition
13+
user=$1
14+
system=$(echo "$2" |tr '[:lower:]' '[:upper:]')
15+
object=$3
16+
17+
# Includes
18+
source $VESTA/func/main.sh
19+
source $VESTA/conf/vesta.conf
20+
21+
22+
#----------------------------------------------------------#
23+
# Verifications #
24+
#----------------------------------------------------------#
25+
26+
check_args '3' "$#" 'USER SYSTEM OBJECT'
27+
validate_format 'user' 'system' 'object'
28+
is_object_valid 'user' 'USER' "$user"
29+
is_object_unsuspended 'user' 'USER' "$user"
30+
31+
# Checking system
32+
case $system in
33+
USER) check='ok' ;;
34+
WEB) check='ok' ;;
35+
DNS) check='ok' ;;
36+
MAIL) check='ok' ;;
37+
DB) check='ok' ;;
38+
CRON) check='ok' ;;
39+
BACKUP) check='ok' ;;
40+
IP) check='ok' ;;
41+
PACKAGE) check='ok' ;;
42+
FIREWALL) check='ok' ;;
43+
*) check_args '2' '0' 'USER SYSTEM OBJECT'
44+
esac
45+
46+
47+
#----------------------------------------------------------#
48+
# Action #
49+
#----------------------------------------------------------#
50+
51+
# Flushing vars
52+
USER=''
53+
WEB=''
54+
DNS=''
55+
MAIL=''
56+
DB=''
57+
CRON=''
58+
BACKUP=''
59+
IP=''
60+
PACKAGE=''
61+
FIREWALL=''
62+
63+
# Creating config just in case
64+
touch $USER_DATA/favourites.conf
65+
66+
# Reading current values
67+
source $USER_DATA/favourites.conf
68+
69+
# Assigning current system value
70+
eval value=\$$system
71+
72+
# Checking if object is new
73+
check_fav=$(echo "$value" |tr ',' '\n'| grep "^$object$")
74+
if [ -z "$check_fav" ]; then
75+
exit 0
76+
fi
77+
78+
# Deleting object from favorites
79+
value=$(echo "$value" |\
80+
sed -e "s/,/\n/g"|\
81+
sed -e "s/^$object$//g"|\
82+
sed -e "/^$/d"|\
83+
sed -e ':a;N;$!ba;s/\n/,/g')
84+
85+
# Updating sytem
86+
eval $system=$value
87+
88+
# Updating user favorites
89+
echo "USER='$USER'
90+
WEB='$WEB'
91+
DNS='$DNS'
92+
MAIL='$MAIL'
93+
DB='$DB'
94+
CRON='$CRON'
95+
BACKUP='$BACKUP'
96+
IP='$IP'
97+
PACKAGE='$PACKAGE'
98+
FIREWALL='$FIREWALL'" > $USER_DATA/favourites.conf
99+
100+
# Changing file permission
101+
chmod 640 $USER_DATA/favourites.conf
102+
103+
#----------------------------------------------------------#
104+
# Vesta #
105+
#----------------------------------------------------------#
106+
107+
# Logging
108+
log_history "deleted starred $object from $system listing"
109+
log_event "$OK" "$EVENT"
110+
111+
exit

bin/v-list-user-favourites

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/bin/bash
2+
# info: list user favourites
3+
# options: USER [FORMAT]
4+
#
5+
# The function for getting the list of favourite user objects
6+
7+
8+
#----------------------------------------------------------#
9+
# Variable&Function #
10+
#----------------------------------------------------------#
11+
12+
# Argument defenition
13+
user=$1
14+
format=${2-shell}
15+
16+
# Includes
17+
source $VESTA/func/main.sh
18+
19+
# Json function
20+
json_list_favourites() {
21+
i=1
22+
fileds_count=$(echo "$fields" | wc -w)
23+
fvrt_data=$(cat $USER_DATA/favourites.conf >/dev/null)
24+
echo '{'
25+
eval $fvrt_data
26+
for field in $fields; do
27+
eval value=$field
28+
if [ $i -eq 1 ]; then
29+
echo -e "\t\"$value\": {"
30+
else
31+
if [ $fileds_count -eq $i ]; then
32+
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
33+
else
34+
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
35+
fi
36+
fi
37+
(( ++i))
38+
done
39+
if [ -n "$value" ]; then
40+
echo -e ' }'
41+
fi
42+
echo -e '}'
43+
}
44+
45+
# Shell function
46+
shell_list_favourites() {
47+
line=$(cat $USER_DATA/favourites.conf 2>/dev/null)
48+
eval $line
49+
for field in $fields; do
50+
eval key="$field"
51+
if [ -z "$key" ]; then
52+
key='NULL'
53+
fi
54+
echo "${field//$/}: $key "
55+
done
56+
}
57+
58+
59+
#----------------------------------------------------------#
60+
# Verifications #
61+
#----------------------------------------------------------#
62+
63+
# Checking args
64+
check_args '1' "$#" 'USER [FORMAT]'
65+
validate_format 'user'
66+
is_object_valid 'user' 'USER' "$user"
67+
68+
69+
#----------------------------------------------------------#
70+
# Action #
71+
#----------------------------------------------------------#
72+
73+
# Flushing vars
74+
USER=''
75+
WEB=''
76+
DNS=''
77+
MAIL=''
78+
DB=''
79+
CRON=''
80+
BACKUP=''
81+
IP=''
82+
PACKAGE=''
83+
FIREWALL=''
84+
85+
# Defining fileds to select
86+
OBJ='Favourites'
87+
fields='$OBJ $USER $WEB $DNS $MAIL $DB $CRON $BACKUP $IP $PACKAGE $FIREWALL'
88+
89+
# Listing favourites
90+
case $format in
91+
json) json_list_favourites ;;
92+
plain) shell_list_favourites ;;
93+
shell) shell_list_favourites | column -t ;;
94+
*) check_args '1' '0' 'USER [FORMAT]'
95+
esac
96+
97+
98+
#----------------------------------------------------------#
99+
# Vesta #
100+
#----------------------------------------------------------#
101+
102+
exit

func/main.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ validate_format_domain() {
670670
validate_format_domain_alias() {
671671
exclude="[!|@|#|$|^|&|(|)|+|=|{|}|:|,|<|>|?|_|/|\|\"|'|;|%|\`| ]"
672672
if [[ "$1" =~ $exclude ]] || [[ "$1" =~ "^[0-9]+$" ]]; then
673-
echo "Error: domain alias $1 is not valid"
673+
echo "Error: $2 $1 is not valid"
674674
log_event "$E_INVALID" "$EVENT"
675675
exit $E_INVALID
676676
fi
@@ -916,6 +916,7 @@ validate_format(){
916916
ns2) validate_format_domain "$arg" 'name_server';;
917917
ns3) validate_format_domain "$arg" 'name_server';;
918918
ns4) validate_format_domain "$arg" 'name_server';;
919+
object) validate_format_domain_alias "$arg" 'object';;
919920
package) validate_format_name "$arg" "$arg_name" ;;
920921
password) validate_format_password "$arg" ;;
921922
port) validate_format_int "$arg" 'port' ;;

0 commit comments

Comments
 (0)