From cad14057cc2d290249bb1e960175f4ae34a64ce8 Mon Sep 17 00:00:00 2001 From: Serghey Rodin Date: Thu, 10 Sep 2015 14:34:37 +0300 Subject: [PATCH] API for user favourites --- bin/v-add-user-favourites | 111 +++++++++++++++++++++++++++++++++++ bin/v-delete-user-favourites | 111 +++++++++++++++++++++++++++++++++++ bin/v-list-user-favourites | 102 ++++++++++++++++++++++++++++++++ func/main.sh | 3 +- 4 files changed, 326 insertions(+), 1 deletion(-) create mode 100755 bin/v-add-user-favourites create mode 100755 bin/v-delete-user-favourites create mode 100755 bin/v-list-user-favourites diff --git a/bin/v-add-user-favourites b/bin/v-add-user-favourites new file mode 100755 index 00000000..d6a92aa2 --- /dev/null +++ b/bin/v-add-user-favourites @@ -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 diff --git a/bin/v-delete-user-favourites b/bin/v-delete-user-favourites new file mode 100755 index 00000000..5c69b889 --- /dev/null +++ b/bin/v-delete-user-favourites @@ -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 diff --git a/bin/v-list-user-favourites b/bin/v-list-user-favourites new file mode 100755 index 00000000..e87fcbef --- /dev/null +++ b/bin/v-list-user-favourites @@ -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 diff --git a/func/main.sh b/func/main.sh index 1edbb3ec..a97f5000 100644 --- a/func/main.sh +++ b/func/main.sh @@ -670,7 +670,7 @@ validate_format_domain() { validate_format_domain_alias() { exclude="[!|@|#|$|^|&|(|)|+|=|{|}|:|,|<|>|?|_|/|\|\"|'|;|%|\`| ]" 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" exit $E_INVALID fi @@ -916,6 +916,7 @@ validate_format(){ ns2) validate_format_domain "$arg" 'name_server';; ns3) 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" ;; password) validate_format_password "$arg" ;; port) validate_format_int "$arg" 'port' ;;