2021-06-19 23:21:37 +02:00
|
|
|
# -*- mode: python -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""Functions checking permissions for some common operations
|
|
|
|
"""
|
2024-08-24 18:26:51 +02:00
|
|
|
from flask import g, render_template
|
2021-06-26 21:57:54 +02:00
|
|
|
from flask_login import current_user
|
2021-06-19 23:21:37 +02:00
|
|
|
|
2021-08-22 13:24:36 +02:00
|
|
|
from app.auth.models import User
|
2024-08-24 18:26:51 +02:00
|
|
|
from app.models import EtudAnnotation, FormSemestre, Identite
|
2021-06-19 23:21:37 +02:00
|
|
|
from app.scodoc.sco_permissions import Permission
|
|
|
|
from app.scodoc import sco_exceptions
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2024-08-24 18:26:51 +02:00
|
|
|
annotation = (
|
|
|
|
EtudAnnotation.query.filter_by(id=annotation_id)
|
|
|
|
.join(Identite)
|
|
|
|
.filter_by(dept_id=g.scodoc_dept_id)
|
|
|
|
.first_or_404()
|
|
|
|
)
|
|
|
|
if not annotation:
|
2021-06-19 23:21:37 +02:00
|
|
|
raise sco_exceptions.ScoValueError("annotation inexistante !")
|
2024-08-24 18:26:51 +02:00
|
|
|
return (current_user.user_name == annotation.author) or current_user.has_permission(
|
2023-09-29 21:17:31 +02:00
|
|
|
Permission.EtudAddAnnotations
|
2021-08-10 17:12:10 +02:00
|
|
|
)
|
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" """
|
2023-09-29 21:17:31 +02:00
|
|
|
return current_user.has_permission(Permission.EtudChangeAdr)
|
2021-06-19 23:21:37 +02:00
|
|
|
|
|
|
|
|
2024-10-19 23:52:35 +02:00
|
|
|
def check_access_diretud(formsemestre: FormSemestre):
|
2023-09-29 21:17:31 +02:00
|
|
|
"""Check if access granted: responsable or EditFormSemestre
|
2021-06-19 23:21:37 +02:00
|
|
|
Return True|False, HTML_error_page
|
|
|
|
"""
|
2024-10-19 23:52:35 +02:00
|
|
|
if not formsemestre.can_be_edited_by(current_user):
|
2021-06-19 23:21:37 +02:00
|
|
|
return (
|
|
|
|
False,
|
2024-08-24 18:26:51 +02:00
|
|
|
render_template(
|
|
|
|
"sco_page.j2",
|
|
|
|
title="Accès interdit",
|
|
|
|
content=f"""<h2>Opération non autorisée pour {current_user}</h2>
|
|
|
|
|
|
|
|
<p>Responsable(s) de ce semestre : <b>{
|
|
|
|
', '.join(
|
2023-07-11 06:57:38 +02:00
|
|
|
[
|
2024-08-24 18:26:51 +02:00
|
|
|
u.get_prenomnom()
|
|
|
|
for u in formsemestre.responsables
|
|
|
|
])
|
|
|
|
}</b>
|
|
|
|
</p>
|
|
|
|
""",
|
2021-06-19 23:21:37 +02:00
|
|
|
),
|
|
|
|
)
|
2024-08-24 18:26:51 +02:00
|
|
|
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
|
2023-09-29 21:17:31 +02:00
|
|
|
if not current_user.has_permission(Permission.UsersAdmin, g.scodoc_dept):
|
2021-06-26 21:57:54 +02:00
|
|
|
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
|