diff --git a/app/auth/models.py b/app/auth/models.py index b79ce703713921fdc6852c2118b31be8930988a8..a3a0c3e47e7f4b1dbb10ad85c286d2b3dee310a7 100644 --- a/app/auth/models.py +++ b/app/auth/models.py @@ -67,14 +67,6 @@ def is_valid_user_name(user_name: str) -> bool: ) -def is_new_cas_id(cas_id: str) -> bool: - "Check that cas_id is a valid new id (uniqueness, allow nulls)" - if not cas_id: - return True - nb_with_this_id = db.session.query(User).filter_by(cas_id=cas_id).count() - return nb_with_this_id == 0 - - class User(UserMixin, ScoDocModel): """ScoDoc users, handled by Flask / SQLAlchemy""" @@ -435,11 +427,18 @@ class User(UserMixin, ScoDocModel): if cas_id: new_cas_id = cas_id if new_cas_id != self.cas_id: - if is_new_cas_id(new_cas_id): + existing: User = ( + db.session.query(User).filter_by(cas_id=new_cas_id).first() + if new_cas_id is not None + else None + ) + if not existing: self.cas_id = new_cas_id else: - log(f"User.from_dict: CAS id invalide pour {self.user_name}") - raise ScoValueError(f"CAS id invalide pour {self.user_name}") + msg = f"""CAS id invalide pour {self.user_name + }, déjà utilisé par {existing.user_name}""" + log(f"User.from_dict: {msg}") + raise ScoValueError(msg) def get_token(self, expires_in=3600): "Un jeton pour cet user. Stocké en base, non commité."