From 6aa33f6bd1514f99cd360c33edc659bc8d52c5e8 Mon Sep 17 00:00:00 2001
From: Emmanuel Viennet
Date: Mon, 4 Sep 2023 07:15:42 +0200
Subject: [PATCH] =?UTF-8?q?Liste=20utilisateurs:=20filtrage=20par=20r?=
=?UTF-8?q?=C3=B4le.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
app/scodoc/sco_dept.py | 5 +--
app/scodoc/sco_users.py | 79 ++++++++++++++++++++++++++++++++---------
app/views/users.py | 7 ++--
3 files changed, 70 insertions(+), 21 deletions(-)
diff --git a/app/scodoc/sco_dept.py b/app/scodoc/sco_dept.py
index 110a764c..010cb539 100644
--- a/app/scodoc/sco_dept.py
+++ b/app/scodoc/sco_dept.py
@@ -94,12 +94,13 @@ def index_html(showcodes=0, showsemtable=0):
sem["groupicon"] = emptygroupicon
# S'il n'y a pas d'utilisateurs dans la base, affiche message
- if not sco_users.get_user_list(dept=g.scodoc_dept):
+ if not sco_users.get_users_count(dept=g.scodoc_dept):
H.append(
"""
Aucun utilisateur défini !
Pour définir des utilisateurs
passez par la page Utilisateurs.
- Définissez au moins un utilisateur avec le rôle AdminXXX (le responsable du département XXX).
+ Définissez au moins un utilisateur avec le rôle AdminXXX
+ (le responsable du département XXX).
"""
)
diff --git a/app/scodoc/sco_users.py b/app/scodoc/sco_users.py
index 72168b83..63ae2aae 100644
--- a/app/scodoc/sco_users.py
+++ b/app/scodoc/sco_users.py
@@ -37,7 +37,7 @@ from flask_login import current_user
from app import db, Departement
-from app.auth.models import Permission, User
+from app.auth.models import Permission, Role, User, UserRole
from app.models import ScoDocSiteConfig, USERNAME_STR_LEN
from app.scodoc import html_sco_header
from app.scodoc import sco_preferences
@@ -47,7 +47,9 @@ from app import cache
from app.scodoc.sco_exceptions import ScoValueError
-def index_html(all_depts=False, with_inactives=False, format="html"):
+def index_html(
+ all_depts=False, having_role_name: str = "", with_inactives=False, fmt="html"
+):
"gestion utilisateurs..."
all_depts = int(all_depts)
with_inactives = int(with_inactives)
@@ -80,25 +82,47 @@ def index_html(all_depts=False, with_inactives=False, format="html"):
olds_checked = "checked"
else:
olds_checked = ""
+
+ menu_roles = "\n".join(
+ f""""""
+ for r in Role.query.order_by(Role.name)
+ )
H.append(
- f"""
"""
- )
- L = list_users(
+
+
+
+
+ """
+ )
+ 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(
g.scodoc_dept,
all_depts=all_depts,
+ fmt=fmt,
+ having_role=having_role,
with_inactives=with_inactives,
- format=format,
with_links=current_user.has_permission(Permission.ScoUsersAdmin, g.scodoc_dept),
)
- if format != "html":
- return L
- H.append(L)
+ if fmt != "html":
+ return content
+ H.append(content)
F = html_sco_header.sco_footer()
return "\n".join(H) + F
@@ -107,22 +131,30 @@ def index_html(all_depts=False, with_inactives=False, format="html"):
def list_users(
dept,
all_depts=False, # tous les departements
- with_inactives=False, # inclut les anciens utilisateurs (status "old")
- format="html",
+ fmt="html",
+ having_role: Role = None,
+ with_inactives=False,
with_links=True,
):
- "List users, returns a table in the specified format"
+ """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.
+ """
from app.scodoc.sco_permissions_check import can_handle_passwd
if dept and not all_depts:
- users = get_user_list(dept=dept, with_inactives=with_inactives)
+ users = get_user_list(
+ dept=dept, having_role=having_role, with_inactives=with_inactives
+ )
comm = f"dept. {dept}"
else:
- users = get_user_list(with_inactives=with_inactives)
+ users = get_user_list(having_role=having_role, with_inactives=with_inactives)
comm = "tous"
if with_inactives:
comm += ", avec anciens"
comm = "(" + comm + ")"
+ if having_role:
+ comm += f" avec le rôle {having_role.name}"
# -- Add some information and links:
rows = []
for u in users:
@@ -208,10 +240,21 @@ def list_users(
preferences=sco_preferences.SemPreferences(),
)
- return tab.make_page(format=format, with_html_headers=False)
+ return tab.make_page(format=fmt, with_html_headers=False)
-def get_user_list(dept=None, with_inactives=False):
+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()
+
+
+def get_user_list(
+ dept=None, with_inactives=False, having_role: Role = None
+) -> list[User]:
"""Returns list of users.
If dept, select users from this dept,
else return all users.
@@ -222,6 +265,8 @@ def get_user_list(dept=None, with_inactives=False):
q = q.filter_by(dept=dept)
if not with_inactives:
q = q.filter_by(active=True)
+ if having_role:
+ q = q.join(UserRole).filter_by(role_id=having_role.id)
return q.order_by(User.nom, User.user_name).all()
diff --git a/app/views/users.py b/app/views/users.py
index 6b179532..6f60b090 100644
--- a/app/views/users.py
+++ b/app/views/users.py
@@ -133,12 +133,15 @@ class Mode(IntEnum):
@scodoc
@permission_required(Permission.ScoUsersView)
@scodoc7func
-def index_html(all_depts=False, with_inactives=False, format="html"):
+def index_html(
+ all_depts=False, having_role_name: str = "", with_inactives=False, fmt="html"
+):
"Page accueil utilisateur: tableau avec liste des comptes"
return sco_users.index_html(
all_depts=all_depts,
+ having_role_name=having_role_name,
with_inactives=with_inactives,
- format=format,
+ fmt=fmt,
)