Skip to content

Commit

Permalink
Add raise_exception arg to @group_required #10658
Browse files Browse the repository at this point in the history
Allows raising a 403 PermissionDenied for API-style
requests, instead of a 302 redirect to nowhere.
  • Loading branch information
jacobtylerwalls committed Mar 6, 2024
1 parent 4863675 commit 3e4a9c2
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion arches/app/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def new_func(*args, **kwargs):
return new_func


def group_required(*group_names):
def group_required(*group_names, raise_exception=False):
"""
Requires user membership in at least one of the groups passed in.
Expand All @@ -69,6 +69,9 @@ def in_groups(u):
if u.is_authenticated:
if u.is_superuser or bool(u.groups.filter(name__in=group_names)):
return True
if raise_exception:
raise PermissionDenied
# else: user_passes_test() redirects to nowhere
return False

return user_passes_test(in_groups)
Expand Down

0 comments on commit 3e4a9c2

Please sign in to comment.