API for user favourites

This commit is contained in:
Serghey Rodin 2015-09-10 14:34:37 +03:00
commit cad14057cc
4 changed files with 326 additions and 1 deletions

111
bin/v-add-user-favourites Executable file
View file

@ -0,0 +1,111 @@
#!/bin/bash
# info: adding user favourites
# options: USER SYSTEM OBJECT
#
# The function adds object to users favourites
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument defenition
user=$1
system=$(echo "$2" |tr '[:lower:]' '[:upper:]')
object=$3
# Includes
source $VESTA/func/main.sh
source $VESTA/conf/vesta.conf
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
check_args '3' "$#" 'USER SYSTEM OBJECT'
validate_format 'user' 'system' 'object'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
# Checking system
case $system in
USER) check='ok' ;;
WEB) check='ok' ;;
DNS) check='ok' ;;
MAIL) check='ok' ;;
DB) check='ok' ;;
CRON) check='ok' ;;
BACKUP) check='ok' ;;
IP) check='ok' ;;
PACKAGE) check='ok' ;;
FIREWALL) check='ok' ;;
*) check_args '2' '0' 'USER SYSTEM OBJECT'
esac
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Flushing vars
USER=''
WEB=''
DNS=''
MAIL=''
DB=''
CRON=''
BACKUP=''
IP=''
PACKAGE=''
FIREWALL=''
# Creating config just in case
touch $USER_DATA/favourites.conf
# Reading current values
source $USER_DATA/favourites.conf
# Assigning current system value
eval value=\$$system
# Checking if object is new
check_fav=$(echo "$value" |tr ',' '\n'| grep "^$object$")
if [ ! -z "$check_fav" ]; then
exit 0
fi
# Adding object to favorites
if [ -z "$value" ]; then
value="$object"
else
value="$value,$object"
fi
# Updating sytem
eval $system=$value
# Updating user favorites
echo "USER='$USER'
WEB='$WEB'
DNS='$DNS'
MAIL='$MAIL'
DB='$DB'
CRON='$CRON'
BACKUP='$BACKUP'
IP='$IP'
PACKAGE='$PACKAGE'
FIREWALL='$FIREWALL'" > $USER_DATA/favourites.conf
# Changing file permission
chmod 640 $USER_DATA/favourites.conf
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
# Logging
log_history "added starred $object in $system listing"
log_event "$OK" "$EVENT"
exit

111
bin/v-delete-user-favourites Executable file
View file

@ -0,0 +1,111 @@
#!/bin/bash
# info: deleting user favourites
# options: USER SYSTEM OBJECT
#
# The function deletes object from users favourites
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument defenition
user=$1
system=$(echo "$2" |tr '[:lower:]' '[:upper:]')
object=$3
# Includes
source $VESTA/func/main.sh
source $VESTA/conf/vesta.conf
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
check_args '3' "$#" 'USER SYSTEM OBJECT'
validate_format 'user' 'system' 'object'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
# Checking system
case $system in
USER) check='ok' ;;
WEB) check='ok' ;;
DNS) check='ok' ;;
MAIL) check='ok' ;;
DB) check='ok' ;;
CRON) check='ok' ;;
BACKUP) check='ok' ;;
IP) check='ok' ;;
PACKAGE) check='ok' ;;
FIREWALL) check='ok' ;;
*) check_args '2' '0' 'USER SYSTEM OBJECT'
esac
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Flushing vars
USER=''
WEB=''
DNS=''
MAIL=''
DB=''
CRON=''
BACKUP=''
IP=''
PACKAGE=''
FIREWALL=''
# Creating config just in case
touch $USER_DATA/favourites.conf
# Reading current values
source $USER_DATA/favourites.conf
# Assigning current system value
eval value=\$$system
# Checking if object is new
check_fav=$(echo "$value" |tr ',' '\n'| grep "^$object$")
if [ -z "$check_fav" ]; then
exit 0
fi
# Deleting object from favorites
value=$(echo "$value" |\
sed -e "s/,/\n/g"|\
sed -e "s/^$object$//g"|\
sed -e "/^$/d"|\
sed -e ':a;N;$!ba;s/\n/,/g')
# Updating sytem
eval $system=$value
# Updating user favorites
echo "USER='$USER'
WEB='$WEB'
DNS='$DNS'
MAIL='$MAIL'
DB='$DB'
CRON='$CRON'
BACKUP='$BACKUP'
IP='$IP'
PACKAGE='$PACKAGE'
FIREWALL='$FIREWALL'" > $USER_DATA/favourites.conf
# Changing file permission
chmod 640 $USER_DATA/favourites.conf
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
# Logging
log_history "deleted starred $object from $system listing"
log_event "$OK" "$EVENT"
exit

