8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-26 22:52:26 +00:00

Décorateur d'accès à une application.

This commit is contained in:
LEVY-FALK Hugo 2017-12-28 13:45:24 +01:00 committed by root
parent 3cff068365
commit 7245354959

View file

@ -69,7 +69,7 @@ def can_create(model):
return decorator return decorator
def can_edit(model): def can_edit(model, *field_list):
"""Decorator to check if an user can edit a model. """Decorator to check if an user can edit a model.
It tries to get an instance of the model, using It tries to get an instance of the model, using
`model.get_instance(*args, **kwargs)` and assumes that the model has a `model.get_instance(*args, **kwargs)` and assumes that the model has a
@ -91,12 +91,20 @@ def can_edit(model):
return redirect(reverse('users:profil', return redirect(reverse('users:profil',
kwargs={'userid':str(request.user.id)} kwargs={'userid':str(request.user.id)}
)) ))
for field in field_list:
can_create = getattr(model, 'can_change_' + field)
can, msg = can_create(instance, request.user, *args, **kwargs)
if not can:
messages.error(request, msg or "Vous ne pouvez pas accéder à ce menu")
return redirect(reverse('users:profil',
kwargs={'userid':str(request.user.id)}
))
return view(request, instance, *args, **kwargs) return view(request, instance, *args, **kwargs)
return wrapper return wrapper
return decorator return decorator
def can_change(model, field_list): def can_change(model, *field_list):
"""Decorator to check if an user can edit a field of a model. """Decorator to check if an user can edit a field of a model.
It assumes that a valid user exists in the request and that the model has a It assumes that a valid user exists in the request and that the model has a
method can_create(user) which returns true if the user can create this kind method can_create(user) which returns true if the user can create this kind
@ -106,7 +114,7 @@ def can_change(model, field_list):
def wrapper(request, *args, **kwargs): def wrapper(request, *args, **kwargs):
for field in field_list: for field in field_list:
can_create = getattr(model, 'can_change_' + field) can_create = getattr(model, 'can_change_' + field)
can, msg = can_create(request.user, *args, **kwargs) can, msg = can_create(None, request.user, *args, **kwargs)
if not can: if not can:
messages.error(request, msg or "Vous ne pouvez pas accéder à ce menu") messages.error(request, msg or "Vous ne pouvez pas accéder à ce menu")
return redirect(reverse('users:profil', return redirect(reverse('users:profil',
@ -209,6 +217,35 @@ def can_view_all(model):
return decorator return decorator
APP_VIEWING_RIGHT = {
'cotisations' : 'cableur',
'logs' : 'cableur',
'machines' : 'cableur',
'preferences' : 'cableur',
'search' : 'cableur',
'topologie' : 'cableur',
'users' : 'cableur',
}
def can_view_app(app_name):
"""Decorator to check if an user can view an application.
"""
assert app_name in APP_VIEWING_RIGHT.keys()
def decorator(view):
def wrapper(request, *args, **kwargs):
if request.user.has_perms((APP_VIEWING_RIGHT[app_name],)):
return view(request, *args, **kwargs)
messages.error(
request,
msg or "Vous ne pouvez pas accéder à l'application " + app_name
)
return redirect(reverse('users:profil',
kwargs={'userid':str(request.user.id)}
))
return wrapper
return decorator
def all_adherent(search_time=DT_NOW): def all_adherent(search_time=DT_NOW):
""" Fonction renvoyant tous les users adherents. Optimisee pour n'est """ Fonction renvoyant tous les users adherents. Optimisee pour n'est
qu'une seule requete sql qu'une seule requete sql