def verify_users(self): """ Return a dict whose keys will be a user's T number and whose values will be the dictionary of LDAP values returned from an LDAP lookup of the user. This is called after the field has been cleaned and a list of valid-format T numbers has been generated, and will raise a validation error if any T number does not map to a valid user. """ # Get information on each user, and raise an error if a T number # returns no information on a user users = {} if self._t_numbers: ldap_conn = LDAPConnection() for t_number in self._t_numbers: info = ldap_conn.get_info_from_tnumber(t_number) if info: users[t_number] = info else: raise forms.ValidationError( _("no user could be found with the T number %(tnumber)s" ) % {'tnumber': t_number}) return users
def verify_users(self): """ Return a dict whose keys will be a user's T number and whose values will be the dictionary of LDAP values returned from an LDAP lookup of the user. This is called after the field has been cleaned and a list of valid-format T numbers has been generated, and will raise a validation error if any T number does not map to a valid user. """ # Get information on each user, and raise an error if a T number # returns no information on a user users = {} if self._t_numbers: ldap_conn = LDAPConnection() for t_number in self._t_numbers: info = ldap_conn.get_info_from_tnumber(t_number) if info: users[t_number] = info else: raise forms.ValidationError(_("no user could be found with the T number %(tnumber)s") % { 'tnumber': t_number }) return users
def authenticate(self, username=None, password=None): """ Check for a user's existence in LDAP, creating a local Django User model for the user if they do not already have one. """ user = None # Connect to the Oberlin server, binding as the user try: ldap_conn = LDAPConnection(user_id=username, user_password=password) except ldap.LDAPError: return None else: user_info = ldap_conn.get_info_from_obie_id(username) # If the user exists in LDAP, get a current record of them in Django's # auth system, or add a new user using the LDAP information, allowing # a descended class to interact with each step of the creation if user_info is not None: try: user = User.objects.get(username=username) except User.DoesNotExist: # Get user information from LDAP create_info = { 'last_name': user_info['last_name'], 'first_name': user_info['first_name'], 'email': user_info['email'] } create_info.update( self.provide_additional_user_creation_info( user_info, create_info)) # Create the user with a random password random_pwd = User.objects.make_random_password(20) user = User(username=username, password=random_pwd, **create_info) # Get their professor status user.is_staff = user_info['professor'] user.save() # Allow child classes to perform further configuration on the new user self.do_post_user_creation_config(user, user_info, random_pwd) # Allow a child class to perform further action on the user self.do_post_validation_actions(user, user_info) return user
def authenticate(self, username=None, password=None): """ Check for a user's existence in LDAP, creating a local Django User model for the user if they do not already have one. """ user = None # Connect to the Oberlin server, binding as the user try: ldap_conn = LDAPConnection(user_id=username, user_password=password) except ldap.LDAPError: return None else: user_info = ldap_conn.get_info_from_obie_id(username) # If the user exists in LDAP, get a current record of them in Django's # auth system, or add a new user using the LDAP information, allowing # a descended class to interact with each step of the creation if user_info is not None: try: user = User.objects.get(username=username) except User.DoesNotExist: # Get user information from LDAP create_info = { 'last_name': user_info['last_name'], 'first_name': user_info['first_name'], 'email': user_info['email'] } create_info.update(self.provide_additional_user_creation_info(user_info, create_info)) # Create the user with a random password random_pwd = User.objects.make_random_password(20) user = User(username=username, password=random_pwd, **create_info) # Get their professor status user.is_staff = user_info['professor'] user.save() # Allow child classes to perform further configuration on the new user self.do_post_user_creation_config(user, user_info, random_pwd) # Allow a child class to perform further action on the user self.do_post_validation_actions(user, user_info) return user