mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-12-27 01:13:46 +00:00
décorateur can_create
This commit is contained in:
parent
09157a6a0d
commit
b22b15eef6
2 changed files with 19 additions and 13 deletions
|
@ -762,17 +762,17 @@ class User(AbstractBaseUser):
|
||||||
num += 1
|
num += 1
|
||||||
return composed_pseudo(num)
|
return composed_pseudo(num)
|
||||||
|
|
||||||
def can_create(user):
|
def can_create(user, perms=('cableur',)):
|
||||||
options, _created = OptionalUser.objects.get_or_create()
|
options, _created = OptionalUser.objects.get_or_create()
|
||||||
if options.all_can_create:
|
if options.all_can_create:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return user.has_perms(('cableur',))
|
return user.has_perms(perms)
|
||||||
|
|
||||||
def can_edit(self, user):
|
def can_edit(self, user):
|
||||||
if self.is_class_club and user.is_class_adherent:
|
if self.is_class_club and user.is_class_adherent:
|
||||||
return self == user or user.has_perms(('cableur',)) or\
|
return self == user or user.has_perms(('cableur',)) or\
|
||||||
user.adherent in self.club.administrators.all()
|
user.adherent in self.club.administrators.all()
|
||||||
else:
|
else:
|
||||||
return self == user or user.has_perms(('cableur',))
|
return self == user or user.has_perms(('cableur',))
|
||||||
|
|
||||||
|
@ -846,7 +846,6 @@ def user_post_delete(sender, **kwargs):
|
||||||
user.ldap_del()
|
user.ldap_del()
|
||||||
regen('mailing')
|
regen('mailing')
|
||||||
|
|
||||||
|
|
||||||
class ServiceUser(AbstractBaseUser):
|
class ServiceUser(AbstractBaseUser):
|
||||||
""" Classe des users daemons, règle leurs accès au ldap"""
|
""" Classe des users daemons, règle leurs accès au ldap"""
|
||||||
readonly = 'readonly'
|
readonly = 'readonly'
|
||||||
|
|
|
@ -63,7 +63,7 @@ from users.models import (
|
||||||
Request,
|
Request,
|
||||||
ServiceUser,
|
ServiceUser,
|
||||||
Adherent,
|
Adherent,
|
||||||
Club
|
Club,
|
||||||
)
|
)
|
||||||
from users.forms import (
|
from users.forms import (
|
||||||
DelRightForm,
|
DelRightForm,
|
||||||
|
@ -109,17 +109,24 @@ def password_change_action(u_form, user, request, req=False):
|
||||||
kwargs={'userid':str(user.id)}
|
kwargs={'userid':str(user.id)}
|
||||||
))
|
))
|
||||||
|
|
||||||
|
def can_create(perms=('cableur',)):
|
||||||
|
"""Décorateur qui vérifie si l'utilisateur peut créer un objet."""
|
||||||
|
def decorator(view):
|
||||||
|
def wrapper(request,*args, **kwargs):
|
||||||
|
if not request.user.can_create(perms=perms):
|
||||||
|
messages.error(request, "Vous ne pouvez pas accéder à ce menu")
|
||||||
|
return redirect(reverse('users:profil',
|
||||||
|
kwargs={'userid':str(request.user.id)}
|
||||||
|
))
|
||||||
|
return view(request, *args, **kwargs)
|
||||||
|
return wrapper
|
||||||
|
return decorator
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
|
@can_create()
|
||||||
def new_user(request):
|
def new_user(request):
|
||||||
""" Vue de création d'un nouvel utilisateur,
|
""" Vue de création d'un nouvel utilisateur,
|
||||||
envoie un mail pour le mot de passe"""
|
envoie un mail pour le mot de passe"""
|
||||||
if not User.can_create(request.user):
|
|
||||||
messages.error(request, "Vous ne pouvez pas accéder à ce menu")
|
|
||||||
return redirect(reverse(
|
|
||||||
'users:profil',
|
|
||||||
kwargs={'userid':str(request.user.id)}
|
|
||||||
))
|
|
||||||
user = AdherentForm(request.POST or None)
|
user = AdherentForm(request.POST or None)
|
||||||
if user.is_valid():
|
if user.is_valid():
|
||||||
user = user.save(commit=False)
|
user = user.save(commit=False)
|
||||||
|
@ -138,7 +145,7 @@ def new_user(request):
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
@permission_required('cableur')
|
@can_create()
|
||||||
def new_club(request):
|
def new_club(request):
|
||||||
""" Vue de création d'un nouveau club,
|
""" Vue de création d'un nouveau club,
|
||||||
envoie un mail pour le mot de passe"""
|
envoie un mail pour le mot de passe"""
|
||||||
|
@ -303,7 +310,7 @@ def password(request, userid):
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
@permission_required('infra')
|
@can_create(('infra',))
|
||||||
def new_serviceuser(request):
|
def new_serviceuser(request):
|
||||||
""" Vue de création d'un nouvel utilisateur service"""
|
""" Vue de création d'un nouvel utilisateur service"""
|
||||||
user = ServiceUserForm(request.POST or None)
|
user = ServiceUserForm(request.POST or None)
|
||||||
|
|
Loading…
Reference in a new issue