forked from ScoDoc/ScoDoc
256 lines
7.6 KiB
Python
256 lines
7.6 KiB
Python
# -*- mode: python -*-
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
#
|
|
# Gestion scolarite IUT
|
|
#
|
|
# Copyright (c) 1999 - 2021 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éécrite avec flask/SQLAlchemy
|
|
|
|
import string
|
|
import re
|
|
import time
|
|
import md5
|
|
import base64
|
|
import jaxml
|
|
|
|
from flask import current_app, url_for, g
|
|
from flask_login import current_user
|
|
|
|
import cracklib # pylint: disable=import-error
|
|
|
|
|
|
from app.auth.models import Permission
|
|
from app.auth.models import User
|
|
|
|
from app.scodoc import html_sco_header
|
|
from app.scodoc import sco_cache
|
|
from app.scodoc import sco_etud
|
|
from app.scodoc import sco_excel
|
|
from app.scodoc import sco_import_users
|
|
from app.scodoc import sco_preferences
|
|
from app.scodoc.gen_tables import GenTable
|
|
from app.scodoc.notes_log import log
|
|
from app.scodoc.sco_permissions_check import can_handle_passwd
|
|
from app.scodoc.scolog import logdb
|
|
from app.scodoc.sco_etud import format_prenom, format_nom
|
|
from app.scodoc.TrivialFormulator import TrivialFormulator, tf_error_message
|
|
import app.scodoc.notesdb as ndb
|
|
import app.scodoc.sco_utils as scu
|
|
|
|
from app.scodoc.sco_exceptions import (
|
|
AccessDenied,
|
|
ScoException,
|
|
ScoValueError,
|
|
ScoInvalidDateError,
|
|
ScoLockedFormError,
|
|
ScoGenError,
|
|
)
|
|
|
|
|
|
def is_valid_password(cleartxt):
|
|
"""Check password.
|
|
returns True if OK.
|
|
"""
|
|
if (
|
|
hasattr(scu.CONFIG, "MIN_PASSWORD_LENGTH")
|
|
and scu.CONFIG.MIN_PASSWORD_LENGTH > 0
|
|
and len(cleartxt) < scu.CONFIG.MIN_PASSWORD_LENGTH
|
|
):
|
|
return False # invalid: too short
|
|
try:
|
|
_ = cracklib.FascistCheck(cleartxt)
|
|
return True
|
|
except ValueError:
|
|
return False
|
|
|
|
|
|
# ---------------
|
|
|
|
# ---------------
|
|
# XXX TODO supprimer ndb.GetUsersDBConnexion
|
|
|
|
|
|
def index_html(context, REQUEST, all_depts=False, with_olds=False, format="html"):
|
|
"gestion utilisateurs..."
|
|
all_depts = int(all_depts)
|
|
with_olds = int(with_olds)
|
|
|
|
H = [html_sco_header.html_sem_header(context, REQUEST, "Gestion des utilisateurs")]
|
|
H.append("<h2>Gestion des utilisateurs</h2>")
|
|
|
|
if current_user.has_permission(Permission.ScoUsersAdmin, g.scodoc_dept):
|
|
H.append(
|
|
'<p><a href="{}" class="stdlink">Ajouter un utilisateur</a>'.format(
|
|
url_for("users.create_user_form", scodoc_dept=g.scodoc_dept).encode(
|
|
scu.SCO_ENCODING
|
|
) # sco8
|
|
)
|
|
)
|
|
H.append(
|
|
' <a href="{}" class="stdlink">Importer des utilisateurs</a></p>'.format(
|
|
url_for("users.import_users_form", scodoc_dept=g.scodoc_dept).encode(
|
|
scu.SCO_ENCODING
|
|
) # sco8
|
|
)
|
|
)
|
|
if all_depts:
|
|
checked = "checked"
|
|
else:
|
|
checked = ""
|
|
if with_olds:
|
|
olds_checked = "checked"
|
|
else:
|
|
olds_checked = ""
|
|
H.append(
|
|
"""<p><form name="f" action="%s" method="get">
|
|
<input type="checkbox" name="all_depts" value="1" onchange="document.f.submit();" %s>Tous les départements</input>
|
|
<input type="checkbox" name="with_olds" value="1" onchange="document.f.submit();" %s>Avec anciens utilisateurs</input>
|
|
</form></p>"""
|
|
% (REQUEST.URL0, checked, olds_checked)
|
|
)
|
|
|
|
L = list_users(
|
|
context,
|
|
g.scodoc_dept,
|
|
all_depts=all_depts,
|
|
with_olds=with_olds,
|
|
format=format,
|
|
REQUEST=REQUEST,
|
|
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(context, REQUEST)
|
|
return "\n".join(H) + F
|
|
|
|
|
|
def list_users(
|
|
context,
|
|
dept,
|
|
all_depts=False, # tous les departements
|
|
with_olds=False, # inclut les anciens utilisateurs (status "old")
|
|
format="html",
|
|
with_links=True,
|
|
REQUEST=None,
|
|
):
|
|
"List users, returns a table in the specified format"
|
|
if dept and not all_depts:
|
|
users = get_user_list(dept=dept, with_olds=with_olds)
|
|
comm = "dept. %s" % dept.encode(scu.SCO_ENCODING) # sco8
|
|
else:
|
|
r = get_user_list(with_olds=with_olds)
|
|
comm = "tous"
|
|
if with_olds:
|
|
comm += ", avec anciens"
|
|
comm = "(" + comm + ")"
|
|
# -- Add some information and links:
|
|
r = []
|
|
for u in users:
|
|
# Can current user modify this user ?
|
|
can_modify = can_handle_passwd(u, allow_admindepts=True)
|
|
|
|
d = u.to_dict()
|
|
r.append(d)
|
|
# Add links
|
|
if with_links and can_modify:
|
|
target = url_for(
|
|
"users.userinfo", scodoc_dept=g.scodoc_dept, user_name=u.user_name
|
|
).encode(
|
|
scu.SCO_ENCODING
|
|
) # sco8
|
|
d["_user_name_target"] = target
|
|
d["_nom_target"] = target
|
|
d["_prenom_target"] = target
|
|
|
|
# Hide passwd modification date (depending on visitor's permission)
|
|
if not can_modify:
|
|
d["date_modif_passwd"] = "(non visible)"
|
|
|
|
title = "Utilisateurs définis dans ScoDoc"
|
|
tab = GenTable(
|
|
rows=r,
|
|
columns_ids=(
|
|
"user_name",
|
|
"nom_fmt",
|
|
"prenom_fmt",
|
|
"email",
|
|
"dept",
|
|
"roles_string",
|
|
"date_expiration",
|
|
"date_modif_passwd",
|
|
"passwd_temp",
|
|
"status_txt",
|
|
),
|
|
titles={
|
|
"user_name": "Login",
|
|
"nom_fmt": "Nom",
|
|
"prenom_fmt": "Prénom",
|
|
"email": "Mail",
|
|
"dept": "Dept.",
|
|
"roles": "Rôles",
|
|
"date_expiration": "Expiration",
|
|
"date_modif_passwd": "Modif. mot de passe",
|
|
"passwd_temp": "Temp.",
|
|
"status_txt": "Etat",
|
|
},
|
|
caption=title,
|
|
page_title="title",
|
|
html_title="""<h2>%d utilisateurs %s</h2>
|
|
<p class="help">Cliquer sur un nom pour changer son mot de passe</p>"""
|
|
% (len(r), comm),
|
|
html_class="table_leftalign list_users",
|
|
html_with_td_classes=True,
|
|
html_sortable=True,
|
|
base_url="%s?all=%s" % (REQUEST.URL0, all),
|
|
pdf_link=False, # table is too wide to fit in a paper page => disable pdf
|
|
preferences=sco_preferences.SemPreferences(context),
|
|
)
|
|
|
|
return tab.make_page(
|
|
context, format=format, with_html_headers=False, REQUEST=REQUEST
|
|
)
|
|
|
|
|
|
def get_user_list(dept=None, with_olds=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_olds:
|
|
q = q.filter_by(is_old=False)
|
|
return q.all()
|
|
|
|
|
|
# def get_user_infos(user_list):
|
|
# return [ user_info(u) for u in user_list ]
|