Skip to content

Commit

Permalink
Fix: change device activation mail template
Browse files Browse the repository at this point in the history
  • Loading branch information
Rotheem committed Jan 15, 2025
1 parent 65984ed commit b5e4097
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 57 deletions.
48 changes: 40 additions & 8 deletions app/core/myeclpay/endpoints_myeclpay.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
is_user_an_ecl_member,
is_user_in,
)
from app.utils import tools
from app.utils.communication.notifications import NotificationTool
from app.utils.mail.mailworker import send_email
from app.utils.tools import get_display_name
Expand Down Expand Up @@ -179,6 +178,7 @@ async def delete_structure(
async def init_update_structure_manager(
structure_id: UUID,
transfer_info: schemas_myeclpay.StructureTranfert,
background_tasks: BackgroundTasks,
db: AsyncSession = Depends(get_db),
user: CoreUser = Depends(is_user()),
settings: Settings = Depends(get_settings),
Expand Down Expand Up @@ -212,14 +212,43 @@ async def init_update_structure_manager(
status_code=404,
detail="User does not exist",
)
await tools.create_and_send_structure_manager_transfer_email(
email=user.email,

await cruds_myeclpay.delete_structure_manager_transfer_by_structure(
structure_id=structure_id,
db=db,
)

confirmation_token = security.generate_token()

await cruds_myeclpay.init_structure_manager_transfer(
structure_id=structure_id,
new_manager_user_id=transfer_info.new_manager_user_id,
user_id=transfer_info.new_manager_user_id,
confirmation_token=confirmation_token,
valid_until=datetime.now(tz=UTC)
+ timedelta(minutes=settings.MYECLPAY_MANAGER_TRANSFER_TOKEN_EXPIRES_MINUTES),
db=db,
settings=settings,
)

if settings.SMTP_ACTIVE:
migration_content = templates.get_template(
"structure_manager_transfer.html",
).render(
{
"transfer_link": f"{settings.CLIENT_URL}myeclpay/structures/manager/confirm-transfer?token={confirmation_token}",
},
)
background_tasks.add_task(
send_email,
recipient=user_db.email,
subject="MyECL - Confirm the structure manager transfer",
content=migration_content,
settings=settings,
)
else:
hyperion_security_logger.info(
f"You can confirm the transfer by clicking the following link: {settings.CLIENT_URL}myeclpay/structures/manager/confirm-transfer?token={confirmation_token}",
)


@router.get(
"/myeclpay/structures/confirm-manager-transfer",
Expand Down Expand Up @@ -1221,11 +1250,14 @@ async def create_user_devices(

await db.commit()

# TODO: use the correct template content
if settings.SMTP_ACTIVE:
account_exists_content = templates.get_template(
"account_exists_mail.html",
).render()
"activate_myeclpay_device.html",
).render(
{
"activation_link": f"{settings.CLIENT_URL}myeclpay/users/me/wallet/devices/activate/{activation_token}",
},
)
background_tasks.add_task(
send_email,
recipient=user.email,
Expand Down
49 changes: 0 additions & 49 deletions app/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import secrets
import unicodedata
from collections.abc import Callable, Sequence
from datetime import UTC, datetime, timedelta
from inspect import iscoroutinefunction
from pathlib import Path
from typing import TYPE_CHECKING, Any, TypeVar
from uuid import UUID

import aiofiles
import fitz
Expand All @@ -24,7 +22,6 @@
from app.core.groups import cruds_groups
from app.core.groups.groups_type import AccountType, GroupType
from app.core.models_core import CoreUser
from app.core.myeclpay import cruds_myeclpay
from app.core.users import cruds_users
from app.types import core_data
from app.types.content_type import ContentType
Expand Down Expand Up @@ -499,52 +496,6 @@ async def create_and_send_email_migration(
)


async def create_and_send_structure_manager_transfer_email(
email: str,
structure_id: UUID,
new_manager_user_id: str,
db: AsyncSession,
settings: "Settings",
) -> None:
"""
Create a structure manager transfer token, add it to the database and send an email to the user.
"""
await cruds_myeclpay.delete_structure_manager_transfer_by_structure(
structure_id=structure_id,
db=db,
)

confirmation_token = security.generate_token()

await cruds_myeclpay.init_structure_manager_transfer(
structure_id=structure_id,
user_id=new_manager_user_id,
confirmation_token=confirmation_token,
valid_until=datetime.now(tz=UTC)
+ timedelta(minutes=settings.MYECLPAY_MANAGER_TRANSFER_TOKEN_EXPIRES_MINUTES),
db=db,
)

if settings.SMTP_ACTIVE:
migration_content = templates.get_template(
"structure_manager_transfer.html",
).render(
{
"transfer_link": f"{settings.CLIENT_URL}myeclpay/structures/manager/confirm-transfer?token={confirmation_token}",
},
)
send_email(
recipient=email,
subject="MyECL - Confirm the structure manager transfer",
content=migration_content,
settings=settings,
)
else:
hyperion_security_logger.info(
f"You can confirm the transfer by clicking the following link: {settings.CLIENT_URL}myeclpay/structures/manager/confirm-transfer?token={confirmation_token}",
)


async def execute_async_or_sync_method(
job_function: Callable[..., Any],
**kwargs,
Expand Down
27 changes: 27 additions & 0 deletions assets/templates/activate_myeclpay_device.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% extends "base_mail.html" %}

{% block title %}MyECL - activate your device{% endblock %}

{% block french_message %}
<h1>Confirmation de votre nouvel accès à MyECLPay</h1>
<p>
Pour confirmer votre nouvel accès à MyECLPay, vous pouvez utiliser ce
<a href="{{ activation_link }}">lien unique</a>.

<br><br>

Si vous n'avez pas demandé cet accès, changez immédiatement votre mot de passe, votre compte a pu être compromis.
</p>
{% endblock %}

{% block english_message %}
<h1>Confirm your new MyECLPay access</h1>
<p>
To confirm your new access to MyECLPay, you can use this
<a href="{{ activation_link }}">unique link</a>.

<br><br>

If you did not request this access, change your password immediately, your account may have been compromised.
</p>
{% endblock %}

0 comments on commit b5e4097

Please sign in to comment.