forked from DGNum/gestioCOF
Migrate django_cas_ng accounts to authens
This commit is contained in:
parent
0815f79739
commit
3967831e92
1 changed files with 40 additions and 0 deletions
40
gestioncof/migrations/0018_django_cas_to_authens.py
Normal file
40
gestioncof/migrations/0018_django_cas_to_authens.py
Normal file
|
@ -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),
|
||||
]
|
Loading…
Reference in a new issue