2019-02-27 00:45:00 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2021-09-27 11:06:26 +00:00
|
|
|
|
2021-11-08 07:12:12 +00:00
|
|
|
from django.conf import settings
|
2022-05-07 08:20:12 +00:00
|
|
|
|
2022-04-12 07:11:51 +00:00
|
|
|
from common.utils import validate_ip, get_ip_city, get_request_ip
|
2020-06-30 09:12:38 +00:00
|
|
|
from common.utils import get_logger
|
2022-05-07 08:20:12 +00:00
|
|
|
from audits.models import UserLoginLog
|
|
|
|
from audits.const import DEFAULT_CITY
|
|
|
|
from .notifications import DifferentCityLoginMessage
|
2020-06-30 09:12:38 +00:00
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
2021-11-08 07:12:12 +00:00
|
|
|
def check_different_city_login_if_need(user, request):
|
|
|
|
if not settings.SECURITY_CHECK_DIFFERENT_CITY_LOGIN:
|
|
|
|
return
|
|
|
|
|
2021-09-27 11:06:26 +00:00
|
|
|
ip = get_request_ip(request) or '0.0.0.0'
|
|
|
|
if not (ip and validate_ip(ip)):
|
|
|
|
city = DEFAULT_CITY
|
|
|
|
else:
|
|
|
|
city = get_ip_city(ip) or DEFAULT_CITY
|
|
|
|
|
2021-11-02 07:41:25 +00:00
|
|
|
city_white = ['LAN', ]
|
|
|
|
if city not in city_white:
|
|
|
|
last_user_login = UserLoginLog.objects.exclude(city__in=city_white) \
|
|
|
|
.filter(username=user.username, status=True).first()
|
|
|
|
|
|
|
|
if last_user_login and last_user_login.city != city:
|
|
|
|
DifferentCityLoginMessage(user, ip, city).publish_async()
|