Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

Add superuser management script

Chris Nagye3ba968fef1c
diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh
index 5919b14aa..95131699f 100644
--- a/docker/docker-entrypoint.sh
+++ b/docker/docker-entrypoint.sh
@@ -49,6 +49,19 @@ wait_for_postgres() {
}
+superuser() {
+
+ if [[ -n "${PAPERLESS_DBHOST}" ]]
+ then
+ wait_for_postgres
+ fi
+
+ if [[ ! -z "${PAPERLESS_ADMIN_PASSWORD}" ]]
+ then
+ sudo -HEu paperless python3 manage.py manage_superuser
+ fi
+
+}
migrations() {
@@ -86,6 +99,7 @@ initialize() {
chown -R paperless:paperless /tmp/paperless
migrations
+ superuser
}
diff --git a/docker/install_management_commands.sh b/docker/install_management_commands.sh
index 17fb8f277..468711d3c 100755
--- a/docker/install_management_commands.sh
+++ b/docker/install_management_commands.sh
@@ -1,4 +1,4 @@
-for command in document_archiver document_exporter document_importer mail_fetcher document_create_classifier document_index document_renamer document_retagger document_thumbnails document_sanity_checker;
+for command in document_archiver document_exporter document_importer mail_fetcher document_create_classifier document_index document_renamer document_retagger document_thumbnails document_sanity_checker manage_superuser;
do
echo "installing $command..."
sed "s/management_command/$command/g" management_script.sh > /usr/local/bin/$command
diff --git a/src/documents/management/commands/manage_superuser.py b/src/documents/management/commands/manage_superuser.py
new file mode 100644
index 000000000..3c5341cb3
--- /dev/null
+++ b/src/documents/management/commands/manage_superuser.py
@@ -0,0 +1,30 @@
+import os
+
+from django.contrib.auth.models import User
+from django.core.management.base import BaseCommand, CommandError
+
+class Command(BaseCommand):
+
+ help = """
+ Creates a Django superuser based on env variables.
+ """.replace(" ", "")
+
+ def handle(self, *args, **options):
+
+ # Get user details from env variables
+ PAPERLESS_ADMIN_USER=os.getenv('PAPERLESS_ADMIN_USER')
+ PAPERLESS_ADMIN_MAIL=os.getenv('PAPERLESS_ADMIN_MAIL', 'root@localhost')
+ PAPERLESS_ADMIN_PASSWORD=os.getenv('PAPERLESS_ADMIN_PASSWORD')
+
+ # If PAPERLESS_ADMIN_USER env variable is set
+ if PAPERLESS_ADMIN_USER:
+ try:
+ # Check if user exists already, leave as is if it does
+ if User.objects.filter(username=PAPERLESS_ADMIN_USER).exists():
+ self.stdout.write(f'The user "{PAPERLESS_ADMIN_USER}" already exists! Leaving user as is.')
+ elif PAPERLESS_ADMIN_PASSWORD:
+ # Create superuser based on env variables
+ User.objects.create_superuser(PAPERLESS_ADMIN_USER, PAPERLESS_ADMIN_MAIL, PAPERLESS_ADMIN_PASSWORD)
+ self.stdout.write(f'Created superuser "{PAPERLESS_ADMIN_USER}" with provided password.')
+ except Exception as error:
+ self.stdout.write(f'Exception occured while managing superuser: {error}')
\ No newline at end of file