2017-01-30 23:19:38 +01:00
|
|
|
|
2017-11-16 00:50:28 +01:00
|
|
|
from typing import Any, Dict, Optional
|
|
|
|
|
2017-01-30 23:19:38 +01:00
|
|
|
from django.conf import settings
|
2017-11-16 00:50:28 +01:00
|
|
|
from django.contrib.auth.signals import user_logged_in
|
|
|
|
from django.dispatch import receiver
|
2017-01-30 23:19:38 +01:00
|
|
|
from django.template import loader
|
2017-11-16 00:50:28 +01:00
|
|
|
from django.utils.timezone import \
|
|
|
|
get_current_timezone_name as timezone_get_current_timezone_name
|
2017-04-15 04:03:56 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2018-04-30 21:04:01 +02:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2017-11-16 00:50:28 +01:00
|
|
|
|
2017-11-29 08:34:09 +01:00
|
|
|
from zerver.lib.queue import queue_json_publish
|
2018-04-26 20:11:45 +02:00
|
|
|
from zerver.lib.send_email import FromAddress
|
2017-03-26 03:06:00 +02:00
|
|
|
from zerver.models import UserProfile
|
2018-05-27 00:17:08 +02:00
|
|
|
from zerver.lib.timezone import get_timezone
|
2017-01-30 23:19:38 +01:00
|
|
|
|
2018-08-10 00:58:44 +02:00
|
|
|
JUST_CREATED_THRESHOLD = 60
|
|
|
|
|
2017-11-27 07:33:05 +01:00
|
|
|
def get_device_browser(user_agent: str) -> Optional[str]:
|
2017-01-30 23:19:38 +01:00
|
|
|
user_agent = user_agent.lower()
|
2017-07-03 19:10:50 +02:00
|
|
|
if "zulip" in user_agent:
|
|
|
|
return "Zulip"
|
|
|
|
elif "edge" in user_agent:
|
2017-06-22 06:30:33 +02:00
|
|
|
return "Edge"
|
2017-06-22 06:34:26 +02:00
|
|
|
elif "opera" in user_agent or "opr/" in user_agent:
|
|
|
|
return "Opera"
|
2017-11-19 11:08:43 +01:00
|
|
|
elif ("chrome" in user_agent or "crios" in user_agent) and "chromium" not in user_agent:
|
2017-01-30 23:19:38 +01:00
|
|
|
return 'Chrome'
|
|
|
|
elif "firefox" in user_agent and "seamonkey" not in user_agent and "chrome" not in user_agent:
|
|
|
|
return "Firefox"
|
|
|
|
elif "chromium" in user_agent:
|
|
|
|
return "Chromium"
|
|
|
|
elif "safari" in user_agent and "chrome" not in user_agent and "chromium" not in user_agent:
|
|
|
|
return "Safari"
|
|
|
|
elif "msie" in user_agent or "trident" in user_agent:
|
|
|
|
return "Internet Explorer"
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2017-11-27 07:33:05 +01:00
|
|
|
def get_device_os(user_agent: str) -> Optional[str]:
|
2017-01-30 23:19:38 +01:00
|
|
|
user_agent = user_agent.lower()
|
|
|
|
if "windows" in user_agent:
|
|
|
|
return "Windows"
|
|
|
|
elif "macintosh" in user_agent:
|
2017-08-26 09:33:47 +02:00
|
|
|
return "macOS"
|
2017-01-30 23:19:38 +01:00
|
|
|
elif "linux" in user_agent and "android" not in user_agent:
|
|
|
|
return "Linux"
|
|
|
|
elif "android" in user_agent:
|
|
|
|
return "Android"
|
2017-07-07 22:34:25 +02:00
|
|
|
elif "ios" in user_agent:
|
|
|
|
return "iOS"
|
2017-01-30 23:19:38 +01:00
|
|
|
elif "like mac os x" in user_agent:
|
|
|
|
return "iOS"
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(user_logged_in, dispatch_uid="only_on_login")
|
2017-11-27 07:33:05 +01:00
|
|
|
def email_on_new_login(sender: Any, user: UserProfile, request: Any, **kwargs: Any) -> None:
|
2017-03-26 01:27:45 +01:00
|
|
|
# We import here to minimize the dependencies of this module,
|
|
|
|
# since it runs as part of `manage.py` initialization
|
|
|
|
from zerver.context_processors import common_context
|
|
|
|
|
2017-01-30 23:19:38 +01:00
|
|
|
if not settings.SEND_LOGIN_EMAILS:
|
|
|
|
return
|
|
|
|
|
|
|
|
if request:
|
2017-08-23 01:14:45 +02:00
|
|
|
# If the user's account was just created, avoid sending an email.
|
2018-08-10 00:58:44 +02:00
|
|
|
if (timezone_now() - user.date_joined).total_seconds() <= JUST_CREATED_THRESHOLD:
|
2017-08-23 01:14:45 +02:00
|
|
|
return
|
2017-01-30 23:19:38 +01:00
|
|
|
|
|
|
|
user_agent = request.META.get('HTTP_USER_AGENT', "").lower()
|
|
|
|
|
|
|
|
context = common_context(user)
|
2017-12-22 22:24:46 +01:00
|
|
|
context['user_email'] = user.email
|
2018-05-27 00:17:08 +02:00
|
|
|
user_tz = user.timezone
|
|
|
|
if user_tz == '':
|
|
|
|
user_tz = timezone_get_current_timezone_name()
|
|
|
|
local_time = timezone_now().astimezone(get_timezone(user_tz))
|
2018-08-13 23:00:51 +02:00
|
|
|
if user.twenty_four_hour_time:
|
|
|
|
hhmm_string = local_time.strftime('%H:%M')
|
|
|
|
else:
|
|
|
|
hhmm_string = local_time.strftime('%I:%M%p')
|
|
|
|
context['login_time'] = local_time.strftime('%A, %B %d, %Y at {} %Z'.format(hhmm_string))
|
2018-04-30 21:04:01 +02:00
|
|
|
context['device_ip'] = request.META.get('REMOTE_ADDR') or _("Unknown IP address")
|
2018-04-30 17:40:59 +02:00
|
|
|
context['device_os'] = get_device_os(user_agent)
|
|
|
|
context['device_browser'] = get_device_browser(user_agent)
|
2017-01-30 23:19:38 +01:00
|
|
|
|
2017-11-29 08:34:09 +01:00
|
|
|
email_dict = {
|
|
|
|
'template_prefix': 'zerver/emails/notify_new_login',
|
|
|
|
'to_user_id': user.id,
|
|
|
|
'from_name': 'Zulip Account Security',
|
|
|
|
'from_address': FromAddress.NOREPLY,
|
|
|
|
'context': context}
|
|
|
|
queue_json_publish("email_senders", email_dict)
|