File size: 1,571 Bytes
fa152ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Security utilities module.
"""

from fastapi import HTTPException
from jinja2 import Environment, FileSystemLoader, select_autoescape

from cbh.api.account.dto import RegistrationType
from cbh.api.account.models import AccountModel
from cbh.core.config import settings


def check_account_to_reset(account_obj: AccountModel, account: AccountModel | None = None) -> None:
    """
    Check if the account can be reset.
    """
    if not account and account_obj.registrationType != RegistrationType.ORGANIC:
        raise HTTPException(
            status_code=422,
            detail="Please sign in with social providers",
        )
    elif account and account_obj.registrationType != RegistrationType.ORGANIC:
        raise HTTPException(
            status_code=422,
            detail="Password reset is not available for social login accounts",
        )


async def send_password_reset_email(
    code: str, account_obj: AccountModel
) -> None:
    """
    Send a password reset email.
    """
    templates_path = settings.BASE_DIR / "cbh" / "templates" / "emails"
    env = Environment(
        loader=FileSystemLoader(templates_path),
        autoescape=select_autoescape(["html", "xml"]),
    )
    template = env.get_template("resetPassword.html")

    template_content = template.render(
        link=f"{settings.Audience}/change-password?code={code}",
        audience_link=settings.Audience,
    )
    await settings.EMAIL_CLIENT.send_email(
        account_obj.email,
        "You requested a password reset in Arena",
        template_content,
    )