from django.db.models import Q
from django.utils.http import urlquote
-from project.groups import grp_drh, grp_drh2, grp_correspondants_rh
-from project.groups import get_employe_from_user
+from project import groups
def redirect_interdiction(request, msg=u"Vous n'avez pas accès à cette page"):
"""
Teste si un user Django fait parti du groupe DRH, DRH2 ou s'il est admin
"""
- groups = user.groups.all()
+ user_groups = [g.name for g in user.groups.all()]
if user.is_superuser or \
- grp_drh in groups or \
- grp_drh2 in groups:
+ groups.DRH_NIVEAU_1 in user_groups or \
+ groups.DRH_NIVEAU_2 in user_groups:
return True
else:
return False
def wrapped(request, id):
if request.user.is_superuser:
return func(request, id)
- user_groups = request.user.groups.all()
- if grp_drh in user_groups:
+ user_groups = [g.name for g in request.user.groups.all()]
+ if groups.DRH_NIVEAU_1 in user_groups or \
+ groups.DRH_NIVEAU_2 in user_groups:
return func(request, id)
- if grp_correspondants_rh in user_groups:
- employe = get_employe_from_user(request.user)
- q = Q(**{
- model.prefix_implantation: employe.implantation.region
- })
+ if groups.CORRESPONDANT_RH in user_groups or \
+ groups.ADMINISTRATEURS in user_groups or \
+ groups.DIRECTEUR_DE_BUREAU in user_groups:
+ zones = groups.get_zones_from_user(request.user)
+ qkey = '%s__in' % model.prefix_implantation
+ q = Q(**{ qkey: zones })
qs = model.objects.filter(q)
if int(id) in [o.id for o in qs]:
return func(request, id)
return redirect_interdiction(request)
return wrapped
return wrapper
+
+
+def in_one_of_group(groups):
+ """
+ Test si le user appartient au moins 1 des ces groupes
+ """
+ def wrapper(fn):
+ def wrapped(request, *args, **kwargs):
+ user_groups = [g.name for g in request.user.groups.all()]
+ for g in user_groups:
+ if g in groups:
+ return fn(request, *args, **kwargs)
+ msg = u"Votre compte ne permet pas d'accéder à cette partie de l'application."
+ return redirect_interdiction(request, msg)
+ return wrapped
+ return wrapper