Changeset 76ebb1b in OpenWorkouts-current for ow/views


Ignore:
Timestamp:
Feb 18, 2019, 12:54:45 PM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current, feature/docs, master
Children:
4af38e8
Parents:
d6da99e
Message:

(#29) Add user verification by email on signup.

From now on, when a new user signs up, we set the account into an "unverified"
state. In order to complete the signup procedure, the user has to click on a
link we send by email to the email address provided on signup.

IMPORTANT: A new dependency has been added, pyramid_mailer, so remember to
install it in any existing openworkouts environment (this is done automatically
if you use the ./bin/start script):

pip install pyramid_mailer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/views/user.py

    rd6da99e r76ebb1b  
    2222from ..models.root import OpenWorkouts
    2323from ..views.renderers import OWFormRenderer
    24 from ..utilities import timedelta_to_hms
     24from ..utilities import timedelta_to_hms, get_verification_token
     25from ..mail import send_verification_email
    2526
    2627_ = TranslationStringFactory('OpenWorkouts')
     
    5960        user = context.get_user_by_email(email)
    6061        if user:
    61             password = request.POST.get('password', None)
    62             if password is not None and user.check_password(password):
    63                 headers = remember(request, str(user.uid))
    64                 redirect_url = return_to or request.resource_url(user)
    65                 return HTTPFound(location=redirect_url, headers=headers)
     62            if user.verified:
     63                password = request.POST.get('password', None)
     64                if password is not None and user.check_password(password):
     65                    headers = remember(request, str(user.uid))
     66                    redirect_url = return_to or request.resource_url(user)
     67                    return HTTPFound(location=redirect_url, headers=headers)
     68                else:
     69                    message = _('Wrong password')
    6670            else:
    67                 message = _('Wrong password')
     71                message = _('You have to verify your account first')
    6872        else:
    6973            message = _('Wrong email address')
     
    9498    if 'submit' in request.POST and form.validate():
    9599        user = form.bind(User(), exclude=['password_confirm'])
     100        user.verified = False
     101        user.verification_token = get_verification_token()
    96102        context.add_user(user)
     103        # send a verification link to the user email address
     104        send_verification_email(request, user)
    97105        # Send to login
    98106        return HTTPFound(location=request.resource_url(context))
     
    101109        'form': OWFormRenderer(form)
    102110    }
     111
     112
     113@view_config(
     114    context=User,
     115    name="verify",
     116    renderer='ow:templates/verify.pt')
     117def verify(context, request):
     118    redirect_url = request.resource_url(context)
     119
     120    # user has been verified already, send to dashboard
     121    if getattr(context, 'verified', False):
     122        return HTTPFound(location=redirect_url)
     123
     124    # Look for a verification token, then check if we can verify the user with
     125    # that token
     126    verified = len(request.subpath) > 0
     127    token = getattr(context, 'verification_token', False)
     128    verified = verified and token and str(token) == request.subpath[0]
     129    if verified:
     130        # verified, log in automatically and send to the dashboard
     131        context.verified = True
     132        headers = remember(request, str(context.uid))
     133        return HTTPFound(location=redirect_url, headers=headers)
     134
     135    # if we can not verify the user, show a page with some info about it
     136    return {}
    103137
    104138
Note: See TracChangeset for help on using the changeset viewer.