Skip to content

Commit

Permalink
migrate send_password_reset_mail to .mail and delete _old
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjuhrich committed Sep 20, 2024
1 parent 325a20e commit 88354de
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 48 deletions.
4 changes: 1 addition & 3 deletions pycroft/lib/user/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from ._old import (
send_password_reset_mail,
)
from .user_id import (
encode_type1_user_id,
decode_type1_user_id,
Expand Down Expand Up @@ -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,
Expand Down
45 changes: 0 additions & 45 deletions pycroft/lib/user/_old.py

This file was deleted.

23 changes: 23 additions & 0 deletions pycroft/lib/user/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
MailTemplate,
Mail,
UserConfirmEmailTemplate,
UserResetPasswordTemplate,
MemberRequestMergedTemplate,
)
from pycroft.model import session
Expand All @@ -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:
Expand Down Expand Up @@ -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

0 comments on commit 88354de

Please sign in to comment.