Ticket #2507: ldapauth.patch
File ldapauth.patch, 4.5 KB (added by , 16 years ago) |
---|
-
ldapauth.py.
old new 109 109 110 110 if self.settings['LDAP_OPTIONS']: 111 111 for k in self.settings['LDAP_OPTIONS']: 112 self.ldap.set_option(k, self.settings.LDAP_OPTIONS[k]) 112 self.ldap.set_option(k, self.settings["LDAP_OPTIONS"][k]) 113 113 114 114 115 l = self.ldap.initialize(self.settings['LDAP_SERVER_URI']) 115 116 116 bind_string = self._pre_bind(l, username) 117 ldap_username = username.replace('_', '.') 118 bind_string = self._pre_bind(l, ldap_username) 117 119 if not bind_string: 118 120 if self.settings['LDAP_DEBUG']: 119 121 logging.info('LDAPBackend.authenticate failed: _pre_bind return no bind_string (%s, %s)' % ( 120 l, username))122 l, ldap_username)) 121 123 return None 122 124 123 125 try: … … 136 138 try: 137 139 user = self._get_user_by_name(username) 138 140 except User.DoesNotExist: 139 user = self._get_ldap_user(l, username)141 user = self._get_ldap_user(l, ldap_username) 140 142 141 143 if user is not None: 142 144 if self.settings['LDAP_UPDATE_FIELDS']: … … 151 153 return user 152 154 153 155 # Functions provided to override to customize to your LDAP configuration. 154 def _pre_bind(self, l, username):156 def _pre_bind(self, l, ldap_username): 155 157 """ 156 158 Function that returns the dn to bind against ldap with. 157 called as: self._pre_bind(ldapobject, username)159 called as: self._pre_bind(ldapobject, ldap_username) 158 160 """ 159 161 if not self.settings['LDAP_BINDDN']: 160 162 # When the LDAP_BINDDN setting is blank we try to find the … … 170 172 return None 171 173 172 174 # Now do the actual search 173 filter = self.settings['LDAP_SEARCH_FILTER'] % username175 filter = self.settings['LDAP_SEARCH_FILTER'] % ldap_username 174 176 result = l.search_s(self.settings['LDAP_SEARCHDN'], 175 177 self.settings['LDAP_SCOPE'], filter, attrsonly=1) 176 178 … … 182 184 return result[0][0] 183 185 else: 184 186 # LDAP_BINDDN is set so we use it as a template. 185 return "%s=%s,%s" % (self.settings['LDAP_BIND_ATTRIBUTE'], username,187 return "%s=%s,%s" % (self.settings['LDAP_BIND_ATTRIBUTE'], ldap_username, 186 188 self.settings['LDAP_BINDDN']) 187 189 188 190 def _get_user_by_name(self, username): … … 191 193 username. 192 194 called as: self._get_user_by_name(username) 193 195 """ 196 194 197 return User.objects.get(username=username) 195 198 196 199 def _create_user_object(self, username, password): … … 198 201 Creates and returns an object of contrib.auth.models.User. 199 202 called as: self._create_user_object(username, password) 200 203 """ 204 201 205 return User(username=username, password=password) 202 206 203 207 # Required for an authentication backend … … 208 212 return None 209 213 # End of functions to override 210 214 211 def _get_ldap_user(self, l, username):215 def _get_ldap_user(self, l, ldap_username): 212 216 """ 213 217 Helper method, makes a user object and call update_user to populate 214 218 """ 215 219 216 220 # Generate a random password string. 217 password = User.objects.make_random_password(10) 221 password = User.objects.make_random_password(12) 222 username = ldap_username.replace(".","_") 218 223 user = self._create_user_object(username, password) 219 224 return user 220 225 … … 223 228 Helper method, populates a user object with various attributes from 224 229 LDAP. 225 230 """ 226 227 username = user.username228 filter = self.settings['LDAP_SEARCH_FILTER'] % username231 232 ldap_username = user.username.replace("_",".") 233 filter = self.settings['LDAP_SEARCH_FILTER'] % ldap_username 229 234 230 235 # Get results of search and make sure something was found. 231 236 # At this point this shouldn't fail. … … 265 270 if emailf and emailf in attrs: 266 271 user.email = attrs[emailf][0] 267 272 elif self.settings['LDAP_DEFAULT_EMAIL_SUFFIX']: 268 user.email = username + self.settings['LDAP_DEFAULT_EMAIL_SUFFIX']273 user.email = ldap_username + self.settings['LDAP_DEFAULT_EMAIL_SUFFIX'] 269 274 270 275 271 276 # Check if we are mapping an ldap id to check if the user is staff or super