Added new permission denied middleware
This commit is contained in:
12
apps/common/templates/403.html
Normal file
12
apps/common/templates/403.html
Normal file
@@ -0,0 +1,12 @@
|
||||
{% extends "base.html" %}
|
||||
{% load i18n %}
|
||||
{% block title %} :: {% blocktrans %}Insufficient permissions{% endblocktrans %}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="content">
|
||||
<h2 class="title">{% blocktrans %}Insufficient permissions{% endblocktrans %}</h2>
|
||||
<div class="inner">
|
||||
<p>{% blocktrans %}You don't have enough permissions for this operation.{% endblocktrans %}</p>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -1,10 +1,10 @@
|
||||
{% extends "base.html" %}
|
||||
{% load i18n %}
|
||||
{% block title %}{% trans "Page not found" %}{% endblock %}
|
||||
{% block title %} :: {% blocktrans %}Page not found{% endblocktrans %}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="content">
|
||||
<h2 class="title">{% blocktrans %}Page not found or insufficient permissions.{% endblocktrans %}</h2>
|
||||
<h2 class="title">{% blocktrans %}Page not found{% endblocktrans %}</h2>
|
||||
<div class="inner">
|
||||
<p>{% blocktrans %}Sorry, but the requested page could not be found.{% endblocktrans %}</p>
|
||||
</div>
|
||||
|
||||
0
apps/permissions/middleware/__init__.py
Normal file
0
apps/permissions/middleware/__init__.py
Normal file
43
apps/permissions/middleware/permissiondeniedmiddleware.py
Normal file
43
apps/permissions/middleware/permissiondeniedmiddleware.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.http import HttpResponseForbidden
|
||||
from django.template import RequestContext, Template, loader, TemplateDoesNotExist
|
||||
from django.utils.importlib import import_module
|
||||
|
||||
#http://mitchfournier.com/2010/07/12/show-a-custom-403-forbidden-error-page-in-django/
|
||||
class PermissionDeniedMiddleware(object):
|
||||
def process_exception(self, request, exception):
|
||||
if isinstance(exception, PermissionDenied):
|
||||
if settings.DEBUG==123:
|
||||
raise PermissionDenied
|
||||
else:
|
||||
try:
|
||||
# Handle import error but allow any type error from view
|
||||
callback = getattr(import_module(settings.ROOT_URLCONF),'handler403')
|
||||
return callback(request,exception)
|
||||
except (ImportError,AttributeError):
|
||||
# Try to get a 403 template
|
||||
try:
|
||||
# First look for a user-defined template named "403.html"
|
||||
t = loader.get_template('403.html')
|
||||
except TemplateDoesNotExist:
|
||||
# If a template doesn't exist in the projct, use the following hardcoded template
|
||||
t = Template("""{% load i18n %}
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
|
||||
"http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>{% trans "403 ERROR: Access denied" %}</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>{% trans "Access Denied (403)" %}</h1>
|
||||
{% trans "We're sorry, but you are not authorized to view this page." %}
|
||||
</body>
|
||||
</html>""")
|
||||
|
||||
# Now use context and render template
|
||||
c = RequestContext(request, {
|
||||
'message': exception.message
|
||||
})
|
||||
|
||||
return HttpResponseForbidden(t.render(c))
|
||||
@@ -1,7 +1,8 @@
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.template import TemplateSyntaxError, Library, \
|
||||
VariableDoesNotExist, Node, Variable
|
||||
|
||||
from permissions.api import check_permissions as check_permission_function, Unauthorized
|
||||
from permissions.api import check_permissions as check_permission_function
|
||||
|
||||
register = Library()
|
||||
|
||||
@@ -20,7 +21,7 @@ class CheckPermissionsNode(Node):
|
||||
check_permission_function(requester, namespace, permission_list)
|
||||
context['permission'] = True
|
||||
return ''
|
||||
except Unauthorized:
|
||||
except PermissionDenied:
|
||||
context['permission'] = False
|
||||
return ''
|
||||
|
||||
|
||||
@@ -98,6 +98,7 @@ MIDDLEWARE_CLASSES = (
|
||||
'common.middleware.login_required_middleware.LoginRequiredMiddleware',
|
||||
'pagination.middleware.PaginationMiddleware',
|
||||
'common.middleware.strip_spaces_widdleware.SpacelessMiddleware',
|
||||
'permissions.middleware.permissiondeniedmiddleware.PermissionDeniedMiddleware',
|
||||
)
|
||||
|
||||
ROOT_URLCONF = 'urls'
|
||||
|
||||
Reference in New Issue
Block a user