diff --git a/apps/authentication/notifications.py b/apps/authentication/notifications.py index 5c5f8de2f..52ebb604e 100644 --- a/apps/authentication/notifications.py +++ b/apps/authentication/notifications.py @@ -1,9 +1,9 @@ -from django.utils import timezone from django.utils.translation import ugettext as _ from django.template.loader import render_to_string from notifications.notifications import UserMessage from common.utils import get_logger +from common.utils.timezone import local_now_display logger = get_logger(__file__) @@ -15,8 +15,7 @@ class DifferentCityLoginMessage(UserMessage): super().__init__(user) def get_html_msg(self) -> dict: - now_local = timezone.localtime(timezone.now()) - now = now_local.strftime("%Y-%m-%d %H:%M:%S") + now = local_now_display() subject = _('Different city login reminder') context = dict( subject=subject, @@ -39,3 +38,36 @@ class DifferentCityLoginMessage(UserMessage): ip = '8.8.8.8' city = '洛杉矶' return cls(user, ip, city) + + +class OAuthBindMessage(UserMessage): + def __init__(self, user, ip, oauth_name, oauth_id): + super().__init__(user) + self.ip = ip + self.oauth_name = oauth_name + self.oauth_id = oauth_id + + def get_html_msg(self) -> dict: + now = local_now_display() + subject = self.oauth_name + _('binding reminder') + context = dict( + subject=subject, + name=self.user.name, + username=self.user.username, + ip=self.ip, + time=now, + oauth_name=self.oauth_name, + oauth_id=self.oauth_id + ) + message = render_to_string('authentication/_msg_oauth_bind.html', context) + return { + 'subject': subject, + 'message': message + } + + @classmethod + def gen_test_msg(cls): + from users.models import User + user = User.objects.first() + ip = '8.8.8.8' + return cls(user, ip, _('WeCom'), '000000') diff --git a/apps/authentication/templates/authentication/_msg_oauth_bind.html b/apps/authentication/templates/authentication/_msg_oauth_bind.html new file mode 100644 index 000000000..7a37b9218 --- /dev/null +++ b/apps/authentication/templates/authentication/_msg_oauth_bind.html @@ -0,0 +1,18 @@ +{% load i18n %} +

+ {% trans 'Hello' %} {{ name }}, +

+

+ {% trans 'Your account has just been bound to' %} {{ oauth_name }} +

+

+ {% trans 'Username' %}: {{ username }}
+ {{ oauth_name }}: {{ oauth_id }}
+ {% trans 'Time' %}: {{ time }}
+ {% trans 'IP' %}: {{ ip }} +

+ +- +

+ {% trans 'If the operation is not your own, unbind and change the password.' %} +

diff --git a/apps/authentication/views/dingtalk.py b/apps/authentication/views/dingtalk.py index 70bb9211d..b9b6f6814 100644 --- a/apps/authentication/views/dingtalk.py +++ b/apps/authentication/views/dingtalk.py @@ -19,6 +19,8 @@ from common.mixins.views import PermissionsMixin from authentication import errors from authentication.mixins import AuthMixin from common.sdk.im.dingtalk import DingTalk +from common.utils.common import get_request_ip +from authentication.notifications import OAuthBindMessage logger = get_logger(__file__) @@ -154,6 +156,8 @@ class DingTalkQRBindCallbackView(DingTalkQRMixin, View): return response raise e + ip = get_request_ip(request) + OAuthBindMessage(user, ip, _('WeCom'), user_id).publish_async() msg = _('Binding DingTalk successfully') response = self.get_success_response(redirect_url, msg, msg) return response diff --git a/apps/authentication/views/feishu.py b/apps/authentication/views/feishu.py index 597b83e46..109f01288 100644 --- a/apps/authentication/views/feishu.py +++ b/apps/authentication/views/feishu.py @@ -16,8 +16,10 @@ from common.utils.random import random_string from common.utils.django import reverse, get_object_or_none from common.mixins.views import PermissionsMixin from common.sdk.im.feishu import FeiShu, URL +from common.utils.common import get_request_ip from authentication import errors from authentication.mixins import AuthMixin +from authentication.notifications import OAuthBindMessage logger = get_logger(__file__) @@ -142,6 +144,8 @@ class FeiShuQRBindCallbackView(FeiShuQRMixin, View): return response raise e + ip = get_request_ip(request) + OAuthBindMessage(user, ip, _('WeCom'), user_id).publish_async() msg = _('Binding FeiShu successfully') response = self.get_success_response(redirect_url, msg, msg) return response diff --git a/apps/authentication/views/wecom.py b/apps/authentication/views/wecom.py index 793479ccc..3ac1df686 100644 --- a/apps/authentication/views/wecom.py +++ b/apps/authentication/views/wecom.py @@ -17,8 +17,10 @@ from common.utils.django import reverse, get_object_or_none from common.sdk.im.wecom import URL from common.sdk.im.wecom import WeCom from common.mixins.views import PermissionsMixin +from common.utils.common import get_request_ip from authentication import errors from authentication.mixins import AuthMixin +from authentication.notifications import OAuthBindMessage logger = get_logger(__file__) @@ -152,6 +154,8 @@ class WeComQRBindCallbackView(WeComQRMixin, View): return response raise e + ip = get_request_ip(request) + OAuthBindMessage(user, ip, _('WeCom'), wecom_userid).publish_async() msg = _('Binding WeCom successfully') response = self.get_success_response(redirect_url, msg, msg) return response