102
bin/v-list-user-favourites Executable file
View file

@ -0,0 +1,102 @@
#!/bin/bash
# info: list user favourites
# options: USER [FORMAT]
#
# The function for getting the list of favourite user objects
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument defenition
user=$1
format=${2-shell}
# Includes
source $VESTA/func/main.sh
# Json function
json_list_favourites() {
i=1
fileds_count=$(echo "$fields" | wc -w)
fvrt_data=$(cat $USER_DATA/favourites.conf >/dev/null)
echo '{'
eval $fvrt_data
for field in $fields; do
eval value=$field
if [ $i -eq 1 ]; then
echo -e "\t\"$value\": {"
else
if [ $fileds_count -eq $i ]; then
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
else
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
fi
fi
(( ++i))
done
if [ -n "$value" ]; then
echo -e ' }'
fi
echo -e '}'
}
# Shell function
shell_list_favourites() {
line=$(cat $USER_DATA/favourites.conf 2>/dev/null)
eval $line
for field in $fields; do
eval key="$field"
if [ -z "$key" ]; then
key='NULL'
fi
echo "${field//$/}: $key "
done
}
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
# Checking args
check_args '1' "$#" 'USER [FORMAT]'
validate_format 'user'
is_object_valid 'user' 'USER' "$user"
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Flushing vars
USER=''
WEB=''
DNS=''
MAIL=''
DB=''
CRON=''
BACKUP=''
IP=''
PACKAGE=''
FIREWALL=''
# Defining fileds to select
OBJ='Favourites'
fields='$OBJ $USER $WEB $DNS $MAIL $DB $CRON $BACKUP $IP $PACKAGE $FIREWALL'
# Listing favourites
case $format in
json) json_list_favourites ;;
plain) shell_list_favourites ;;
shell) shell_list_favourites | column -t ;;
*) check_args '1' '0' 'USER [FORMAT]'
esac
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
exit

View file

@ -670,7 +670,7 @@ validate_format_domain() {
validate_format_domain_alias() { validate_format_domain_alias() {
exclude="[!|@|#|$|^|&|(|)|+|=|{|}|:|,|<|>|?|_|/|\|\"|'|;|%|\`| ]" exclude="[!|@|#|$|^|&|(|)|+|=|{|}|:|,|<|>|?|_|/|\|\"|'|;|%|\`| ]"
if [[ "$1" =~ $exclude ]] || [[ "$1" =~ "^[0-9]+$" ]]; then if [[ "$1" =~ $exclude ]] || [[ "$1" =~ "^[0-9]+$" ]]; then
echo "Error: domain alias $1 is not valid" echo "Error: $2 $1 is not valid"
log_event "$E_INVALID" "$EVENT" log_event "$E_INVALID" "$EVENT"
exit $E_INVALID exit $E_INVALID
fi fi
@ -916,6 +916,7 @@ validate_format(){
ns2) validate_format_domain "$arg" 'name_server';; ns2) validate_format_domain "$arg" 'name_server';;
ns3) validate_format_domain "$arg" 'name_server';; ns3) validate_format_domain "$arg" 'name_server';;
ns4) validate_format_domain "$arg" 'name_server';; ns4) validate_format_domain "$arg" 'name_server';;
object) validate_format_domain_alias "$arg" 'object';;
package) validate_format_name "$arg" "$arg_name" ;; package) validate_format_name "$arg" "$arg_name" ;;
password) validate_format_password "$arg" ;; password) validate_format_password "$arg" ;;
port) validate_format_int "$arg" 'port' ;; port) validate_format_int "$arg" 'port' ;;