Commits

Adam Gomaa committed 3f96264 Draft

initial and possibly final commit

Comments (0)

Files changed (3)

+===============================
+django-vicar - live vicariously
+===============================
+
+django-vicar is a middleware class and a view for allowing superusers
+to impersonate other users. It's not even right to call this thing an
+app, it's one file and <100 lines.
+
+This is an itch-scratch thing, I'm tired of going to /stop/ with
+django-impersonate, and I have no interest in making the functionality
+I want work with it.
+
+Deliberately excluding:
+
+ * any kind of search or listing
+ * any authz beyond superuser
+ * putting a User object in session
+ * any kind of filtering in the middleware beyond the session data
+
+There is no urlconf. There is only one view. Integrate like so:
+
+    from django_vicar import vicar
+    # ...
+        url(r'^vicar/(.*)$', vicar),
+    # ...
+
+And by putting 'django_vicar.VicarMiddleware' somewhere in your
+MIDDLEWARE_CLASSES. It probably needs to go either before or after
+session middleware and/or authentication middleware, but I didn't
+really check, just put it wherever you'd put ImpersonateMiddleware.
+
+
+Similar Projects
+================
+
+django-impersonate
+  https://bitbucket.org/petersanchez/django-impersonate/

django_vicar/__init__.py

+from django.http import HttpResponse
+from django.contrib.auth.models import User
+
+
+
+class VicarMiddleware(object):
+    def process_request(self, request):
+        if request.user.is_authenticated() and '_vicar_user' in request.session:
+            orig_user = User.objects.get(id=request.session['_orig_user'])
+            assert orig_user.is_superuser
+            vicar_user = User.objects.get(id=request.session['_vicar_user'])
+            request.user = vicar_user
+
+
+def vicar(request, ident):
+    if request.session.get('_orig_user'):
+        orig_user = User.objects.get(id=request.session['_orig_user'])
+    else:
+        orig_user = None
+
+    if request.session.get('_vicar_user'):
+        vicar_user = User.objects.get(id=request.session['_vicar_user'])
+    else:
+        vicar_user = None
+
+    if orig_user is not None:
+        assert orig_user.is_superuser
+    else:
+        assert request.user.is_superuser
+
+    # /vicar/0 to go back to original user"
+    if ident == '0':
+        del request.session['_vicar_user']
+        del request.session['_orig_user']
+        return HttpResponse("suddenly you're boring again...")
+
+    # /vicar/uken
+    if ident.startswith('u'):
+        wannabe = User.objects.get(username=ident[1:])
+
+    # /vicar/1
+    else:
+        wannabe = User.objects.get(id=int(ident))
+
+    if orig_user is None:
+        request.session['_orig_user'] = request.user.id
+    request.session['_vicar_user'] = wannabe.id
+
+    return HttpResponse("you're a poseur. it's french for poser.")
+from setuptools import setup
+
+setup(
+    name='django-vicar',
+    version='0.0.1',
+    packages=['django_vicar'],
+    install_requires=[],
+    description="live vicariously",
+)