Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Moved Apache auth handler to django/contrib/auth/handlers/modpython.py

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1500 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3cb20c45c73c003e4f8417ea44bf8c7d9ec6bd27 1 parent 5066fe5
Adrian Holovaty authored November 30, 2005
0  auth/handlers/__init__.py b/django/contrib/auth/handlers/__init__.py
No changes.
44  django/contrib/auth/handlers/modpython.py
... ...
@@ -0,0 +1,44 @@
  1
+from mod_python import apache
  2
+import os
  3
+
  4
+def authenhandler(req, **kwargs):
  5
+    """
  6
+    Authentication handler that checks against Django's auth database.
  7
+    """
  8
+
  9
+    # mod_python fakes the environ, and thus doesn't process SetEnv.  This fixes
  10
+    # that so that the following import works
  11
+    os.environ.update(req.subprocess_env)
  12
+
  13
+    from django.models.auth import users
  14
+
  15
+    # check for PythonOptions
  16
+    _str_to_bool = lambda s: s.lower() in '1', 'true', 'on', 'yes'
  17
+
  18
+    options = req.get_options()
  19
+    permission_name = options.get('DjangoPermissionName', None)
  20
+    staff_only = _str_to_bool(options.get('DjangoRequireStaffStatus', "on"))
  21
+    superuser_only = _str_to_bool(options.get('DjangoRequireSuperuserStatus', "off"))
  22
+
  23
+    # check that the username is valid
  24
+    kwargs = {'username__exact': req.user, 'is_active__exact': True}
  25
+    if staff_only:
  26
+        kwargs['is_staff__exact'] = True
  27
+    if superuser_only:
  28
+        kwargs['is_superuser__exact'] = True
  29
+    try:
  30
+        user = users.get_object(**kwargs)
  31
+    except users.UserDoesNotExist:
  32
+        return apache.HTTP_UNAUTHORIZED
  33
+
  34
+    # check the password and any permission given
  35
+    if user.check_password(req.get_basic_auth_pw()):
  36
+        if permission_name:
  37
+            if user.has_perm(permission_name):
  38
+                return apache.OK
  39
+            else:
  40
+                return apache.HTTP_UNAUTHORIZED
  41
+        else:
  42
+            return apache.OK
  43
+    else:
  44
+        return apache.HTTP_UNAUTHORIZED
43  django/core/handlers/modpython.py
@@ -163,46 +163,3 @@ def populate_apache_request(http_response, mod_python_req):
163 163
 def handler(req):
164 164
     # mod_python hooks into this function.
165 165
     return ModPythonHandler()(req)
166  
-
167  
-def authenhandler(req, **kwargs):
168  
-    """
169  
-    Authentication handler that checks against Django's auth database.
170  
-    """
171  
-    from mod_python import apache
172  
-    
173  
-    # mod_python fakes the environ, and thus doesn't process SetEnv.  This fixes 
174  
-    # that so that the following import works
175  
-    os.environ.update(req.subprocess_env)
176  
-    from django.models.auth import users
177  
-    
178  
-    # check for PythonOptions
179  
-    _str_to_bool = lambda s: s.lower() in '1', 'true', 'on', 'yes'
180  
-    
181  
-    options = req.get_options()
182  
-    permission_name = options.get('DjangoPermissionName', None)
183  
-    staff_only = _str_to_bool(options.get('DjangoRequireStaffStatus', "on"))
184  
-    superuser_only = _str_to_bool(options.get('DjangoRequireSuperuserStatus', "off"))
185  
-    
186  
-    # check that the username is valid
187  
-    kwargs = {'username__exact': req.user, 'is_active__exact': True}
188  
-    if staff_only:
189  
-        kwargs['is_staff__exact'] = True
190  
-    if superuser_only:
191  
-        kwargs['is_superuser__exact'] = True
192  
-    try:
193  
-        user = users.get_object(**kwargs)
194  
-    except users.UserDoesNotExist:
195  
-        return apache.HTTP_UNAUTHORIZED
196  
-        
197  
-    # check the password and any permission given
198  
-    if user.check_password(req.get_basic_auth_pw()):
199  
-        if permission_name:
200  
-            if user.has_perm(permission_name):
201  
-                return apache.OK
202  
-            else:
203  
-                return apache.HTTP_UNAUTHORIZED
204  
-        else:
205  
-            return apache.OK
206  
-    else:
207  
-        return apache.HTTP_UNAUTHORIZED
208  
-    
26  docs/apache_auth.txt
@@ -7,12 +7,12 @@ dealing with Apache, you can configuring Apache to authenticate against Django's
7 7
 `authentication system`_ directly.  For example, you could:
8 8
 
9 9
     * Serve media files directly from Apache only to authenticated users.
10  
-    
  10
+
11 11
     * Authenticate access to a Subversion_ repository against Django users with
12 12
       a certain permission.
13  
-      
  13
+
14 14
     * Allow certain users to connect to a WebDAV share created with mod_dav_.
15  
-        
  15
+
16 16
 Configuring Apache
17 17
 ==================
18 18
 
@@ -24,9 +24,9 @@ with the standard ``Auth*`` and ``Require`` directives::
24 24
         AuthType basic
25 25
         AuthName "example.com"
26 26
         Require valid-user
27  
-        
  27
+
28 28
         SetEnv DJANGO_SETTINGS_MODULE mysite.settings
29  
-        PythonAuthenHandler django.core.handlers.modpython
  29
+        PythonAuthenHandler django.contrib.auth.handlers.modpython
30 30
     </Location>
31 31
 
32 32
 By default, the authentication handler will limit access to the ``/example/``
@@ -37,26 +37,26 @@ location to users marked as staff members.  You can use a set of
37 37
     ``PythonOption``                  Explanation
38 38
     ================================  =========================================
39 39
     ``DjangoRequireStaffStatus``      If set to ``on`` only "staff" users (i.e.
40  
-                                      those with the ``is_staff`` flag set) 
  40
+                                      those with the ``is_staff`` flag set)
41 41
                                       will be allowed.
42  
-                                      
  42
+
43 43
                                       Defaults to ``on``.
44 44
 
45 45
     ``DjangoRequireSuperuserStatus``  If set to ``on`` only superusers (i.e.
46 46
                                       those with the ``is_superuser`` flag set)
47 47
                                       will be allowed.
48  
-                                      
  48
+
49 49
                                       Defaults to ``off``.
50  
-    
  50
+
51 51
     ``DjangoPermissionName``          The name of a permission to require for
52  
-                                      access.  See `custom permissions`_ for
  52
+                                      access. See `custom permissions`_ for
53 53
                                       more information.
54  
-                                      
  54
+
55 55
                                       By default no specific permission will be
56 56
                                       required.
57 57
     ================================  =========================================
58  
-    
  58
+
59 59
 .. _authentication system: http://www.djangoproject.com/documentation/authentication/
60 60
 .. _Subversion: http://subversion.tigris.org/
61 61
 .. _mod_dav: http://httpd.apache.org/docs/2.0/mod/mod_dav.html
62  
-.. _custom permissions: http://www.djangoproject.com/documentation/authentication/#custom-permissions
  62
+.. _custom permissions: http://www.djangoproject.com/documentation/authentication/#custom-permissions

0 notes on commit 3cb20c4

Please sign in to comment.
Something went wrong with that request. Please try again.