From 71f68e2d4b1f4588d22f28ce42cc46d9e0afb358 Mon Sep 17 00:00:00 2001 From: pedro Date: Wed, 16 Oct 2024 21:54:54 +0200 Subject: [PATCH] add predefined_token as an alternative to the randomly generated, which is also possible when no predefined_token is defined also update .env.example vars --- .env.example | 19 +++++++++++++++++++ docker-compose.yml | 1 + docker/devicehub-django.entrypoint.sh | 3 ++- user/management/commands/add_user.py | 12 +++++++++--- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index cf39121..1495f9d 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,21 @@ DOMAIN=localhost DEMO=false + +STATIC_ROOT=/tmp/static/ +MEDIA_ROOT=/tmp/media/ +ALLOWED_HOSTS=localhost,localhost:8000,127.0.0.1, +DOMAIN=localhost +DEBUG=True +EMAIL_HOST="mail.example.org" +EMAIL_HOST_USER="fillme_noreply" +EMAIL_HOST_PASSWORD="fillme_passwd" +EMAIL_PORT=587 +EMAIL_USE_TLS=True +EMAIL_BACKEND="django.core.mail.backends.smtp.EmailBackend" +EMAIL_FILE_PATH="/tmp/app-messages" +ENABLE_EMAIL=false +COMMIT=$(git log --format="%H %ad" --date=iso -n 1) +PREDEFINED_TOKEN='5018dd65-9abd-4a62-8896-80f34ac66150' +# TODO review these vars +#SNAPSHOTS_DIR=/path/to/TODO +#EVIDENCES_DIR=/path/to/TODO diff --git a/docker-compose.yml b/docker-compose.yml index ee59640..7d144bd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,6 +7,7 @@ services: - DEBUG=true - DOMAIN=${DOMAIN:-localhost} - DEMO=${DEMO:-n} + - PREDEFINED_TOKEN=${PREDEFINED_TOKEN:-} volumes: - .:/opt/devicehub-django ports: diff --git a/docker/devicehub-django.entrypoint.sh b/docker/devicehub-django.entrypoint.sh index 9361fdb..e988d4c 100644 --- a/docker/devicehub-django.entrypoint.sh +++ b/docker/devicehub-django.entrypoint.sh @@ -25,9 +25,10 @@ deploy() { INIT_USER="${INIT_USER:-user@example.org}" INIT_PASSWD="${INIT_PASSWD:-1234}" ADMIN='True' + PREDEFINED_TOKEN="${PREDEFINED_TOKEN:-}" ./manage.py add_institution "${INIT_ORG}" # TODO: one error on add_user, and you don't add user anymore - ./manage.py add_user "${INIT_ORG}" "${INIT_USER}" "${INIT_PASSWD}" "${ADMIN}" + ./manage.py add_user "${INIT_ORG}" "${INIT_USER}" "${INIT_PASSWD}" "${ADMIN}" "${PREDEFINED_TOKEN}" if [ "${DEMO:-}" ]; then ./manage.py up_snapshots example/snapshots/ "${INIT_USER}" diff --git a/user/management/commands/add_user.py b/user/management/commands/add_user.py index 60f3dd2..6593fee 100644 --- a/user/management/commands/add_user.py +++ b/user/management/commands/add_user.py @@ -17,15 +17,17 @@ class Command(BaseCommand): parser.add_argument('email', type=str, help='email') parser.add_argument('password', type=str, help='password') parser.add_argument('is_admin', nargs='?', default=False, type=str, help='is admin') + parser.add_argument('predefined_token', nargs='?', default='', type=str, help='predefined token') def handle(self, *args, **kwargs): email = kwargs['email'] password = kwargs['password'] is_admin = kwargs['is_admin'] + predefined_token = kwargs['predefined_token'] institution = Institution.objects.get(name=kwargs['institution']) - self.create_user(institution, email, password, is_admin) + self.create_user(institution, email, password, is_admin, predefined_token) - def create_user(self, institution, email, password, is_admin): + def create_user(self, institution, email, password, is_admin, predefined_token): self.u = User.objects.create( institution=institution, email=email, @@ -34,6 +36,10 @@ class Command(BaseCommand): ) self.u.set_password(password) self.u.save() - token = uuid4() + if predefined_token: + token = predefined_token + else: + token = uuid4() + Token.objects.create(token=token, owner=self.u) print(f"TOKEN: {token}")