diff --git a/apps/acls/notifications.py b/apps/acls/notifications.py index ec4d99cd1..5d3c59e13 100644 --- a/apps/acls/notifications.py +++ b/apps/acls/notifications.py @@ -1,6 +1,7 @@ from django.template.loader import render_to_string from django.utils.translation import gettext_lazy as _ +from assets.models import Asset from audits.models import UserLoginLog from notifications.notifications import UserMessage from users.models import User @@ -35,3 +36,31 @@ class UserLoginReminderMsg(UserMessage): user = User.objects.first() user_log = UserLoginLog.objects.first() return cls(user, user_log) + + +class AssetLoginReminderMsg(UserMessage): + subject = _('Asset login reminder') + + def __init__(self, user, asset: Asset, login_user: User): + self.asset = asset + self.login_user = login_user + super().__init__(user) + + def get_html_msg(self) -> dict: + context = { + 'recipient': self.user.username, + 'username': self.login_user.username, + 'asset': str(self.asset), + } + message = render_to_string('acls/asset_login_reminder.html', context) + + return { + 'subject': str(self.subject), + 'message': message + } + + @classmethod + def gen_test_msg(cls): + user = User.objects.first() + asset = Asset.objects.first() + return cls(user, asset, user) diff --git a/apps/acls/templates/acls/asset_login_reminder.html b/apps/acls/templates/acls/asset_login_reminder.html new file mode 100644 index 000000000..9a30cda49 --- /dev/null +++ b/apps/acls/templates/acls/asset_login_reminder.html @@ -0,0 +1,12 @@ +{% load i18n %} + +
{% trans 'Username' %}: [{{ username }}]
+{% trans 'Assets' %}: [{{ asset }}]
+{% trans 'The user has just successfully logged into the asset. Please ensure that this is an authorized operation. If you suspect that this is an unauthorized access, please take appropriate measures immediately.' %}
+ +{% trans 'Thank you' %}!
+ diff --git a/apps/authentication/api/connection_token.py b/apps/authentication/api/connection_token.py index 2c1b0d0f1..5b9481da0 100644 --- a/apps/authentication/api/connection_token.py +++ b/apps/authentication/api/connection_token.py @@ -15,6 +15,7 @@ from rest_framework.request import Request from rest_framework.response import Response from accounts.const import AliasAccount +from acls.notifications import AssetLoginReminderMsg from common.api import JMSModelViewSet from common.exceptions import JMSException from common.utils import random_string, get_logger, get_request_ip @@ -409,6 +410,10 @@ class ConnectionTokenViewSet(ExtraActionApiMixin, RootOrgViewMixin, JMSModelView assignees=acl.reviewers.all(), org_id=asset.org_id ) return ticket + if acl.is_action(acl.ActionChoices.notice): + reviewers = acl.reviewers.all() + for reviewer in reviewers: + AssetLoginReminderMsg(reviewer, asset, user).publish_async() class SuperConnectionTokenViewSet(ConnectionTokenViewSet):