| | 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 | # Validate the user |
| | 63 | if validate_user(user, options): |
| | 64 | return apache.OK |
| | 65 | else: |
| | 66 | return apache.HTTP_UNAUTHORIZED |
| | 67 | finally: |
| | 68 | dispatcher.send(signal=signals.request_finished) |
| 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 |
| | 78 | # Set up middleware, now that settings works we can do it now. |
| | 79 | base_handler = BaseHandler() |
| | 80 | base_handler.load_middleware() |
| | 81 | |
| | 82 | 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 |
| | 84 | request = ModPythonRequest(req) |
| | 85 | |
| | 86 | # Apply request middleware |
| | 87 | for middleware_method in base_handler._request_middleware: |
| | 88 | response = middleware_method(request) |
| | 89 | if response: |
| | 90 | # If we get a response, we should probably stop processing any |
| | 91 | # remaining request middleware. |
| | 92 | break |
| | 93 | |
| | 94 | # Validate the user |
| | 95 | user = getattr(request, 'user', None) |
| | 96 | if validate_user(user, options): |
| | 97 | return apache.OK |