From 642ac94b7a3bb8ee7ba60d7fbc839eef4fbe5448 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Wed, 28 Oct 2020 17:19:25 +0100 Subject: [PATCH] Use a custom Matrix username --- Dockerfile | 5 +++++ apps/registration/auth.py | 14 ++++++++++++++ apps/registration/fixtures/initial.json | 17 +++++++++++++++++ corres2math/settings.py | 2 ++ entrypoint.sh | 3 --- 5 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 apps/registration/auth.py create mode 100644 apps/registration/fixtures/initial.json diff --git a/Dockerfile b/Dockerfile index 53f1cb6..9be9d7f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,6 +15,11 @@ RUN pip install -r requirements.txt psycopg2-binary sympasoap --no-cache-dir COPY . /code/ +RUN python manage.py collectstatic --noinput && \ + python manage.py compilemessages && \ + python manage.py migrate && \ + python manage.py loaddata initial + # Configure nginx RUN mkdir /run/nginx RUN ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log diff --git a/apps/registration/auth.py b/apps/registration/auth.py new file mode 100644 index 0000000..779aee3 --- /dev/null +++ b/apps/registration/auth.py @@ -0,0 +1,14 @@ +from cas_server.auth import DjangoAuthUser +from django.utils.text import slugify + + +class CustomAuthUser(DjangoAuthUser): + """ + Override Django Auth User model to define a custom Matrix username. + """ + + def attributs(self): + d = super().attributs() + if self.user: + d["matrix_username"] = slugify(str(self.user.registration)) + return d diff --git a/apps/registration/fixtures/initial.json b/apps/registration/fixtures/initial.json new file mode 100644 index 0000000..9cd99b0 --- /dev/null +++ b/apps/registration/fixtures/initial.json @@ -0,0 +1,17 @@ +[ +{ + "model": "cas_server.servicepattern", + "pk": 1, + "fields": { + "pos": 100, + "name": "Plateforme des Correspondances", + "pattern": "^https://correspondances-maths.fr:8448/.*$", + "user_field": "matrix_username", + "restrict_users": false, + "proxy": true, + "proxy_callback": true, + "single_log_out": true, + "single_log_out_callback": "" + } +} +] diff --git a/corres2math/settings.py b/corres2math/settings.py index 0b53117..79564cc 100644 --- a/corres2math/settings.py +++ b/corres2math/settings.py @@ -131,6 +131,8 @@ PASSWORD_HASHERS = [ 'django.contrib.auth.hashers.BCryptPasswordHasher', ] +CAS_AUTH_CLASS = 'registration.auth.CustomAuthUser' + REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': [ 'rest_framework.permissions.IsAdminUser' diff --git a/entrypoint.sh b/entrypoint.sh index 8827c4d..044f92e 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,9 +2,6 @@ crond -l 0 -python manage.py compilemessages -python manage.py migrate - nginx if [ "$CORRES2MATH_STAGE" = "prod" ]; then