CLI: user-delete: renforce vérifications

This commit is contained in:
ilona 2024-10-18 23:12:32 +02:00
parent 3b984ea823
commit 2666d82351

View File

@ -14,7 +14,8 @@ from flask.cli import with_appcontext
from flask.templating import render_template from flask.templating import render_template
from flask_login import login_user, logout_user, current_user from flask_login import login_user, logout_user, current_user
import psycopg2 import psycopg2
import sqlalchemy import sqlalchemy as sa
import app as mapp import app as mapp
from app import create_app, cli, db from app import create_app, cli, db
@ -40,6 +41,8 @@ from app.models.but_refcomp import (
) )
from app.models.but_validations import ApcValidationAnnee, ApcValidationRCUE from app.models.but_validations import ApcValidationAnnee, ApcValidationRCUE
from app.models.evaluations import Evaluation from app.models.evaluations import Evaluation
from app.models.formsemestre import notes_formsemestre_responsables
from app.models.moduleimpls import notes_modules_enseignants
from app.scodoc import sco_dump_db from app.scodoc import sco_dump_db
from app.scodoc.sco_logos import make_logo_local from app.scodoc.sco_logos import make_logo_local
from app.scodoc.sco_permissions import Permission from app.scodoc.sco_permissions import Permission
@ -214,10 +217,36 @@ def user_delete(username): # user-delete
if not u: if not u:
sys.stderr.write(f"user_delete: user {username} not found\n") sys.stderr.write(f"user_delete: user {username} not found\n")
return 2 return 2
# Vérifie que l'utilisateur n'est pas... utilisé
# - Resp. FormSemestre (table assoc)
query = sa.select(sa.func.count()).where(
notes_formsemestre_responsables.c.responsable_id == u.id
)
nb = db.session.execute(query).scalar()
if nb > 0:
sys.stderr.write(
"\nuser_delete: erreur: utilisateur déclaré comme resp. de semestre\n"
)
return 1
# - Resp. Module
if ModuleImpl.query.filter_by(responsable_id=217).count() > 0:
sys.stderr.write(
"\nuser_delete: erreur: utilisateur déclaré comme enseignant\n"
)
return 1
# - Enseignant d'un module (table assoc)
query = sa.select(sa.func.count()).where(notes_modules_enseignants.c.ens_id == u.id)
nb = db.session.execute(query).scalar()
if nb > 0:
sys.stderr.write(
"\nuser_delete: erreur: utilisateur déclaré comme enseignant\n"
)
return 1
#
db.session.delete(u) db.session.delete(u)
try: try:
db.session.commit() db.session.commit()
except (sqlalchemy.exc.IntegrityError, psycopg2.errors.ForeignKeyViolation): except (sa.exc.IntegrityError, psycopg2.errors.ForeignKeyViolation):
sys.stderr.write( sys.stderr.write(
f"""\nuser_delete: ne peux pas supprimer l'utilisateur {username}\ncar il est associé à des objets dans ScoDoc (modules, notes, ...).\n""" f"""\nuser_delete: ne peux pas supprimer l'utilisateur {username}\ncar il est associé à des objets dans ScoDoc (modules, notes, ...).\n"""
) )