1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- # -*- coding: utf-8 -*-
- """
- flaskbb.auth.services.activation
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Handlers for activating accounts in FlaskBB
- :copyright: (c) 2014-2018 the FlaskBB Team
- :license: BSD, see LICENSE for more details
- """
- from flask_babelplus import gettext as _
- from ...core.auth.activation import AccountActivator as _AccountActivator
- from ...core.exceptions import ValidationError
- from ...core.tokens import Token, TokenActions, TokenError
- from ...email import send_activation_token
- class AccountActivator(_AccountActivator):
- def __init__(self, token_serializer, users):
- self.token_serializer = token_serializer
- self.users = users
- def initiate_account_activation(self, email):
- """
- Looks a user up via email and sends an activation token.
- Will raise a
- :class:`ValidationError<flaskbb.core.exceptions.ValidationError>`
- if either the email doesn't exist in the application or the account
- tied to the email is already activated.
- """
- user = self.users.query.filter_by(email=email).first()
- if user is None:
- raise ValidationError('email', _("Entered email doesn't exist"))
- if user.activated:
- raise ValidationError('email', _('Account is already activated'))
- token = self.token_serializer.dumps(
- Token(user_id=user.id, operation=TokenActions.ACTIVATE_ACCOUNT)
- )
- send_activation_token.delay(
- token=token, username=user.username, email=user.email
- )
- def activate_account(self, token):
- """
- Activates an account based on the supplied token.
- Will raise
- :class:`TokenError<flaskbb.core.tokens.TokenError>` if the supplied
- token is not an account activation token and a
- :class:`ValidationError<flaskbb.core.exceptions.ValidationError>`
- if the account is already activated.
- Otherwise marks the account as activated.
- """
- token = self.token_serializer.loads(token)
- if token.operation != TokenActions.ACTIVATE_ACCOUNT:
- raise TokenError.invalid()
- user = self.users.query.get(token.user_id)
- if user.activated:
- raise ValidationError(
- 'activated', _('Account is already activated')
- )
- user.activated = True
|