diff --git a/authens/migrations/0002_old_cas_account.py b/authens/migrations/0002_old_cas_account.py new file mode 100644 index 0000000..b1efde5 --- /dev/null +++ b/authens/migrations/0002_old_cas_account.py @@ -0,0 +1,33 @@ +# Generated by Django 3.0.6 on 2020-06-12 17:26 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('authens', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='OldCASAccount', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('cas_login', models.CharField(max_length=1023, verbose_name='ancien login CAS')), + ('entrance_year', models.SmallIntegerField(verbose_name='année de création du compte CAS')), + ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='old_cas_account', to=settings.AUTH_USER_MODEL, verbose_name='utilisateurice')), + ], + options={ + 'verbose_name': 'Ancien compte CAS', + 'verbose_name_plural': 'Anciens comptes CAS', + }, + ), + migrations.AddConstraint( + model_name='oldcasaccount', + constraint=models.UniqueConstraint(fields=('cas_login', 'entrance_year'), name='clipper_year_uniqueness'), + ), + ] diff --git a/authens/models.py b/authens/models.py index 90b107c..93a5d61 100644 --- a/authens/models.py +++ b/authens/models.py @@ -8,7 +8,7 @@ User = get_user_model() class CASAccount(models.Model): """Information about CAS accounts. - A user is given an instance of this model iff she has a CAS account. + A user is given an instance of this model iff they have a CAS account. Instances of this model should only be created by the `ENSCASBackend` authentication backend. @@ -37,3 +37,46 @@ class CASAccount(models.Model): "entrance_year": self.entrance_year, "user": self.user.username, } + + +class OldCASAccount(models.Model): + """Information about expired CAS accounts + + A² user is given an instance of this model iff they had a CAS account that expired. + + Instances of this model should not be created with a new user. + """ + + user = models.OneToOneField( + User, + verbose_name=_("utilisateurice"), + on_delete=models.CASCADE, + related_name="old_cas_account", + ) + + cas_login = models.CharField( + verbose_name=_("ancien login CAS"), max_length=1023, blank=False + ) + + entrance_year = models.SmallIntegerField( + verbose_name=_("année de création du compte CAS"), blank=False, null=False + ) + + class Meta: + # `unique_together` to be deprecated soon : we use `constraints` + constraints = [ + models.UniqueConstraint( + fields=["cas_login", "entrance_year"], name="clipper_year_uniqueness", + ) + ] + verbose_name = _("Ancien compte CAS") + verbose_name_plural = _("Anciens comptes CAS") + + def __str__(self): + return _( + "Ancien compte CAS %(cas_login) (promo %(entrance_year)s) lié à %(user)s" + ) % { + "cas_login": self.cas_login, + "entrance_year": self.entrance_year, + "user": self.user.username, + }