def signup(): form = user_forms.SignUp() if form.validate_on_submit(): # Create a user who hasn't validated his email address user = models.User( first_name=form.first_name.data, last_name=form.last_name.data, phone=form.phone.data, email=form.email.data, confirmation=False, password=form.password.data, ) # Insert the user in the database db.session.add(user) db.session.commit() # Subject of the confirmation email subject = 'Please confirm your email address.' # Generate a random token token = ts.dumps(user.email, salt='email-confirm-key') # Build a confirm link with token confirmUrl = url_for('userbp.confirm', token=token, _external=True) # Render an HTML template to send by email html = render_template('email/confirm.html', confirm_url=confirmUrl) # Send the email to user email.send(user.email, subject, html) # Send back to the home page flash('Check your emails to confirm your email address.', 'positive') return redirect(url_for('index')) return render_template('user/signup.html', form=form, title='Sign up')
def forgot(): ''' Functions to handle cases when user forgets email id Redirect user to index page after ''' form = user_forms.Forgot() if form.validate_on_submit(): user = models.User.query.filter_by(email=form.email.data).first() # Check the user exists if user is not None: # Subject of the confirmation email subject = 'Reset your password.' # Generate a random token token = ts.dumps(user.email, salt='password-reset-key') # Build a reset link with token resetUrl = url_for('userbp.reset', token=token, _external=True) # Render an HTML template to send by email html = render_template('email/reset.html', reset_url=resetUrl) # Send the email to user email.send(user.email, subject, html) # Send back to the home page flash('Check your emails to reset your password.', 'positive') return redirect(url_for('index')) else: flash('Unknown email address.', 'negative') return redirect(url_for('userbp.forgot')) return render_template('user/forgot.html', form=form)
def resend(): form = user_forms.Resend() if form.validate_on_submit(): user = models.User.query.filter_by(email=form.email.data).first() # Check the user exists if user is not None: subject = 'Please confirm your email address.' # Generate a random token token = ts.dumps(user.email, salt='email-confirm-key') # Build a confirm link with token confirmUrl = url_for('userbp.confirm', token=token, _external=True) # Render an HTML template to send by email html = render_template('email/confirm.html', confirm_url=confirmUrl) # Send the email to user email.send(user.email, subject, html) # Send back to the home page flash( 'Your confirmation email has been successfully resent. ' 'Check your emails to confirm your email address.', 'positive') return redirect(url_for('index')) else: flash('Unknown email address.', 'negative') return redirect(url_for('index')) return render_template('user/resend.html', form=form)
def signup(): form = user_forms.SignUp() if form.validate_on_submit(): # Create a user who hasn't validated his email address user = models.User( name=form.name.data, surname=form.surname.data, phone=form.phone.data, email=form.email.data, confirmation=False, password=form.password.data, ) # Insert the user in the database db.session.add(user) db.session.commit() # Subject of the confirmation email subject = 'Please confirm your email address.' # Generate a random token token = ts.dumps(user.email, salt='email-confirm-key') # Build a confirm link with token confirmUrl = url_for('userbp.confirm', token=token, _external=True) # Render an HTML template to send by email html = render_template('email/confirm.html', confirm_url=confirmUrl) # Send the email to user email.send(user.email, subject, html) # Send back to the home page flash('Check your emails to confirm your email address.', 'positive') return redirect(url_for('index')) return render_template('user/signup.html', form=form, title='Sign up')
def forgot(): # can not forgot password if already logged in if request.method == "GET" and current_user.is_authenticated: return redirect(url_for("sitefe.index")) form = FormForgot() if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() if user: subject = "Reset your password." salt = util.get_salt(app.config["SALT_RESET_PASSWORD"]) token = ts.dumps(user.email, salt=salt) try: user.token = token db.session.commit() except Exception as err: logger.exception(err) flash("can not reset password at this moment", "negative") return redirect(url_for("userfe.forgot")) resetUrl = url_for("userfe.reset", token=token, _external=True) html = render_template("frontend/email/reset.html", reset_url=resetUrl) email.send(user.email, subject, html) now = datetime.now() now_plus_timeout = now + timedelta(minutes=util.TOKEN_TIMEOUT) app.apscheduler.add_job(func=expire_reset_password_token, trigger='date', next_run_time=str(now_plus_timeout), args=[token], id=util.generate_random(5)) flash("Check your emails to reset your password. You've got {} minutes before the link expires" .format(util.TOKEN_TIMEOUT), "positive") return redirect(url_for("sitefe.index")) else: flash("Unknown email address.", "negative") return redirect(url_for("userfe.forgot")) return render_template("frontend/user/forgot.html", form=form)
def forgot(): form = user_forms.Forgot() if form.validate_on_submit(): user = models.User.query.filter_by(email=form.email.data).first() # Check the user exists if user is not None: # Subject of the confirmation email subject = 'Reset your password.' # Generate a random token token = ts.dumps(user.email, salt='password-reset-key') # Build a reset link with token resetUrl = url_for('userbp.reset', token=token, _external=True) # Render an HTML template to send by email html = render_template('email/reset.html', reset_url=resetUrl) # Send the email to user email.send(user.email, subject, html) # Send back to the home page flash('Check your emails to reset your password.', 'positive') return redirect(url_for('index')) else: flash('Unknown email address.', 'negative') return redirect(url_for('userbp.forgot')) return render_template('user/forgot.html', form=form)
def forgot_view(): form = ForgotForm() if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() # Check the user exists if user is not None: # Subject of the confirmation email subject = 'Reset your password.' # Generate a random token token = tkn.dumps(user.email, salt='password-reset-key') # Build a reset link with token resetUrl = url_for('auth.reset', token=token, _external=True) # Render an HTML template to send by email html = render_template('email/reset.html', reset_url=resetUrl) # Send the email to user email.send(user.email, subject, html) # Send back to the home page flash('Check your emails to reset your password.', 'positive') return redirect(url_for('auth.login')) else: flash('Unknown email address.', 'negative') return redirect(url_for('auth.forgot')) return render_template('forms/forgot.html', form=form)
def signup_view(): form = SignupForm() if form.validate_on_submit(): mail = form.email.data user = User.query.filter_by(email=mail).first() if user: # if a user is found, we want to redirect back to signup page so user can try again flash('Email address already exists') return redirect(url_for('auth.signup')) else: # Create a user who hasn't validated his email address user = User( public_id=str(uuid.uuid4()), first_name=form.first_name.data, last_name=form.last_name.data, email=form.email.data, password=generate_password_hash(form.password.data), ) # Insert the user in the database db.session.add(user) db.session.commit() # Subject of the confirmation email subject = 'Please confirm your email address.' # Generate a random token token = tkn.dumps(user.email, salt='email-confirm-key') # Build a confirm link with token confirmUrl = url_for('auth.confirm', token=token, _external=True) # Render an HTML template to send by email html = render_template('email/confirm.html', confirm_url=confirmUrl) # Send the email to user email.send(user.email, subject, html) # Send back to the home page flash('Check your emails to confirm your email address.', 'positive') return redirect(url_for('auth.signup')) return render_template('forms/signup.html', form=form)
def signup(): form = user_forms.SignUp() if form.validate_on_submit(): # Create a user who hasn't validated his email address user = models.User( first_name=form.first_name.data, last_name=form.last_name.data, # phone=form.phone.data, email=form.email.data, confirmation=False, password=form.password.data) # Create a ldap_user ldap_user = { 'objectClass': ['inetOrgPerson', 'posixAccount', 'top'], 'givenName': user.first_name, 'sn': user.last_name, 'gidNumber': 500, 'uidNumber': random.randint(1000, 1000000), 'uid': user.email.split('@', 1)[0], 'homeDirectory': '/home/users/' + user.email.split('@', 1)[0], 'userPassword': form.password.data } user_ldap_dn = 'cn=' + ldap_user['uid'] + ',ou=Users,dc=ldap,dc=com' # Insert the user in the database db.session.add(user) # Add user in LDAP try: c = Connection(s, user=app.config['LDAP_SERVICE_USERNAME'], password=app.config['LDAP_SERVICE_PASSWORD']) c.open() c.start_tls() c.bind() c.add(user_ldap_dn, attributes=ldap_user) except Exception as e: # remove the user from the db session print(e) db.session.rollback() db.session.commit() logger.error('User account creation failed', user=user.get_id()) flash( 'There was an error in creating your account, if the issue persists, ' 'please contact and administrator.', 'negative') return redirect(url_for('index')) logger.info('User account created successfully', user=user.get_id()) # If there was no error in adding user via LDAP we will commit the session to the database c.unbind() db.session.commit() # Subject of the confirmation email subject = 'Please confirm your email address.' # Generate a random token token = ts.dumps(user.email, salt='email-confirm-key') # Build a confirm link with token confirmUrl = url_for('userbp.confirm', token=token, _external=True) # Render an HTML template to send by email html = render_template('email/confirm.html', confirm_url=confirmUrl) # Send the email to user email.send(user.email, subject, html) # Send back to the home page flash('Check your emails to confirm your email address.', 'positive') return redirect(url_for('index')) return render_template('user/signup.html', form=form, title='Sign up')
def register(): # can not register if already logged in if request.method == "GET" and current_user.is_authenticated: return redirect(url_for("sitefe.index")) form = FormRegister() if form.validate_on_submit(): user = User( name=form.name.data.strip(), phone=form.phone.data.strip(), email=form.email.data.strip(), password=form.password.data.strip() ) # assign role as normal user user_role = Role.query.filter_by(name=util.USER).first() if user_role: user.roles.append(user_role) # insert the user in the database try: db.session.add(user) db.session.commit() subject = "Please confirm your email address." salt = util.get_salt(app.config["SALT_CONFIRM_EMAIL"]) token = ts.dumps(user.email, salt=salt) user.token = token db.session.commit() confirm_url = url_for("userfe.confirm", token=token, _external=True) html = render_template("frontend/email/confirm.html", confirm_url=confirm_url) email.send(user.email, subject, html) now = datetime.now() now_plus_timeout = now + timedelta(minutes=util.TOKEN_TIMEOUT) app.apscheduler.add_job(func=expire_confirm_email_token, trigger='date', next_run_time=str(now_plus_timeout), args=[token], id=util.generate_random(5)) flash("Check your email to confirm. You've got {} minutes before the link expires" .format(util.TOKEN_TIMEOUT), "positive") return redirect(url_for("userfe.login")) except Exception as err: logger.exception(err) flash("can not register at this moment", "negative") return redirect(url_for("userfe.register")) finally: print("now in finally") user = User.query.filter_by(email=form.email.data.strip()).first() if user: print("found ghost user") db.session.delete(user) db.session.commit() return render_template("frontend/user/register.html", form=form, title="Register")