Skip to content

Instantly share code, notes, and snippets.

@Diviei
Created April 1, 2015 10:17
Show Gist options
  • Save Diviei/7b2161871953f0c12615 to your computer and use it in GitHub Desktop.
Save Diviei/7b2161871953f0c12615 to your computer and use it in GitHub Desktop.
Hide admin panel for unprivileged users
# -*- coding: utf-8 -*-
from django.http import HttpResponseNotFound
class AdminSiteSecurizeMiddleware(object):
"""Hide admin panel for unprivileged users"""
def process_response(self, request, response):
"""
Return a 404 Not Found page if there is no authenticated user
or if user has no enough privileges
"""
if '/admin/' in request.META.get("PATH_INFO"):
if not request.user.is_authenticated() or not request.user.is_staff:
return HttpResponseNotFound()
return response
@Diviei
Copy link
Author

Diviei commented Apr 1, 2015

I would like to find a better way to identify and admin panel request instead of checking "PATH_INFO" value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment