2021-01-16 14:02:18 +01:00
|
|
|
# -*- mode: python -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# Gestion scolarite IUT
|
|
|
|
#
|
2023-01-02 13:16:27 +01:00
|
|
|
# Copyright (c) 1999 - 2023 Emmanuel Viennet. All rights reserved.
|
2021-01-16 14:02:18 +01:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
# Emmanuel Viennet emmanuel.viennet@viennet.net
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
"""Fonctions sur les utilisateurs
|
|
|
|
"""
|
|
|
|
|
2023-02-26 21:24:07 +01:00
|
|
|
# Anciennement ZScoUsers.py, fonctions de gestion des données réécrites avec flask/SQLAlchemy
|
2021-10-10 09:26:46 +02:00
|
|
|
import re
|
2021-06-26 21:57:54 +02:00
|
|
|
|
2021-09-18 10:10:02 +02:00
|
|
|
from flask import url_for, g, request
|
2021-06-26 21:57:54 +02:00
|
|
|
from flask_login import current_user
|
2021-01-16 14:02:18 +01:00
|
|
|
|
|
|
|
|
2021-10-10 10:52:06 +02:00
|
|
|
from app import db, Departement
|
2021-06-26 21:57:54 +02:00
|
|
|
|
2023-09-04 07:15:42 +02:00
|
|
|
from app.auth.models import Permission, Role, User, UserRole
|
2023-03-02 22:55:25 +01:00
|
|
|
from app.models import ScoDocSiteConfig, USERNAME_STR_LEN
|
2021-06-26 21:57:54 +02:00
|
|
|
from app.scodoc import html_sco_header
|
|
|
|
from app.scodoc import sco_preferences
|
|
|
|
from app.scodoc.gen_tables import GenTable
|
2023-02-26 21:24:07 +01:00
|
|
|
from app import cache
|
2021-01-16 14:02:18 +01:00
|
|
|
|
2023-02-26 21:24:07 +01:00
|
|
|
from app.scodoc.sco_exceptions import ScoValueError
|
2021-06-26 21:57:54 +02:00
|
|
|
|
|
|
|
|
2023-09-04 07:15:42 +02:00
|
|
|
def index_html(
|
|
|
|
all_depts=False, having_role_name: str = "", with_inactives=False, fmt="html"
|
|
|
|
):
|
2021-06-26 21:57:54 +02:00
|
|
|
"gestion utilisateurs..."
|
|
|
|
all_depts = int(all_depts)
|
2021-06-27 12:11:39 +02:00
|
|
|
with_inactives = int(with_inactives)
|
2021-06-26 21:57:54 +02:00
|
|
|
|
2021-09-24 12:10:53 +02:00
|
|
|
H = [html_sco_header.html_sem_header("Gestion des utilisateurs")]
|
2021-06-26 21:57:54 +02:00
|
|
|
|
|
|
|
if current_user.has_permission(Permission.ScoUsersAdmin, g.scodoc_dept):
|
|
|
|
H.append(
|
2023-02-26 21:24:07 +01:00
|
|
|
f"""<p><a href="{url_for("users.create_user_form",
|
|
|
|
scodoc_dept=g.scodoc_dept)
|
|
|
|
}" class="stdlink">Ajouter un utilisateur</a>"""
|
2021-06-26 21:57:54 +02:00
|
|
|
)
|
2021-08-22 13:24:36 +02:00
|
|
|
if current_user.is_administrator():
|
|
|
|
H.append(
|
2023-03-01 19:10:37 +01:00
|
|
|
f""" <a href="{url_for("users.import_users_form",
|
2023-02-26 21:24:07 +01:00
|
|
|
scodoc_dept=g.scodoc_dept)
|
|
|
|
}" class="stdlink">Importer des utilisateurs</a></p>"""
|
2021-08-22 13:24:36 +02:00
|
|
|
)
|
2023-02-26 21:24:07 +01:00
|
|
|
|
2021-08-22 13:24:36 +02:00
|
|
|
else:
|
|
|
|
H.append(
|
2023-02-26 21:24:07 +01:00
|
|
|
""" Pour importer des utilisateurs en masse (via fichier xlsx)
|
|
|
|
contactez votre administrateur scodoc."""
|
2021-06-26 21:57:54 +02:00
|
|
|
)
|
|
|
|
if all_depts:
|
|
|
|
checked = "checked"
|
|
|
|
else:
|
|
|
|
checked = ""
|
2021-06-27 12:11:39 +02:00
|
|
|
if with_inactives:
|
2021-06-26 21:57:54 +02:00
|
|
|
olds_checked = "checked"
|
|
|
|
else:
|
|
|
|
olds_checked = ""
|
2023-09-04 07:15:42 +02:00
|
|
|
|
|
|
|
menu_roles = "\n".join(
|
|
|
|
f"""<option value="{r.name}" {
|
|
|
|
'selected' if having_role_name == r.name else ''
|
|
|
|
}>{r.name}</option>"""
|
|
|
|
for r in Role.query.order_by(Role.name)
|
|
|
|
)
|
2021-06-26 21:57:54 +02:00
|
|
|
H.append(
|
2023-09-04 07:15:42 +02:00
|
|
|
f"""
|
|
|
|
<form name="f" action="{request.base_url}" method="get">
|
2023-02-26 21:24:07 +01:00
|
|
|
<input type="checkbox" name="all_depts" value="1" onchange="document.f.submit();"
|
|
|
|
{checked}>Tous les départements</input>
|
|
|
|
<input type="checkbox" name="with_inactives" value="1" onchange="document.f.submit();"
|
|
|
|
{olds_checked}>Avec anciens utilisateurs</input>
|
2021-06-26 21:57:54 +02:00
|
|
|
|
2023-09-04 07:15:42 +02:00
|
|
|
<label for="having_role_name" style="margin-left:16px;">Filtrer par rôle:</label>
|
|
|
|
<select id="having_role_name" name="having_role_name" onchange="document.f.submit();">
|
|
|
|
<option value="">--Choisir--</option>
|
|
|
|
{menu_roles}
|
|
|
|
</select>
|
|
|
|
|
|
|
|
</form>
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
if having_role_name:
|
|
|
|
having_role: Role = Role.query.filter_by(name=having_role_name).first()
|
|
|
|
if not having_role:
|
|
|
|
raise ScoValueError("nom de rôle invalide")
|
|
|
|
else:
|
|
|
|
having_role = None
|
|
|
|
content = list_users(
|
2021-06-26 21:57:54 +02:00
|
|
|
g.scodoc_dept,
|
|
|
|
all_depts=all_depts,
|
2023-09-04 07:15:42 +02:00
|
|
|
fmt=fmt,
|
|
|
|
having_role=having_role,
|
2021-06-27 12:11:39 +02:00
|
|
|
with_inactives=with_inactives,
|
2021-06-26 21:57:54 +02:00
|
|
|
with_links=current_user.has_permission(Permission.ScoUsersAdmin, g.scodoc_dept),
|
|
|
|
)
|
2023-09-04 07:15:42 +02:00
|
|
|
if fmt != "html":
|
|
|
|
return content
|
|
|
|
H.append(content)
|
2021-06-26 21:57:54 +02:00
|
|
|
|
2021-07-29 10:19:00 +02:00
|
|
|
F = html_sco_header.sco_footer()
|
2021-06-26 21:57:54 +02:00
|
|
|
return "\n".join(H) + F
|
|
|
|
|
|
|
|
|
|
|
|
def list_users(
|
|
|
|
dept,
|
|
|
|
all_depts=False, # tous les departements
|
2023-09-04 07:15:42 +02:00
|
|
|
fmt="html",
|
|
|
|
having_role: Role = None,
|
|
|
|
with_inactives=False,
|
2021-06-26 21:57:54 +02:00
|
|
|
with_links=True,
|
|
|
|
):
|
2023-09-04 07:15:42 +02:00
|
|
|
"""List users, returns a table in the specified format.
|
|
|
|
Si with_inactives, inclut les anciens utilisateurs (status "old").
|
|
|
|
Si having_role, ne liste que les utilisateurs ayant ce rôle.
|
|
|
|
"""
|
2021-07-03 23:35:32 +02:00
|
|
|
from app.scodoc.sco_permissions_check import can_handle_passwd
|
|
|
|
|
2021-06-26 21:57:54 +02:00
|
|
|
if dept and not all_depts:
|
2023-09-04 07:15:42 +02:00
|
|
|
users = get_user_list(
|
|
|
|
dept=dept, having_role=having_role, with_inactives=with_inactives
|
|
|
|
)
|
2023-02-26 22:05:21 +01:00
|
|
|
comm = f"dept. {dept}"
|
2021-06-26 21:57:54 +02:00
|
|
|
else:
|
2023-09-04 07:15:42 +02:00
|
|
|
users = get_user_list(having_role=having_role, with_inactives=with_inactives)
|
2021-06-26 21:57:54 +02:00
|
|
|
comm = "tous"
|
2021-06-27 12:11:39 +02:00
|
|
|
if with_inactives:
|
2021-06-26 21:57:54 +02:00
|
|
|
comm += ", avec anciens"
|
|
|
|
comm = "(" + comm + ")"
|
2023-09-04 07:15:42 +02:00
|
|
|
if having_role:
|
|
|
|
comm += f" avec le rôle {having_role.name}"
|
2021-06-26 21:57:54 +02:00
|
|
|
# -- Add some information and links:
|
2023-02-26 22:05:21 +01:00
|
|
|
rows = []
|
2021-06-26 21:57:54 +02:00
|
|
|
for u in users:
|
|
|
|
# Can current user modify this user ?
|
|
|
|
can_modify = can_handle_passwd(u, allow_admindepts=True)
|
|
|
|
|
|
|
|
d = u.to_dict()
|
2023-02-26 22:05:21 +01:00
|
|
|
rows.append(d)
|
2021-06-26 21:57:54 +02:00
|
|
|
# Add links
|
|
|
|
if with_links and can_modify:
|
|
|
|
target = url_for(
|
2021-06-27 12:11:39 +02:00
|
|
|
"users.user_info_page", scodoc_dept=dept, user_name=u.user_name
|
2021-07-27 16:55:50 +02:00
|
|
|
)
|
2021-06-26 21:57:54 +02:00
|
|
|
d["_user_name_target"] = target
|
|
|
|
d["_nom_target"] = target
|
|
|
|
d["_prenom_target"] = target
|
|
|
|
|
|
|
|
# Hide passwd modification date (depending on visitor's permission)
|
2023-03-03 14:33:26 +01:00
|
|
|
if can_modify:
|
|
|
|
d["non_migre"] = (
|
|
|
|
"NON MIGRÉ" if u.passwd_temp or u.password_scodoc7 else "ok"
|
|
|
|
)
|
|
|
|
else:
|
2021-06-26 21:57:54 +02:00
|
|
|
d["date_modif_passwd"] = "(non visible)"
|
2023-03-03 14:33:26 +01:00
|
|
|
d["non_migre"] = ""
|
2021-06-26 21:57:54 +02:00
|
|
|
|
2021-10-03 18:19:51 +02:00
|
|
|
columns_ids = [
|
|
|
|
"user_name",
|
|
|
|
"nom_fmt",
|
|
|
|
"prenom_fmt",
|
|
|
|
"email",
|
|
|
|
"dept",
|
|
|
|
"roles_string",
|
|
|
|
"date_expiration",
|
|
|
|
"date_modif_passwd",
|
2023-03-03 14:33:26 +01:00
|
|
|
"non_migre",
|
2021-10-03 18:19:51 +02:00
|
|
|
"status_txt",
|
|
|
|
]
|
|
|
|
# Seul l'admin peut voir les dates de dernière connexion
|
2023-02-26 22:05:21 +01:00
|
|
|
# et les infos CAS
|
2021-10-03 18:19:51 +02:00
|
|
|
if current_user.is_administrator():
|
|
|
|
columns_ids.append("last_seen")
|
2023-02-26 22:05:21 +01:00
|
|
|
if ScoDocSiteConfig.is_cas_enabled():
|
2023-03-02 14:49:18 +01:00
|
|
|
columns_ids += [
|
|
|
|
"cas_id",
|
|
|
|
"cas_allow_login",
|
|
|
|
"cas_allow_scodoc_login",
|
|
|
|
"cas_last_login",
|
|
|
|
]
|
2023-03-14 16:30:27 +01:00
|
|
|
columns_ids.append("email_institutionnel")
|
2023-02-26 22:05:21 +01:00
|
|
|
|
2021-06-26 21:57:54 +02:00
|
|
|
title = "Utilisateurs définis dans ScoDoc"
|
|
|
|
tab = GenTable(
|
2023-02-26 22:05:21 +01:00
|
|
|
rows=rows,
|
2021-10-03 18:19:51 +02:00
|
|
|
columns_ids=columns_ids,
|
2021-06-26 21:57:54 +02:00
|
|
|
titles={
|
|
|
|
"user_name": "Login",
|
|
|
|
"nom_fmt": "Nom",
|
|
|
|
"prenom_fmt": "Prénom",
|
|
|
|
"email": "Mail",
|
2023-03-14 16:30:27 +01:00
|
|
|
"email_institutionnel": "Mail institutionnel (opt.)",
|
2021-06-26 21:57:54 +02:00
|
|
|
"dept": "Dept.",
|
2021-08-20 03:28:33 +02:00
|
|
|
"roles_string": "Rôles",
|
2021-06-26 21:57:54 +02:00
|
|
|
"date_expiration": "Expiration",
|
|
|
|
"date_modif_passwd": "Modif. mot de passe",
|
2021-10-03 18:19:51 +02:00
|
|
|
"last_seen": "Dernière cnx.",
|
2023-03-03 14:33:26 +01:00
|
|
|
"non_migre": "Non migré (!)",
|
2021-06-26 21:57:54 +02:00
|
|
|
"status_txt": "Etat",
|
2023-02-26 22:05:21 +01:00
|
|
|
"cas_id": "Id CAS",
|
|
|
|
"cas_allow_login": "CAS autorisé",
|
|
|
|
"cas_allow_scodoc_login": "Cnx sans CAS",
|
2023-03-02 14:49:18 +01:00
|
|
|
"cas_last_login": "Dernier login CAS",
|
2021-06-26 21:57:54 +02:00
|
|
|
},
|
|
|
|
caption=title,
|
|
|
|
page_title="title",
|
2023-02-26 22:05:21 +01:00
|
|
|
html_title=f"""<h2>{len(rows)} utilisateurs {comm}</h2>
|
2023-02-26 21:24:07 +01:00
|
|
|
<p class="help">Cliquer sur un nom pour changer son mot de passe</p>""",
|
2021-06-26 21:57:54 +02:00
|
|
|
html_class="table_leftalign list_users",
|
|
|
|
html_with_td_classes=True,
|
|
|
|
html_sortable=True,
|
2021-10-03 18:19:51 +02:00
|
|
|
base_url="%s?all_depts=%s" % (request.base_url, 1 if all_depts else 0),
|
2021-06-26 21:57:54 +02:00
|
|
|
pdf_link=False, # table is too wide to fit in a paper page => disable pdf
|
2021-08-21 00:24:51 +02:00
|
|
|
preferences=sco_preferences.SemPreferences(),
|
2021-06-26 21:57:54 +02:00
|
|
|
)
|
|
|
|
|
2023-09-21 10:20:19 +02:00
|
|
|
return tab.make_page(fmt=fmt, with_html_headers=False)
|
2023-09-04 07:15:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_users_count(dept=None) -> int:
|
|
|
|
"""Nombre de comptes utilisateurs, tout état confondu, dans ce dept
|
|
|
|
(ou dans tous si None)"""
|
|
|
|
q = User.query
|
|
|
|
if dept is not None:
|
|
|
|
q = q.filter_by(dept=dept)
|
|
|
|
return q.count()
|
2021-06-26 21:57:54 +02:00
|
|
|
|
|
|
|
|
2023-09-04 07:15:42 +02:00
|
|
|
def get_user_list(
|
|
|
|
dept=None, with_inactives=False, having_role: Role = None
|
|
|
|
) -> list[User]:
|
2021-06-26 21:57:54 +02:00
|
|
|
"""Returns list of users.
|
|
|
|
If dept, select users from this dept,
|
|
|
|
else return all users.
|
|
|
|
"""
|
|
|
|
# was get_userlist
|
|
|
|
q = User.query
|
|
|
|
if dept is not None:
|
|
|
|
q = q.filter_by(dept=dept)
|
2021-06-27 12:11:39 +02:00
|
|
|
if not with_inactives:
|
|
|
|
q = q.filter_by(active=True)
|
2023-09-04 07:15:42 +02:00
|
|
|
if having_role:
|
|
|
|
q = q.join(UserRole).filter_by(role_id=having_role.id)
|
2021-06-27 12:11:39 +02:00
|
|
|
return q.order_by(User.nom, User.user_name).all()
|
|
|
|
|
|
|
|
|
2021-11-02 23:42:46 +01:00
|
|
|
@cache.memoize(timeout=50) # seconds
|
2022-04-05 22:23:55 +02:00
|
|
|
def user_info(user_name_or_id=None, user: User = None):
|
2021-06-28 10:45:00 +02:00
|
|
|
"""Dict avec infos sur l'utilisateur (qui peut ne pas etre dans notre base).
|
2021-08-10 12:57:38 +02:00
|
|
|
Si user_name est specifie (string ou id), interroge la BD. Sinon, user doit etre une instance
|
2021-07-21 15:53:15 +02:00
|
|
|
de User.
|
2021-06-27 12:11:39 +02:00
|
|
|
"""
|
2021-08-22 13:24:36 +02:00
|
|
|
if user_name_or_id is not None:
|
|
|
|
if isinstance(user_name_or_id, int):
|
|
|
|
u = User.query.filter_by(id=user_name_or_id).first()
|
2021-08-10 12:57:38 +02:00
|
|
|
else:
|
2021-08-22 13:24:36 +02:00
|
|
|
u = User.query.filter_by(user_name=user_name_or_id).first()
|
2021-08-10 12:57:38 +02:00
|
|
|
if u:
|
2021-08-22 13:24:36 +02:00
|
|
|
user_name = u.user_name
|
2021-08-10 12:57:38 +02:00
|
|
|
info = u.to_dict()
|
|
|
|
else:
|
|
|
|
info = None
|
2021-09-11 15:59:06 +02:00
|
|
|
user_name = "inconnu"
|
2021-06-27 12:11:39 +02:00
|
|
|
else:
|
2021-07-21 15:53:15 +02:00
|
|
|
info = user.to_dict()
|
|
|
|
user_name = user.user_name
|
2021-06-27 12:11:39 +02:00
|
|
|
|
|
|
|
if not info:
|
|
|
|
# special case: user is not in our database
|
|
|
|
return {
|
|
|
|
"user_name": user_name,
|
|
|
|
"nom": user_name,
|
|
|
|
"prenom": "",
|
|
|
|
"email": "",
|
|
|
|
"dept": "",
|
|
|
|
"nomprenom": user_name,
|
|
|
|
"prenomnom": user_name,
|
|
|
|
"prenom_fmt": "",
|
|
|
|
"nom_fmt": user_name,
|
|
|
|
"nomcomplet": user_name,
|
|
|
|
"nomplogin": user_name,
|
2021-07-04 12:32:13 +02:00
|
|
|
# "nomnoacc": scu.suppress_accents(user_name),
|
2021-06-27 12:11:39 +02:00
|
|
|
"passwd_temp": 0,
|
|
|
|
"status": "",
|
|
|
|
"date_expiration": None,
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
# Ensure we never publish password hash
|
|
|
|
if "password_hash" in info:
|
|
|
|
del info["password_hash"]
|
|
|
|
return info
|
2021-06-26 21:57:54 +02:00
|
|
|
|
|
|
|
|
2023-02-26 21:24:07 +01:00
|
|
|
MSG_OPT = """<br>Attention: (vous pouvez forcer l'opération en cochant "<em>Ignorer les avertissements</em>" en bas de page)"""
|
|
|
|
|
|
|
|
|
2021-10-10 10:52:06 +02:00
|
|
|
def check_modif_user(
|
2023-03-02 22:55:25 +01:00
|
|
|
edit: bool,
|
|
|
|
enforce_optionals: bool = False,
|
|
|
|
user_name: str = "",
|
|
|
|
nom: str = "",
|
|
|
|
prenom: str = "",
|
|
|
|
email: str = "",
|
|
|
|
dept: str = "",
|
2023-02-26 21:24:07 +01:00
|
|
|
roles: list = None,
|
|
|
|
cas_id: str = None,
|
2023-03-02 22:55:25 +01:00
|
|
|
) -> tuple[bool, str]:
|
2021-07-03 16:19:42 +02:00
|
|
|
"""Vérifie que cet utilisateur peut être créé (edit=0) ou modifié (edit=1)
|
|
|
|
Cherche homonymes.
|
2023-03-02 14:49:18 +01:00
|
|
|
Ne vérifie PAS que l'on a la permission de faire la modif.
|
2023-03-02 22:55:25 +01:00
|
|
|
|
|
|
|
edit: si vrai, mode "edition" (modif d'un objet existant)
|
|
|
|
enforce_optionals: vérifie que les champs optionnels sont cohérents.
|
|
|
|
|
|
|
|
Returns (ok, msg)
|
2021-07-03 16:19:42 +02:00
|
|
|
- ok : si vrai, peut continuer avec ces parametres
|
|
|
|
(si ok est faux, l'utilisateur peut quand même forcer la creation)
|
2021-10-13 10:33:20 +02:00
|
|
|
- msg: message warning à presenter à l'utilisateur
|
2021-07-03 16:19:42 +02:00
|
|
|
"""
|
2023-02-26 21:24:07 +01:00
|
|
|
roles = roles or []
|
2021-07-03 16:19:42 +02:00
|
|
|
# ce login existe ?
|
2023-02-26 21:24:07 +01:00
|
|
|
user = User.query.filter_by(user_name=user_name).first()
|
2021-07-21 15:53:15 +02:00
|
|
|
if edit and not user: # safety net, le user_name ne devrait pas changer
|
2023-02-26 21:24:07 +01:00
|
|
|
return False, f"identifiant {user_name} inexistant"
|
2021-07-21 15:53:15 +02:00
|
|
|
if not edit and user:
|
2023-02-26 21:24:07 +01:00
|
|
|
return False, f"identifiant {user_name} déjà utilisé"
|
2021-10-10 10:52:06 +02:00
|
|
|
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,
|
2023-02-26 21:24:07 +01:00
|
|
|
f"identifiant '{user_name}' invalide (pas d'accents ni de caractères spéciaux)",
|
2021-10-10 10:52:06 +02:00
|
|
|
)
|
2023-03-02 22:55:25 +01:00
|
|
|
if len(user_name) > USERNAME_STR_LEN:
|
|
|
|
return (
|
|
|
|
False,
|
|
|
|
f"identifiant '{user_name}' trop long ({USERNAME_STR_LEN} caractères)",
|
|
|
|
)
|
|
|
|
if len(nom) > USERNAME_STR_LEN:
|
|
|
|
return False, f"nom '{nom}' trop long ({USERNAME_STR_LEN} caractères)" + MSG_OPT
|
|
|
|
if len(prenom) > 64:
|
|
|
|
return (
|
|
|
|
False,
|
|
|
|
f"prenom '{prenom}' trop long ({USERNAME_STR_LEN} caractères)" + MSG_OPT,
|
|
|
|
)
|
2023-02-26 21:24:07 +01:00
|
|
|
# check that same user_name has not already been described in this import
|
2021-10-10 10:52:06 +02:00
|
|
|
if not email:
|
|
|
|
return False, "vous devriez indiquer le mail de l'utilisateur créé !"
|
|
|
|
if len(email) > 120:
|
2023-02-26 21:24:07 +01:00
|
|
|
return False, f"email '{email}' trop long (120 caractères)"
|
2021-10-10 10:52:06 +02:00
|
|
|
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 (
|
2021-10-13 16:32:43 +02:00
|
|
|
enforce_optionals
|
2022-08-05 17:05:24 +02:00
|
|
|
and dept
|
2021-10-10 10:52:06 +02:00
|
|
|
and Departement.query.filter_by(acronym=dept).first() is None
|
|
|
|
):
|
2023-02-26 21:24:07 +01:00
|
|
|
return False, f"département '{dept}' inexistant" + MSG_OPT
|
2021-10-13 16:32:43 +02:00
|
|
|
if enforce_optionals and not roles:
|
2021-10-10 10:52:06 +02:00
|
|
|
return False, "aucun rôle sélectionné, êtes vous sûr ?" + MSG_OPT
|
2021-10-15 19:17:40 +02:00
|
|
|
# Unicité du mail
|
|
|
|
users_with_this_mail = User.query.filter_by(email=email).all()
|
|
|
|
if edit: # modification
|
2023-02-26 21:24:07 +01:00
|
|
|
if email != user.email and len(users_with_this_mail) > 0:
|
2021-10-15 19:17:40 +02:00
|
|
|
return False, "un autre utilisateur existe déjà avec cette adresse mail"
|
|
|
|
else: # création utilisateur
|
|
|
|
if len(users_with_this_mail) > 0:
|
|
|
|
return False, "un autre utilisateur existe déjà avec cette adresse mail"
|
|
|
|
|
2023-02-26 21:24:07 +01:00
|
|
|
# Unicité du cas_id
|
|
|
|
if cas_id:
|
2023-05-12 12:59:23 +02:00
|
|
|
cas_users = User.query.filter_by(cas_id=str(cas_id)).all()
|
2023-02-26 21:24:07 +01:00
|
|
|
if edit:
|
|
|
|
if cas_users and (
|
|
|
|
len(cas_users) > 1 or cas_users[0].user_name != user_name
|
|
|
|
):
|
|
|
|
return (
|
|
|
|
False,
|
|
|
|
"un autre utilisateur existe déjà avec cet identifiant CAS",
|
|
|
|
)
|
|
|
|
elif cas_users:
|
|
|
|
return False, "un autre utilisateur existe déjà avec cet identifiant CAS"
|
2021-07-03 16:19:42 +02:00
|
|
|
# Des noms/prénoms semblables existent ?
|
|
|
|
nom = nom.lower().strip()
|
|
|
|
prenom = prenom.lower().strip()
|
|
|
|
similar_users = User.query.filter(
|
|
|
|
User.nom.ilike(nom), User.prenom.ilike(prenom)
|
|
|
|
).all()
|
|
|
|
if edit:
|
|
|
|
minmatch = 1
|
|
|
|
else:
|
|
|
|
minmatch = 0
|
2021-10-13 16:32:43 +02:00
|
|
|
if enforce_optionals and len(similar_users) > minmatch:
|
2021-07-03 16:19:42 +02:00
|
|
|
return (
|
|
|
|
False,
|
|
|
|
"des utilisateurs proches existent: "
|
|
|
|
+ ", ".join(
|
|
|
|
[
|
|
|
|
"%s %s (pseudo=%s)" % (x.prenom, x.nom, x.user_name)
|
|
|
|
for x in similar_users
|
|
|
|
]
|
2021-10-10 21:09:27 +02:00
|
|
|
)
|
|
|
|
+ MSG_OPT,
|
2021-07-03 16:19:42 +02:00
|
|
|
)
|
|
|
|
# Roles ?
|
|
|
|
return True, ""
|
|
|
|
|
|
|
|
|
|
|
|
def user_edit(user_name, vals):
|
|
|
|
"""Edit the user specified by user_name
|
|
|
|
(ported from Zope to SQLAlchemy, hence strange !)
|
|
|
|
"""
|
2023-02-26 21:24:07 +01:00
|
|
|
u: User = User.query.filter_by(user_name=user_name).first()
|
2021-07-03 16:19:42 +02:00
|
|
|
if not u:
|
|
|
|
raise ScoValueError("Invalid user_name")
|
|
|
|
u.from_dict(vals)
|
|
|
|
db.session.add(u)
|
|
|
|
db.session.commit()
|