From 3967831e92c7f9c9517691a47f6b83dfb108e6b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Tue, 22 Sep 2020 20:53:27 +0200 Subject: [PATCH] Migrate django_cas_ng accounts to authens --- .../migrations/0018_django_cas_to_authens.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 gestioncof/migrations/0018_django_cas_to_authens.py diff --git a/gestioncof/migrations/0018_django_cas_to_authens.py b/gestioncof/migrations/0018_django_cas_to_authens.py new file mode 100644 index 00000000..9b689a7e --- /dev/null +++ b/gestioncof/migrations/0018_django_cas_to_authens.py @@ -0,0 +1,40 @@ +import sys + +from authens.shortcuts import fetch_cas_account +from django.db import migrations + + +def to_authens(apps, schema_editor): + User = apps.get_model("auth", "User") + CASAccount = apps.get_model("authens", "CASAccount") + + failures = [] + for user in User.objects.select_related("profile").all(): + login_clipper = user.profile.login_clipper + if login_clipper: + ldap_info = fetch_cas_account(login_clipper) + if ldap_info is None: + failures.append(user) + else: + entrance_year = ldap_info["entrance_year"] + CASAccount.objects.create( + user=user, cas_login=login_clipper, entrance_year=entrance_year + ) + + if failures: + sys.stderr.write(" ---------- \n") + sys.stderr.write("Some accounts could not be linked to a CAS account:\n") + for user in failures: + sys.stderr.write(f"- {user.username}\n") + sys.stderr.write(" ---------- \n") + + +class Migration(migrations.Migration): + + dependencies = [ + ("gestioncof", "0017_petitscours_uniqueness"), + ] + + operations = [ + migrations.RunPython(to_authens, migrations.RunPython.noop), + ]