mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-05 17:30:30 +00:00
perf: 优化消息通知 (#7024)
* perf: 优化系统用户列表 * stash * perf: 优化消息通知 * perf: 修改钉钉 * perf: 修改优化消息通知 * perf: 修改requirements * perf: 优化datetime Co-authored-by: ibuler <ibuler@qq.com>
This commit is contained in:
@@ -1,84 +1,42 @@
|
||||
from datetime import datetime
|
||||
from urllib.parse import urljoin
|
||||
|
||||
from django.utils import timezone
|
||||
from django.utils.translation import ugettext as _
|
||||
from django.conf import settings
|
||||
from django.template.loader import render_to_string
|
||||
|
||||
from common.utils import reverse, get_request_ip_or_data, get_request_user_agent, lazyproperty
|
||||
from common.utils import reverse, get_request_ip_or_data, get_request_user_agent
|
||||
from notifications.notifications import UserMessage
|
||||
|
||||
|
||||
class ResetPasswordMsg(UserMessage):
|
||||
|
||||
def __init__(self, user):
|
||||
super().__init__(user)
|
||||
self.reset_passwd_token = user.generate_reset_token()
|
||||
|
||||
def get_text_msg(self) -> dict:
|
||||
user = self.user
|
||||
subject = _('Reset password')
|
||||
message = _("""
|
||||
Hello %(name)s:
|
||||
Please click the link below to reset your password, if not your request, concern your account security
|
||||
|
||||
Click here reset password 👇
|
||||
%(rest_password_url)s?token=%(rest_password_token)s
|
||||
|
||||
This link is valid for 1 hour. After it expires,
|
||||
|
||||
request new one 👇
|
||||
%(forget_password_url)s?email=%(email)s
|
||||
|
||||
-------------------
|
||||
|
||||
Login direct 👇
|
||||
%(login_url)s
|
||||
|
||||
""") % {
|
||||
'name': user.name,
|
||||
'rest_password_url': reverse('authentication:reset-password', external=True),
|
||||
'rest_password_token': self.reset_passwd_token,
|
||||
'forget_password_url': reverse('authentication:forgot-password', external=True),
|
||||
'email': user.email,
|
||||
'login_url': reverse('authentication:login', external=True),
|
||||
}
|
||||
return {
|
||||
'subject': subject,
|
||||
'message': message
|
||||
}
|
||||
|
||||
def get_html_msg(self) -> dict:
|
||||
user = self.user
|
||||
subject = _('Reset password')
|
||||
message = _("""
|
||||
Hello %(name)s:
|
||||
<br>
|
||||
Please click the link below to reset your password, if not your request, concern your account security
|
||||
<br>
|
||||
<a href="%(rest_password_url)s?token=%(rest_password_token)s">Click here reset password</a>
|
||||
<br>
|
||||
This link is valid for 1 hour. After it expires, <a href="%(forget_password_url)s?email=%(email)s">request new one</a>
|
||||
|
||||
<br>
|
||||
---
|
||||
|
||||
<br>
|
||||
<a href="%(login_url)s">Login direct</a>
|
||||
|
||||
<br>
|
||||
""") % {
|
||||
'name': user.name,
|
||||
context = {
|
||||
'user': user,
|
||||
'rest_password_url': reverse('authentication:reset-password', external=True),
|
||||
'rest_password_token': self.reset_passwd_token,
|
||||
'forget_password_url': reverse('authentication:forgot-password', external=True),
|
||||
'email': user.email,
|
||||
'login_url': reverse('authentication:login', external=True),
|
||||
}
|
||||
message = render_to_string('authentication/_msg_reset_password.html', context)
|
||||
return {
|
||||
'subject': subject,
|
||||
'message': message
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def gen_test_msg(cls):
|
||||
from users.models import User
|
||||
user = User.objects.first()
|
||||
return cls(user)
|
||||
|
||||
|
||||
class ResetPasswordSuccessMsg(UserMessage):
|
||||
def __init__(self, user, request):
|
||||
@@ -86,280 +44,119 @@ class ResetPasswordSuccessMsg(UserMessage):
|
||||
self.ip_address = get_request_ip_or_data(request)
|
||||
self.browser = get_request_user_agent(request)
|
||||
|
||||
def get_text_msg(self) -> dict:
|
||||
user = self.user
|
||||
|
||||
subject = _('Reset password success')
|
||||
message = _("""
|
||||
|
||||
Hi %(name)s:
|
||||
|
||||
Your JumpServer password has just been successfully updated.
|
||||
|
||||
If the password update was not initiated by you, your account may have security issues.
|
||||
It is recommended that you log on to the JumpServer immediately and change your password.
|
||||
|
||||
If you have any questions, you can contact the administrator.
|
||||
|
||||
-------------------
|
||||
|
||||
|
||||
IP Address: %(ip_address)s
|
||||
\n
|
||||
Browser: %(browser)s
|
||||
|
||||
""") % {
|
||||
'name': user.name,
|
||||
'ip_address': self.ip_address,
|
||||
'browser': self.browser,
|
||||
}
|
||||
return {
|
||||
'subject': subject,
|
||||
'message': message
|
||||
}
|
||||
|
||||
def get_html_msg(self) -> dict:
|
||||
user = self.user
|
||||
|
||||
subject = _('Reset password success')
|
||||
message = _("""
|
||||
|
||||
Hi %(name)s:
|
||||
<br>
|
||||
|
||||
|
||||
<br>
|
||||
Your JumpServer password has just been successfully updated.
|
||||
<br>
|
||||
|
||||
<br>
|
||||
If the password update was not initiated by you, your account may have security issues.
|
||||
It is recommended that you log on to the JumpServer immediately and change your password.
|
||||
<br>
|
||||
|
||||
<br>
|
||||
If you have any questions, you can contact the administrator.
|
||||
<br>
|
||||
<br>
|
||||
---
|
||||
<br>
|
||||
<br>
|
||||
IP Address: %(ip_address)s
|
||||
<br>
|
||||
<br>
|
||||
Browser: %(browser)s
|
||||
<br>
|
||||
|
||||
""") % {
|
||||
context = {
|
||||
'name': user.name,
|
||||
'ip_address': self.ip_address,
|
||||
'browser': self.browser,
|
||||
}
|
||||
message = render_to_string('authentication/_msg_rest_password_success.html', context)
|
||||
return {
|
||||
'subject': subject,
|
||||
'message': message
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def gen_test_msg(cls):
|
||||
from users.models import User
|
||||
from rest_framework.test import APIRequestFactory
|
||||
from rest_framework.request import Request
|
||||
factory = APIRequestFactory()
|
||||
request = Request(factory.get('/notes/'))
|
||||
user = User.objects.first()
|
||||
return cls(user, request)
|
||||
|
||||
|
||||
class PasswordExpirationReminderMsg(UserMessage):
|
||||
update_password_url = urljoin(settings.SITE_URL, '/ui/#/users/profile/?activeTab=PasswordUpdate')
|
||||
|
||||
def get_text_msg(self) -> dict:
|
||||
user = self.user
|
||||
|
||||
subject = _('Security notice')
|
||||
message = _("""
|
||||
Hello %(name)s:
|
||||
|
||||
Your password will expire in %(date_password_expired)s,
|
||||
|
||||
For your account security, please click on the link below to update your password in time
|
||||
|
||||
Click here update password 👇
|
||||
%(update_password_url)s
|
||||
|
||||
If your password has expired, please click 👇 to apply for a password reset email.
|
||||
%(forget_password_url)s?email=%(email)s
|
||||
|
||||
-------------------
|
||||
|
||||
Login direct 👇
|
||||
%(login_url)s
|
||||
|
||||
""") % {
|
||||
'name': user.name,
|
||||
'date_password_expired': datetime.fromtimestamp(datetime.timestamp(
|
||||
user.date_password_expired)).strftime('%Y-%m-%d %H:%M'),
|
||||
'update_password_url': self.update_password_url,
|
||||
'forget_password_url': reverse('authentication:forgot-password', external=True),
|
||||
'email': user.email,
|
||||
'login_url': reverse('authentication:login', external=True),
|
||||
}
|
||||
return {
|
||||
'subject': subject,
|
||||
'message': message
|
||||
}
|
||||
|
||||
def get_html_msg(self) -> dict:
|
||||
user = self.user
|
||||
subject = _('Password is about expire')
|
||||
|
||||
subject = _('Security notice')
|
||||
message = _("""
|
||||
Hello %(name)s:
|
||||
<br>
|
||||
Your password will expire in %(date_password_expired)s,
|
||||
<br>
|
||||
For your account security, please click on the link below to update your password in time
|
||||
<br>
|
||||
<a href="%(update_password_url)s">Click here update password</a>
|
||||
<br>
|
||||
If your password has expired, please click
|
||||
<a href="%(forget_password_url)s?email=%(email)s">Password expired</a>
|
||||
to apply for a password reset email.
|
||||
|
||||
<br>
|
||||
---
|
||||
|
||||
<br>
|
||||
<a href="%(login_url)s">Login direct</a>
|
||||
|
||||
<br>
|
||||
""") % {
|
||||
date_password_expired_local = timezone.localtime(user.date_password_expired)
|
||||
date_password_expired = date_password_expired_local.strftime('%Y-%m-%d %H:%M:%S')
|
||||
context = {
|
||||
'name': user.name,
|
||||
'date_password_expired': datetime.fromtimestamp(datetime.timestamp(
|
||||
user.date_password_expired)).strftime('%Y-%m-%d %H:%M'),
|
||||
'date_password_expired': date_password_expired,
|
||||
'update_password_url': self.update_password_url,
|
||||
'forget_password_url': reverse('authentication:forgot-password', external=True),
|
||||
'email': user.email,
|
||||
'login_url': reverse('authentication:login', external=True),
|
||||
}
|
||||
message = render_to_string('users/_msg_password_expire_reminder.html', context)
|
||||
return {
|
||||
'subject': subject,
|
||||
'message': message
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def gen_test_msg(cls):
|
||||
from users.models import User
|
||||
user = User.objects.get(username='admin')
|
||||
return cls(user)
|
||||
|
||||
|
||||
class UserExpirationReminderMsg(UserMessage):
|
||||
def get_text_msg(self) -> dict:
|
||||
subject = _('Expiration notice')
|
||||
message = _("""
|
||||
Hello %(name)s:
|
||||
|
||||
Your account will expire in %(date_expired)s,
|
||||
|
||||
In order not to affect your normal work, please contact the administrator for confirmation.
|
||||
|
||||
""") % {
|
||||
'name': self.user.name,
|
||||
'date_expired': datetime.fromtimestamp(datetime.timestamp(
|
||||
self.user.date_expired)).strftime('%Y-%m-%d %H:%M'),
|
||||
}
|
||||
return {
|
||||
'subject': subject,
|
||||
'message': message
|
||||
}
|
||||
|
||||
def get_html_msg(self) -> dict:
|
||||
subject = _('Expiration notice')
|
||||
message = _("""
|
||||
Hello %(name)s:
|
||||
<br>
|
||||
Your account will expire in %(date_expired)s,
|
||||
<br>
|
||||
In order not to affect your normal work, please contact the administrator for confirmation.
|
||||
<br>
|
||||
""") % {
|
||||
'name': self.user.name,
|
||||
'date_expired': datetime.fromtimestamp(datetime.timestamp(
|
||||
self.user.date_expired)).strftime('%Y-%m-%d %H:%M'),
|
||||
subject = _('Account is about expire')
|
||||
date_expired_local = timezone.localtime(self.user.date_password_expired)
|
||||
date_expired = date_expired_local.strftime('%Y-%m-%d %H:%M:%S')
|
||||
context = {
|
||||
'name': self.user.name,
|
||||
'date_expired': date_expired
|
||||
}
|
||||
message = render_to_string('users/_msg_account_expire_reminder.html', context)
|
||||
return {
|
||||
'subject': subject,
|
||||
'message': message
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def gen_test_msg(cls):
|
||||
from users.models import User
|
||||
user = User.objects.get(username='admin')
|
||||
return cls(user)
|
||||
|
||||
|
||||
class ResetSSHKeyMsg(UserMessage):
|
||||
def get_text_msg(self) -> dict:
|
||||
subject = _('SSH Key Reset')
|
||||
message = _("""
|
||||
Hello %(name)s:
|
||||
|
||||
Your ssh public key has been reset by site administrator.
|
||||
Please login and reset your ssh public key.
|
||||
|
||||
Login direct 👇
|
||||
%(login_url)s
|
||||
|
||||
""") % {
|
||||
'name': self.user.name,
|
||||
'login_url': reverse('authentication:login', external=True),
|
||||
}
|
||||
|
||||
return {
|
||||
'subject': subject,
|
||||
'message': message
|
||||
}
|
||||
|
||||
def get_html_msg(self) -> dict:
|
||||
subject = _('SSH Key Reset')
|
||||
message = _("""
|
||||
Hello %(name)s:
|
||||
<br>
|
||||
Your ssh public key has been reset by site administrator.
|
||||
Please login and reset your ssh public key.
|
||||
<br>
|
||||
<a href="%(login_url)s">Login direct</a>
|
||||
|
||||
<br>
|
||||
""") % {
|
||||
subject = _('Reset SSH Key')
|
||||
context = {
|
||||
'name': self.user.name,
|
||||
'login_url': reverse('authentication:login', external=True),
|
||||
}
|
||||
|
||||
message = render_to_string('users/_msg_reset_ssh_key.html', context)
|
||||
return {
|
||||
'subject': subject,
|
||||
'message': message
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def gen_test_msg(cls):
|
||||
from users.models import User
|
||||
user = User.objects.get(username='admin')
|
||||
return cls(user)
|
||||
|
||||
|
||||
class ResetMFAMsg(UserMessage):
|
||||
def get_text_msg(self) -> dict:
|
||||
subject = _('MFA Reset')
|
||||
message = _("""
|
||||
Hello %(name)s:
|
||||
|
||||
Your MFA has been reset by site administrator.
|
||||
Please login and reset your MFA.
|
||||
|
||||
Login direct 👇
|
||||
%(login_url)s
|
||||
|
||||
""") % {
|
||||
'name': self.user.name,
|
||||
'login_url': reverse('authentication:login', external=True),
|
||||
}
|
||||
return {
|
||||
'subject': subject,
|
||||
'message': message
|
||||
}
|
||||
|
||||
def get_html_msg(self) -> dict:
|
||||
subject = _('MFA Reset')
|
||||
message = _("""
|
||||
Hello %(name)s:
|
||||
<br>
|
||||
Your MFA has been reset by site administrator.
|
||||
Please login and reset your MFA.
|
||||
<br>
|
||||
<a href="%(login_url)s">Login direct</a>
|
||||
|
||||
<br>
|
||||
""") % {
|
||||
subject = _('Reset MFA')
|
||||
context = {
|
||||
'name': self.user.name,
|
||||
'login_url': reverse('authentication:login', external=True),
|
||||
}
|
||||
message = render_to_string('users/_msg_reset_mfa.html', context)
|
||||
return {
|
||||
'subject': subject,
|
||||
'message': message
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def gen_test_msg(cls):
|
||||
from users.models import User
|
||||
user = User.objects.get(username='admin')
|
||||
return cls(user)
|
||||
|
Reference in New Issue
Block a user