diff options
author | Rodney Ewing <ewing.rj@gmail.com> | 2013-05-20 08:10:19 -0700 |
---|---|---|
committer | Rodney Ewing <ewing.rj@gmail.com> | 2013-05-24 16:52:49 -0700 |
commit | c3e3882e39e9830c3b60b2847713aa0419b31a28 (patch) | |
tree | 5aa71b1d8133828ef1d5981aad0329bda450e256 /mediagoblin/auth/tools.py | |
parent | 20583e8a4de3987e499dec730d7e7e1a22e69b3d (diff) | |
download | mediagoblin-c3e3882e39e9830c3b60b2847713aa0419b31a28.tar.lz mediagoblin-c3e3882e39e9830c3b60b2847713aa0419b31a28.tar.xz mediagoblin-c3e3882e39e9830c3b60b2847713aa0419b31a28.zip |
modified auth/__init__ hooks to work better with multiple plugins. Removed auth/lib.py. And added a basic_extra_verification function that all plugins will use.
Diffstat (limited to 'mediagoblin/auth/tools.py')
-rw-r--r-- | mediagoblin/auth/tools.py | 65 |
1 files changed, 62 insertions, 3 deletions
diff --git a/mediagoblin/auth/tools.py b/mediagoblin/auth/tools.py index 7d051a66..10fb6491 100644 --- a/mediagoblin/auth/tools.py +++ b/mediagoblin/auth/tools.py @@ -17,10 +17,14 @@ import logging import wtforms -from mediagoblin import mg_globals -from mediagoblin.tools.mail import normalize_email +from mediagoblin import messages, mg_globals +from mediagoblin.tools.mail import normalize_email, send_email from mediagoblin.tools.translate import lazy_pass_to_ugettext as _ +from mediagoblin.tools.template import render_template from mediagoblin.tools.pluginapi import hook_handle +from mediagoblin.tools.response import redirect +from mediagoblin import auth +from mediagoblin.db.models import User _log = logging.getLogger(__name__) @@ -66,7 +70,7 @@ class AuthError(Exception): def check_auth_enabled(): no_auth = mg_globals.app_config['no_auth'] - auth_plugin = True if hook_handle('authentication') is not None else False + auth_plugin = hook_handle('authentication') if no_auth == 'false' and not auth_plugin: raise AuthError @@ -82,3 +86,58 @@ def no_auth_logout(request): """Log out the user if in no_auth mode""" if not mg_globals.app.auth: request.session.delete() + + +def basic_extra_validation(register_form, *args): + users_with_username = User.query.filter_by( + username=register_form.data['username']).count() + users_with_email = User.query.filter_by( + email=register_form.data['email']).count() + + extra_validation_passes = True + + if users_with_username: + register_form.username.errors.append( + _(u'Sorry, a user with that name already exists.')) + extra_validation_passes = False + if users_with_email: + register_form.email.errors.append( + _(u'Sorry, a user with that email address already exists.')) + extra_validation_passes = False + + return extra_validation_passes + + +EMAIL_VERIFICATION_TEMPLATE = ( + u"http://{host}{uri}?" + u"userid={userid}&token={verification_key}") + + +def send_verification_email(user, request): + """ + Send the verification email to users to activate their accounts. + + Args: + - user: a user object + - request: the request + """ + rendered_email = render_template( + request, 'mediagoblin/auth/verification_email.txt', + {'username': user.username, + 'verification_url': EMAIL_VERIFICATION_TEMPLATE.format( + host=request.host, + uri=request.urlgen('mediagoblin.auth.verify_email'), + userid=unicode(user.id), + verification_key=user.verification_key)}) + + # TODO: There is no error handling in place + send_email( + mg_globals.app_config['email_sender_address'], + [user.email], + # TODO + # Due to the distributed nature of GNU MediaGoblin, we should + # find a way to send some additional information about the + # specific GNU MediaGoblin instance in the subject line. For + # example "GNU MediaGoblin @ Wandborg - [...]". + 'GNU MediaGoblin - Verify your email!', + rendered_email) |