Exemple #1
0
 def delete_account(self, delete_posts=False, token=''):
     token_to_save = 'accountdelete'
     if not token:
         # there is no token if the user has no email address: they are deleted immediately. They must be logged in.
         user_to_delete = self.enki_user
     else:
         # a user has followed a accountdelete token link. The user account associated with the token will be deleted
         tokenEntity = EnkiModelTokenVerify.get_by_token(token)
         user_to_delete = EnkiModelUser.get_by_id(tokenEntity.user_id)
         # delete all user related tokens except any verify token related to account deletion that's not yet been used
         if tokenEntity.type == token_to_save:
             token_to_save = 'accountandpostsdelete'
     verify_tokens_to_delete = EnkiModelTokenVerify.fetch_keys_by_user_id_except_type(
         user_to_delete.key.id(), token_to_save)
     if verify_tokens_to_delete:
         ndb.delete_multi(verify_tokens_to_delete)
     email_rollback_tokens_to_delete = EnkiModelTokenEmailRollback.fetch_keys_by_user_id(
         user_to_delete.key.id())
     if email_rollback_tokens_to_delete:
         ndb.delete_multi(email_rollback_tokens_to_delete)
     # Delete the user account and log them out.
     if not HandlerBase.account_is_active(user_to_delete.key.id()):
         # delete user if the account is inactive
         display_names = EnkiModelDisplayName.fetch_keys_by_user_id(
             user_to_delete.key.id())
         if display_names:
             ndb.delete_multi(display_names)
         user_to_delete.key.delete()
     else:
         # anonymise the user
         if user_to_delete.email:
             # delete email subscriptions
             EnkiModelEmailSubscriptions.remove_by_email(
                 user_to_delete.email)
             user_to_delete.email = None
         if user_to_delete.password:
             user_to_delete.password = None
         if user_to_delete.auth_ids_provider:
             user_to_delete.auth_ids_provider = []
         user_to_delete.put()
         # keep all historical display_names. Add a new current display_name '[deleted]' (unless it's already been deleted)
         display_name = EnkiModelDisplayName.get_by_user_id_current(
             user_to_delete.key.id())
         if display_name:
             if display_name.prefix != EnkiModelDisplayName.DELETED_PREFIX or display_name.suffix != EnkiModelDisplayName.DELETED_SUFFIX:
                 EnkiModelDisplayName.set_display_name(
                     user_to_delete.key.id(),
                     EnkiModelDisplayName.DELETED_PREFIX,
                     EnkiModelDisplayName.DELETED_SUFFIX)
         # delete user's sent and received messages
         EnkiModelMessage.delete_user_messages(user_to_delete.key.id())
         # delete user's posts if required
         if delete_posts:
             EnkiModelPost.delete_user_posts(user_to_delete.key.id())
     # log the deleted user out
     if self.enki_user == user_to_delete.key.id():
         self.log_out()
     EnkiModelTokenAuth.revoke_user_authentications(user_to_delete.key.id())
 def get_data(self, handler):
     count_email_subscriptions = 0
     if handler.is_logged_in(
     ) and handler.enki_user.email and handler.enki_user.email != 'removed':
         count_email_subscriptions = EnkiModelEmailSubscriptions.count_newsletters_by_email(
             handler.enki_user.email)
     return count_email_subscriptions
 def get(self, verifytoken):
     tokenEntity = EnkiModelTokenVerify.get_by_token_type(
         xstr(verifytoken), 'emailsubscriptionconfirm')
     if tokenEntity:
         newsletter = tokenEntity.state
         unsubscribe_token = EnkiModelEmailSubscriptions.add_newsletter(
             tokenEntity.email, newsletter)
         self.add_infomessage(MSG.SUCCESS(),
                              MSG.EMAIL_SUBSCRIBED(newsletter))
         # send welcome email with unsubscribe link
         link = enki.libutil.get_local_url('emailunsubscribe', {
             'unsubscribetoken': unsubscribe_token,
             'newsletter': newsletter
         })
         self.send_email(
             tokenEntity.email,
             MSG.SEND_EMAIL_EMAIL_NEWSLETTER_WELCOME_SUBJECT(newsletter),
             MSG.SEND_EMAIL_EMAIL_NEWSLETTER_WELCOME_BODY(newsletter, link))
         self.add_infomessage(
             MSG.INFORMATION(),
             MSG.EMAIL_NEWSLETTER_WELCOME_EMAIL_SENT(newsletter))
         self.redirect(enki.libutil.get_local_url('home'))
         tokenEntity.key.delete()
     else:
         self.add_infomessage(MSG.INFORMATION(),
                              MSG.EMAIL_SUBSCRIPTION_FAILED())
         self.redirect(enki.libutil.get_local_url('emailsubscriptions'))
 def get(self, unsubscribetoken):
     unsubscribetoken = xstr(unsubscribetoken)
     tokenEntity = EnkiModelEmailSubscriptions.get_by_token(
         unsubscribetoken)
     newsletter = self.request.get(xstr('newsletter'))
     if not newsletter:  # unsubscribe from all newsletters (or was already unsubscribed)
         if tokenEntity:
             EnkiModelEmailSubscriptions.remove_by_email(tokenEntity.email)
         self.add_infomessage(MSG.SUCCESS(), MSG.EMAIL_UNSUBSCRIBED_ALL())
     elif (newsletter in settings.email_newsletter_name) or (
             newsletter in tokenEntity.newsletters
     ):  # unsubscribe from a specific newsletter (or was already unsubscribed)
         if tokenEntity:
             EnkiModelEmailSubscriptions.remove_newsletter_by_token(
                 tokenEntity.token, newsletter)
         self.add_infomessage(MSG.SUCCESS(),
                              MSG.EMAIL_UNSUBSCRIBED(newsletter))
     else:  # newsletter doesn't exist hence the user's already unsubscribed
         self.add_infomessage(MSG.SUCCESS(), MSG.EMAIL_UNSUBSCRIBED(''))
     self.redirect(enki.libutil.get_local_url('emailsubscriptions'))
 def post(self):
     if self.ensure_is_logged_in():
         self.check_CSRF()
         submit_type = self.request.get('submittype')
         if submit_type == 'reset':
             self.redirect(enki.libutil.get_local_url('emailbatchsending'))
         elif submit_type == 'send' or submit_type == 'sendtest':
             ready_to_send = 'off'
             newsletter = self.request.get('newsletter')
             subject = self.request.get('subject')
             body_text = self.request.get('body_text')
             footer_template = self.get_mailgun_email_footer_template(
                 newsletter)
             if submit_type == 'sendtest':
                 self.send_email(self.enki_user.email, subject,
                                 body_text + footer_template)
                 self.add_infomessage(
                     MSG.INFORMATION(),
                     'Test email sent to ' + self.enki_user.email)
             elif submit_type == 'send':
                 ready_to_send = self.request.get('readytosend')
                 if ready_to_send == 'on':
                     batches_emails, batches_emails_recipient_variables = EnkiModelEmailSubscriptions.get_mailgun_email_batches(
                         newsletter)
                     send_success = False
                     for batch_emails, batch_emails_recipient_variables in zip(
                             batches_emails,
                             batches_emails_recipient_variables):
                         send_success = self.send_mailgun_batch_email(
                             batch_emails, subject, body_text,
                             footer_template,
                             batch_emails_recipient_variables)
                     if send_success:
                         self.add_infomessage(MSG.SUCCESS(),
                                              'Batch email sent')
                         self.redirect(
                             enki.libutil.get_local_url(
                                 'emailbatchsending'))
                         return
                     else:
                         self.add_infomessage(MSG.WARNING(),
                                              'Batch email sending failed')
             self.render_tmpl('emailbatchsending.html',
                              newsletter=newsletter,
                              subject=subject,
                              body_text=body_text,
                              footer=footer_template,
                              ready_to_send=ready_to_send)
 def get_email_subscriptions_data(self):
     is_logged_in = False
     has_email_subscriptions = False
     has_email = ''
     contact_us = settings.ORGANISATION_CONTACT
     newsletter = settings.email_newsletter_name[0]
     if self.is_logged_in() and self.enki_user.email:
         is_logged_in = True
         has_email = self.enki_user.email if (
             self.enki_user.email != 'removed') else ''
         if has_email:
             has_email_subscriptions = True if EnkiModelEmailSubscriptions.count_newsletters_by_email(
                 self.enki_user.email) else False
     return [
         is_logged_in, has_email_subscriptions, has_email, contact_us,
         newsletter
     ]
