From f49e4f751960ee898354a79b0d16c48c2033cbe2 Mon Sep 17 00:00:00 2001 From: r350178982 <32759763+r350178982@users.noreply.github.com> Date: Tue, 20 Aug 2024 15:31:12 +0800 Subject: [PATCH] update --- seahub/api2/endpoints/user.py | 10 ++++--- seahub/api2/utils.py | 27 ++++++++----------- seahub/auth/utils.py | 22 +++++++++++++-- seahub/auth/views.py | 20 ++++---------- .../templates/registration/login_email.html | 6 ++++- .../registration/password_change_email.html | 14 +++++++--- 6 files changed, 58 insertions(+), 41 deletions(-) diff --git a/seahub/api2/endpoints/user.py b/seahub/api2/endpoints/user.py index c9160b052f..9fb99e70be 100644 --- a/seahub/api2/endpoints/user.py +++ b/seahub/api2/endpoints/user.py @@ -16,7 +16,7 @@ from seahub.organizations.models import OrgSettings from seahub.organizations.settings import ORG_AUTO_URL_PREFIX from seahub.organizations.views import gen_org_url_prefix from seahub.password_session import update_session_auth_hash -from seahub.utils import is_valid_email, send_html_email, get_site_name +from seahub.utils import is_valid_email, send_html_email, get_site_name, IS_EMAIL_CONFIGURED from seahub.api2.authentication import TokenAuthentication from seahub.api2.throttling import UserRateThrottle from seahub.api2.utils import api_error @@ -253,15 +253,17 @@ class ResetPasswordView(APIView): user.set_password(new_password) user.save() enable_pwd_email = bool(UserOptions.objects.get_password_update_email_enable_status(user.username)) - if enable_pwd_email: + + if IS_EMAIL_CONFIGURED and enable_pwd_email: email_template_name = 'registration/password_change_email.html' send_to = email2contact_email(request.user.username) site_name = get_site_name() c = { - 'email': send_to + 'email': send_to, + 'name': email2nickname(user.username) } try: - send_html_email(_("Successfully Changed Password on %s") % site_name, + send_html_email(_("[%s]Your Password Has Been Successfully Updated") % site_name, email_template_name, c, None, [send_to]) except Exception as e: diff --git a/seahub/api2/utils.py b/seahub/api2/utils.py index 1701950ccc..f16bc1dc1f 100644 --- a/seahub/api2/utils.py +++ b/seahub/api2/utils.py @@ -22,6 +22,7 @@ from seaserv import seafile_api, ccnet_api, \ get_group, seafserv_threaded_rpc from pysearpc import SearpcError +from seahub.auth.utils import send_login_email from seahub.base.templatetags.seahub_tags import email2nickname, \ translate_seahub_time, file_icon_filter, email2contact_email from seahub.constants import REPO_TYPE_WIKI @@ -30,7 +31,7 @@ from seahub.group.utils import is_group_member from seahub.api2.models import Token, TokenV2, DESKTOP_PLATFORMS from seahub.avatar.settings import AVATAR_DEFAULT_SIZE from seahub.avatar.templatetags.avatar_tags import api_avatar_url -from seahub.utils import get_user_repos, send_html_email, get_site_name +from seahub.utils import get_user_repos, send_html_email, get_site_name, IS_EMAIL_CONFIGURED from seahub.utils.mail import send_html_email_with_dj_template from django.utils.translation import gettext as _ import seahub.settings as settings @@ -193,6 +194,7 @@ def get_token_v1(username): return token _ANDROID_DEVICE_ID_PATTERN = re.compile('^[a-f0-9]{1,16}$') + def get_token_v2(request, username, platform, device_id, device_name, client_version, platform_version): @@ -213,23 +215,16 @@ def get_token_v2(request, username, platform, device_id, device_name, else: raise serializers.ValidationError('invalid platform') enable_new_device_email = bool(UserOptions.objects.get_login_email_enable_status(username)) - if not TokenV2.objects.filter(user=username, device_id=device_id).first() and enable_new_device_email: - email_template_name='registration/login_email.html' - send_to = email2contact_email(username) - site_name = get_site_name() - c = { - 'name': email2nickname(username) - } - try: - send_html_email(_("Welcome to %s") % site_name, - email_template_name, c, None, - [send_to]) - except Exception as e: - logger.error('Failed to send notification to %s' % send_to) - - return TokenV2.objects.get_or_create_token( + + has_device = TokenV2.objects.filter(user=username, device_id=device_id, platform=platform).first() + token = TokenV2.objects.get_or_create_token( username, platform, device_id, device_name, client_version, platform_version, get_client_ip(request)) + + if IS_EMAIL_CONFIGURED and enable_new_device_email and (not has_device): + send_login_email(username) + + return token def get_api_token(request, keys=None, key_prefix='shib_'): diff --git a/seahub/auth/utils.py b/seahub/auth/utils.py index 893e8ed425..6bd101fb7f 100644 --- a/seahub/auth/utils.py +++ b/seahub/auth/utils.py @@ -1,13 +1,15 @@ # Copyright (c) 2012-2016 Seafile Ltd. +import logging from django.core.cache import cache from django.conf import settings +from django.utils.translation import gettext as _ from seahub.profile.models import Profile -from seahub.utils import normalize_cache_key +from seahub.utils import normalize_cache_key, get_site_name, send_html_email from seahub.utils.ip import get_remote_ip LOGIN_ATTEMPT_PREFIX = 'UserLoginAttempt_' - +logger = logging.getLogger(__name__) def get_login_failed_attempts(username=None, ip=None): """Get login failed attempts base on username and ip. @@ -84,3 +86,19 @@ def get_virtual_id_by_email(email): return email else: return p.user + + +def send_login_email(username): + from seahub.base.templatetags.seahub_tags import email2contact_email, email2nickname + email_template_name = 'registration/login_email.html' + send_to = email2contact_email(username) + site_name = get_site_name() + c = { + 'name': email2nickname(username) + } + try: + send_html_email(_("Welcome to %s") % site_name, + email_template_name, c, None, + [send_to]) + except Exception as e: + logger.error('Failed to send notification to %s, %s' % (send_to, e)) diff --git a/seahub/auth/views.py b/seahub/auth/views.py index 8901dc55af..1583faabf9 100644 --- a/seahub/auth/views.py +++ b/seahub/auth/views.py @@ -30,12 +30,12 @@ from seahub.auth.signals import user_logged_in_failed from seahub.auth.tokens import default_token_generator from seahub.auth.utils import ( get_login_failed_attempts, incr_login_failed_attempts, - clear_login_failed_attempts) + clear_login_failed_attempts, send_login_email) from seahub.base.accounts import User, UNUSABLE_PASSWORD from seahub.options.models import UserOptions from seahub.profile.models import Profile from seahub.two_factor.views.login import is_device_remembered -from seahub.utils import render_error, get_site_name, is_valid_email, get_service_url +from seahub.utils import render_error, get_site_name, is_valid_email, get_service_url, IS_EMAIL_CONFIGURED from seahub.utils.http import rate_limit from seahub.utils.ip import get_remote_ip from seahub.utils.file_size import get_quota_from_string @@ -79,19 +79,9 @@ def log_user_in(request, user, redirect_to): auth_login(request, user) enable_login_email = bool(UserOptions.objects.get_login_email_enable_status(user.username)) already_login_users = request.session.get(SESSION_USERS_LOGIN, []) - if user.username not in already_login_users and enable_login_email: - email_template_name = 'registration/login_email.html' - send_to = email2contact_email(request.user.username) - site_name = get_site_name() - c = { - 'name': email2nickname(user.username) - } - try: - send_html_email(_("Welcome to %s") % site_name, - email_template_name, c, None, - [send_to]) - except Exception as e: - logger.error(e) + + if IS_EMAIL_CONFIGURED and (user.username not in already_login_users) and enable_login_email: + send_login_email(user.username) return HttpResponseRedirect(redirect_to) diff --git a/seahub/templates/registration/login_email.html b/seahub/templates/registration/login_email.html index b0dcbb6443..6117cda989 100644 --- a/seahub/templates/registration/login_email.html +++ b/seahub/templates/registration/login_email.html @@ -10,10 +10,14 @@ {% blocktrans with account=name %}Dear, {{ account }}{% endblocktrans %}
+{% blocktrans %} Greetings! +{% endblocktrans %}
-{% trans " We are thrilled to inform you that you have successfully joined Seafile - a cloud-based platform dedicated to file management and team collaboration. Here, you'll find seamless and secure digital workspaces for document sharing, version control, and teamwork." %} +{% blocktrans %} + We are thrilled to inform you that you have successfully joined {{ site_name }} - a cloud-based platform dedicated to file management and team collaboration. Here, you'll find seamless and secure digital workspaces for document sharing, version control, and teamwork. +{% endblocktrans %}
{% endautoescape %} diff --git a/seahub/templates/registration/password_change_email.html b/seahub/templates/registration/password_change_email.html index 00f17daa07..8598677090 100644 --- a/seahub/templates/registration/password_change_email.html +++ b/seahub/templates/registration/password_change_email.html @@ -4,14 +4,22 @@ {% block email_con %} -{% trans "Hi," %}
+{% autoescape off %}-{% blocktrans with account=email %}Your password was changed. {% endblocktrans %} +{% blocktrans with account=name %}Dear, {{ account }}{% endblocktrans %}
-{{ email }} +{% blocktrans %} +Greetings! +{% endblocktrans %} +
++{% blocktrans %} + We are pleased to inform you that your password for {{ site_name }} has been successfully updated. To ensure the security of your account, we recommend regularly changing your password and avoiding simple or personally identifiable combinations. +{% endblocktrans %}
+{% endautoescape %} {% endblock %}