forked from ScoDoc/ScoDoc
Liste utilisateurs: filtrage par rôle.
This commit is contained in:
parent
b9679d1c01
commit
6aa33f6bd1
@ -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(
|
||||
"""<h2>Aucun utilisateur défini !</h2><p>Pour définir des utilisateurs
|
||||
<a href="Users">passez par la page Utilisateurs</a>.
|
||||
<br>
|
||||
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).
|
||||
</p>
|
||||
"""
|
||||
)
|
||||
|
@ -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"""<option value="{r.name}" {
|
||||
'selected' if having_role_name == r.name else ''
|
||||
}>{r.name}</option>"""
|
||||
for r in Role.query.order_by(Role.name)
|
||||
)
|
||||
H.append(
|
||||
f"""<p><form name="f" action="{request.base_url}" method="get">
|
||||
f"""
|
||||
<form name="f" action="{request.base_url}" method="get">
|
||||
<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>
|
||||
</form></p>"""
|
||||
)
|
||||
|
||||
L = list_users(
|
||||
<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(
|
||||
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()
|
||||
|
||||
|
||||
|
@ -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,
|
||||
)
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user