forked from ScoDoc/ScoDoc
refactor check_modif_user
This commit is contained in:
parent
76bc957373
commit
7d5eff4f82
@ -179,11 +179,13 @@ def import_users(users):
|
||||
line = line + 1
|
||||
user_ok, msg = sco_users.check_modif_user(
|
||||
0,
|
||||
ignore_optionals=False,
|
||||
user_name=u["user_name"],
|
||||
nom=u["nom"],
|
||||
prenom=u["prenom"],
|
||||
email=u["email"],
|
||||
roles=u["roles"].split(","),
|
||||
dept=u["dept"],
|
||||
)
|
||||
if not user_ok:
|
||||
append_msg("identifiant '%s' %s" % (u["user_name"], msg))
|
||||
@ -193,39 +195,12 @@ def import_users(users):
|
||||
u["passwd"] = generate_password()
|
||||
#
|
||||
# check identifiant
|
||||
if not re.match(r"^[a-zA-Z0-9@\\\-_\\\.]*$", u["user_name"]):
|
||||
user_ok = False
|
||||
append_msg(
|
||||
"identifiant '%s' invalide (pas d'accents ni de caractères spéciaux)"
|
||||
% u["user_name"]
|
||||
)
|
||||
if len(u["user_name"]) > 64:
|
||||
user_ok = False
|
||||
append_msg(
|
||||
"identifiant '%s' trop long (64 caractères)" % u["user_name"]
|
||||
)
|
||||
if len(u["nom"]) > 64:
|
||||
user_ok = False
|
||||
append_msg("nom '%s' trop long (64 caractères)" % u["nom"])
|
||||
if len(u["prenom"]) > 64:
|
||||
user_ok = False
|
||||
append_msg("prenom '%s' trop long (64 caractères)" % u["prenom"])
|
||||
if len(u["email"]) > 120:
|
||||
user_ok = False
|
||||
append_msg("email '%s' trop long (120 caractères)" % u["email"])
|
||||
# check that tha same user_name has not already been described in this import
|
||||
if u["user_name"] in created.keys():
|
||||
user_ok = False
|
||||
append_msg(
|
||||
"l'utilisateur '%s' a déjà été décrit ligne %s"
|
||||
% (u["user_name"], created[u["user_name"]]["line"])
|
||||
)
|
||||
# check département
|
||||
if u["dept"] != "":
|
||||
dept = Departement.query.filter_by(acronym=u["dept"]).first()
|
||||
if dept is None:
|
||||
user_ok = False
|
||||
append_msg("département '%s' inexistant" % u["dept"])
|
||||
# check roles / ignore whitespaces around roles / build roles_string
|
||||
# roles_string (expected by User) appears as column 'roles' in excel file
|
||||
roles_list = []
|
||||
|
@ -36,7 +36,7 @@ from flask_login import current_user
|
||||
|
||||
import cracklib # pylint: disable=import-error
|
||||
|
||||
from app import db
|
||||
from app import db, Departement
|
||||
|
||||
from app.auth.models import Permission
|
||||
from app.auth.models import User
|
||||
@ -385,7 +385,16 @@ def user_info_page(user_name=None):
|
||||
return "\n".join(H) + F
|
||||
|
||||
|
||||
def check_modif_user(edit, user_name="", nom="", prenom="", email="", roles=[]):
|
||||
def check_modif_user(
|
||||
edit,
|
||||
ignore_optionals=False,
|
||||
user_name="",
|
||||
nom="",
|
||||
prenom="",
|
||||
email="",
|
||||
dept="",
|
||||
roles=[],
|
||||
):
|
||||
"""Vérifie que cet utilisateur peut être créé (edit=0) ou modifié (edit=1)
|
||||
Cherche homonymes.
|
||||
returns (ok, msg)
|
||||
@ -393,19 +402,44 @@ def check_modif_user(edit, user_name="", nom="", prenom="", email="", roles=[]):
|
||||
(si ok est faux, l'utilisateur peut quand même forcer la creation)
|
||||
- msg: message warning a presenter l'utilisateur
|
||||
"""
|
||||
if not user_name or not nom or not prenom:
|
||||
return False, "champ requis vide"
|
||||
if not email:
|
||||
return False, "vous devriez indiquer le mail de l'utilisateur créé !"
|
||||
if not re.fullmatch(r"[^@]+@[^@]+\.[^@]+", email):
|
||||
return False, "l'adresse mail semble incorrecte"
|
||||
MSG_OPT = """Attention: %s (vous pouvez forcer l'opération en cochant "<em>Ignorer les avertissements</em>" en bas de page)"""
|
||||
# ce login existe ?
|
||||
user = _user_list(user_name)
|
||||
if edit and not user: # safety net, le user_name ne devrait pas changer
|
||||
return False, "identifiant %s inexistant" % user_name
|
||||
if not edit and user:
|
||||
return False, "identifiant %s déjà utilisé" % user_name
|
||||
|
||||
if not user_name or not nom or not prenom:
|
||||
return False, "champ requis vide"
|
||||
if not re.match(r"^[a-zA-Z0-9@\\\-_\\\.]*$", user_name):
|
||||
return (
|
||||
False,
|
||||
"identifiant '%s' invalide (pas d'accents ni de caractères spéciaux)"
|
||||
% user_name,
|
||||
)
|
||||
if ignore_optionals and len(user_name) > 64:
|
||||
return False, "identifiant '%s' trop long (64 caractères)" % user_name
|
||||
if ignore_optionals and len(nom) > 64:
|
||||
return False, "nom '%s' trop long (64 caractères)" % nom + MSG_OPT
|
||||
if ignore_optionals and len(prenom) > 64:
|
||||
return False, "prenom '%s' trop long (64 caractères)" % prenom + MSG_OPT
|
||||
# check that tha same user_name has not already been described in this import
|
||||
if not email:
|
||||
return False, "vous devriez indiquer le mail de l'utilisateur créé !"
|
||||
if len(email) > 120:
|
||||
return False, "email '%s' trop long (120 caractères)" % email
|
||||
if not re.fullmatch(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b", email):
|
||||
return False, "l'adresse mail semble incorrecte"
|
||||
# check département
|
||||
if (
|
||||
ignore_optionals
|
||||
and dept != ""
|
||||
and Departement.query.filter_by(acronym=dept).first() is None
|
||||
):
|
||||
return False, "département '%s' inexistant" % u["dept"] + MSG_OPT
|
||||
if ignore_optionals and not roles:
|
||||
return False, "aucun rôle sélectionné, êtes vous sûr ?" + MSG_OPT
|
||||
# ok
|
||||
# Des noms/prénoms semblables existent ?
|
||||
nom = nom.lower().strip()
|
||||
prenom = prenom.lower().strip()
|
||||
@ -425,12 +459,9 @@ def check_modif_user(edit, user_name="", nom="", prenom="", email="", roles=[]):
|
||||
"%s %s (pseudo=%s)" % (x.prenom, x.nom, x.user_name)
|
||||
for x in similar_users
|
||||
]
|
||||
),
|
||||
) + MSG_OPT,
|
||||
)
|
||||
# Roles ?
|
||||
if not roles:
|
||||
return False, "aucun rôle sélectionné, êtes vous sûr ?"
|
||||
# ok
|
||||
return True, ""
|
||||
|
||||
|
||||
|
@ -381,9 +381,9 @@ def create_user_form(user_name=None, edit=0, all_roles=1):
|
||||
H.append(tf_error_message("""Erreur: %s""" % err))
|
||||
return "\n".join(H) + "\n" + tf[1] + F
|
||||
|
||||
if not force:
|
||||
ok, msg = sco_users.check_modif_user(
|
||||
edit,
|
||||
ignore_optionals= force,
|
||||
user_name=user_name,
|
||||
nom=vals["nom"],
|
||||
prenom=vals["prenom"],
|
||||
@ -392,12 +392,8 @@ def create_user_form(user_name=None, edit=0, all_roles=1):
|
||||
)
|
||||
if not ok:
|
||||
H.append(
|
||||
tf_error_message(
|
||||
"""Attention: %s (vous pouvez forcer l'opération en cochant "<em>Ignorer les avertissements</em>" en bas de page)"""
|
||||
% msg
|
||||
tf_error_message(msg)
|
||||
)
|
||||
)
|
||||
|
||||
return "\n".join(H) + "\n" + tf[1] + F
|
||||
|
||||
if "date_expiration" in vals:
|
||||
|
Loading…
Reference in New Issue
Block a user