Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed changing user email #22

Merged
merged 1 commit into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 7 additions & 133 deletions app/account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,32 +77,6 @@ def manage():
return render_template('account/manage.html', user=current_user, form=None)


@account.route('/reset-password', methods=['GET', 'POST'])
def reset_password_request():
"""Respond to existing user's request to reset their password."""
if not current_user.is_anonymous:
return redirect(url_for('main.index'))
form = RequestResetPasswordForm()
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
if user:
token = user.generate_password_reset_token()
reset_link = url_for(
'account.reset_password', token=token, _external=True)
get_queue().enqueue(
send_email,
recipient=user.email,
subject='Reset Your Password',
template='account/email/reset_password',
user=user,
reset_link=reset_link,
next=request.args.get('next'))
flash('A password reset link has been sent to {}.'.format(
form.email.data), 'warning')
return redirect(url_for('account.login'))
return render_template('account/reset_password.html', form=form)


@account.route('/reset-password/<token>', methods=['GET', 'POST'])
def reset_password(token):
"""Reset an existing user's password."""
Expand Down Expand Up @@ -148,22 +122,13 @@ def change_email_request():
form = ChangeEmailForm()
if form.validate_on_submit():
if current_user.verify_password(form.password.data):
new_email = form.email.data
token = current_user.generate_email_change_token(new_email)
change_email_link = url_for(
'account.change_email', token=token, _external=True)
get_queue().enqueue(
send_email,
recipient=new_email,
subject='Confirm Your New Email',
template='account/email/change_email',
# current_user is a LocalProxy, we want the underlying user
# object
user=current_user._get_current_object(),
change_email_link=change_email_link)
flash('A confirmation link has been sent to {}.'.format(new_email),
'warning')
return redirect(url_for('main.index'))
user_id = current_user.id
user = User.query.filter_by(id=user_id).first()
user.email = form.email.data
db.session.add(user)
db.session.commit()
flash('Email for user {} successfully changed to {}.'.format(user.full_name(), user.email), 'form-success')
return redirect(url_for('account.manage'))
else:
flash('Invalid email or password.', 'form-error')
return render_template('account/manage.html', form=form)
Expand All @@ -180,97 +145,6 @@ def change_email(token):
return redirect(url_for('main.index'))


@account.route('/confirm-account')
@login_required
def confirm_request():
"""Respond to new user's request to confirm their account."""
token = current_user.generate_confirmation_token()
confirm_link = url_for('account.confirm', token=token, _external=True)
get_queue().enqueue(
send_email,
recipient=current_user.email,
subject='Confirm Your Account',
template='account/email/confirm',
# current_user is a LocalProxy, we want the underlying user object
user=current_user._get_current_object(),
confirm_link=confirm_link)
flash('A new confirmation link has been sent to {}.'.format(
current_user.email), 'warning')
return redirect(url_for('main.index'))


@account.route('/confirm-account/<token>')
@login_required
def confirm(token):
"""Confirm new user's account with provided token."""
if current_user.confirmed:
return redirect(url_for('main.index'))
if current_user.confirm_account(token):
flash('Your account has been confirmed.', 'success')
else:
flash('The confirmation link is invalid or has expired.', 'error')
return redirect(url_for('main.index'))


@account.route(
'/join-from-invite/<int:user_id>/<token>', methods=['GET', 'POST'])
def join_from_invite(user_id, token):
"""
Confirm new user's account with provided token and prompt them to set
a password.
"""
if current_user is not None and current_user.is_authenticated:
flash('You are already logged in.', 'error')
return redirect(url_for('main.index'))

new_user = User.query.get(user_id)
if new_user is None:
return redirect(404)

if new_user.password_hash is not None:
flash('You have already joined.', 'error')
return redirect(url_for('main.index'))

if new_user.confirm_account(token):
form = CreatePasswordForm()
if form.validate_on_submit():
new_user.password = form.password.data
db.session.add(new_user)
db.session.commit()
flash('Your password has been set. After you log in, you can '
'go to the "Your Account" page to review your account '
'information and settings.', 'success')
return redirect(url_for('account.login'))
return render_template('account/join_invite.html', form=form)
else:
flash('The confirmation link is invalid or has expired. Another '
'invite email with a new link has been sent to you.', 'error')
token = new_user.generate_confirmation_token()
invite_link = url_for(
'account.join_from_invite',
user_id=user_id,
token=token,
_external=True)
get_queue().enqueue(
send_email,
recipient=new_user.email,
subject='You Are Invited To Join',
template='account/email/invite',
user=new_user,
invite_link=invite_link)
return redirect(url_for('main.index'))


@account.before_app_request
def before_request():
"""Force user to confirm email before accessing login-required routes."""
if current_user.is_authenticated \
and not current_user.confirmed \
and request.endpoint[:8] != 'account.' \
and request.endpoint != 'static':
return redirect(url_for('account.unconfirmed'))


@account.route('/unconfirmed')
def unconfirmed():
"""Catch users with unconfirmed emails."""
Expand Down
15 changes: 13 additions & 2 deletions app/apps/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,21 @@ def validate_name(self, field):
dclient = docker.from_env()
if any(app.name == field.data for app in dclient.containers.list(all=True)):
raise ValidationError('name already exists')
for app in dclient.containers.list(all=True):
print(app)


def validate_ports(form, field):
hp,cp = [],[]
dclient = docker.from_env()
for app in dclient.containers.list(all=True):
assign = app.ports
a = tuple(assign.keys())
hp += a
cp += [assign[k][0]['HostPort'] for k in a if assign[k]]
for port in field.data:
a,b = port.split(':',1)
if a in cp or b in hp:
raise ValidationError('port already in use')

class _VolumeForm(Form):
container = StringField('Container Path')
bind = StringField('Host Path')
Expand Down