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 diff --git a/pycroft/lib/user/mail_confirmation.py b/pycroft/lib/user/mail_confirmation.py index d5a6017e4..852d800b8 100644 --- a/pycroft/lib/user/mail_confirmation.py +++ b/pycroft/lib/user/mail_confirmation.py @@ -9,7 +9,7 @@ ) from .member_request import finish_member_request -from ._old import user_send_mail +from .mail import user_send_mail @with_transaction diff --git a/pycroft/lib/user/member_request.py b/pycroft/lib/user/member_request.py index fb7745425..2baab4cc5 100644 --- a/pycroft/lib/user/member_request.py +++ b/pycroft/lib/user/member_request.py @@ -23,9 +23,6 @@ RoomHistoryEntry, ) -from ._old import ( - user_send_mail, -) from .edit import ( edit_birthdate, edit_name, @@ -48,6 +45,7 @@ ) from .mail import ( send_confirmation_email, + user_send_mail, ) from .user_id import ( check_user_id,