add abra.sh function to import users from CSV files
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Moritz 2023-06-08 18:30:10 +02:00
parent c0fda956be
commit 8f6554b55b
2 changed files with 48 additions and 0 deletions

View File

@ -54,6 +54,18 @@ Set the nextcloud Icon using `abra app cmd -l -d <app_name> 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 <app_name> 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 `<assets_path>`.

36
abra.sh
View File

@ -27,6 +27,42 @@ customize() {
done
}
import_user() {
if [ -z "$1" ]
then
echo "Usage: ... import_user <users.csv>"
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)