1. Chris Vigelius
  2. django-impersonate

Commits

Chris Vigelius  committed 8fe4287 Draft

allow custom redirect target when impersonating

  • Participants
  • Parent commits 3bdbaeb
  • Branches default

Comments (0)

Files changed (5)

File README.txt

View file
  • Ignore whitespace
 and IMPERSONATE_REQUIRE_SUPERUSER apply.
 
 
+IMPERSONATE_REDIRECT_FIELD_NAME
+
+A string that represents the name of a request (GET) parameter which contains
+the URL to redirect to after impersonating a user. This can be used to redirect
+to a custom page after impersonating a user. Example:
+
+    # in settings.py
+    IMPERSONATE_REDIRECT_FIELD_NAME = 'next'
+    
+    # in your view
+    <a href="/impersonate/list/?next=/some/url/">switch user</a>
+
+To return always to the current page after impersonating a user, use 
+
+    <a href="/impersonate/list/?next={{request.path}}">switch user</a>
+
 Copyright & Warranty
 --------------------
 All documentation, libraries, and sample code are 

File impersonate/helpers.py

View file
  • Ignore whitespace
 from django.conf import settings
 from django.contrib.auth.models import User
 from django.core.paginator import Paginator, EmptyPage
+from django.utils.safestring import mark_safe
 
  
-def get_redir_path():        
-    return getattr(
+def get_redir_path(request=None):
+    next = None
+    redirect_field_name = getattr(settings,'IMPERSONATE_REDIRECT_FIELD_NAME', None)
+    if request and redirect_field_name:
+        next = request.GET.get(redirect_field_name, None)
+    return next or getattr(
         settings,
         'IMPERSONATE_REDIRECT_URL',
         getattr(settings, 'LOGIN_REDIRECT_URL', '/'),
     )       
-        
+
+def get_redir_arg(request):
+    redirect_field_name = getattr(settings,'IMPERSONATE_REDIRECT_FIELD_NAME', None)
+    if redirect_field_name:
+        next = request.GET.get(redirect_field_name, None)
+        if next:
+            return '?%s=%s' % (redirect_field_name, next)
+    return '' 
+
+def get_redir_field(request):
+    redirect_field_name = getattr(settings,'IMPERSONATE_REDIRECT_FIELD_NAME', None)
+    if redirect_field_name:
+        next = request.GET.get(redirect_field_name, None)
+        if next:
+            return mark_safe('<input type="hidden" name="%s" value="%s"/>' % (redirect_field_name,next))
+    return '' 
     
 def get_paginator(request, qs):
     try:

File impersonate/templates/impersonate/list_users.html

View file
  • Ignore whitespace
 {% if page.object_list %}
   <ul>
     {% for user in page.object_list %}
-      <li><a href="{% url impersonate-start user.id %}">{{ user.username }}</a> - Impersonate</li>
+      <li><a href="{% url impersonate-start user.id %}{{redirect}}">{{ user.username }}</a> - Impersonate</li>
     {% endfor %}
   </ul>
 {% endif %}

File impersonate/templates/impersonate/search_users.html

View file
  • Ignore whitespace
 <form action="{% url impersonate-search %}" method="GET">
 Enter Search Query:<br />
 <input type="text" name="q" value="{% if query %}{{ query }}{% endif %}"><br />
+{{redirect_field}}
 <input type="submit" value="Search"><br />
 </form>
 {% if query and page.object_list %}
   <ul>
     {% for user in page.object_list %}
-      <li><a href="{% url impersonate-start user.id %}">{{ user.username }}</a> - Impersonate</li>
+      <li><a href="{% url impersonate-start user.id %}{{redirect}}">{{ user.username }}</a> - Impersonate</li>
     {% endfor %}
   </ul>
 {% endif %}

File impersonate/views.py

View file
  • Ignore whitespace
 from django.contrib.auth.models import User
 from django.shortcuts import get_object_or_404, redirect, render_to_response
 from decorators import allowed_user_required
-from impersonate.helpers import get_redir_path, get_paginator, \
-                                check_allow_for_user, users_impersonable
+from impersonate.helpers import get_redir_path, get_redir_arg, get_redir_field,\
+                                get_paginator, check_allow_for_user, users_impersonable
 
 
 @allowed_user_required
     if check_allow_for_user(request, new_user):
         request.session['_impersonate'] = new_user
         request.session.modified = True  # Let's make sure...
-    return redirect(get_redir_path())
+    return redirect(get_redir_path(request))
 
 
 def stop_impersonate(request):
     if '_impersonate' in request.session:
         del request.session['_impersonate']
         request.session.modified = True
-    return redirect(get_redir_path())
+    return redirect(get_redir_path(request))
 
 
 @allowed_user_required
 def list_users(request, template):
     ''' List all users in the system.
-        Will add 4 items to the context.
+        Will add 5 items to the context.
           * users - queryset of all users
           * paginator - Django Paginator instance
           * page - Current page of objects (from Paginator)
           * page_number - Current page number, defaults to 1
+          * redirect - arg for redirect target, e.g. "?next=/foo/bar"
     '''
     users = users_impersonable(request)
 
     paginator, page, page_number = get_paginator(request, users)
-
+    
     return render_to_response(template, {
         'users': users,
         'paginator': paginator,
         'page': page,
         'page_number': page_number,
+        'redirect': get_redir_arg(request),
     }, context_instance=RequestContext(request))
 
 
         'page': page,
         'page_number': page_number,
         'query': query,
+        'redirect': get_redir_arg(request),
+        'redirect_field': get_redir_field(request),
     }, context_instance=RequestContext(request))