2012-09-28 22:29:48 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright: (c) 2008, Jarek Zgoda <jarek.zgoda@gmail.com>
|
|
|
|
|
|
|
|
__revision__ = '$Id: models.py 28 2009-10-22 15:03:02Z jarek.zgoda $'
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.sites.models import Site
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2016-10-10 14:52:01 +02:00
|
|
|
from django.contrib.contenttypes.fields import GenericForeignKey
|
2017-04-15 04:03:56 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2012-09-28 22:29:48 +02:00
|
|
|
|
2017-05-02 01:15:58 +02:00
|
|
|
from zerver.lib.send_email import send_email
|
2013-08-08 16:50:58 +02:00
|
|
|
from zerver.lib.utils import generate_random_token
|
2017-01-20 12:27:38 +01:00
|
|
|
from zerver.models import PreregistrationUser, EmailChangeStatus
|
2017-03-03 19:01:52 +01:00
|
|
|
from typing import Any, Dict, Optional, Text, Union
|
2012-09-28 22:29:48 +02:00
|
|
|
|
2013-02-28 20:07:04 +01:00
|
|
|
B16_RE = re.compile('^[a-f0-9]{40}$')
|
|
|
|
|
|
|
|
def generate_key():
|
2016-11-23 05:21:41 +01:00
|
|
|
# type: () -> Text
|
2013-08-08 16:50:58 +02:00
|
|
|
return generate_random_token(40)
|
2013-02-28 20:07:04 +01:00
|
|
|
|
2012-09-28 22:29:48 +02:00
|
|
|
class ConfirmationManager(models.Manager):
|
2017-07-07 09:08:53 +02:00
|
|
|
url_pattern_name = 'confirmation.views.confirm'
|
|
|
|
|
2012-09-28 22:29:48 +02:00
|
|
|
def confirm(self, confirmation_key):
|
2017-01-20 12:27:38 +01:00
|
|
|
# type: (str) -> Union[bool, PreregistrationUser, EmailChangeStatus]
|
2013-02-28 20:07:04 +01:00
|
|
|
if B16_RE.search(confirmation_key):
|
2012-09-28 22:29:48 +02:00
|
|
|
try:
|
|
|
|
confirmation = self.get(confirmation_key=confirmation_key)
|
|
|
|
except self.model.DoesNotExist:
|
|
|
|
return False
|
2017-01-20 10:09:35 +01:00
|
|
|
|
2017-04-15 04:03:56 +02:00
|
|
|
time_elapsed = timezone_now() - confirmation.date_sent
|
2017-07-07 07:55:53 +02:00
|
|
|
if time_elapsed.total_seconds() > settings.EMAIL_CONFIRMATION_DAYS * 24 * 3600:
|
2017-01-20 10:09:35 +01:00
|
|
|
return False
|
|
|
|
|
2012-09-28 22:35:05 +02:00
|
|
|
obj = confirmation.content_object
|
2017-07-07 09:28:37 +02:00
|
|
|
obj.status = getattr(settings, 'STATUS_ACTIVE', 1)
|
|
|
|
obj.save(update_fields=['status'])
|
2012-09-28 22:35:05 +02:00
|
|
|
return obj
|
2012-09-28 22:29:48 +02:00
|
|
|
return False
|
|
|
|
|
2017-07-07 08:51:52 +02:00
|
|
|
def get_link_for_object(self, obj, host):
|
|
|
|
# type: (Union[ContentType, int], str) -> Text
|
2013-02-28 20:07:54 +01:00
|
|
|
key = generate_key()
|
2017-04-15 04:03:56 +02:00
|
|
|
self.create(content_object=obj, date_sent=timezone_now(), confirmation_key=key)
|
2017-07-07 08:51:52 +02:00
|
|
|
return self.get_activation_url(key, host)
|
2017-01-17 11:11:51 +01:00
|
|
|
|
2017-07-07 08:51:52 +02:00
|
|
|
def get_activation_url(self, confirmation_key, host):
|
|
|
|
# type: (Text, str) -> Text
|
2017-06-11 01:41:26 +02:00
|
|
|
return u'%s%s%s' % (settings.EXTERNAL_URI_SCHEME,
|
|
|
|
host,
|
2017-07-07 09:08:53 +02:00
|
|
|
reverse(self.url_pattern_name,
|
2017-06-11 01:41:26 +02:00
|
|
|
kwargs={'confirmation_key': confirmation_key}))
|
2013-02-28 20:07:54 +01:00
|
|
|
|
2017-01-20 12:27:38 +01:00
|
|
|
class EmailChangeConfirmationManager(ConfirmationManager):
|
2017-07-07 09:08:53 +02:00
|
|
|
url_pattern_name = 'zerver.views.user_settings.confirm_email_change'
|
2017-01-20 12:27:38 +01:00
|
|
|
|
2012-09-28 22:29:48 +02:00
|
|
|
class Confirmation(models.Model):
|
|
|
|
content_type = models.ForeignKey(ContentType)
|
|
|
|
object_id = models.PositiveIntegerField()
|
2016-10-10 14:52:01 +02:00
|
|
|
content_object = GenericForeignKey('content_type', 'object_id')
|
2017-03-09 09:11:43 +01:00
|
|
|
date_sent = models.DateTimeField('sent')
|
|
|
|
confirmation_key = models.CharField('activation key', max_length=40)
|
2012-09-28 22:29:48 +02:00
|
|
|
|
|
|
|
objects = ConfirmationManager()
|
|
|
|
|
2015-10-14 22:43:04 +02:00
|
|
|
class Meta(object):
|
2017-03-09 09:11:43 +01:00
|
|
|
verbose_name = 'confirmation email'
|
|
|
|
verbose_name_plural = 'confirmation emails'
|
2012-09-28 22:29:48 +02:00
|
|
|
|
|
|
|
def __unicode__(self):
|
2016-11-23 05:21:41 +01:00
|
|
|
# type: () -> Text
|
2017-03-09 09:11:43 +01:00
|
|
|
return 'confirmation email for %s' % (self.content_object,)
|
2016-06-22 21:16:02 +02:00
|
|
|
|
2017-01-20 12:27:38 +01:00
|
|
|
class EmailChangeConfirmation(Confirmation):
|
|
|
|
class Meta(object):
|
|
|
|
proxy = True
|
|
|
|
|
|
|
|
objects = EmailChangeConfirmationManager()
|
|
|
|
|
2017-07-06 06:59:17 +02:00
|
|
|
# Conirmation pathways for which there is no content_object that we need to
|
|
|
|
# keep track of.
|
|
|
|
|
|
|
|
def check_key_is_valid(creation_key):
|
|
|
|
# type: (Text) -> bool
|
|
|
|
if not RealmCreationKey.objects.filter(creation_key=creation_key).exists():
|
|
|
|
return False
|
|
|
|
days_sofar = (timezone_now() - RealmCreationKey.objects.get(creation_key=creation_key).date_created).days
|
|
|
|
# Realm creation link expires after settings.REALM_CREATION_LINK_VALIDITY_DAYS
|
|
|
|
if days_sofar <= settings.REALM_CREATION_LINK_VALIDITY_DAYS:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def generate_realm_creation_url():
|
|
|
|
# type: () -> Text
|
|
|
|
key = generate_key()
|
|
|
|
RealmCreationKey.objects.create(creation_key=key, date_created=timezone_now())
|
|
|
|
return u'%s%s%s' % (settings.EXTERNAL_URI_SCHEME,
|
|
|
|
settings.EXTERNAL_HOST,
|
|
|
|
reverse('zerver.views.create_realm',
|
|
|
|
kwargs={'creation_key': key}))
|
|
|
|
|
2016-06-22 21:16:02 +02:00
|
|
|
class RealmCreationKey(models.Model):
|
2017-03-09 09:11:43 +01:00
|
|
|
creation_key = models.CharField('activation key', max_length=40)
|
2017-04-15 04:03:56 +02:00
|
|
|
date_created = models.DateTimeField('created', default=timezone_now)
|