2021-11-03 03:17:18 +00:00
|
|
|
|
from collections import defaultdict
|
2023-07-24 03:52:25 +00:00
|
|
|
|
from urllib.parse import urljoin
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
2021-09-16 05:28:43 +00:00
|
|
|
|
from django.conf import settings
|
2021-10-20 11:45:37 +00:00
|
|
|
|
from django.template.loader import render_to_string
|
2023-07-24 03:52:25 +00:00
|
|
|
|
from django.utils import timezone
|
|
|
|
|
from django.utils.translation import gettext as _
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
from common.utils import reverse, get_request_ip_or_data, get_request_user_agent
|
2021-08-24 06:20:54 +00:00
|
|
|
|
from notifications.notifications import UserMessage
|
|
|
|
|
|
|
|
|
|
|
2021-10-22 12:06:16 +00:00
|
|
|
|
class UserCreatedMsg(UserMessage):
|
|
|
|
|
def get_html_msg(self) -> dict:
|
|
|
|
|
user = self.user
|
|
|
|
|
|
2021-11-03 03:17:18 +00:00
|
|
|
|
mail_context = {
|
|
|
|
|
'subject': str(settings.EMAIL_CUSTOM_USER_CREATED_SUBJECT),
|
|
|
|
|
'honorific': str(settings.EMAIL_CUSTOM_USER_CREATED_HONORIFIC),
|
|
|
|
|
'content': str(settings.EMAIL_CUSTOM_USER_CREATED_BODY)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user_info = {'username': user.username, 'name': user.name, 'email': user.email}
|
|
|
|
|
# 转换成 defaultdict,否则 format 时会报 KeyError
|
|
|
|
|
user_info = defaultdict(str, **user_info)
|
|
|
|
|
mail_context = {k: v.format_map(user_info) for k, v in mail_context.items()}
|
2021-10-22 12:06:16 +00:00
|
|
|
|
|
|
|
|
|
context = {
|
2021-11-03 03:17:18 +00:00
|
|
|
|
**mail_context,
|
2021-10-26 02:52:23 +00:00
|
|
|
|
'user': user,
|
2021-10-22 12:06:16 +00:00
|
|
|
|
'rest_password_url': reverse('authentication:reset-password', external=True),
|
|
|
|
|
'rest_password_token': user.generate_reset_token(),
|
|
|
|
|
'forget_password_url': reverse('authentication:forgot-password', external=True),
|
|
|
|
|
'login_url': reverse('authentication:login', external=True),
|
|
|
|
|
}
|
|
|
|
|
message = render_to_string('users/_msg_user_created.html', context)
|
|
|
|
|
return {
|
2021-11-03 03:17:18 +00:00
|
|
|
|
'subject': mail_context['subject'],
|
2021-10-22 12:06:16 +00:00
|
|
|
|
'message': message
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def gen_test_msg(cls):
|
|
|
|
|
user = cls.get_test_user()
|
|
|
|
|
return cls(user)
|
|
|
|
|
|
|
|
|
|
|
2021-09-13 09:16:20 +00:00
|
|
|
|
class ResetPasswordMsg(UserMessage):
|
2021-09-14 12:08:33 +00:00
|
|
|
|
def __init__(self, user):
|
|
|
|
|
super().__init__(user)
|
|
|
|
|
self.reset_passwd_token = user.generate_reset_token()
|
|
|
|
|
|
2021-08-24 06:20:54 +00:00
|
|
|
|
def get_html_msg(self) -> dict:
|
|
|
|
|
user = self.user
|
|
|
|
|
subject = _('Reset password')
|
2021-10-20 11:45:37 +00:00
|
|
|
|
context = {
|
|
|
|
|
'user': user,
|
2021-08-24 06:20:54 +00:00
|
|
|
|
'rest_password_url': reverse('authentication:reset-password', external=True),
|
2021-09-14 12:08:33 +00:00
|
|
|
|
'rest_password_token': self.reset_passwd_token,
|
2021-08-24 06:20:54 +00:00
|
|
|
|
'forget_password_url': reverse('authentication:forgot-password', external=True),
|
|
|
|
|
'login_url': reverse('authentication:login', external=True),
|
|
|
|
|
}
|
2021-10-20 11:45:37 +00:00
|
|
|
|
message = render_to_string('authentication/_msg_reset_password.html', context)
|
2021-08-24 06:20:54 +00:00
|
|
|
|
return {
|
|
|
|
|
'subject': subject,
|
|
|
|
|
'message': message
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def gen_test_msg(cls):
|
|
|
|
|
from users.models import User
|
|
|
|
|
user = User.objects.first()
|
|
|
|
|
return cls(user)
|
|
|
|
|
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
2021-09-13 09:16:20 +00:00
|
|
|
|
class ResetPasswordSuccessMsg(UserMessage):
|
2021-08-24 06:20:54 +00:00
|
|
|
|
def __init__(self, user, request):
|
|
|
|
|
super().__init__(user)
|
|
|
|
|
self.ip_address = get_request_ip_or_data(request)
|
|
|
|
|
self.browser = get_request_user_agent(request)
|
|
|
|
|
|
|
|
|
|
def get_html_msg(self) -> dict:
|
|
|
|
|
user = self.user
|
|
|
|
|
|
|
|
|
|
subject = _('Reset password success')
|
2021-10-20 11:45:37 +00:00
|
|
|
|
context = {
|
2021-08-24 06:20:54 +00:00
|
|
|
|
'name': user.name,
|
|
|
|
|
'ip_address': self.ip_address,
|
|
|
|
|
'browser': self.browser,
|
|
|
|
|
}
|
2021-10-20 11:45:37 +00:00
|
|
|
|
message = render_to_string('authentication/_msg_rest_password_success.html', context)
|
2021-08-24 06:20:54 +00:00
|
|
|
|
return {
|
|
|
|
|
'subject': subject,
|
|
|
|
|
'message': message
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
@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)
|
|
|
|
|
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
2021-11-12 07:33:33 +00:00
|
|
|
|
class ResetPublicKeySuccessMsg(UserMessage):
|
|
|
|
|
def __init__(self, user, request):
|
|
|
|
|
super().__init__(user)
|
|
|
|
|
self.ip_address = get_request_ip_or_data(request)
|
|
|
|
|
self.browser = get_request_user_agent(request)
|
|
|
|
|
|
|
|
|
|
def get_html_msg(self) -> dict:
|
|
|
|
|
user = self.user
|
|
|
|
|
|
|
|
|
|
subject = _('Reset public key success')
|
|
|
|
|
context = {
|
|
|
|
|
'name': user.name,
|
|
|
|
|
'ip_address': self.ip_address,
|
|
|
|
|
'browser': self.browser,
|
|
|
|
|
}
|
|
|
|
|
message = render_to_string('authentication/_msg_rest_public_key_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)
|
|
|
|
|
|
|
|
|
|
|
2021-09-13 09:16:20 +00:00
|
|
|
|
class PasswordExpirationReminderMsg(UserMessage):
|
2021-08-24 06:20:54 +00:00
|
|
|
|
def get_html_msg(self) -> dict:
|
|
|
|
|
user = self.user
|
2021-10-20 11:45:37 +00:00
|
|
|
|
subject = _('Password is about expire')
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
date_password_expired_local = timezone.localtime(user.date_password_expired)
|
2022-05-24 03:03:12 +00:00
|
|
|
|
update_password_url = urljoin(settings.SITE_URL, '/ui/#/profile/setting/?activeTab=PasswordUpdate')
|
2021-10-20 11:45:37 +00:00
|
|
|
|
date_password_expired = date_password_expired_local.strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
|
context = {
|
2021-08-24 06:20:54 +00:00
|
|
|
|
'name': user.name,
|
2021-10-20 11:45:37 +00:00
|
|
|
|
'date_password_expired': date_password_expired,
|
2021-10-22 12:06:16 +00:00
|
|
|
|
'update_password_url': update_password_url,
|
2021-08-24 06:20:54 +00:00
|
|
|
|
'forget_password_url': reverse('authentication:forgot-password', external=True),
|
|
|
|
|
'email': user.email,
|
|
|
|
|
'login_url': reverse('authentication:login', external=True),
|
|
|
|
|
}
|
2021-10-20 11:45:37 +00:00
|
|
|
|
message = render_to_string('users/_msg_password_expire_reminder.html', context)
|
2021-08-24 06:20:54 +00:00
|
|
|
|
return {
|
|
|
|
|
'subject': subject,
|
|
|
|
|
'message': message
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def gen_test_msg(cls):
|
|
|
|
|
from users.models import User
|
|
|
|
|
user = User.objects.get(username='admin')
|
|
|
|
|
return cls(user)
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
class UserExpirationReminderMsg(UserMessage):
|
2021-08-24 06:20:54 +00:00
|
|
|
|
def get_html_msg(self) -> dict:
|
2021-10-20 11:45:37 +00:00
|
|
|
|
subject = _('Account is about expire')
|
2021-12-31 09:18:43 +00:00
|
|
|
|
date_expired_local = timezone.localtime(self.user.date_expired)
|
2021-10-20 11:45:37 +00:00
|
|
|
|
date_expired = date_expired_local.strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
|
context = {
|
|
|
|
|
'name': self.user.name,
|
|
|
|
|
'date_expired': date_expired
|
2021-08-24 06:20:54 +00:00
|
|
|
|
}
|
2021-10-20 11:45:37 +00:00
|
|
|
|
message = render_to_string('users/_msg_account_expire_reminder.html', context)
|
2021-08-24 06:20:54 +00:00
|
|
|
|
return {
|
|
|
|
|
'subject': subject,
|
|
|
|
|
'message': message
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def gen_test_msg(cls):
|
|
|
|
|
from users.models import User
|
|
|
|
|
user = User.objects.get(username='admin')
|
|
|
|
|
return cls(user)
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
class ResetSSHKeyMsg(UserMessage):
|
2021-08-24 06:20:54 +00:00
|
|
|
|
def get_html_msg(self) -> dict:
|
2021-10-20 11:45:37 +00:00
|
|
|
|
subject = _('Reset SSH Key')
|
2022-09-21 07:19:40 +00:00
|
|
|
|
update_url = urljoin(settings.SITE_URL, '/ui/#/profile/setting/?activeTab=SSHUpdate')
|
2021-10-20 11:45:37 +00:00
|
|
|
|
context = {
|
2021-08-24 06:20:54 +00:00
|
|
|
|
'name': self.user.name,
|
2021-10-22 12:06:16 +00:00
|
|
|
|
'url': update_url,
|
2021-08-24 06:20:54 +00:00
|
|
|
|
}
|
2021-10-20 11:45:37 +00:00
|
|
|
|
message = render_to_string('users/_msg_reset_ssh_key.html', context)
|
2021-08-24 06:20:54 +00:00
|
|
|
|
return {
|
|
|
|
|
'subject': subject,
|
|
|
|
|
'message': message
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def gen_test_msg(cls):
|
|
|
|
|
from users.models import User
|
|
|
|
|
user = User.objects.get(username='admin')
|
|
|
|
|
return cls(user)
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
class ResetMFAMsg(UserMessage):
|
2021-08-24 06:20:54 +00:00
|
|
|
|
def get_html_msg(self) -> dict:
|
2021-10-20 11:45:37 +00:00
|
|
|
|
subject = _('Reset MFA')
|
|
|
|
|
context = {
|
2021-08-24 06:20:54 +00:00
|
|
|
|
'name': self.user.name,
|
2021-10-22 12:06:16 +00:00
|
|
|
|
'url': reverse('authentication:user-otp-enable-start', external=True),
|
2021-08-24 06:20:54 +00:00
|
|
|
|
}
|
2021-10-20 11:45:37 +00:00
|
|
|
|
message = render_to_string('users/_msg_reset_mfa.html', context)
|
2021-08-24 06:20:54 +00:00
|
|
|
|
return {
|
|
|
|
|
'subject': subject,
|
|
|
|
|
'message': message
|
|
|
|
|
}
|
2021-10-20 11:45:37 +00:00
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def gen_test_msg(cls):
|
|
|
|
|
from users.models import User
|
|
|
|
|
user = User.objects.get(username='admin')
|
|
|
|
|
return cls(user)
|