1
0
forked from ScoDoc/ScoDoc

Améliore gestion erreur lors import utilisateurs CAS

This commit is contained in:
Emmanuel Viennet 2023-03-14 20:06:12 +01:00
parent 7bd03f7630
commit c44ec8c619

View File

@ -116,6 +116,7 @@ CAS_USER_INFO_IDS = (
"cas_id", "cas_id",
"cas_allow_login", "cas_allow_login",
"cas_allow_scodoc_login", "cas_allow_scodoc_login",
"email_institutionnel",
) )
CAS_USER_INFO_COMMENTS = ( CAS_USER_INFO_COMMENTS = (
"""user_name: """user_name:
@ -143,6 +144,9 @@ CAS_USER_INFO_COMMENTS = (
"""cas_allow_scodoc_login """cas_allow_scodoc_login
autorise connexion via ScoDoc même si CAS obligatoire (optionnel, faux par défaut) autorise connexion via ScoDoc même si CAS obligatoire (optionnel, faux par défaut)
""", """,
"""email_institutionnel
optionnel, le mail officiel de l'utilisateur.
Maximum 120 caractères.""",
) )
@ -202,9 +206,16 @@ def cas_users_import_data(users_infos: list[dict]) -> int:
for info in users_infos: for info in users_infos:
user: User = User.query.filter_by(user_name=info["user_name"]).first() user: User = User.query.filter_by(user_name=info["user_name"]).first()
if not user: if not user:
return False, f"utilisateur {info['user_name']} inexistant", 0 db.session.rollback() # au cas où auto-flush
raise ScoValueError(f"""Utilisateur '{info["user_name"]}' inexistant""")
modif = False modif = False
if info["cas_id"].strip() != (user.cas_id or ""): new_cas_id = info["cas_id"].strip()
if new_cas_id != (user.cas_id or ""):
# check unicity
other = User.query.filter_by(cas_id=new_cas_id).first()
if other and other.id != user.id:
db.session.rollback() # au cas où auto-flush
raise ScoValueError(f"cas_id {new_cas_id} dupliqué")
user.cas_id = info["cas_id"].strip() or None user.cas_id = info["cas_id"].strip() or None
modif = True modif = True
val = scu.to_bool(info["cas_allow_login"]) val = scu.to_bool(info["cas_allow_login"])