about summary refs log tree commit diff
path: root/scripts/manage-user
blob: 43c7526089abf5f43a47cc101dd086f302379361 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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