1 | from mod_python import apache
|
---|
2 | import os
|
---|
3 | from django.core.handlers.base import BaseHandler
|
---|
4 | from django.core.handlers.modpython import ModPythonRequest
|
---|
5 |
|
---|
6 | class AccessHandler(BaseHandler):
|
---|
7 | def __call__(self, req):
|
---|
8 | from django.conf import settings
|
---|
9 | # set up middleware
|
---|
10 | if self._request_middleware is None:
|
---|
11 | self.load_middleware()
|
---|
12 | # populate the request object
|
---|
13 | request = ModPythonRequest(req)
|
---|
14 | # and apply the middleware to it
|
---|
15 | # actually only session and auth middleware would be needed here
|
---|
16 | for middleware_method in self._request_middleware:
|
---|
17 | middleware_method(request)
|
---|
18 | return request
|
---|
19 |
|
---|
20 | def accesshandler(req):
|
---|
21 | os.environ.update(req.subprocess_env)
|
---|
22 |
|
---|
23 | # check for PythonOptions
|
---|
24 | _str_to_bool = lambda s: s.lower() in ('1', 'true', 'on', 'yes')
|
---|
25 |
|
---|
26 | options = req.get_options()
|
---|
27 | permission_name = options.get('DjangoPermissionName', None)
|
---|
28 | staff_only = _str_to_bool(options.get('DjangoRequireStaffStatus', "on"))
|
---|
29 | superuser_only = _str_to_bool(options.get('DjangoRequireSuperuserStatus', "off"))
|
---|
30 | settings_module = options.get('DJANGO_SETTINGS_MODULE', None)
|
---|
31 |
|
---|
32 | if settings_module:
|
---|
33 | os.environ['DJANGO_SETTINGS_MODULE'] = settings_module
|
---|
34 |
|
---|
35 | request=AccessHandler()(req)
|
---|
36 | if request.user.is_authenticated():
|
---|
37 | if superuser_only and request.user.is_superuser:
|
---|
38 | return apache.OK
|
---|
39 | elif staff_only and request.user.is_staff:
|
---|
40 | return apache.OK
|
---|
41 | elif permission_name and request.user.has_perm(permission_name):
|
---|
42 | return apache.OK
|
---|
43 | return apache.HTTP_UNAUTHORIZED
|
---|