def accept_invitation(event):
    """
    Event handler for :class:`AcceptToken` event fired by invitation framework

    :param event: The event object
    :type event: :class:`AcceptToken`
    :return:
    """
    request = getRequest()
    email = _get_storage().get(event.token_id)
    if email is None:
        api.portal.show_message(_('The token has expired.'), request)
        return
    acl_users = api.portal.get_tool('acl_users')
    if api.user.get(username=email) is None:
        api.user.create(
            email=email,
            username=email,
        )
    acl_users.updateCredentials(
        request,
        request.response,
        email,
        None
    )
 def __call__(self):
     if self.token_id is None:
         raise KeyError(
             _("No token id given in sub-path."
               "Use .../@@accept-token/tokenid")
         )
     util = getUtility(ITokenUtility)
     portal = api.portal.get()
     if util.valid(self.token_id):
         notify(TokenAccepted(self.token_id))
         util._consume_token(self.token_id)
         token = util._fetch_token(self.token_id)
         if token.redirect_path is not None:
             return self.request.response.redirect('%s/%s' % (
                 portal.absolute_url(),
                 token.redirect_path
             ))
     else:
         api.portal.show_message(
             _('Token no longer valid'),
             self.request,
             type='error'
         )
     return self.request.response.redirect(portal.absolute_url())
    def invite_user(self, email):
        """
        Get new token, build email and send it to the given email address

        :param email: Email address to send invitation to, and for new user
        :type email: str
        :return: Tuple of token id and token url
        """
        # TODO - check that this email address is not already registered
        # TODO - Body and subject to become content
        portal = api.portal.get()
        token_util = getUtility(ITokenUtility)
        days = 3
        valid_until, expire_seconds = _ceil_datetime(days=days)
        token_id, token_url = token_util.generate_new_token(
            expire_seconds=expire_seconds)
        _store_invite(token_id, email)
        message = """You've been invited to join %s.

The following is a unique URL tied to your email address (%s).
Clicking the link will create you an account and log in you automatically.

%s

This token will be valid %d days and expire on %s.

Once your account has been created, you can also set up a password
to access the site here:

%s/mail_password_form?userid=%s
""" % (portal.Title(), email, token_url, days, valid_until,
            portal.absolute_url(), email)
        api.portal.send_email(
            recipient=email,
            subject='Your invitation to %s' % portal.Title(),
            body=message
        )

        api.portal.show_message(
            _('Invitation sent to %s') % email,
            self.request,
        )
        return token_id, token_url