forked from ScoDoc/ScoDoc
140 lines
5.0 KiB
Python
140 lines
5.0 KiB
Python
|
# -*- mode: python -*-
|
||
|
# -*- coding: utf-8 -*-
|
||
|
|
||
|
"""Functions checking permissions for some common operations
|
||
|
"""
|
||
|
|
||
|
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_formsemestre
|
||
|
from app.scodoc import sco_moduleimpl
|
||
|
from app.scodoc import sco_parcours_dut
|
||
|
|
||
|
|
||
|
def can_edit_notes(context, authuser, moduleimpl_id, allow_ens=True):
|
||
|
"""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).
|
||
|
"""
|
||
|
uid = str(authuser)
|
||
|
M = sco_moduleimpl.do_moduleimpl_list(context, moduleimpl_id=moduleimpl_id)[0]
|
||
|
sem = sco_formsemestre.get_formsemestre(context, M["formsemestre_id"])
|
||
|
if sem["etat"] != "1":
|
||
|
return False # semestre verrouillé
|
||
|
|
||
|
if sco_parcours_dut.formsemestre_has_decisions(context, sem["formsemestre_id"]):
|
||
|
# il y a des décisions de jury dans ce semestre !
|
||
|
return (
|
||
|
authuser.has_permission(Permission.ScoEditAllNotes)
|
||
|
or uid in sem["responsables"]
|
||
|
)
|
||
|
else:
|
||
|
if (
|
||
|
(not authuser.has_permission(Permission.ScoEditAllNotes))
|
||
|
and uid != M["responsable_id"]
|
||
|
and uid not in sem["responsables"]
|
||
|
):
|
||
|
# enseignant (chargé de TD) ?
|
||
|
if allow_ens:
|
||
|
for ens in M["ens"]:
|
||
|
if ens["ens_id"] == uid:
|
||
|
return True
|
||
|
return False
|
||
|
else:
|
||
|
return True
|
||
|
|
||
|
|
||
|
def can_suppress_annotation(context, annotation_id, REQUEST):
|
||
|
"""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]
|
||
|
authuser = REQUEST.AUTHENTICATED_USER
|
||
|
# note: les anciennes installations n'ont pas le role ScoEtudSupprAnnotations
|
||
|
# c'est pourquoi on teste aussi ScoEtudInscrit (normalement détenue par le chef)
|
||
|
return (
|
||
|
(str(authuser) == anno["zope_authenticated_user"])
|
||
|
or authuser.has_permission(Permission.ScoEtudSupprAnnotations)
|
||
|
or authuser.has_permission(Permission.ScoEtudInscrit)
|
||
|
)
|
||
|
|
||
|
|
||
|
def can_edit_suivi(context, REQUEST=None):
|
||
|
"""Vrai si l'utilisateur peut modifier les informations de suivi sur la page etud" """
|
||
|
authuser = REQUEST.AUTHENTICATED_USER
|
||
|
return authuser.has_permission(Permission.ScoEtudChangeAdr)
|
||
|
|
||
|
|
||
|
def can_validate_sem(context, REQUEST, formsemestre_id):
|
||
|
"Vrai si utilisateur peut saisir decision de jury dans ce semestre"
|
||
|
sem = sco_formsemestre.get_formsemestre(context, formsemestre_id)
|
||
|
if sem["etat"] != "1":
|
||
|
return False # semestre verrouillé
|
||
|
|
||
|
return is_chef_or_diretud(context, REQUEST, sem)
|
||
|
|
||
|
|
||
|
def can_edit_pv(context, REQUEST, formsemestre_id):
|
||
|
"Vrai si utilisateur peut editer un PV de jury de ce semestre"
|
||
|
|
||
|
sem = sco_formsemestre.get_formsemestre(context, formsemestre_id)
|
||
|
if is_chef_or_diretud(context, REQUEST, sem):
|
||
|
return True
|
||
|
# Autorise les secrétariats, repérés via la permission ScoEtudChangeAdr
|
||
|
# (ceci nous évite d'ajouter une permission Zope aux installations existantes)
|
||
|
authuser = REQUEST.AUTHENTICATED_USER
|
||
|
return authuser.has_permission(Permission.ScoEtudChangeAdr)
|
||
|
|
||
|
|
||
|
def is_chef_or_diretud(context, REQUEST, sem):
|
||
|
"Vrai si utilisateur est admin, chef dept ou responsable du semestre"
|
||
|
authuser = REQUEST.AUTHENTICATED_USER
|
||
|
if authuser.has_permission(Permission.ScoImplement):
|
||
|
return True # admin, chef dept
|
||
|
uid = str(authuser)
|
||
|
if uid in sem["responsables"]:
|
||
|
return True
|
||
|
|
||
|
return False
|
||
|
|
||
|
|
||
|
def check_access_diretud(
|
||
|
context, formsemestre_id, REQUEST, required_permission=Permission.ScoImplement
|
||
|
):
|
||
|
"""Check if access granted: responsable or ScoImplement
|
||
|
Return True|False, HTML_error_page
|
||
|
"""
|
||
|
authuser = REQUEST.AUTHENTICATED_USER
|
||
|
sem = sco_formsemestre.get_formsemestre(context, formsemestre_id)
|
||
|
header = html_sco_header.sco_header(
|
||
|
context, page_title="Accès interdit", REQUEST=REQUEST
|
||
|
)
|
||
|
footer = html_sco_header.sco_footer(context, REQUEST)
|
||
|
if (str(authuser) not in sem["responsables"]) and not authuser.has_permission(
|
||
|
required_permission
|
||
|
):
|
||
|
return (
|
||
|
False,
|
||
|
"\n".join(
|
||
|
[
|
||
|
header,
|
||
|
"<h2>Opération non autorisée pour %s</h2>" % authuser,
|
||
|
"<p>Responsable de ce semestre : <b>%s</b></p>"
|
||
|
% ", ".join(sem["responsables"]),
|
||
|
footer,
|
||
|
]
|
||
|
),
|
||
|
)
|
||
|
else:
|
||
|
return True, ""
|