2022-08-12 13:17:21 +02:00
|
|
|
from newrelic import agent
|
2022-06-20 14:34:20 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
from app.db import Session
|
2024-06-26 12:17:24 +02:00
|
|
|
from app.log import LOG
|
2022-06-20 14:34:20 +02:00
|
|
|
from app.errors import ProtonPartnerNotSetUp
|
2022-08-12 13:17:21 +02:00
|
|
|
from app.models import Partner, PartnerUser, User
|
2024-10-16 16:57:59 +02:00
|
|
|
from app.user_audit_log_utils import emit_user_audit_log, UserAuditLogAction
|
2022-06-20 14:34:20 +02:00
|
|
|
|
|
|
|
PROTON_PARTNER_NAME = "Proton"
|
|
|
|
_PROTON_PARTNER: Optional[Partner] = None
|
|
|
|
|
|
|
|
|
|
|
|
def get_proton_partner() -> Partner:
|
|
|
|
global _PROTON_PARTNER
|
|
|
|
if _PROTON_PARTNER is None:
|
|
|
|
partner = Partner.get_by(name=PROTON_PARTNER_NAME)
|
|
|
|
if partner is None:
|
|
|
|
raise ProtonPartnerNotSetUp
|
|
|
|
Session.expunge(partner)
|
|
|
|
_PROTON_PARTNER = partner
|
|
|
|
return _PROTON_PARTNER
|
|
|
|
|
|
|
|
|
2022-07-26 12:38:18 +02:00
|
|
|
def is_proton_partner(partner: Partner) -> bool:
|
2022-06-20 14:34:20 +02:00
|
|
|
return partner.name == PROTON_PARTNER_NAME
|
2022-08-12 13:17:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
def perform_proton_account_unlink(current_user: User):
|
|
|
|
proton_partner = get_proton_partner()
|
|
|
|
partner_user = PartnerUser.get_by(
|
|
|
|
user_id=current_user.id, partner_id=proton_partner.id
|
|
|
|
)
|
|
|
|
if partner_user is not None:
|
2024-06-26 12:17:24 +02:00
|
|
|
LOG.info(f"User {current_user} has unlinked the account from {partner_user}")
|
2024-10-16 16:57:59 +02:00
|
|
|
emit_user_audit_log(
|
|
|
|
user=current_user,
|
|
|
|
action=UserAuditLogAction.UnlinkAccount,
|
|
|
|
message=f"User has unlinked the account (email={partner_user.partner_email} | external_user_id={partner_user.external_user_id})",
|
|
|
|
)
|
2022-08-12 13:17:21 +02:00
|
|
|
PartnerUser.delete(partner_user.id)
|
|
|
|
Session.commit()
|
|
|
|
agent.record_custom_event("AccountUnlinked", {"partner": proton_partner.name})
|