Exemple #7
0
 def account_is_active(cls, user_id):
     # detect activity on a user account
     result = False
     has_friends = True if EnkiModelFriends.fetch_by_user_id(
         user_id) else False
     has_messages = True if EnkiModelMessage.exist_sent_or_received(
         user_id) else False
     has_forum_posts = True if EnkiModelPost.fetch_by_author(
         user_id) else False
     has_product = True if EnkiModelProductKey.exist_by_purchaser_or_activator(
         user_id) else False
     has_email_subscriptions = False
     email = ndb.Key(EnkiModelUser, user_id).get().email
     if email:
         has_email_subscriptions = True if EnkiModelEmailSubscriptions.exist_by_email(
             email) else False
     if has_friends or has_messages or has_forum_posts or has_product or has_email_subscriptions:
         result = True
     return result
 def post(self):
     self.check_CSRF()
     submit_type = self.request.get('submittype')
     data = self.get_email_subscriptions_data()
     email = ''
     error_message = ''
     if data[0]:  # is_logged_in
         if submit_type == 'subscribe':
             # delete eventual verification tokens for this email and newsletter
             tokenEntity = EnkiModelTokenVerify.get_by_email_state_type(
                 self.enki_user.email, settings.email_newsletter_name[0],
                 'emailsubscriptionconfirm')
             if tokenEntity:
                 tokenEntity.key.delete()
             EnkiModelEmailSubscriptions.add_newsletter(
                 self.enki_user.email, settings.email_newsletter_name[0])
             self.add_infomessage(
                 MSG.SUCCESS(),
                 MSG.EMAIL_SUBSCRIBED(settings.email_newsletter_name[0]))
             data[1] = True  # has_email_subscriptions
         elif submit_type == 'unsubscribe':
             EnkiModelEmailSubscriptions.remove_newsletter_by_email(
                 self.enki_user.email, settings.email_newsletter_name[0])
             self.add_infomessage(
                 MSG.SUCCESS(),
                 MSG.EMAIL_UNSUBSCRIBED(settings.email_newsletter_name[0]))
             data[
                 1] = False  # has_email_subscriptions - ASSUMPTION: ONLY ONE NEWSLETTER AVAILABLE
     if submit_type == 'subscribeemail':
         email_unsafe = self.request.get('email')
         email, result = self.validate_email(email_unsafe)
         if result == self.ERROR_EMAIL_FORMAT_INVALID:
             error_message = MSG.WRONG_EMAIL_FORMAT()
         elif result == self.ERROR_EMAIL_MISSING:
             error_message = MSG.MISSING_EMAIL()
         elif result == enki.libutil.ENKILIB_OK:
             if EnkiModelBackoffTimer.get(self.request.remote_addr,
                                          increment=True,
                                          expires=True) == 0:
                 if not EnkiModelEmailSubscriptions.exist_by_email_newsletter(
                         email, settings.email_newsletter_name[0]):
                     # if not already subscribed, create a token and send email with a link to allow the user to confirm their subscription
                     if not EnkiModelTokenVerify.get_by_email_state_type(
                             email, settings.email_newsletter_name[0],
                             'emailsubscriptionconfirm'):
                         # if a verify token already exists, skip as an email was already sent.
                         token = security.generate_random_string(
                             entropy=256)
                         emailToken = EnkiModelTokenVerify(
                             token=token,
                             email=email,
                             state=settings.email_newsletter_name[0],
                             type='emailsubscriptionconfirm')
                         emailToken.put()
                         link = enki.libutil.get_local_url(
                             'emailsubscriptionconfirm',
                             {'verifytoken': token})
                         self.send_email(
                             email,
                             MSG.SEND_EMAIL_EMAIL_SUBSCRIBE_CONFIRM_SUBJECT(
                             ),
                             MSG.SEND_EMAIL_EMAIL_SUBSCRIBE_CONFIRM_BODY(
                                 link, settings.email_newsletter_name[0]))
                 self.add_infomessage(
                     MSG.INFORMATION(),
                     MSG.EMAIL_SUBSCRIBE_CONFIRM_EMAIL_SENT(
                         email, settings.email_newsletter_name[0]))
                 self.add_debugmessage(
                     'Comment - whether the email is sent or not, the feedback through the UI is identical to prevent email checking.'
                 )
                 email = ''
             else:
                 backoff_timer = EnkiModelBackoffTimer.get(
                     self.request.remote_addr, expires=True)
                 if backoff_timer != 0:
                     error_message = MSG.TIMEOUT(
                         enki.libutil.format_timedelta(backoff_timer))
     if data[0]:  # is_logged_in
         self.redirect(enki.libutil.get_local_url('profile'))
     else:
         self.render_tmpl('emailsubscriptions.html',
                          active_menu='profile',
                          data=data,
                          email=email,
                          error=error_message)