1. dhan
  2. django-impersonate

Commits

dhan  committed 71c24f9

Added ability to exclude impersonation for specific urls

  • Participants
  • Parent commits 7de5965
  • Branches default

Comments (0)

Files changed (2)

File impersonate/helpers.py

View file
  • Ignore whitespace
         return True
 
     return False
+
+def check_allow_for_uri(uri):
+    import re
+
+    uri = uri.lstrip('/')
+
+    exclusions = getattr(settings, 'IMPERSONATE_URI_EXCLUSIONS', (r'^admin/',))
+    if not isinstance(exclusions, (list, tuple)):
+        exclusions = (exclusions,)
+
+    for exclusion in exclusions:
+        if re.search(exclusion, uri):
+            return False
+
+    return True

File impersonate/middleware.py

View file
  • Ignore whitespace
 from django.conf import settings
-from helpers import check_allow_for_user
+from helpers import check_allow_for_user, check_allow_for_uri
 
 
 class ImpersonateMiddleware(object):
         if request.user.is_authenticated() and \
            '_impersonate' in request.session:
             new_user = request.session['_impersonate']
-            if check_allow_for_user(request.user, new_user):
+            if check_allow_for_user(request.user, new_user) and check_allow_for_uri(request.path):
                 request.user = new_user
                 request.user.is_impersonate = True