Customization of PermissionDenied
Koldar opened this issue · comments
Hi,
I wonder if there is a way to customize the error PermissionDenied
(specifically its error message), thrown whenever the autheticated user has invalid permissions to gain access to the given function. Currently I am using hte permission_required
decorator.
Just for completeness sake, what I am trying to achieve is to add to the error message the list of missing permissions the user do not have.
Thanks
Hey @Koldar,
You can write auth decorators with just a few lines:
def permission_required(perm):
def check_perms(user):
if isinstance(perm, str):
perms = (perm,)
else:
perms = perm
return user.has_perms(perms)
return user_passes_test(check_perms, exc=Exception("you shall not pass!"))
..and easier for @login_required
:
login_required = user_passes_test(
lambda u: u.is_authenticated,
exc=Exception("you shall not pass!"),
)