2021-06-19 23:21:37 +02:00
|
|
|
# -*- mode: python -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""Functions checking permissions for some common operations
|
|
|
|
"""
|
2021-06-26 21:57:54 +02:00
|
|
|
from flask import g
|
|
|
|
from flask_login import current_user
|
2021-06-19 23:21:37 +02:00
|
|
|
|
2023-07-11 06:57:38 +02:00
|
|
|
from app import db
|
2021-08-22 13:24:36 +02:00
|
|
|
from app.auth.models import User
|
2023-02-01 14:35:25 +01:00
|
|
|
from app.models import FormSemestre
|
2021-06-19 23:21:37 +02:00
|
|
|
import app.scodoc.notesdb as ndb
|
|
|
|
from app.scodoc.sco_permissions import Permission
|
|
|
|
from app.scodoc import html_sco_header
|
|
|
|
from app.scodoc import sco_etud
|
|
|
|
from app.scodoc import sco_exceptions
|
|
|
|
from app.scodoc import sco_moduleimpl
|
|
|
|
|
|
|
|
|
2021-07-31 18:01:10 +02:00
|
|
|
def can_edit_notes(authuser, moduleimpl_id, allow_ens=True):
|
2021-06-19 23:21:37 +02:00
|
|
|
"""True if authuser can enter or edit notes in this module.
|
|
|
|
If allow_ens, grant access to all ens in this module
|
|
|
|
|
|
|
|
Si des décisions de jury ont déjà été saisies dans ce semestre,
|
|
|
|
seul le directeur des études peut saisir des notes (et il ne devrait pas).
|
|
|
|
"""
|
2021-06-21 10:17:16 +02:00
|
|
|
from app.scodoc import sco_formsemestre
|
2022-07-07 16:24:52 +02:00
|
|
|
from app.scodoc import sco_cursus_dut
|
2021-06-21 10:17:16 +02:00
|
|
|
|
2021-10-15 14:00:51 +02:00
|
|
|
M = sco_moduleimpl.moduleimpl_list(moduleimpl_id=moduleimpl_id)[0]
|
2021-08-19 10:28:35 +02:00
|
|
|
sem = sco_formsemestre.get_formsemestre(M["formsemestre_id"])
|
2021-08-10 12:57:38 +02:00
|
|
|
if not sem["etat"]:
|
2021-06-19 23:21:37 +02:00
|
|
|
return False # semestre verrouillé
|
|
|
|
|
2022-07-07 16:24:52 +02:00
|
|
|
if sco_cursus_dut.formsemestre_has_decisions(sem["formsemestre_id"]):
|
2021-06-19 23:21:37 +02:00
|
|
|
# il y a des décisions de jury dans ce semestre !
|
|
|
|
return (
|
|
|
|
authuser.has_permission(Permission.ScoEditAllNotes)
|
2021-08-22 13:24:36 +02:00
|
|
|
or authuser.id in sem["responsables"]
|
2021-06-19 23:21:37 +02:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
if (
|
|
|
|
(not authuser.has_permission(Permission.ScoEditAllNotes))
|
2021-08-22 13:24:36 +02:00
|
|
|
and authuser.id != M["responsable_id"]
|
|
|
|
and authuser.id not in sem["responsables"]
|
2021-06-19 23:21:37 +02:00
|
|
|
):
|
|
|
|
# enseignant (chargé de TD) ?
|
|
|
|
if allow_ens:
|
|
|
|
for ens in M["ens"]:
|
2021-08-22 13:24:36 +02:00
|
|
|
if ens["ens_id"] == authuser.id:
|
2021-06-19 23:21:37 +02:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-07-29 16:31:15 +02:00
|
|
|
def can_suppress_annotation(annotation_id):
|
2021-06-19 23:21:37 +02:00
|
|
|
"""True if current user can suppress this annotation
|
|
|
|
Seuls l'auteur de l'annotation et le chef de dept peuvent supprimer
|
|
|
|
une annotation.
|
|
|
|
"""
|
|
|
|
cnx = ndb.GetDBConnexion()
|
|
|
|
annos = sco_etud.etud_annotations_list(cnx, args={"id": annotation_id})
|
|
|
|
if len(annos) != 1:
|
|
|
|
raise sco_exceptions.ScoValueError("annotation inexistante !")
|
|
|
|
anno = annos[0]
|
2021-08-10 17:12:10 +02:00
|
|
|
return (current_user.user_name == anno["author"]) or current_user.has_permission(
|
|
|
|
Permission.ScoEtudAddAnnotations
|
|
|
|
)
|
2021-06-19 23:21:37 +02:00
|
|
|
|
|
|
|
|
2021-07-29 16:31:15 +02:00
|
|
|
def can_edit_suivi():
|
2021-06-19 23:21:37 +02:00
|
|
|
"""Vrai si l'utilisateur peut modifier les informations de suivi sur la page etud" """
|
2021-07-29 16:31:15 +02:00
|
|
|
return current_user.has_permission(Permission.ScoEtudChangeAdr)
|
2021-06-19 23:21:37 +02:00
|
|
|
|
|
|
|
|
2023-02-12 01:13:43 +01:00
|
|
|
def is_chef_or_diretud(sem): # remplacé par formsemestre.est_chef_or_diretud
|
2021-06-19 23:21:37 +02:00
|
|
|
"Vrai si utilisateur est admin, chef dept ou responsable du semestre"
|
2021-07-29 16:31:15 +02:00
|
|
|
if (
|
|
|
|
current_user.has_permission(Permission.ScoImplement)
|
2021-08-22 13:24:36 +02:00
|
|
|
or current_user.id in sem["responsables"]
|
2021-07-29 16:31:15 +02:00
|
|
|
):
|
2021-06-19 23:21:37 +02:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2021-07-31 18:01:10 +02:00
|
|
|
def check_access_diretud(formsemestre_id, required_permission=Permission.ScoImplement):
|
2021-06-19 23:21:37 +02:00
|
|
|
"""Check if access granted: responsable or ScoImplement
|
|
|
|
Return True|False, HTML_error_page
|
|
|
|
"""
|
2021-06-21 10:17:16 +02:00
|
|
|
from app.scodoc import sco_formsemestre
|
|
|
|
|
2021-08-19 10:28:35 +02:00
|
|
|
sem = sco_formsemestre.get_formsemestre(formsemestre_id)
|
2021-07-29 16:58:18 +02:00
|
|
|
header = html_sco_header.sco_header(page_title="Accès interdit")
|
2021-07-29 10:19:00 +02:00
|
|
|
footer = html_sco_header.sco_footer()
|
2021-08-22 13:24:36 +02:00
|
|
|
if (current_user.id not in sem["responsables"]) and not current_user.has_permission(
|
|
|
|
required_permission
|
|
|
|
):
|
2021-06-19 23:21:37 +02:00
|
|
|
return (
|
|
|
|
False,
|
|
|
|
"\n".join(
|
|
|
|
[
|
|
|
|
header,
|
2021-07-29 16:31:15 +02:00
|
|
|
"<h2>Opération non autorisée pour %s</h2>" % current_user,
|
2021-06-19 23:21:37 +02:00
|
|
|
"<p>Responsable de ce semestre : <b>%s</b></p>"
|
2021-08-22 13:24:36 +02:00
|
|
|
% ", ".join(
|
2023-07-11 06:57:38 +02:00
|
|
|
[
|
|
|
|
db.session.get(User, i).get_prenomnom()
|
|
|
|
for i in sem["responsables"]
|
|
|
|
]
|
2021-08-22 13:24:36 +02:00
|
|
|
),
|
2021-06-19 23:21:37 +02:00
|
|
|
footer,
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return True, ""
|
2021-06-21 10:17:16 +02:00
|
|
|
|
|
|
|
|
2022-08-25 12:47:57 +02:00
|
|
|
def can_handle_passwd(user: User, allow_admindepts=False) -> bool:
|
2021-06-26 21:57:54 +02:00
|
|
|
"""True if the current user can see or change passwd info of user.
|
|
|
|
If allow_admindepts, allow Admin from all depts (so they can view users from other depts
|
|
|
|
and add roles to them).
|
|
|
|
user is a User instance.
|
|
|
|
"""
|
2021-07-01 18:54:07 +02:00
|
|
|
if not user:
|
|
|
|
return False
|
2021-06-26 21:57:54 +02:00
|
|
|
if current_user.is_administrator():
|
|
|
|
return True # super admin
|
|
|
|
# Anyone can change his own passwd (or see his informations)
|
|
|
|
if user.user_name == current_user.user_name:
|
|
|
|
return True
|
|
|
|
# If don't have permission in the current dept, abort
|
|
|
|
if not current_user.has_permission(Permission.ScoUsersAdmin, g.scodoc_dept):
|
|
|
|
return False
|
|
|
|
# Now check that current_user can manage users from this departement
|
|
|
|
if not current_user.dept:
|
|
|
|
return True # if no dept, can access users from all depts !
|
|
|
|
if (current_user.dept == user.dept) or allow_admindepts:
|
|
|
|
return True
|
2022-08-25 12:47:57 +02:00
|
|
|
return False
|