diff --git a/README.md b/README.md index 37a98b6..5da4f35 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,18 @@ Set the nextcloud Icon using `abra app cmd -l -d set_icons` The configuration inside Nextcloud can be found in the [nextcloud recipe](https://git.coopcloud.tech/coop-cloud/nextcloud#authentik-integration) +## Import User from CSV + +Users can be imported from a CSV file of the following format: + +`First and last name, username, email@example.com, group1;group2;group3` + +Run the following command to import the file `users.csv`: + +`abra app cmd -l import_user users.csv` + +Users will only be created if the username does not exits. I a group does not exists it will be created. + ## Customization Place the files you want to overwrite in a directory ``. diff --git a/abra.sh b/abra.sh index 642a4a2..f8ec307 100644 --- a/abra.sh +++ b/abra.sh @@ -27,6 +27,42 @@ customize() { done } +import_user() { + if [ -z "$1" ] + then + echo "Usage: ... import_user " + exit 1 + fi + source_file=$1 + filename=$(basename $source_file) + abra app cp $APP_NAME $source_file worker:/tmp/ + abra app cmd -T $APP_NAME worker _import_user $filename +} + +_import_user() { +/manage.py shell -c """ +import csv +new_user = User() +with open('/tmp/$1', newline='') as file: + reader = csv.reader(file) + for row in reader: + name = row[0].strip() + username = row[1].strip() + email = row[2].strip() + groups = row[3].split(';') + if User.objects.filter(username=username): + continue + new_user = User.objects.create(name=name, username=username, email=email) + for group_name in groups: + group_name = group_name.strip() + if Group.objects.filter(name=group_name): + group = Group.objects.get(name=group_name) + else: + group = Group.objects.create(name=group_name) + group.users.add(new_user) +""" 2>&1 | quieten +} + set_admin_pass() { password=$(cat /run/secrets/admin_pass) token=$(cat /run/secrets/admin_token)