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
|
2017-11-16 00:50:28 +01:00
|
|
|
|
|
|
|
from zerver.lib.send_email import FromAddress, send_email
|
2017-03-26 03:06:00 +02:00
|
|
|
from zerver.models import UserProfile
|
2017-01-30 23:19:38 +01:00
|
|
|
|
|
|
|
def get_device_browser(user_agent):
|
|
|
|
# type: (str) -> Optional[str]
|
|
|
|
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-06-22 06:30:33 +02:00
|
|
|
elif "chrome" 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
|
|
|
|
|
|
|
|
|
|
|
|
def get_device_os(user_agent):
|
|
|
|
# type: (str) -> Optional[str]
|
|
|
|
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")
|
|
|
|
def email_on_new_login(sender, user, request, **kwargs):
|
2017-07-06 08:21:11 +02:00
|
|
|
# type: (Any, UserProfile, Any, **Any) -> None
|
2017-01-30 23:19:38 +01:00
|
|
|
|
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.
|
|
|
|
if getattr(user, "just_registered", False):
|
|
|
|
return
|
2017-01-30 23:19:38 +01:00
|
|
|
|
2017-04-15 04:03:56 +02:00
|
|
|
login_time = timezone_now().strftime('%A, %B %d, %Y at %I:%M%p ') + \
|
2017-04-15 04:06:26 +02:00
|
|
|
timezone_get_current_timezone_name()
|
2017-01-30 23:19:38 +01:00
|
|
|
user_agent = request.META.get('HTTP_USER_AGENT', "").lower()
|
|
|
|
device_browser = get_device_browser(user_agent)
|
|
|
|
device_os = get_device_os(user_agent)
|
|
|
|
device_ip = request.META.get('REMOTE_ADDR') or "Uknown IP address"
|
|
|
|
device_info = {"device_browser": device_browser,
|
|
|
|
"device_os": device_os,
|
|
|
|
"device_ip": device_ip,
|
|
|
|
"login_time": login_time
|
|
|
|
}
|
|
|
|
|
|
|
|
context = common_context(user)
|
|
|
|
context['device_info'] = device_info
|
|
|
|
context['user'] = user
|
|
|
|
|
2017-07-11 05:01:32 +02:00
|
|
|
send_email('zerver/emails/notify_new_login', to_user_id=user.id,
|
|
|
|
from_name='Zulip Account Security', from_address=FromAddress.NOREPLY,
|
|
|
|
context=context)
|