#!/bin/bash
usage=$(cat << HEREDOC
-----------------------------------------------------------------------
J.Berndt
2026-05-25
https://jbwiki.fred25.de/doku.php
https://chat.deepseek.com/a/chat/s/c17ac438-707d-4258-95ab-b07cbdd8ff64
update:check Check for server and app updates
user
user:add adds an account
user:auth-tokens:add [user:add-app-password] Add app password for the named account
user:auth-tokens:delete Deletes an authentication token
user:auth-tokens:list List authentication tokens of an user
user:clear-avatar-cache clear avatar cache
user:delete deletes the specified user
user:disable disables the specified user
user:enable enables the specified user
user:info show user info
user:keys:verify Verify if the stored public key matches the stored private key
user:lastseen shows when the user was logged in last time
user:list list configured users
user:profile Read and modify user profile properties
user:report shows how many users have access
user:resetpassword Resets the password of the named user
user:setting Read and modify user settings
user:sync-account-data sync user backend data to accounts table for configured users
user:welcome Sends the welcome email
## funktionierender Weg um den User anzulegen ################
sudo -u www-data php occ user:add --generate-password --display-name "Reinhold Berndt" --group web_admins --email juergen-berndt@gmx.net reinhold
1. Es soll kein Passwort vorgegeben werden
-----------------------------------------------------------------------
HEREDOC
)
debug=0
#### nur root darf diese Skript ausführen #####
[[ $(id -u) -ne 0 ]] && { echo -e "root Privilegien erforderlich!\n --> ${BASH_SOURCE##*/}:$LINENO\n\n"; exit; }
# Konfiguration – BITTE ANPASSEN!
NEXTCLOUD_PATH="/var/www/html/nc.dcb-charlot.de" # Pfad zur Nextcloud-Installation
OCC="sudo -u www-data php ${NEXTCLOUD_PATH}/occ"
NEXTCLOUD_URL="https://nc.dcb-charlot.de" # Deine Nextcloud-URL
JSON_CONFIG="config.json"
STANDARD_GRUPPE="Mitglieder"
aROT="\033[31m"
eROT="\033[0m"
## wenn beim letzen Mal etwas schief gegangen war dann
## gibt es noch die config.json um nicht alles wieder eingeben zu müssen
##
if [[ -e $JSON_CONFIG ]];then
[[ $debug -ne 0 ]] && echo -e "JSON_CONFIG gefunden\n";
# Werte aus config.json auslesen und in Variablen speichern
FULLNAME=$(jq -r '.FULLNAME' config.json)
EMAIL=$(jq -r '.EMAIL' config.json)
USERNAME=$(jq -r '.USERNAME' config.json)
GROUP=$(jq -r '.GROUP' config.json)
fi
# Benutzerdaten interaktiv abfragen
echo "=== Neuen Nextcloud-Benutzer anlegen ==="
read -p "Vollständiger Name [Standard: $FULLNAME]: " input
[[ -n $input ]] && FULLNAME=$input
read -p "E-Mail-Adresse [Standard: $EMAIL]: " input
[[ -n $input ]] && EMAIL=$input
read -p "Benutzername [Standard: $USERNAME]: " input
[[ -n $input ]] && USERNAME=$input
read -p "Gruppe [Standard: $STANDARD_GRUPPE]: " input
[[ -n $input ]] && GROUP=$input || GROUP=$STANDARD_GRUPPE
## Gruppe soll nicht leer sein
if [[ -z $GROUP ]];then
GROUP="$STANDARD_GRUPPE"
fi
if [[ $GROUP == "admin" ]] || [[ $GROUP == "Admin" ]];then
echo -e "\n${aROT}Die admin.Gruppe darf nur im Webinterface vergeben werden!${eROT}\nersetzt durch die Gruppe $STANDARD_GRUPPE\n"
GROUP=$STANDARD_GRUPPE
fi
## in die config schreiben falls etwas nicht klappt
jq -n \
--arg fullname "$FULLNAME" \
--arg email "$EMAIL" \
--arg username "$USERNAME" \
'{FULLNAME: $fullname, EMAIL: $email, USERNAME: $username}' \
> $JSON_CONFIG
## prüfen ob die gewünchte Gruppe existiert
if [[ -z $($OCC group:list |grep "${GROUP}:") ]];then
echo -e "${aROT}Eine Gruppe mit dem Namen $GROUP gibt es nicht${eROT}\nStandardgruppe ($STANDARD_GRUPPE) gesetzt.\n\n"
GROUP=$STANDARD_GRUPPE
fi
# Optional: Ausgabe zur Kontrolle
if [[ $debug -ne 0 ]];then
echo -e ""
echo -e "FULLNAME: $FULLNAME"
echo -e "EMAIL: $EMAIL"
echo -e "USERNAME: $USERNAME"
echo -e "GROUP: $GROUP"
fi
## die vorhandenen Emails auslesen
usernamen=$($OCC user:list |sed 's/ \+//g; s/^-//g' |cut -d: -f1 )
[[ $debug -ne 0 ]] && echo -e "\nalle usernamen\n=$usernamen";
declare -i fehler=0
for user in $usernamen;
do
[[ $debug -ne 0 ]] && echo -e "user='$user'";
user_json=$(echo "$($OCC user:info $user --output=json)")
[[ $debug -ne 0 ]] && echo -e "user_json=$user_json";
if [[ $user == $USERNAME ]];then
echo -e "${aROT}der USERNAME:$USERNAME ist schon vohanden${eROT}"
((fehler++))
fi
if [[ $(echo "$user_json" | jq -r '.email') == $EMAIL ]];then
echo -e "${aROT}die Emailadresse:$EMAIL ist schon vorhanden${eROT}"
((fehler++))
fi
echo
done
if [[ $fehler -ne 0 ]];then
echo -e "\n${aROT}mit Fehlern beendet${eROT}\n\n"
exit
fi
if ! $OCC user:add --generate-password --display-name="$FULLNAME" --email "$EMAIL" --group="$GROUP" "$USERNAME"; then
echo "Fehler: Benutzer '$USERNAME' konnte nicht angelegt werden." >&2
exit 1
else
x=x
[[ -e $JSON_CONFIG ]] && rm $JSON_CONFIG
fi
echo -e ""
$OCC user:info $USERNAME --output json_pretty
echo -e "\n"