from datetime import datetime
from urllib.parse import urljoin
from django.utils.translation import ugettext as _
from django.conf import settings
from common.utils import reverse, get_request_ip_or_data, get_request_user_agent, lazyproperty
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:
Please click the link below to reset your password, if not your request, concern your account security
Click here reset password
This link is valid for 1 hour. After it expires, request new one
---
Login direct
""") % {
'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
}
class ResetPasswordSuccessMsg(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_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:
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
Browser: %(browser)s
""") % {
'name': user.name,
'ip_address': self.ip_address,
'browser': self.browser,
}
return {
'subject': subject,
'message': message
}
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 = _('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
If your password has expired, please click
Password expired
to apply for a password reset email.
---
Login direct
""") % {
'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
}
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:
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
}
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:
Your ssh public key has been reset by site administrator.
Please login and reset your ssh public key.
Login direct
""") % {
'name': self.user.name,
'login_url': reverse('authentication:login', external=True),
}
return {
'subject': subject,
'message': message
}
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:
Your MFA has been reset by site administrator.
Please login and reset your MFA.
Login direct
""") % {
'name': self.user.name,
'login_url': reverse('authentication:login', external=True),
}
return {
'subject': subject,
'message': message
}