| 9 | _str_to_bool = lambda s: s.lower() in ('1', 'true', 'on', 'yes') |
| 10 | |
| 11 | class ModPythonAuthOptions: |
| 12 | def __init__(self, req): |
| 13 | options = req.get_options() |
| 14 | self.permission_name = options.get('DjangoPermissionName', None) |
| 15 | self.staff_only = _str_to_bool(options.get('DjangoRequireStaffStatus', "on")) |
| 16 | self.superuser_only = _str_to_bool(options.get('DjangoRequireSuperuserStatus', "off")) |
| 17 | self.settings_module = options.get('DJANGO_SETTINGS_MODULE', None) |
| 18 | |
| 19 | def setup_environment(req, options): |
| 20 | """ |
| 21 | mod_python fakes the environ, and thus doesn't process SetEnv. This ensures |
| 22 | any future imports relying on settings will work. |
| 23 | """ |
| 24 | os.environ.update(req.subprocess_env) |
| 25 | if options.settings_module: |
| 26 | os.environ['DJANGO_SETTINGS_MODULE'] = options.settings_module |
| 27 | |
| 28 | def validate_user(user, options): |
| 29 | if not user: |
| 30 | return False |
| 31 | # Don't require an is_authenticated property, but if it's there then check it |
| 32 | if hasattr(user, 'is_authenticated') and not user.is_authenticated(): |
| 33 | return False |
| 34 | # Don't require an is_active property, but if it's there then check it |
| 35 | if hasattr(user, 'is_active') and not user.is_active: |
| 36 | return False |
| 37 | if options.staff_only and not getattr(user, 'is_staff', None): |
| 38 | return False |
| 39 | if options.superuser_only and not getattr(user, 'is_superuser', None): |
| 40 | return False |
| 41 | # If a permission is required then user must have a has_perm function to validate |
| 42 | if options.permission_name and (not hasattr(user, 'has_perm') or not user.has_perm(self.permission_name)): |
| 43 | return False |
| 44 | return True |
| 45 | |
16 | | options = req.get_options() |
17 | | permission_name = options.get('DjangoPermissionName', None) |
18 | | staff_only = _str_to_bool(options.get('DjangoRequireStaffStatus', "on")) |
19 | | superuser_only = _str_to_bool(options.get('DjangoRequireSuperuserStatus', "off")) |
20 | | settings_module = options.get('DJANGO_SETTINGS_MODULE', None) |
21 | | if settings_module: |
22 | | os.environ['DJANGO_SETTINGS_MODULE'] = settings_module |
| 62 | # Raise unauthorized if the user wasn't authenticated to bring up |
| 63 | # a password dialog box to allow the user to authenticate. |
| 64 | if not user: |
| 65 | return apache.HTTP_UNAUTHORIZED |
28 | | # check that the username is valid |
29 | | kwargs = {'username': req.user, 'is_active': True} |
30 | | if staff_only: |
31 | | kwargs['is_staff'] = True |
32 | | if superuser_only: |
33 | | kwargs['is_superuser'] = True |
| 77 | def accesshandler(req): |
| 78 | """ |
| 79 | mod_python access handler that uses the contrib.auth framework (with |
| 80 | sessions and therefore requiring a session cookie). |
| 81 | """ |
| 82 | options = ModPythonAuthOptions(req) |
| 83 | setup_environment(req, options) |
| 84 | |
| 85 | # Set up middleware, now that settings works we can do it now. |
| 86 | base_handler = BaseHandler() |
| 87 | base_handler.load_middleware() |
| 88 | |
| 89 | dispatcher.send(signal=signals.request_started) |
35 | | try: |
36 | | user = User.objects.get(**kwargs) |
37 | | except User.DoesNotExist: |
38 | | return apache.HTTP_UNAUTHORIZED |
39 | | |
40 | | # check the password and any permission given |
41 | | if user.check_password(req.get_basic_auth_pw()): |
42 | | if permission_name: |
43 | | if user.has_perm(permission_name): |
44 | | return apache.OK |
45 | | else: |
46 | | return apache.HTTP_UNAUTHORIZED |
47 | | else: |
48 | | return apache.OK |
| 91 | request = ModPythonRequest(req) |
| 92 | |
| 93 | # Apply request middleware |
| 94 | for middleware_method in base_handler._request_middleware: |
| 95 | response = middleware_method(request) |
| 96 | if response: |
| 97 | # If we get a response, we should probably stop processing any |
| 98 | # remaining request middleware. |
| 99 | break |
| 100 | |
| 101 | # Validate the user |
| 102 | user = getattr(request, 'user', None) |
| 103 | if validate_user(user, options): |
| 104 | return apache.OK |