From 1b1049a73f3b8fe7e594873f73da0c19a0abf6d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Sun, 10 May 2020 13:15:45 +0200 Subject: [PATCH] A minimal Clipper model for clipper metadata --- .gitignore | 2 ++ authens/__init__.py | 0 authens/apps.py | 5 +++ authens/migrations/0001_initial.py | 56 ++++++++++++++++++++++++++++++ authens/migrations/__init__.py | 0 authens/models.py | 44 +++++++++++++++++++++++ 6 files changed, 107 insertions(+) create mode 100644 .gitignore create mode 100644 authens/__init__.py create mode 100644 authens/apps.py create mode 100644 authens/migrations/0001_initial.py create mode 100644 authens/migrations/__init__.py create mode 100644 authens/models.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4d43738 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +/venv diff --git a/authens/__init__.py b/authens/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/authens/apps.py b/authens/apps.py new file mode 100644 index 0000000..7f3d1dd --- /dev/null +++ b/authens/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class AuthEnsConfig(AppConfig): + name = "authens" diff --git a/authens/migrations/0001_initial.py b/authens/migrations/0001_initial.py new file mode 100644 index 0000000..4c5e246 --- /dev/null +++ b/authens/migrations/0001_initial.py @@ -0,0 +1,56 @@ +# Generated by Django 3.0.6 on 2020-05-10 19:14 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name="Clipper", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "uid", + models.CharField( + max_length=1023, unique=True, verbose_name="login clipper" + ), + ), + ( + "entrance_year", + models.SmallIntegerField( + verbose_name="année de création du compte clipper" + ), + ), + ( + "user", + models.OneToOneField( + on_delete=django.db.models.deletion.CASCADE, + related_name="clipper", + to=settings.AUTH_USER_MODEL, + verbose_name="utilisateurice", + ), + ), + ], + options={ + "verbose_name": "Compte clipper", + "verbose_name_plural": "Comptes clipper", + }, + ), + ] diff --git a/authens/migrations/__init__.py b/authens/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/authens/models.py b/authens/models.py new file mode 100644 index 0000000..36a67f9 --- /dev/null +++ b/authens/models.py @@ -0,0 +1,44 @@ +from django.contrib.auth import get_user_model +from django.db import models +from django.utils.translation import gettext_lazy as _ + +User = get_user_model() + + +class Clipper(models.Model): + """Information about clipper accounts. + + A user is given an instance of this model iff it has a clipper account. The `uid` + field is the clipper login and is used for CAS authentication. + + At each connection, we check that the `entrance_year` we have is consistent with the + meta-data returned by the SPI's CAS. + - if both entrance years match, we connect `self.user` + - if not, we consider that this account is old and assume a new clipper account with + the same id has been created. We remove this instance and create a new user + associated with a new Clipper account. + """ + + user = models.OneToOneField( + User, + verbose_name=_("utilisateurice"), + on_delete=models.CASCADE, + related_name="clipper", + ) + uid = models.CharField( + verbose_name=_("login clipper"), max_length=1023, blank=False, unique=True, + ) + entrance_year = models.SmallIntegerField( + verbose_name=_("année de création du compte clipper"), blank=False, null=False + ) + + class Meta: + verbose_name = _("Compte clipper") + verbose_name_plural = _("Comptes clipper") + + def __str__(self): + return _("compte clipper %(uid)s@%(entrance_year)s lié à %(user)s") % { + "uid": self.uid, + "entrance_year": self.entrance_year, + "user": self.user.username, + }