From da525e97f4593f363152ae7f73d67b888935be27 Mon Sep 17 00:00:00 2001 From: aldemirlucas Date: Fri, 5 Sep 2025 12:08:13 -0300 Subject: [PATCH] =?UTF-8?q?fix:=20aprimorar=20a=20fun=C3=A7=C3=A3o=20de=20?= =?UTF-8?q?adi=C3=A7=C3=A3o=20de=20usu=C3=A1rios=20ao=20Google=20Groups=20?= =?UTF-8?q?com=20normaliza=C3=A7=C3=A3o=20de=20e-mails=20e=20verifica?= =?UTF-8?q?=C3=A7=C3=A3o=20de=20administradores=20em=20ambientes=20de=20de?= =?UTF-8?q?senvolvimento=20e=20staging?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/apps/account_payment/graphql.py | 4 +-- backend/apps/account_payment/webhooks.py | 42 +++++++++++++++--------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/backend/apps/account_payment/graphql.py b/backend/apps/account_payment/graphql.py index 70e2e156..5a03d2c1 100644 --- a/backend/apps/account_payment/graphql.py +++ b/backend/apps/account_payment/graphql.py @@ -384,7 +384,7 @@ def mutate(cls, root, info, account_id, subscription_id): subscription = Subscription.objects.get(id=subscription_id) assert admin.id == subscription.admin.id - add_user(account.gcp_email or account.email) + add_user(account.gcp_email or account.email, account=account) subscription.subscribers.add(account) return cls(ok=True) except Exception as e: @@ -490,7 +490,7 @@ def mutate(cls, root, info, email): if not is_email_in_group(email): try: - add_user(email) + add_user(email, account=user) except Exception: pass diff --git a/backend/apps/account_payment/webhooks.py b/backend/apps/account_payment/webhooks.py index c5e05a60..17c5efd6 100644 --- a/backend/apps/account_payment/webhooks.py +++ b/backend/apps/account_payment/webhooks.py @@ -14,10 +14,17 @@ from backend.apps.account.models import Account, Subscription from backend.custom.client import send_discord_message as send -from backend.custom.environment import get_backend_url +from backend.custom.environment import get_backend_url, is_dev, is_stg logger = logger.bind(module="payment") +def _normalize_plus(email: str) -> str: + """Normaliza: trim, lower e remove +alias antes do @.""" + email = email.strip().lower() + local, _, domain = email.partition("@") + if "+" in local: + local = local.split("+", 1)[0] + return f"{local}@{domain}" def get_subscription(event: Event) -> Subscription: """Get internal subscription model, mirror of stripe""" @@ -58,12 +65,23 @@ def get_service() -> Resource: return build("admin", "directory_v1", credentials=credentials) -def add_user(email: str, group_key: str = None, role: str = "MEMBER"): +def add_user(email: str, account: Account = None, group_key: str = None, role: str = "MEMBER"): """Add user to google group""" + if is_dev() or is_stg(): + if account is None: + try: + normalized_email = _normalize_plus(email) + account = Account.objects.get(Q(email__iexact=email) | Q(email__iexact=normalized_email)) + except Account.DoesNotExist: + account = None + + if not (account and account.is_admin): + logger.info(f"Ignorando adição do usuário '{email}' em ambiente de dev/staging pois não é admin.") + return + if not group_key: group_key = settings.GOOGLE_DIRECTORY_GROUP_KEY - if "+" in email and email.index("+") < email.index("@"): - email = email.split("+")[0] + "@" + email.split("@")[1] + email = _normalize_plus(email) try: service = get_service() service.members().insert( @@ -78,14 +96,6 @@ def add_user(email: str, group_key: str = None, role: str = "MEMBER"): logger.error(e) raise e -def _normalize_plus(email: str) -> str: - """Normaliza: trim, lower e remove +alias antes do @.""" - email = email.strip().lower() - local, _, domain = email.partition("@") - if "+" in local: - local = local.split("+", 1)[0] - return f"{local}@{domain}" - def remove_user(email: str, group_key: str = None) -> None: """Remove user from Google Group""" if not email or "@" not in email: @@ -196,9 +206,9 @@ def handle_subscription(event: Event): # Add user to google group if subscription exists or not if account: - add_user(account.gcp_email or account.email) + add_user(account.gcp_email or account.email, account=account) else: - add_user(event.customer.email) + add_user(event.customer.email, account=None) else: if subscription: logger.info(f"Removendo a inscrição do cliente {event.customer.email}") @@ -276,9 +286,9 @@ def resume_subscription(event: Event, **kwargs): try: if account: - add_user(account.gcp_email or account.email) + add_user(account.gcp_email or account.email, account=account) else: - add_user(event.customer.email) + add_user(event.customer.email, account=None) except Exception as e: logger.error(e)