simplification for the easiest case
[shellsnippets/shellsnippets.git] / mksh / teckids / whois
1 # -*- mode: sh -*-
2 #-
3 # Copyright © 2014, 2015, 2016, 2017
4 #       Dominik George <dominik.george@teckids.org>
5 # Copyright © 2014, 2015, 2016
6 #       mirabilos <thorsten.glaser@teckids.org>
7 #
8 # Provided that these terms and disclaimer and all copyright notices
9 # are retained or reproduced in an accompanying document, permission
10 # is granted to deal in this work without restriction, including un‐
11 # limited rights to use, publicly perform, distribute, sell, modify,
12 # merge, give away, or sublicence.
13 #
14 # This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
15 # the utmost extent permitted by applicable law, neither express nor
16 # implied; without malicious intent or gross negligence. In no event
17 # may a licensor, author or contributor be held liable for indirect,
18 # direct, other damage, loss, or other issues arising in any way out
19 # of dealing in the work, even if advised of the possibility of such
20 # damage or existence of a defect, except proven that it results out
21 # of said person’s immediate fault when using the work as intended.
22
23 # Teckids utility subcommand that prints a summary about a user
24
25 cmd_options='r\ 1raw\ 10\ 1Rohe Ausgabe erzeugen (LDAP-Feldnamen)'
26 cmd_options+=$'\n''s:\ 1single\ 10\ 1Nur einzelne Felder pro Ergebnis ausgeben'
27 cmd_options+=$'\n''t:\ 1single_sep\ 1,\ 1Dies als Trennzeichen für CSV benutzen'
28
29 teckids_sourcing_wrapper=1
30 . "$(dirname "$0")/teckids"
31
32 if [[ $TECKIDS_RUNNING_COMMAND = */whoami || $0 = ?(*/)whoami ]]; then
33         (( $# == 0 )) || die „whoami“ nimmt keine Argumente, nur Optionen!
34         field=uid
35         if [[ $HOSTNAME = ${TECKIDS_MANAGEMENT_HOST%%.*} ]]; then
36                 value=$(whoami)
37         else
38                 value=$(mksh "$(dirname "$0")/ssh" whoami)
39         fi
40 elif (( $# == 0 )); then
41         field=dn
42         value=$(cat "$TECKIDS_CACHE_DIR"/last_whois)
43         if (( kids_only )) && [[ $value = *,ou=Eltern,* ]]; then
44                 value=${value#*,ou=Eltern,}
45         fi
46 elif (( $# == 1 )); then
47         case $1 in
48                 [a-z]+([a-z0-9])) field=uid ;;
49                 *@*) field=mail ;;
50                 +([0-9])) field=employeeNumber ;;
51                 *) field=cn ;;
52         esac
53         value=$1
54         [[ $field = employeeNumber ]] && (( value >= 20000 )) && field=teckidsCashAccount
55 elif (( $# == 2 )); then
56         field=$1
57         value=$2
58 fi
59
60 if [[ $field = dn ]]; then
61         asso_setldap_sasl users -- -b "$value" -s base
62 else
63         asso_setldap_sasl users -- -b ou=People,dc=teckids,dc=org \
64             "($field=$value)"
65 fi
66
67 teckids_loadk_users
68
69 if (( raw )); then
70         for user_dn in "${asso_y[@]}"; do
71                 asso_loadk users "$user_dn"
72                 for field in "${asso_y[@]}"; do
73                         n=$(asso_getv users "$user_dn" "$field" count)
74                         i=-1
75                         while (( ++i < n )); do
76                                 print -r -- "$field: $(asso_getv users "$user_dn" "$field" $i)"
77                         done
78                 done
79                 print -- ----
80                 print -r -- "$user_dn" >"$TECKIDS_CACHE_DIR"/last_whois
81         done | if [[ -t 1 ]]; then
82                 # stdout is a tty
83                 less +-S
84         else
85                 cat
86         fi
87         exit 0
88 fi
89
90 if [[ $single != 0 ]]; then
91         sIFS=$IFS; IFS=,; set -A single_fields -- $single; IFS=$IFS
92         for user_dn in "${asso_y[@]}"; do
93                 _first=1
94                 for f in "${single_fields[@]}"; do
95                         v=$(asso_getv users "$user_dn" "$f" 0)
96                         (( _first )) || print -rn -- "$single_sep"
97                         print -rn -- "$v"
98                         _first=0
99                 done
100                 print
101         done
102         exit 0
103 fi
104
105 for user_dn in "${asso_y[@]}"; do
106         givenName=$(asso_getv users "$user_dn" givenName 0)
107         sn=$(asso_getv users "$user_dn" sn 0)
108         employeeNumber=$(asso_getv users "$user_dn" employeeNumber 0)
109         dateOfBirth=$(asso_getv users "$user_dn" dateOfBirth 0)
110         mail=$(asso_getv users "$user_dn" mail 0)
111         homePostalAddress=$(asso_getv users "$user_dn" homePostalAddress 0)
112         mobile=$(asso_getv users "$user_dn" mobile 0)
113         uid=$(asso_getv users "$user_dn" uid 0)
114         uidNumber=$(asso_getv users "$user_dn" uidNumber 0)
115         homeDirectory=$(asso_getv users "$user_dn" homeDirectory 0)
116         loginShell=$(asso_getv users "$user_dn" loginShell 0)
117         o=$(asso_getv users "$user_dn" o 0)
118         ou=$(asso_getv users "$user_dn" ou 0)
119         l=$(asso_getv users "$user_dn" l 0)
120         st=$(asso_getv users "$user_dn" st 0)
121         c=$(asso_getv users "$user_dn" c 0)
122         cashAccount=$(asso_getv users "$user_dn" teckidsCashAccount 0)
123         dues=$(asso_getv users "$user_dn" teckidsMemberDues 0)
124         memberSince=$(asso_getv users "$user_dn" teckidsMemberSince 0)
125         iban=$(asso_getv users "$user_dn" iBAN 0)
126         bic=$(asso_getv users "$user_dn" bIC 0)
127
128         : ${c:=Deutschland}
129         [[ -n $st ]] && c="$st, $c"
130
131         if dtchk dtJ "$dateOfBirth" dtv; then
132                 set -A tmGeb -- $(mjd_explode "$dtJ" 0)
133                 set -A tmNow -- $(mjd_explode $(timet2mjd $(date +%s)))
134                 (( age = tmNow[tm_year] - tmGeb[tm_year] - ((tmNow[tm_yday] < tmGeb[tm_yday]) ? 1 : 0) ))
135         else
136                 unset age
137         fi
138
139         print "Personendaten"
140         print "============="
141         print
142         print -r -- "Nachname:|$sn|Vorname:|$givenName"
143         print -r -- "Geburtstag:|${dateOfBirth:- }|Alter:|$age"
144         print
145
146         print "Kontakt"
147         print "======="
148         print
149         print -r -- "E-Mail:|$mail"
150         print -r -- "Handy:|${mobile:- }|Anschrift:|$homePostalAddress"
151         print
152
153         print "Schule"
154         print "======"
155         print
156         print -r -- "Schule:|${o:- }|Schulort:|$l"
157         print -r -- "Klasse:|${ou:- }|Land:|$c"
158         print
159
160         if [[ -n $employeeNumber ]]; then
161                 print "Mitglied"
162                 print "========"
163                 print
164                 print -r -- "Mitglieds-Nr.:|${employeeNumber:- }|Eintritt:|$memberSince"
165                 print -r -- "Beitrag:|$dues €"
166                 print
167         fi
168
169         if [[ -n $cashAccount$employeeNumber ]]; then
170                 print "Buchhaltung"
171                 print "==========="
172                 print
173                 print -r -- "Konto:|${employeeNumber:-$cashAccount}|Kontostand:|$("$ROOT/util/not_teckidscmd/gnc_balance" ${employeeNumber:-$cashAccount}) €"
174                 print -r -- "IBAN:|${iban:- }|BIC:|$bic"
175                 print
176         fi
177
178         if [[ -n $uid ]]; then
179                 print "Account"
180                 print "======="
181                 print
182                 print -r -- "Benutzer:|${uid:- }|UID:|$uidNumber"
183                 print -r -- "Home:|${homeDirectory:- }|Shell:|$loginShell"
184                 print
185         fi
186
187         for a in Gremien Teams Themen Privileges Projekte; do
188                 print "$a"
189                 print "${a//?/=}"
190                 print
191
192                 asso_setldap_sasl groups -- -b ou=$a,ou=Groups,dc=teckids,dc=org \
193                     -s one "(member=$user_dn)"
194
195                 asso_loadk groups
196                 for group_dn in "${asso_y[@]}"; do
197                         cn=$(asso_getv groups "$group_dn" cn 0)
198                         description=$(asso_getv groups "$group_dn" description 0)
199
200                         print -r -- " |$cn| |$description"
201                 done
202
203                 print
204         done
205
206         print -r -- "$user_dn" >"$TECKIDS_CACHE_DIR"/last_whois
207 done | column -s"|" -t -e
208
209 [[ -n "${asso_y[*]}" ]] || exit 1
210 exit 0