diff options
author | sefidel <contact@sefidel.net> | 2023-03-29 20:54:19 +0900 |
---|---|---|
committer | sefidel <contact@sefidel.net> | 2023-04-03 18:32:29 +0900 |
commit | ce06f43476863da90dc60dcee606d2b6c5a89a8e (patch) | |
tree | 5d14946330cb09ff0ebd97bee59407fccee4d860 /scripts/manage-user | |
download | infra-ce06f43476863da90dc60dcee606d2b6c5a89a8e.tar.gz infra-ce06f43476863da90dc60dcee606d2b6c5a89a8e.zip |
project: initial commit
Diffstat (limited to 'scripts/manage-user')
-rwxr-xr-x | scripts/manage-user | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/scripts/manage-user b/scripts/manage-user new file mode 100755 index 0000000..43c7526 --- /dev/null +++ b/scripts/manage-user @@ -0,0 +1,79 @@ +#!/usr/bin/env bash + +domain='exotic.sh' +group="$(echo $domain | awk -F. '{printf $1}')" + +cmd="$1" +user="$2" +email="$3" +ssh_pubkey="$4" + +die() { + echo "$@" >&2; exit 1; +} + +confirm() { + printf '%s\n' "$@" + select yn in "Yes" "No"; do + case $yn in + Yes) return 0; break;; + No) return 1; break;; + esac; + done +} + +help() { + printf '%s user management\n\n' $domain + printf 'Usage: %s [args]\n' "$(basename "$0")" + printf '\tadd <user> <email> <ssh-pubkey> - add user\n' + printf '\tdel <user> - delete user\n' + printf '\tlist - list users\n' + printf '\thelp - display this help command\n' +} + +add_user() { + echo "" +} + +del_user() { + echo "" +} + +list_users() { + echo 'TODO: list LDAP' +} + + +[ "$(id -u)" -ne 0 ] && die 'This script should be run as root!' + +case "$cmd" in + add) [ $# -lt 3 ] && die 'Insufficient arguments!' + if ! id -u "$user" >/dev/null 2>&1; then + printf 'Creating user:\n' + printf '\tUsername: %s\n' $user + printf '\tEmail: %s\n' $email + printf '\tSSH: %s\n\n' $ssh_pubkey + if confirm 'Are those details correct?'; then + add_user + else + echo 'Aborting.' + fi + else + die "User '$user' already exists!" + fi + ;; + del) [ $# -lt 2 ] && die 'Insufficient arguments!' + if id -u "$user" >/dev/null 2>&1; then + printf 'Deleting user: %s\n' $user + if confirm 'Are you sure?'; then + del_user + else + echo 'Aborting.' + fi + else + die "User '$user' doesn't exist!" + fi + ;; + list) list_users;; + help|*) help;; +esac |