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
|
sem["groupicon"] = emptygroupicon
|
||||||
|
|
||||||
# S'il n'y a pas d'utilisateurs dans la base, affiche message
|
# 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(
|
H.append(
|
||||||
"""<h2>Aucun utilisateur défini !</h2><p>Pour définir des utilisateurs
|
"""<h2>Aucun utilisateur défini !</h2><p>Pour définir des utilisateurs
|
||||||
<a href="Users">passez par la page Utilisateurs</a>.
|
<a href="Users">passez par la page Utilisateurs</a>.
|
||||||
<br>
|
<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>
|
</p>
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
@ -37,7 +37,7 @@ from flask_login import current_user
|
|||||||
|
|
||||||
from app import db, Departement
|
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.models import ScoDocSiteConfig, USERNAME_STR_LEN
|
||||||
from app.scodoc import html_sco_header
|
from app.scodoc import html_sco_header
|
||||||
from app.scodoc import sco_preferences
|
from app.scodoc import sco_preferences
|
||||||
@ -47,7 +47,9 @@ from app import cache
|
|||||||
from app.scodoc.sco_exceptions import ScoValueError
|
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..."
|
"gestion utilisateurs..."
|
||||||
all_depts = int(all_depts)
|
all_depts = int(all_depts)
|
||||||
with_inactives = int(with_inactives)
|
with_inactives = int(with_inactives)
|
||||||
@ -80,25 +82,47 @@ def index_html(all_depts=False, with_inactives=False, format="html"):
|
|||||||
olds_checked = "checked"
|
olds_checked = "checked"
|
||||||
else:
|
else:
|
||||||
olds_checked = ""
|
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(
|
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();"
|
<input type="checkbox" name="all_depts" value="1" onchange="document.f.submit();"
|
||||||
{checked}>Tous les départements</input>
|
{checked}>Tous les départements</input>
|
||||||
<input type="checkbox" name="with_inactives" value="1" onchange="document.f.submit();"
|
<input type="checkbox" name="with_inactives" value="1" onchange="document.f.submit();"
|
||||||
{olds_checked}>Avec anciens utilisateurs</input>
|
{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,
|
g.scodoc_dept,
|
||||||
all_depts=all_depts,
|
all_depts=all_depts,
|
||||||
|
fmt=fmt,
|
||||||
|
having_role=having_role,
|
||||||
with_inactives=with_inactives,
|
with_inactives=with_inactives,
|
||||||
format=format,
|
|
||||||
with_links=current_user.has_permission(Permission.ScoUsersAdmin, g.scodoc_dept),
|
with_links=current_user.has_permission(Permission.ScoUsersAdmin, g.scodoc_dept),
|
||||||
)
|
)
|
||||||
if format != "html":
|
if fmt != "html":
|
||||||
return L
|
return content
|
||||||
H.append(L)
|
H.append(content)
|
||||||
|
|
||||||
F = html_sco_header.sco_footer()
|
F = html_sco_header.sco_footer()
|
||||||
return "\n".join(H) + F
|
return "\n".join(H) + F
|
||||||
@ -107,22 +131,30 @@ def index_html(all_depts=False, with_inactives=False, format="html"):
|
|||||||
def list_users(
|
def list_users(
|
||||||
dept,
|
dept,
|
||||||
all_depts=False, # tous les departements
|
all_depts=False, # tous les departements
|
||||||
with_inactives=False, # inclut les anciens utilisateurs (status "old")
|
fmt="html",
|
||||||
format="html",
|
having_role: Role = None,
|
||||||
|
with_inactives=False,
|
||||||
with_links=True,
|
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
|
from app.scodoc.sco_permissions_check import can_handle_passwd
|
||||||
|
|
||||||
if dept and not all_depts:
|
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}"
|
comm = f"dept. {dept}"
|
||||||
else:
|
else:
|
||||||
users = get_user_list(with_inactives=with_inactives)
|
users = get_user_list(having_role=having_role, with_inactives=with_inactives)
|
||||||
comm = "tous"
|
comm = "tous"
|
||||||
if with_inactives:
|
if with_inactives:
|
||||||
comm += ", avec anciens"
|
comm += ", avec anciens"
|
||||||
comm = "(" + comm + ")"
|
comm = "(" + comm + ")"
|
||||||
|
if having_role:
|
||||||
|
comm += f" avec le rôle {having_role.name}"
|
||||||
# -- Add some information and links:
|
# -- Add some information and links:
|
||||||
rows = []
|
rows = []
|
||||||
for u in users:
|
for u in users:
|
||||||
@ -208,10 +240,21 @@ def list_users(
|
|||||||
preferences=sco_preferences.SemPreferences(),
|
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.
|
"""Returns list of users.
|
||||||
If dept, select users from this dept,
|
If dept, select users from this dept,
|
||||||
else return all users.
|
else return all users.
|
||||||
@ -222,6 +265,8 @@ def get_user_list(dept=None, with_inactives=False):
|
|||||||
q = q.filter_by(dept=dept)
|
q = q.filter_by(dept=dept)
|
||||||
if not with_inactives:
|
if not with_inactives:
|
||||||
q = q.filter_by(active=True)
|
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()
|
return q.order_by(User.nom, User.user_name).all()
|
||||||
|
|
||||||
|
|
||||||
|
@ -133,12 +133,15 @@ class Mode(IntEnum):
|
|||||||
@scodoc
|
@scodoc
|
||||||
@permission_required(Permission.ScoUsersView)
|
@permission_required(Permission.ScoUsersView)
|
||||||
@scodoc7func
|
@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"
|
"Page accueil utilisateur: tableau avec liste des comptes"
|
||||||
return sco_users.index_html(
|
return sco_users.index_html(
|
||||||
all_depts=all_depts,
|
all_depts=all_depts,
|
||||||
|
having_role_name=having_role_name,
|
||||||
with_inactives=with_inactives,
|
with_inactives=with_inactives,
|
||||||
format=format,
|
fmt=fmt,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user