def send_restore_password(request): """ Sends email verification code """ logger.debug('Recover password request %s', request) try: username_or_password = request.POST.get('username_or_password') check_captcha(request) user_profile = UserProfile.objects.get( Q(username=username_or_password) | Q(email=username_or_password)) if not user_profile.email: raise ValidationError( "You didn't specify email address for this user") verification = Verification( type_enum=Verification.TypeChoices.password, user_id=user_profile.id) verification.save() try: send_reset_password_email(request, user_profile, verification) except Exception as e: raise ValidationError('Unable to send email: ' + str(e)) message = settings.VALIDATION_IS_OK logger.debug( 'Verification email has been send for token %s to user %s(id=%d)', verification.token, user_profile.username, user_profile.id) except UserProfile.DoesNotExist: message = "User with this email or username doesn't exist" logger.debug("Skipping password recovery request for nonexisting user") except (UserProfile.DoesNotExist, ValidationError) as e: logger.debug('Not sending verification email because %s', e) message = 'Unfortunately we were not able to send you restore password email because {}'.format( e) return HttpResponse(message, content_type='text/plain')
def __send_new_email_ver(self, user, email): new_ver = Verification(user=user, type_enum=Verification.TypeChoices.confirm_email, email=email) new_ver.save() link = "{}#/confirm_email?token={}".format(self.__host, new_ver.token) text = ('Hi {}, you have changed email to curren on pychat \nTo verify it, please click on the url: {}') \ .format(user.username, link) start_message = mark_safe( "You have changed email to current one in <b>Pychat</b>. \n" "To stay safe please verify it by clicking on the url below." ) context = { 'username': user.username, 'magicLink': link, 'btnText': "CONFIRM THIS EMAIL", 'greetings': start_message } html_message = render_to_string('sign_up_email.html', context) self.logger.info('Sending verification email to userId %s (email %s)', user.id, email) yield self.__send_mail( "Confirm this email", text, email, html_message, False ) return new_ver
def send_restore_password(self, username_or_password): try: user_profile = UserProfile.objects.get(Q(username=username_or_password) | Q(email=username_or_password)) if not user_profile.email: raise ValidationError("You didn't specify email address for this user") verification = Verification(type_enum=Verification.TypeChoices.password, user_id=user_profile.id) verification.save() try: yield from self.__send_reset_password_email(user_profile.email, user_profile.username, verification) except Exception as e: raise ValidationError('Unable to send email: ' + str(e)) message = settings.VALIDATION_IS_OK self.logger.debug( 'Verification email has been send for token %s to user %s(id=%d)', verification.token, user_profile.username, user_profile.id ) except UserProfile.DoesNotExist: message = "User with this email or username doesn't exist" self.logger.debug("Skipping password recovery request for nonexisting user") except (UserProfile.DoesNotExist, ValidationError) as e: self.logger.debug('Not sending verification email because %s', e) message = 'Unfortunately we were not able to send you restore password email because {}'.format(e) return message
def send_sign_up_email(user, site_address, request): if user.email is not None: verification = Verification(user=user, type_enum=Verification.TypeChoices.register) verification.save() user.email_verification = verification user.save(update_fields=['email_verification']) link = "{}://{}/confirm_email?token={}".format(settings.SITE_PROTOCOL, site_address, verification.token) text = ('Hi {}, you have registered pychat' '\nTo complete your registration please click on the url bellow: {}' '\n\nIf you find any bugs or propositions you can post them {}').format( user.username, link, settings.ISSUES_REPORT_LINK) start_message = mark_safe(( "You have registered in <b>Pychat</b>. If you find any bugs or propositions you can post them" " <a href='{}'>here</a>. To complete your registration please click on the link below.").format( settings.ISSUES_REPORT_LINK)) context = { 'username': user.username, 'magicLink': link, 'btnText': "CONFIRM SIGN UP", 'greetings': start_message } html_message = render_to_string('sign_up_email.html', context, context_instance=RequestContext(request)) logger.info('Sending verification email to userId %s (email %s)', user.id, user.email) try: send_mail("Confirm chat registration", text, site_address, [user.email, ], html_message=html_message, fail_silently=True) except Exception as e: logging.error("Failed to send registration email because {}".format(e)) else: logger.info('Email %s has been sent', user.email)
def __send_sign_up_email(self, user): verification = Verification(user=user, type_enum=Verification.TypeChoices.register) verification.save() user.email_verification = verification user.save(update_fields=['email_verification']) link = "{}#/confirm_email?token={}".format(self.__host, verification.token) text = ('Hi {}, you have registered pychat' '\nTo complete your registration please click on the url bellow: {}' '\n\nIf you find any bugs or propositions you can post them {}').format( user.username, link, settings.ISSUES_REPORT_LINK) start_message = mark_safe(( "You have registered in <b>Pychat</b>. If you find any bugs or propositions you can post them" " <a href='{}'>here</a>. To complete your registration please click on the link below.").format( settings.ISSUES_REPORT_LINK)) context = { 'username': user.username, 'magicLink': link, 'btnText': "CONFIRM SIGN UP", 'greetings': start_message } html_message = render_to_string('sign_up_email.html', context) self.logger.info('Sending verification email to userId %s (email %s)', user.id, user.email) yield self.__send_mail( "Confirm chat registration", text, user.email, html_message, True )
def send_restore_password(request): """ Sends email verification code """ logger.debug('Recover password request %s', request) try: username_or_password = request.POST.get('username_or_password') check_captcha(request) user_profile = UserProfile.objects.get(Q(username=username_or_password) | Q(email=username_or_password)) if not user_profile.email: raise ValidationError("You didn't specify email address for this user") verification = Verification(type_enum=Verification.TypeChoices.password, user_id=user_profile.id) verification.save() message = "{},\n" \ "You requested to change a password on site {}.\n" \ "To proceed click on the link {}://{}/restore_password?token={}\n" \ "If you didn't request the password change just ignore this mail" \ .format(user_profile.username, request.get_host(), SITE_PROTOCOL, request.get_host(), verification.token) send_mail("Change password", message, request.get_host(), (user_profile.email,), fail_silently=False) message = VALIDATION_IS_OK logger.debug('Verification email has been send for token %s to user %s(id=%d)', verification.token, user_profile.username, user_profile.id) except UserProfile.DoesNotExist: message = "User with this email or username doesn't exist" logger.debug("Skipping password recovery request for nonexisting user") except (UserProfile.DoesNotExist, ValidationError) as e: logger.debug('Not sending verification email because %s', e) message = 'Unfortunately we were not able to send you restore password email because {}'.format(e) return HttpResponse(message, content_type='text/plain')
def post(self, request): logger.info('Saving profile: %s', hide_fields(request.POST, ("base64_image", ), huge=True)) user_profile = UserProfile.objects.get(pk=request.user.id) image_base64 = request.POST.get('base64_image') new_email = request.POST['email'] if not new_email: new_email = None if new_email: utils.validate_email(new_email) utils.validate_user(request.POST['username']) if image_base64 is not None: image = extract_photo(image_base64) request.FILES['photo'] = image passwd = request.POST['password'] if passwd: if request.user.password: is_valid = authenticate(username=request.user.username, password=request.POST['old_password']) if not is_valid: return HttpResponse("Invalid old password", content_type='text/plain') utils.check_password(passwd) request.POST['password'] = make_password(passwd) form = UserProfileForm(request.POST, request.FILES, instance=user_profile) if form.is_valid(): if not passwd: form.instance.password = form.initial['password'] if new_email != form.initial['email']: if form.initial[ 'email'] and form.instance.email_verification and form.instance.email_verification.verified: verification = Verification( type_enum=Verification.TypeChoices.email, user_id=user_profile.id, email=new_email) verification.save() send_email_change(request, request.user.username, form.initial['email'], verification, new_email) raise ValidationError( "In order to change an email please confirm it from you current address. We send you an verification email to {}." .format(form.initial['email'])) if new_email: new_ver = send_new_email_ver(request, request.user, new_email) form.instance.email_verification = new_ver profile = form.save() if passwd and form.initial['email']: send_password_changed(request, form.initial['email']) response = profile.photo.url if 'photo' in request.FILES else settings.VALIDATION_IS_OK else: response = form.errors return HttpResponse(response, content_type='text/plain')
def profile_save_user(self, in_message): message = in_message[VarNames.CONTENT] userprofile = UserProfile.objects.get(id=self.user_id) email_verification_id = userprofile.email_verification_id un = message[UserProfileVarNames.USERNAME] email = message[UserProfileVarNames.EMAIL] if userprofile.username != un: check_user(un) if userprofile.email != email: check_email(email) if userprofile.email and userprofile.email_verification and userprofile.email_verification.verified: verification = Verification( type_enum=Verification.TypeChoices.email, user_id=self.id, email=email) verification.save() send_email_change(self.request, un, userprofile.email, verification, email) self.ws_write( self.default( "In order to change an email please confirm it from you current address. We send you an verification email to {}." .format(userprofile.email), Actions.GROWL_MESSAGE, HandlerNames.WS)) email = userprofile.email # Don't change email, we need to verify it! elif email: new_ver = send_new_email_ver(self.request, userprofile, email) email_verification_id = new_ver.id sex = message[UserProfileVarNames.SEX] UserProfile.objects.filter(id=self.user_id).update( username=un, name=message[UserProfileVarNames.NAME], city=message[UserProfileVarNames.CITY], surname=message[UserProfileVarNames.SURNAME], email=email, birthday=message[UserProfileVarNames.BIRTHDAY], contacts=message[UserProfileVarNames.CONTACTS], sex=settings.GENDERS_STR[sex], email_verification=email_verification_id) self.publish( self.set_user_profile(in_message[VarNames.JS_MESSAGE_ID], message), self.channel) if userprofile.sex_str != sex or userprofile.username != un: self.publish(self.changed_user_profile(sex, self.user_id, un), settings.ALL_ROOM_ID)
def change_email_login(self, email, password): userprofile = UserProfile.objects.get(id=self.user_id) if not userprofile.check_password(password): raise ValidationError("Invalid password") if userprofile.email != email: self.__check_email__(email) if userprofile.email and userprofile.email_verification and userprofile.email_verification.verified: verification = Verification( type_enum=Verification.TypeChoices.email, user_id=self.user_id, email=email) verification.save() elif email: new_ver = yield from self.__send_new_email_ver( userprofile, email) UserProfile.objects.filter(id=self.user_id).update( email_verification_id=new_ver.id, email=email) yield from self.__send_email_changed(userprofile.email, email, userprofile.username) return settings.VALIDATION_IS_OK
def send_email_verification(user, site_address): if user.email is not None: verification = Verification( user=user, type_enum=Verification.TypeChoices.register) verification.save() user.email_verification = verification user.save(update_fields=['email_verification']) text = ( 'Hi {}, you have registered pychat' '\nTo complete your registration click on the url bellow: {}://{}/confirm_email?token={}' '\n\nIf you find any bugs or propositions you can post them {}/report_issue or {}' ).format(user.username, SITE_PROTOCOL, site_address, verification.token, site_address, ISSUES_REPORT_LINK) logger.info('Sending verification email to userId %s (email %s)', user.id, user.email) send_mail("Confirm chat registration", text, site_address, [ user.email, ]) logger.info('Email %s has been sent', user.email)
def send_new_email_ver(request, user, email): new_ver = Verification(user=user, type_enum=Verification.TypeChoices.confirm_email, email=email) new_ver.save() link = "{}://{}/confirm_email?token={}".format(settings.SITE_PROTOCOL, request.get_host(), new_ver.token) text = ('Hi {}, you have changed email to curren on pychat \nTo verify it, please click on the url: {}') \ .format(user.username, link) start_message = mark_safe("You have changed email to current one in <b>Pychat</b>. \n" "To stay safe please verify it by clicking on the url below.") context = { 'username': user.username, 'magicLink': link, 'btnText': "CONFIRM THIS EMAIL", 'greetings': start_message } html_message = render_to_string('sign_up_email.html', context, context_instance=RequestContext(request)) logger.info('Sending verification email to userId %s (email %s)', user.id, email) try: send_mail("Confirm this email", text, request.get_host(), [email, ], html_message=html_message, fail_silently=False) return new_ver except Exception as e: logger.exception("Failed to send email") raise ValidationError(e.message)