2019-06-17 21:20:57 +02:00
|
|
|
import logging
|
2018-11-25 17:42:14 +01:00
|
|
|
from functools import wraps
|
2012-07-11 17:39:20 +02:00
|
|
|
|
2019-06-17 21:20:57 +02:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2019-06-08 15:04:55 +02:00
|
|
|
from django.contrib.auth.mixins import PermissionRequiredMixin
|
2018-11-25 17:42:14 +01:00
|
|
|
from django.shortcuts import render
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2019-06-17 21:20:57 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2012-07-11 17:39:20 +02:00
|
|
|
|
2018-11-25 17:42:14 +01:00
|
|
|
def cof_required(view_func):
|
|
|
|
"""Décorateur qui vérifie que l'utilisateur est connecté et membre du COF.
|
2018-10-06 12:35:49 +02:00
|
|
|
|
2020-08-28 18:00:54 +02:00
|
|
|
- Si l'utilisteur n'est pas connecté, il est redirigé vers la page de
|
|
|
|
connexion
|
|
|
|
- Si l'utilisateur est connecté mais pas membre du COF, il obtient une
|
|
|
|
page d'erreur lui demandant de s'inscrire au COF
|
2018-11-25 17:42:14 +01:00
|
|
|
"""
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2018-11-25 17:42:14 +01:00
|
|
|
def is_cof(user):
|
|
|
|
try:
|
|
|
|
return user.profile.is_cof
|
|
|
|
except AttributeError:
|
|
|
|
return False
|
2012-07-11 17:39:20 +02:00
|
|
|
|
2018-11-25 17:42:14 +01:00
|
|
|
@wraps(view_func)
|
|
|
|
def _wrapped_view(request, *args, **kwargs):
|
|
|
|
if is_cof(request.user):
|
|
|
|
return view_func(request, *args, **kwargs)
|
2012-07-11 17:39:20 +02:00
|
|
|
|
2018-11-25 17:42:14 +01:00
|
|
|
return render(request, "cof-denied.html", status=403)
|
2018-10-06 12:35:49 +02:00
|
|
|
|
2018-11-25 17:42:14 +01:00
|
|
|
return login_required(_wrapped_view)
|
|
|
|
|
|
|
|
|
|
|
|
def buro_required(view_func):
|
|
|
|
"""Décorateur qui vérifie que l'utilisateur est connecté et membre du burô.
|
|
|
|
|
2020-08-28 18:00:54 +02:00
|
|
|
- Si l'utilisateur n'est pas connecté, il est redirigé vers la page de
|
|
|
|
connexion
|
|
|
|
- Si l'utilisateur est connecté mais pas membre du burô, il obtient une
|
|
|
|
page d'erreur 403 Forbidden
|
2018-11-25 17:42:14 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
def is_buro(user):
|
|
|
|
try:
|
|
|
|
return user.profile.is_buro
|
|
|
|
except AttributeError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
@wraps(view_func)
|
|
|
|
def _wrapped_view(request, *args, **kwargs):
|
|
|
|
if is_buro(request.user):
|
|
|
|
return view_func(request, *args, **kwargs)
|
|
|
|
|
|
|
|
return render(request, "buro-denied.html", status=403)
|
|
|
|
|
|
|
|
return login_required(_wrapped_view)
|
2019-06-08 15:04:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CofRequiredMixin(PermissionRequiredMixin):
|
|
|
|
def has_permission(self):
|
2019-06-17 21:38:49 +02:00
|
|
|
if not self.request.user.is_authenticated:
|
|
|
|
return False
|
2019-06-08 15:04:55 +02:00
|
|
|
try:
|
|
|
|
return self.request.user.profile.is_cof
|
|
|
|
except AttributeError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
class BuroRequiredMixin(PermissionRequiredMixin):
|
|
|
|
def has_permission(self):
|
2019-06-17 21:38:49 +02:00
|
|
|
if not self.request.user.is_authenticated:
|
|
|
|
return False
|
2019-06-08 15:04:55 +02:00
|
|
|
try:
|
|
|
|
return self.request.user.profile.is_buro
|
|
|
|
except AttributeError:
|
2019-06-17 21:20:57 +02:00
|
|
|
logger.error(
|
|
|
|
"L'utilisateur %s n'a pas de profil !", self.request.user.username
|
|
|
|
)
|
2019-06-08 15:04:55 +02:00
|
|
|
return False
|