# -*- mode: python -*- # -*- coding: utf-8 -*- ############################################################################## # # Gestion scolarite IUT # # Copyright (c) 1999 - 2023 Emmanuel Viennet. All rights reserved. # # 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 """ # Anciennement ZScoUsers.py, fonctions de gestion des données réécrites avec flask/SQLAlchemy import re from flask import url_for, g, request from flask_login import current_user from app import db, Departement from app.auth.models import Permission, User from app.models import ScoDocSiteConfig, USERNAME_STR_LEN from app.scodoc import html_sco_header from app.scodoc import sco_preferences from app.scodoc.gen_tables import GenTable from app import cache from app.scodoc.sco_exceptions import ScoValueError def index_html(all_depts=False, with_inactives=False, format="html"): "gestion utilisateurs..." all_depts = int(all_depts) with_inactives = int(with_inactives) H = [html_sco_header.html_sem_header("Gestion des utilisateurs")] if current_user.has_permission(Permission.ScoUsersAdmin, g.scodoc_dept): H.append( f"""
Ajouter un utilisateur""" ) if current_user.is_administrator(): H.append( f""" Importer des utilisateurs
""" ) else: H.append( """ Pour importer des utilisateurs en masse (via fichier xlsx) contactez votre administrateur scodoc.""" ) if all_depts: checked = "checked" else: checked = "" if with_inactives: olds_checked = "checked" else: olds_checked = "" H.append( f"""""" ) L = list_users( g.scodoc_dept, all_depts=all_depts, 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) F = html_sco_header.sco_footer() return "\n".join(H) + F def list_users( dept, all_depts=False, # tous les departements with_inactives=False, # inclut les anciens utilisateurs (status "old") format="html", with_links=True, ): "List users, returns a table in the specified format" 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) comm = f"dept. {dept}" else: users = get_user_list(with_inactives=with_inactives) comm = "tous" if with_inactives: comm += ", avec anciens" comm = "(" + comm + ")" # -- Add some information and links: rows = [] for u in users: # Can current user modify this user ? can_modify = can_handle_passwd(u, allow_admindepts=True) d = u.to_dict() rows.append(d) # Add links if with_links and can_modify: target = url_for( "users.user_info_page", scodoc_dept=dept, user_name=u.user_name ) d["_user_name_target"] = target d["_nom_target"] = target d["_prenom_target"] = target # Hide passwd modification date (depending on visitor's permission) if can_modify: d["non_migre"] = ( "NON MIGRÉ" if u.passwd_temp or u.password_scodoc7 else "ok" ) else: d["date_modif_passwd"] = "(non visible)" d["non_migre"] = "" columns_ids = [ "user_name", "nom_fmt", "prenom_fmt", "email", "dept", "roles_string", "date_expiration", "date_modif_passwd", "non_migre", "status_txt", ] # Seul l'admin peut voir les dates de dernière connexion # et les infos CAS if current_user.is_administrator(): columns_ids.append("last_seen") if ScoDocSiteConfig.is_cas_enabled(): columns_ids += [ "cas_id", "cas_allow_login", "cas_allow_scodoc_login", "cas_last_login", ] title = "Utilisateurs définis dans ScoDoc" tab = GenTable( rows=rows, columns_ids=columns_ids, titles={ "user_name": "Login", "nom_fmt": "Nom", "prenom_fmt": "Prénom", "email": "Mail", "dept": "Dept.", "roles_string": "Rôles", "date_expiration": "Expiration", "date_modif_passwd": "Modif. mot de passe", "last_seen": "Dernière cnx.", "non_migre": "Non migré (!)", "status_txt": "Etat", "cas_id": "Id CAS", "cas_allow_login": "CAS autorisé", "cas_allow_scodoc_login": "Cnx sans CAS", "cas_last_login": "Dernier login CAS", }, caption=title, page_title="title", html_title=f"""Cliquer sur un nom pour changer son mot de passe
""", html_class="table_leftalign list_users", html_with_td_classes=True, html_sortable=True, base_url="%s?all_depts=%s" % (request.base_url, 1 if all_depts else 0), pdf_link=False, # table is too wide to fit in a paper page => disable pdf preferences=sco_preferences.SemPreferences(), ) return tab.make_page(format=format, with_html_headers=False) def get_user_list(dept=None, with_inactives=False): """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) if not with_inactives: q = q.filter_by(active=True) return q.order_by(User.nom, User.user_name).all() @cache.memoize(timeout=50) # seconds def user_info(user_name_or_id=None, user: User = None): """Dict avec infos sur l'utilisateur (qui peut ne pas etre dans notre base). Si user_name est specifie (string ou id), interroge la BD. Sinon, user doit etre une instance de User. """ 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() else: u = User.query.filter_by(user_name=user_name_or_id).first() if u: user_name = u.user_name info = u.to_dict() else: info = None user_name = "inconnu" else: info = user.to_dict() user_name = user.user_name 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, # "nomnoacc": scu.suppress_accents(user_name), "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 MSG_OPT = """