This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
user-mgmt/management/user-mgmt.sh
2023-07-14 09:48:10 +02:00

68 lines
1.5 KiB
Bash
Executable file

#!/bin/bash
# User Management Script, powered by an SQL database
# (c) Sangelo | Read Licenses for more info: https://gitpot.dev/sangelo/user-mgmt
# Variables
E_BADARGS=65
MYSQL=`which mysql`
MYSQL_USER="root"
USAGE="
$0 -- user management script
Usage:
init
add {username} {name} {description}
delete {username}
list
"
# Check for correct number of arguments and action
if [ $# -lt 1 ]
then
echo "$USAGE"
exit $E_BADARGS
fi
# Switch between different actions
case "$1" in
"init")
Q1="CREATE SCHEMA if not exists usermgmt; use usermgmt; CREATE TABLE if not exists users (username varchar(255), name varchar(255), description varchar(255));"
SQL="${Q1}"
;;
"add")
if [ $# -ne 4 ]
then
echo "Error: Invalid action."
echo "$USAGE"
exit $E_BADARGS
fi
Q1="INSERT INTO usermgmt.users (username, name, description) VALUES ('$2', '$3', '$4');"
SQL="${Q1}"
;;
"delete")
if [ $# -ne 2 ]
then
echo "Error: Invalid action."
echo "$USAGE"
exit $E_BADARGS
fi
Q1="DELETE FROM usermgmt.users WHERE username = '$2';"
SQL="${Q1}"
;;
"list")
if [ $# -ne 1 ]
then
echo "Error: Invalid action."
echo "$USAGE"
exit $E_BADARGS
fi
Q1="SELECT username, name, description FROM usermgmt.users;"
SQL="${Q1}"
;;
*)
echo "Error: Invalid action. Please use a valid argument."
echo "$USAGE"
exit $E_BADARGS
;;
esac
$MYSQL -u "$MYSQL_USER" -p -e "$SQL"