def save(self, *args, **kwargs): """Save the user email if it has changed.""" email = self.cleaned_data['email'] if self.instance.user.email != email: self.instance.user.email = email self.instance.user.save(update_fields=['email']) # Save email address to LDAP if LDAP is enabled if getattr(settings, 'USE_LDAP', False): set_email(self.instance.user.get_username(), email) bio = self.cleaned_data['bio'] student_org_user_profile = self.instance.get_student_org_user_profile() if student_org_user_profile and student_org_user_profile.bio != bio: student_org_user_profile.bio = bio student_org_user_profile.save(update_fields=['bio']) major = self.cleaned_data['major'] start_term = self.cleaned_data['start_term'] grad_term = self.cleaned_data['grad_term'] college_student_info = self.instance.get_college_student_info() if college_student_info: update_fields = [] if college_student_info.major != major: college_student_info.major = major if college_student_info.start_term != start_term: college_student_info.start_term = start_term update_fields.append('start_term') if college_student_info.grad_term != grad_term: college_student_info.grad_term = grad_term update_fields.append('grad_term') college_student_info.save(update_fields=update_fields) return super(UserProfileForm, self).save(*args, **kwargs)
def save(self, *args, **kwargs): """Save the user email if it has changed.""" email = self.cleaned_data['email'] if self.instance.user.email != email: self.instance.user.email = email self.instance.user.save(update_fields=['email']) # Save email address to LDAP if LDAP is enabled if getattr(settings, 'USE_LDAP', False): set_email(self.instance.user.get_username(), email) return super(UserProfileForm, 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)
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)