2016-10-13 17:05:37 +01:00
|
|
|
import json
|
|
|
|
|
|
2018-12-12 13:10:46 +00:00
|
|
|
from flask import current_app, redirect, render_template, session, url_for
|
2018-02-20 11:22:17 +00:00
|
|
|
from flask_login import current_user, login_required
|
2016-10-13 17:05:37 +01:00
|
|
|
from notifications_utils.url_safe_token import check_token
|
2016-03-17 13:07:52 +00:00
|
|
|
|
2018-02-20 11:22:17 +00:00
|
|
|
from app import user_api_client
|
2016-01-12 09:52:00 +00:00
|
|
|
from app.main import main
|
2016-01-12 10:28:14 +00:00
|
|
|
from app.main.forms import (
|
2016-03-17 13:07:52 +00:00
|
|
|
ChangeEmailForm,
|
|
|
|
|
ChangeMobileNumberForm,
|
2018-02-20 11:22:17 +00:00
|
|
|
ChangeNameForm,
|
|
|
|
|
ChangePasswordForm,
|
|
|
|
|
ConfirmPasswordForm,
|
2018-05-07 22:53:36 +01:00
|
|
|
TwoFactorForm,
|
2016-01-12 10:28:14 +00:00
|
|
|
)
|
2018-12-12 13:10:46 +00:00
|
|
|
from app.utils import user_is_gov_user
|
2016-10-28 11:45:05 +01:00
|
|
|
|
2016-01-25 10:47:27 +00:00
|
|
|
NEW_EMAIL = 'new-email'
|
|
|
|
|
NEW_MOBILE = 'new-mob'
|
|
|
|
|
NEW_MOBILE_PASSWORD_CONFIRMED = 'new-mob-password-confirmed'
|
|
|
|
|
|
2016-01-12 09:52:00 +00:00
|
|
|
|
|
|
|
|
@main.route("/user-profile")
|
2016-01-27 12:22:32 +00:00
|
|
|
@login_required
|
2016-01-15 17:46:09 +00:00
|
|
|
def user_profile():
|
2016-10-28 11:45:05 +01:00
|
|
|
return render_template(
|
|
|
|
|
'views/user-profile.html',
|
2018-12-12 12:29:08 +00:00
|
|
|
can_see_edit=current_user.is_gov_user,
|
2016-10-28 11:45:05 +01:00
|
|
|
)
|
2016-01-12 10:28:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route("/user-profile/name", methods=['GET', 'POST'])
|
2016-01-27 12:22:32 +00:00
|
|
|
@login_required
|
2016-01-15 17:46:09 +00:00
|
|
|
def user_profile_name():
|
2016-01-12 10:28:14 +00:00
|
|
|
|
2016-01-22 16:34:36 +00:00
|
|
|
form = ChangeNameForm(new_name=current_user.name)
|
|
|
|
|
|
|
|
|
|
if form.validate_on_submit():
|
2017-11-15 17:19:32 +00:00
|
|
|
user_api_client.update_user_attribute(current_user.id, name=form.new_name.data)
|
2016-01-15 17:46:09 +00:00
|
|
|
return redirect(url_for('.user_profile'))
|
2016-01-12 10:28:14 +00:00
|
|
|
|
2016-01-22 16:34:36 +00:00
|
|
|
return render_template(
|
|
|
|
|
'views/user-profile/change.html',
|
|
|
|
|
thing='name',
|
|
|
|
|
form_field=form.new_name
|
|
|
|
|
)
|
|
|
|
|
|
2016-01-12 10:28:14 +00:00
|
|
|
|
|
|
|
|
@main.route("/user-profile/email", methods=['GET', 'POST'])
|
2016-01-27 12:22:32 +00:00
|
|
|
@login_required
|
2018-12-12 13:10:46 +00:00
|
|
|
@user_is_gov_user
|
2016-01-15 17:46:09 +00:00
|
|
|
def user_profile_email():
|
2016-01-12 10:28:14 +00:00
|
|
|
|
2018-02-19 16:53:29 +00:00
|
|
|
def _is_email_already_in_use(email):
|
|
|
|
|
return user_api_client.is_email_already_in_use(email)
|
|
|
|
|
form = ChangeEmailForm(_is_email_already_in_use,
|
2016-01-27 12:22:32 +00:00
|
|
|
email_address=current_user.email_address)
|
2016-01-22 16:34:36 +00:00
|
|
|
|
|
|
|
|
if form.validate_on_submit():
|
2016-01-25 10:47:27 +00:00
|
|
|
session[NEW_EMAIL] = form.email_address.data
|
2016-01-15 17:46:09 +00:00
|
|
|
return redirect(url_for('.user_profile_email_authenticate'))
|
2016-01-22 16:34:36 +00:00
|
|
|
return render_template(
|
|
|
|
|
'views/user-profile/change.html',
|
|
|
|
|
thing='email address',
|
|
|
|
|
form_field=form.email_address
|
|
|
|
|
)
|
|
|
|
|
|
2016-01-12 11:25:46 +00:00
|
|
|
|
|
|
|
|
@main.route("/user-profile/email/authenticate", methods=['GET', 'POST'])
|
2016-01-27 12:22:32 +00:00
|
|
|
@login_required
|
2016-01-15 17:46:09 +00:00
|
|
|
def user_profile_email_authenticate():
|
2016-01-22 16:34:36 +00:00
|
|
|
# Validate password for form
|
|
|
|
|
def _check_password(pwd):
|
2016-03-30 09:58:10 +01:00
|
|
|
return user_api_client.verify_password(current_user.id, pwd)
|
2016-01-22 16:34:36 +00:00
|
|
|
form = ConfirmPasswordForm(_check_password)
|
2016-01-12 11:25:46 +00:00
|
|
|
|
2016-01-25 10:47:27 +00:00
|
|
|
if NEW_EMAIL not in session:
|
|
|
|
|
return redirect('main.user_profile_email')
|
|
|
|
|
|
2016-01-22 16:34:36 +00:00
|
|
|
if form.validate_on_submit():
|
2016-10-13 17:05:37 +01:00
|
|
|
user_api_client.send_change_email_verification(current_user.id, session[NEW_EMAIL])
|
|
|
|
|
return render_template('views/change-email-continue.html',
|
|
|
|
|
new_email=session[NEW_EMAIL])
|
2016-01-12 10:28:14 +00:00
|
|
|
|
2016-01-22 16:34:36 +00:00
|
|
|
return render_template(
|
2016-09-28 09:34:16 +01:00
|
|
|
'views/user-profile/authenticate.html',
|
|
|
|
|
thing='email address',
|
|
|
|
|
form=form,
|
|
|
|
|
back_link=url_for('.user_profile_email')
|
2016-01-22 16:34:36 +00:00
|
|
|
)
|
|
|
|
|
|
2016-01-12 10:28:14 +00:00
|
|
|
|
2016-10-13 17:05:37 +01:00
|
|
|
@main.route("/user-profile/email/confirm/<token>", methods=['GET'])
|
|
|
|
|
@login_required
|
|
|
|
|
def user_profile_email_confirm(token):
|
|
|
|
|
token_data = check_token(token,
|
|
|
|
|
current_app.config['SECRET_KEY'],
|
|
|
|
|
current_app.config['DANGEROUS_SALT'],
|
|
|
|
|
current_app.config['EMAIL_EXPIRY_SECONDS'])
|
|
|
|
|
token_data = json.loads(token_data)
|
|
|
|
|
user_id = token_data['user_id']
|
|
|
|
|
new_email = token_data['email']
|
2017-11-15 17:19:32 +00:00
|
|
|
user_api_client.update_user_attribute(user_id, email_address=new_email)
|
2016-10-14 14:46:31 +01:00
|
|
|
session.pop(NEW_EMAIL, None)
|
2016-10-13 17:05:37 +01:00
|
|
|
|
|
|
|
|
return redirect(url_for('.user_profile'))
|
|
|
|
|
|
|
|
|
|
|
2016-01-12 10:28:14 +00:00
|
|
|
@main.route("/user-profile/mobile-number", methods=['GET', 'POST'])
|
2016-01-27 12:22:32 +00:00
|
|
|
@login_required
|
2016-01-15 17:46:09 +00:00
|
|
|
def user_profile_mobile_number():
|
2016-01-12 10:28:14 +00:00
|
|
|
|
2016-01-22 16:34:36 +00:00
|
|
|
form = ChangeMobileNumberForm(mobile_number=current_user.mobile_number)
|
|
|
|
|
|
|
|
|
|
if form.validate_on_submit():
|
2016-01-25 10:47:27 +00:00
|
|
|
session[NEW_MOBILE] = form.mobile_number.data
|
2016-01-15 17:46:09 +00:00
|
|
|
return redirect(url_for('.user_profile_mobile_number_authenticate'))
|
2016-01-12 11:25:46 +00:00
|
|
|
|
2016-01-22 16:34:36 +00:00
|
|
|
return render_template(
|
|
|
|
|
'views/user-profile/change.html',
|
|
|
|
|
thing='mobile number',
|
|
|
|
|
form_field=form.mobile_number
|
|
|
|
|
)
|
|
|
|
|
|
2016-01-12 11:25:46 +00:00
|
|
|
|
|
|
|
|
@main.route("/user-profile/mobile-number/authenticate", methods=['GET', 'POST'])
|
2016-01-27 12:22:32 +00:00
|
|
|
@login_required
|
2016-01-15 17:46:09 +00:00
|
|
|
def user_profile_mobile_number_authenticate():
|
2016-01-12 11:25:46 +00:00
|
|
|
|
2016-01-22 16:34:36 +00:00
|
|
|
# Validate password for form
|
|
|
|
|
def _check_password(pwd):
|
2016-03-30 09:58:10 +01:00
|
|
|
return user_api_client.verify_password(current_user.id, pwd)
|
2016-01-22 16:34:36 +00:00
|
|
|
form = ConfirmPasswordForm(_check_password)
|
2016-01-12 11:25:46 +00:00
|
|
|
|
2016-01-25 10:47:27 +00:00
|
|
|
if NEW_MOBILE not in session:
|
|
|
|
|
return redirect(url_for('.user_profile_mobile_number'))
|
|
|
|
|
|
2016-01-22 16:34:36 +00:00
|
|
|
if form.validate_on_submit():
|
2016-01-25 10:47:27 +00:00
|
|
|
session[NEW_MOBILE_PASSWORD_CONFIRMED] = True
|
2016-03-30 09:58:10 +01:00
|
|
|
user_api_client.send_verify_code(current_user.id, 'sms', session[NEW_MOBILE])
|
2016-01-15 17:46:09 +00:00
|
|
|
return redirect(url_for('.user_profile_mobile_number_confirm'))
|
2016-01-12 10:28:14 +00:00
|
|
|
|
2016-01-22 16:34:36 +00:00
|
|
|
return render_template(
|
|
|
|
|
'views/user-profile/authenticate.html',
|
|
|
|
|
thing='mobile number',
|
|
|
|
|
form=form,
|
|
|
|
|
back_link=url_for('.user_profile_mobile_number_confirm')
|
|
|
|
|
)
|
|
|
|
|
|
2016-01-12 10:28:14 +00:00
|
|
|
|
|
|
|
|
@main.route("/user-profile/mobile-number/confirm", methods=['GET', 'POST'])
|
2016-01-27 12:22:32 +00:00
|
|
|
@login_required
|
2016-01-15 17:46:09 +00:00
|
|
|
def user_profile_mobile_number_confirm():
|
2016-01-12 10:28:14 +00:00
|
|
|
|
2016-01-27 12:22:32 +00:00
|
|
|
# Validate verify code for form
|
|
|
|
|
def _check_code(cde):
|
2016-03-30 09:58:10 +01:00
|
|
|
return user_api_client.check_verify_code(current_user.id, cde, 'sms')
|
2016-01-27 12:22:32 +00:00
|
|
|
|
|
|
|
|
if NEW_MOBILE_PASSWORD_CONFIRMED not in session:
|
|
|
|
|
return redirect(url_for('.user_profile_mobile_number'))
|
|
|
|
|
|
2018-05-07 22:53:36 +01:00
|
|
|
form = TwoFactorForm(_check_code)
|
2016-01-12 10:28:14 +00:00
|
|
|
|
2016-01-22 16:34:36 +00:00
|
|
|
if form.validate_on_submit():
|
2017-02-24 16:21:41 +00:00
|
|
|
user = user_api_client.get_user(current_user.id)
|
|
|
|
|
# the user will have a new current_session_id set by the API - store it in the cookie for future requests
|
|
|
|
|
session['current_session_id'] = user.current_session_id
|
2016-11-03 11:20:24 +00:00
|
|
|
mobile_number = session[NEW_MOBILE]
|
2016-01-25 10:47:27 +00:00
|
|
|
del session[NEW_MOBILE]
|
|
|
|
|
del session[NEW_MOBILE_PASSWORD_CONFIRMED]
|
2017-11-15 17:19:32 +00:00
|
|
|
user_api_client.update_user_attribute(current_user.id, mobile_number=mobile_number)
|
2016-01-15 17:46:09 +00:00
|
|
|
return redirect(url_for('.user_profile'))
|
2016-01-12 10:28:14 +00:00
|
|
|
|
2016-01-22 16:34:36 +00:00
|
|
|
return render_template(
|
|
|
|
|
'views/user-profile/confirm.html',
|
|
|
|
|
form_field=form.sms_code,
|
|
|
|
|
thing='mobile number'
|
|
|
|
|
)
|
|
|
|
|
|
2016-01-12 10:28:14 +00:00
|
|
|
|
|
|
|
|
@main.route("/user-profile/password", methods=['GET', 'POST'])
|
2016-01-27 12:22:32 +00:00
|
|
|
@login_required
|
2016-01-15 17:46:09 +00:00
|
|
|
def user_profile_password():
|
2016-01-12 10:28:14 +00:00
|
|
|
|
2016-01-27 12:22:32 +00:00
|
|
|
# Validate password for form
|
|
|
|
|
def _check_password(pwd):
|
2016-03-30 09:58:10 +01:00
|
|
|
return user_api_client.verify_password(current_user.id, pwd)
|
2016-01-27 12:22:32 +00:00
|
|
|
form = ChangePasswordForm(_check_password)
|
2016-01-12 10:28:14 +00:00
|
|
|
|
2016-01-22 16:34:36 +00:00
|
|
|
if form.validate_on_submit():
|
2017-02-07 13:32:20 +00:00
|
|
|
user_api_client.update_password(current_user.id, password=form.new_password.data)
|
2016-01-15 17:46:09 +00:00
|
|
|
return redirect(url_for('.user_profile'))
|
2016-01-22 16:34:36 +00:00
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
|
'views/user-profile/change-password.html',
|
|
|
|
|
form=form
|
|
|
|
|
)
|