Skip to content

Instantly share code, notes, and snippets.

@Emasoft
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Emasoft/2f849d36a21d758484c1 to your computer and use it in GitHub Desktop.
Save Emasoft/2f849d36a21d758484c1 to your computer and use it in GitHub Desktop.
Liquid Feedback privileges editor bash script
#!/bin/bash
echo "---------------- PRIVILEGES EDITOR v0.1 alpha ---------"
echo "Interactive Script to Edit Liquid Feedback User Privileges"
echo "Written by Emanuele Sabetta (emanuele.sabetta@gmail.com)"
echo " "
echo "Requirements: Debian + Liquid Feedback 3.0 + postgre user"
echo "----------------------------------------------------------"
echo " "
#PARAMETERS CHECKS
# Make sure there are exactly 2 arguments
if [[ $# -ne 1 ]]
then
echo "Usage: main.sh <name-of-database>"
echo "Example: change_privileges.sh 'lf2_sicilia_attivisti'"
exit 65
fi
echo "Arguments are: $1 "
# Make sure we're running as root
echo "My euid is $EUID"
#if [ $EUID -eq 0 ];
#then
echo " "
echo "ERROR: This script must be run as root."
echo " "
#exit 1
#fi
#VARIABLES
#example db: "lf2_sicilia_attivisti"
dbname=$1
username="postgres"
selected_user_id=0
selected_unit_name=0
role_admin=0
role_unit=0
role_area=0
role_member=0
right_initiative=0
right_voting=0
right_polling=0
NUMCHOICE=
echo "ok"
su - postgres
#FUNCTIONS
#arguments: data_base_name
#example: check_if_database_exist_sql_command data_base_name
function check_if_database_exist_sql_command () {
#echo "(DEBUG) check_if_database_exist_sql_command"
data_base_name=$1
sudo -u postgres -- psql -lqt | cut -d \| -f 1 | grep -w $data_base_name
if [ $? -eq "0" ]
then
# database exists
# $? is 0
echo " "
echo "Found database named $data_base_name."
else
# ruh-roh
# $? is 1
echo " "
echo "ERROR: No existing databases are named $data_base_name."
echo "Program Terminated. "
exit 1
fi
}
#arguments: unit_id member_id privilege_field enabled_state
#example: change_privileges_sql_command 3 2 unit_manager false
#privileges: admin_manager, unit_manager, area_manager, member_manager, initiative_right, voting_right, polling_right
function change_privileges_sql_command() {
echo "(DEBUG) change_privileges_sql_command"
unit_id=$1
member_id=$2
privilege_field=$3
enabled_state=$4
sudo -u postgres -H -- psql -d $dbname -U $username -t -c "UPDATE privilege
SET $privilege_field = $enabled_state
WHERE unit_id = $unit_id AND member_id = $member_id "
}
#arguments: unit_id member_id privilege_field
#example: read_privileges_sql_command 3 2 unit_manager
#privileges: admin_manager, unit_manager, area_manager, member_manager, initiative_right, voting_right, polling_right
function read_privileges_sql_command() {
#echo "(DEBUG) read_privileges_sql_command"
unit_id=$1
member_id=$2
privilege_field=$3
su - postgres
psql -d $dbname -U $username -t -c "SELECT $privilege_field FROM privilege WHERE unit_id = $unit_id AND member_id = $member_id"
#sudo -u postgres -H -- psql -d $dbname -U $username -t -c "SELECT $privilege_field FROM privilege WHERE unit_id = $unit_id AND member_id = $member_id"
if [ $? -eq "1" ]
then
#echo $?
return 1
else
#echo $?
return 0
fi
}
#arguments: selected_user_name
#example: check_user_name_sql_command selected_user_name
#
function check_user_name_sql_command() {
echo "(DEBUG) check_user_name_sql_command"
selected_user_name=$1
sudo -u postgres -H -- psql -d $dbname -U $username -t -c "SELECT COUNT(*) FROM member WHERE login = '$selected_user_name'"
if [ $? -eq 0 ]
then
sudo -u postgres -H -- psql -d $dbname -U $username -t -c "SELECT id FROM member WHERE login = '$selected_user_name'"
selected_user_id=$?
echo "User '$selected_user_name' with ID '$selected_user_id' selected."
return 0
else
echo ""
echo "*** ERROR: No user '$selected_user_name'"
echo ""
return 1
fi
}
#arguments: selected_unit_id
#example: check_unitid_sql_command selected_unit_id
#
function check_unitid_sql_command() {
echo "(DEBUG) check_unitid_sql_command"
selected_unit_id=$1
sudo -u postgres -H -- psql -d $dbname -U $username -t -c "SELECT COUNT(*) FROM unit WHERE id = $selected_unit_id"
if [ $? -eq 0 ];
then
sudo -u postgres -H -- psql -d $dbname -U $username -t -c "SELECT name FROM unit WHERE id = $selected_unit_id"
selected_unit_name=$?
echo "Unit '$selected_unit_name' with ID '$selected_unit_id' selected."
return 0
else
echo ""
echo "*** Error: No unit with ID '$selected_unit_id'"
echo ""
return 1
fi
}
#DATABASE CHECKS
##check_if_database_exist_sql_command $dbname
#OPTIONS MENU 1
while :
do
echo "OPTIONS MENU 1"
echo "Please, insert the username of the user you want to edit (0 to exit):"
echo ""
echo -n ":"
read STRUSERNAME
echo ""
if [ "$STRUSERNAME" = "0" ]; then
exit 1
fi
check_user_name_sql_command $STRUSERNAME
if [ "$?" -eq "0" ]; then
echo "OK"
break
else
echo "USERNAME $STRUSERNAME NOT FOUND"
fi
done
#OPTIONS MENU 2
while :
do
echo "OPTIONS MENU 2"
echo "Please, insert the Unit ID number you want to edit privileges (0 to exit):"
echo ""
echo -n ":"
read NUMUNITID
echo ""
if [ "$NUMUNITID" = "0" ]; then
exit 1
fi
check_unitid_sql_command $NUMUNITID
if [ "$?" -eq "0" ]; then
echo "You have selected the Unit $selected_unit_name with ID $NUMUNITID."
break
else
echo "Unit ID $NUMUNITID NOT FOUND"
echo "Please insert another Unit ID"
fi
done
#OPTIONS MENU 3
while :
do
#privileges: admin_manager, unit_manager, area_manager, member_manager, initiative_right, voting_right, polling_right
read_privileges_sql_command $NUMUNITID $selected_user_id admin_manager
role_admin=$?
read_privileges_sql_command $NUMUNITID $selected_user_id unit_manager
role_unit=$?
read_privileges_sql_command $NUMUNITID $selected_user_id area_manager
role_area=$?
read_privileges_sql_command $NUMUNITID $selected_user_id member_manager
role_member=$?
read_privileges_sql_command $NUMUNITID $selected_user_id initiative_right
right_initiative=$?
read_privileges_sql_command $NUMUNITID $selected_user_id voting_right
right_voting=$?
echo read_privileges_sql_command $NUMUNITID $selected_user_id polling_right
right_polling=$?
echo "OPTIONS MENU 3"
echo "Selected User is $STRUSERNAME , ID $selected_user_id"
echo "Selected Unit is $selected_unit_name, ID $NUMUNITID"
echo "ROLES: Admin=$role_admin Unit=$role_unit Area=$role_area Member=$role_member "
echo "RIGHTS: Initiative=$right_initiative Voting=$right_voting Polling=$right_polling "
echo "Please, choose the privilege you want to change:"
echo " "
echo "1 - Grant or Revoke role of Admin Manager"
echo "2 - Grant or Revoke role of Unit Manager"
echo "3 - Grant or Revoke role of Area Manager"
echo "4 - Grant or Revoke role of Member Manager"
echo "5 - Grant or Revoke Initiative Right"
echo "6 - Grant or Revoke Voting Right"
echo "7 - Grant or Revoke Polling Right"
echo ""
echo "0 - Exit Program"
echo ""
echo -n "Enter Selection:"
read NUMCHOICE
echo " "
if [ "$NUMCHOICE" -eq "0" ]; then
echo "You have selected the option 0. Exiting."
break
else
case $NUMCHOICE in
1 ) echo "You selected option n. $NUMCHOICE"
change_privileges_sql_command $NUMUNITID $selected_user_id admin_manager !$role_admin;;
2 ) echo "You selected option n. $NUMCHOICE"
change_privileges_sql_command $NUMUNITID $selected_user_id unit_manager !$role_unit;;
3 ) echo "You selected option n. $NUMCHOICE"
change_privileges_sql_command $NUMUNITID $selected_user_id area_manager !$role_area;;
4 ) echo "You selected option n. $NUMCHOICE"
change_privileges_sql_command $NUMUNITID $selected_user_id member_manager !$role_member;;
5 ) echo "You selected option n. $NUMCHOICE"
change_privileges_sql_command $NUMUNITID $selected_user_id initiative_right !$right_initiative;;
6 ) echo "You selected option n. $NUMCHOICE"
change_privileges_sql_command $NUMUNITID $selected_user_id voting_right !$right_voting;;
7 ) echo "You selected option n. $NUMCHOICE"
change_privileges_sql_command $NUMUNITID $selected_user_id polling_right !$right_polling;;
0 ) echo "You selected option n. $NUMCHOICE"
echo "Program Terminated."
break;;
* ) echo "Please enter 1,2,3,4,5,6,7 or 0";;
esac
fi
done
echo "Program Terminated."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment