Skip to content

Commit

Permalink
Optimize localadmin checks
Browse files Browse the repository at this point in the history
- Get rid of needless queries
  • Loading branch information
pehala committed Dec 22, 2024
1 parent b05562b commit 179ac77
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion backend/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ def is_superadmin(request):
return request.user.is_authenticated and request.user.is_superuser


# def is_localadmin(request):
# """Returns True, if the current user is Tenant-level administrator"""
# user = request.user
# return user.is_authenticated and request.tenant.admins.filter(id=user.id).exists() or is_superadmin(request)


def is_localadmin(request):
"""Returns True, if the current user is Tenant-level administrator"""
user = request.user
return user.is_authenticated and request.tenant.admins.filter(id=user.id).exists() or is_superadmin(request)
return is_superadmin(request) or user.is_localadmin


def is_authenticated(request):
Expand Down
5 changes: 5 additions & 0 deletions tenants/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def __call__(self, request):
if not query_set.exists():
raise Http404(f"Unable to find Tenant for hostname {hostname}")
request.tenant = query_set.first()
request.user.is_localadmin = (
request.user.is_authenticated
and request.tenant.admins.filter(id=request.user.id).exists()
or request.user.is_superuser
)
except Http404 as exception:
if settings.DEBUG:
request.tenant = Tenant.objects.first()
Expand Down

0 comments on commit 179ac77

Please sign in to comment.