Login

Log username in Apache access logs

Author:
arthur
Posted:
March 2, 2010
Language:
Python
Version:
1.1
Score:
1 (after 1 ratings)

This is a piece of middleware that reports the logged-in user back to Apache. This should cause the logged-in user to be present in the apache access log.

Put it in settings.MIDDLEWARE_CLASSES after AuthenticationMiddleware.

This has been tested with mod_python but does not work with wsgi.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class LogRemoteUserMiddleware(object):
    """
    Middleware for logging the session user in Apache. This sets the username
    information so that Apache is aware of the logged in user and can log
    the username in the access_log (just like when the user is logged in
    with basic authentication).
    """

    def process_request(self, request):
        # AuthenticationMiddleware is required so that request.user exists
        if not hasattr(request, 'user'):
            raise ImproperlyConfigured(
                "LogRemoteUserMiddleware requires AuthenticationMiddleware"
                " to be in MIDDLEWARE_CLASSES before LogRemoteUserMiddleware.")
        # set the REMOTE_USER variable if a user is logged in
        if request.user.is_authenticated():
            # specific for mod_python
            if isinstance(request, ModPythonRequest):
                request._req.user = str(request.user.username)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 6 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 6 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 1 month ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 1 month ago
  5. Help text hyperlinks by sa2812 1 year, 2 months ago

Comments

sjaensch (on March 2, 2010):

Unfortunately, this does not work with mod_wsgi. Any hints on how to get it working?

#

Please login first before commenting.