From 88354def7db9b56dd1912925950472c9cbe4af0c Mon Sep 17 00:00:00 2001 From: Lukas Juhrich Date: Fri, 20 Sep 2024 12:57:06 +0200 Subject: [PATCH] migrate `send_password_reset_mail` to `.mail` and delete `_old` --- pycroft/lib/user/__init__.py | 4 +--- pycroft/lib/user/_old.py | 45 ------------------------------------ pycroft/lib/user/mail.py | 23 ++++++++++++++++++ 3 files changed, 24 insertions(+), 48 deletions(-) delete mode 100644 pycroft/lib/user/_old.py diff --git a/pycroft/lib/user/__init__.py b/pycroft/lib/user/__init__.py index f202f58cf..e6899742d 100644 --- a/pycroft/lib/user/__init__.py +++ b/pycroft/lib/user/__init__.py @@ -1,6 +1,3 @@ -from ._old import ( - send_password_reset_mail, -) from .user_id import ( encode_type1_user_id, decode_type1_user_id, @@ -70,6 +67,7 @@ group_send_mail, send_member_request_merged_email, send_confirmation_email, + send_password_reset_mail, ) from .mail_confirmation import ( confirm_mail_address, diff --git a/pycroft/lib/user/_old.py b/pycroft/lib/user/_old.py deleted file mode 100644 index 2f26df4d5..000000000 --- a/pycroft/lib/user/_old.py +++ /dev/null @@ -1,45 +0,0 @@ -# Copyright (c) 2015 The Pycroft Authors. See the AUTHORS file. -# This file is part of the Pycroft project and licensed under the terms of -# the Apache License, Version 2.0. See the LICENSE file for details. -""" -pycroft.lib.user -~~~~~~~~~~~~~~~~ - -This module contains. - -:copyright: (c) 2012 by AG DSN. -""" -import os - - -from pycroft.helpers.user import generate_random_str -from pycroft.lib.mail import ( - UserResetPasswordTemplate, -) -from pycroft.model.session import with_transaction -from pycroft.model.user import ( - User, -) - -from .mail import user_send_mail - - -password_reset_url = os.getenv('PASSWORD_RESET_URL') - - -@with_transaction -def send_password_reset_mail(user: User) -> bool: - user.password_reset_token = generate_random_str(64) - - if not password_reset_url: - raise ValueError("No url specified in PASSWORD_RESET_URL") - - try: - user_send_mail(user, UserResetPasswordTemplate( - password_reset_url=password_reset_url.format(user.password_reset_token)), - use_internal=False) - except ValueError: - user.password_reset_token = None - return False - - return True diff --git a/pycroft/lib/user/mail.py b/pycroft/lib/user/mail.py index d166dc5c9..f07dae99d 100644 --- a/pycroft/lib/user/mail.py +++ b/pycroft/lib/user/mail.py @@ -9,6 +9,7 @@ MailTemplate, Mail, UserConfirmEmailTemplate, + UserResetPasswordTemplate, MemberRequestMergedTemplate, ) from pycroft.model import session @@ -27,6 +28,7 @@ ) mail_confirm_url = os.getenv("MAIL_CONFIRM_URL") +password_reset_url = os.getenv("PASSWORD_RESET_URL") def format_user_mail(user: User, text: str) -> str: @@ -159,3 +161,24 @@ def send_confirmation_email(user: BaseUser) -> None: email_confirm_url=mail_confirm_url.format(user.email_confirmation_key) ), ) + + +def send_password_reset_mail(user: User) -> bool: + user.password_reset_token = generate_random_str(64) + + if not password_reset_url: + raise ValueError("No url specified in PASSWORD_RESET_URL") + + try: + user_send_mail( + user, + UserResetPasswordTemplate( + password_reset_url=password_reset_url.format(user.password_reset_token) + ), + use_internal=False, + ) + except ValueError: + user.password_reset_token = None + return False + + return True