8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-23 03:43:12 +00:00

Ajoute un message d'erreur si can_xxx renvoie False

This commit is contained in:
Maël Kervella 2017-11-30 19:02:15 +00:00 committed by root
parent db9de53a8c
commit edfe06791c
2 changed files with 36 additions and 17 deletions

View file

@ -58,8 +58,9 @@ def can_create(model):
""" """
def decorator(view): def decorator(view):
def wrapper(request,*args, **kwargs): def wrapper(request,*args, **kwargs):
if not model.can_create(request.user): can, msg = model.can_create(request.user)
messages.error(request, "Vous ne pouvez pas accéder à ce menu") if not can:
messages.error(request, msg or "Vous ne pouvez pas accéder à ce menu")
return redirect(reverse('users:profil', return redirect(reverse('users:profil',
kwargs={'userid':str(request.user.id)} kwargs={'userid':str(request.user.id)}
)) ))
@ -84,8 +85,9 @@ 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)}
)) ))
if not model.can_edit(instance, request.user): can, msg = model.can_edit(instance, request.user)
messages.error(request, "Vous ne pouvez pas accéder à ce menu") if not can:
messages.error(request, msg or "Vous ne pouvez pas accéder à ce menu")
return redirect(reverse('users:profil', return redirect(reverse('users:profil',
kwargs={'userid':str(request.user.id)} kwargs={'userid':str(request.user.id)}
)) ))

View file

@ -765,24 +765,37 @@ class User(AbstractBaseUser):
def can_create(user): def can_create(user):
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, None
else: else:
return user.has_perms(('cableur',)) return user.has_perms(('cableur',)), u"Vous n'avez pas le\
droit de créer un utilisateur"
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\ if self == user or user.has_perms(('cableur',)) or\
user.adherent in self.club.administrators.all() user.adherent in self.club.administrators.all():
return True, None
else:
return False, u"Vous n'avez pas le droit d'éditer ce club"
else: else:
return self == user or user.has_perms(('cableur',)) if self == user or user.has_perms(('cableur',)):
return True, None
else:
return False, u"Vous ne pouvez éditer un autre utilisateur que vous même"
def can_view(self, user): def can_view(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\ if self == user or user.has_perms(('cableur',)) or\
user.adherent in self.club.administrators.all() or\ user.adherent in self.club.administrators.all() or\
user.adherent in self.club.members.all() user.adherent in self.club.members.all():
return True, None
else:
return False, u"Vous n'avez pas le droit de voir ce club"
else: else:
return self == user or user.has_perms(('cableur',)) if self == user or user.has_perms(('cableur',)):
return True, None
else:
return False, u"Vous ne pouvez voir un autre utilisateur que vous même"
def get_instance(userid): def get_instance(userid):
return User.objects.get(pk=userid) return User.objects.get(pk=userid)
@ -917,12 +930,14 @@ class ServiceUser(AbstractBaseUser):
def can_create(user): def can_create(user):
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, None
else: else:
return user.has_perms(('infra',)) return user.has_perms(('infra',)), u"Vous n'avez pas le droit de\
créer un service user"
def can_edit(instance, user): def can_edit(instance, user):
return user.has_perms(('infra',)) return user.has_perms(('infra',)), u"Vous n'avez pas le droit d'éditer\
les services users"
def get_instance(userid): def get_instance(userid):
return ServiceUser.objects.get(pk=userid) return ServiceUser.objects.get(pk=userid)
@ -957,7 +972,8 @@ class Right(models.Model):
return str(self.user) return str(self.user)
def can_create(user): def can_create(user):
return user.has_perms('bureau') return user.has_perms('bureau'), u"Vous n'avez pas le droit de\
créer des droits"
@receiver(post_save, sender=Right) @receiver(post_save, sender=Right)
@ -1104,7 +1120,8 @@ class Ban(models.Model):
return str(self.user) + ' ' + str(self.raison) return str(self.user) + ' ' + str(self.raison)
def can_create(user): def can_create(user):
return user.has_perms(('bofh',)) return user.has_perms(('bofh',)), u"Vous n'avez pas le droit de\
créer des bannissement"
@receiver(post_save, sender=Ban) @receiver(post_save, sender=Ban)