1 2 3 4 5 6 7 8 9 10 | from django.contrib.auth.decorators import user_passes_test
def group_required(*group_names):
"""Requires user membership in at least one of the groups passed in."""
def in_groups(u):
if u.is_authenticated():
if bool(u.groups.filter(name__in=group_names)) | u.is_superuser:
return True
return False
return user_passes_test(in_groups)
|
More like this
- Auth decorators with 403 by Magus 5 years, 12 months ago
- is_staff decorator by munhitsu 4 years, 9 months ago
- Check Group Membership Filter by Mogga 4 years, 10 months ago
- HTTP basic auth decorator by bthomas 4 years, 3 months ago
- view by view basic authentication decorator by Scanner 6 years ago
Comments
For using it in urls.py shoud write
#
Avoid a potentially unnecessary database hit by swapping the position of is_superuser and the groups.filter.
#