Middleware to prevent access to the admin when user ip not in INTERNAL_IPS

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
from django.conf import settings
from django.core.urlresolvers import reverse, NoReverseMatch
from django.http import Http404

class InternalUseOnlyMiddleware(object):
    """
    Middleware to prevent access to the admin if the user IP
    isn't in the INTERNAL_IPS setting.
    """
    def process_request(self, request):
        try:
            admin_index = reverse('admin:index')
        except NoReverseMatch:
            return
        if not request.path.startswith(admin_index):
            return
        remote_addr = request.META.get(
            'HTTP_X_REAL_IP', request.META.get('REMOTE_ADDR', None))
        if not remote_addr in settings.INTERNAL_IPS and not settings.DEBUG:
            raise Http404

More like this

  1. Firebug Lite Middleware by jfw 4 years, 7 months ago
  2. another request logging middleware with request time and extra info by yoav 1 year, 5 months ago
  3. Internal view decorator by gsakkis 2 years, 10 months ago
  4. Include entire networks in INTERNAL_IPS setting by pmclanahan 4 years, 2 months ago
  5. Debug SQL Query in Template by dario.agliottone 1 year ago

Comments

diverman (on July 5, 2010):

What about raising "Forbidden" instead of "Not found"? Have you tried access control using Apache directives?

#

blueyonder (on March 29, 2012):

works like a charm, thanks

#

(Forgotten your password?)