def test_rename_user(self): """Checks rename_user handles bad username properly and only renames successfully if new username is valid""" invalid_name = (False, 'Invalid username. %s' % (settings.USERNAME_HELPTEXT)) correct = (True, 'User %s renamed to %s' % (self.user, self.new_user)) self.assertTrue(utils.username_exists(self.user)) # This is a bad username because underscores are not allowed self.assertEqual(utils.rename_user(self.user, 'bad_username'), invalid_name) # Nothing is changed self.assertTrue(utils.username_exists(self.user)) # Actual renaming self.assertEqual(utils.rename_user(self.user, self.new_user), correct) # Model has not been updated yet, but LDAP has been self.assertFalse(utils.username_exists(self.user)) self.assertTrue(utils.username_exists(self.new_user))
def test_rename_user(self): """Checks rename_user handles bad username properly and only renames successfully if new username is valid""" invalid_name = (False, 'Invalid username. %s' % ( settings.USERNAME_HELPTEXT)) correct = (True, 'User %s renamed to %s' % (self.user, self.new_user)) self.assertTrue(utils.username_exists(self.user)) # This is a bad username because underscores are not allowed self.assertEqual(utils.rename_user(self.user, 'bad_username'), invalid_name) # Nothing is changed self.assertTrue(utils.username_exists(self.user)) # Actual renaming self.assertEqual(utils.rename_user(self.user, self.new_user), correct) # Model has not been updated yet, but LDAP has been self.assertFalse(utils.username_exists(self.user)) self.assertTrue(utils.username_exists(self.new_user))
def save(self, *args, **kwargs): """Only save the instance if user exists in LDAP. Allows renaming username, but does not update other LDAP entry attributes (yet).""" new_username = self.get_username() # Password can be None or '' for unusuable password if (not new_username or not self.email or not self.first_name or not self.last_name): raise ValidationError('Users must have username, email, ' 'first name and last name') try: old_user = LDAPUser.objects.get(pk=self.pk) old_username = old_user.get_username() renaming_user = old_username != new_username updating_email = old_user.email != self.email except LDAPUser.DoesNotExist: renaming_user = False updating_email = False # Update username if renaming_user: success, reason = ldap_utils.rename_user( old_username, new_username) if not success: raise ValidationError( 'Encountered error while renaming username from {old} ' 'to {new}. Reason: {reason}'.format( old=old_username, new=new_username, reason=reason)) elif not ldap_utils.username_exists(new_username): if ldap_utils.create_user(new_username, self.password, self.email, self.first_name, self.last_name): # Successfully created LDAP entry for new user # Set an unusable password for the Django DB instance super(LDAPUser, self).set_unusable_password() else: # Failed to create user some how. raise ValidationError( 'Error creating new LDAP entry for {name} with username ' '"{username}"'.format( name=self.get_full_name(), username=new_username)) # Update email # New username should exist by now after rename. if (updating_email and not ldap_utils.set_email(new_username, self.email)): raise ValidationError( 'Encountered error while updating user email to {new}'.format( new=self.email)) # TODO(flieee): update LDAP other entry attributes (i.e. name) too? super(LDAPUser, self).save(*args, **kwargs)
def save(self, *args, **kwargs): """Only save the instance if user exists in LDAP. Allows renaming username, but does not update other LDAP entry attributes (yet).""" new_username = self.get_username() # Password can be None or '' for unusuable password if (not new_username or not self.email or not self.first_name or not self.last_name): raise ValidationError('Users must have username, email, ' 'first name and last name') try: old_user = LDAPUser.objects.get(pk=self.pk) old_username = old_user.get_username() renaming_user = old_username != new_username updating_email = old_user.email != self.email except LDAPUser.DoesNotExist: renaming_user = False updating_email = False # Update username if renaming_user: success, reason = ldap_utils.rename_user(old_username, new_username) if not success: raise ValidationError( 'Encountered error while renaming username from {old} ' 'to {new}. Reason: {reason}'.format(old=old_username, new=new_username, reason=reason)) elif not ldap_utils.username_exists(new_username): if ldap_utils.create_user(new_username, self.password, self.email, self.first_name, self.last_name): # Successfully created LDAP entry for new user # Set an unusable password for the Django DB instance super(LDAPUser, self).set_unusable_password() else: # Failed to create user some how. raise ValidationError( 'Error creating new LDAP entry for {name} with username ' '"{username}"'.format(name=self.get_full_name(), username=new_username)) # Update email # New username should exist by now after rename. if (updating_email and not ldap_utils.set_email(new_username, self.email)): raise ValidationError( 'Encountered error while updating user email to {new}'.format( new=self.email)) # TODO(flieee): update LDAP other entry attributes (i.e. name) too? super(LDAPUser, self).save(*args, **kwargs)