diff --git a/dtable_events/automations/actions.py b/dtable_events/automations/actions.py index a1416f2f..e0f582a4 100644 --- a/dtable_events/automations/actions.py +++ b/dtable_events/automations/actions.py @@ -1038,7 +1038,7 @@ def __init__(self, auto_rule, action_type, data, msg, account_id, msg_type): self.init_notify(msg) def init_notify(self, msg): - account_dict = get_third_party_account(self.auto_rule.db_session, self.account_id) + account_dict = get_third_party_account(self.auto_rule.db_session, self.account_id, self.auto_rule.dtable_uuid) if not account_dict: raise RuleInvalidException('Send wechat no account') blanks = set(re.findall(r'\{([^{]*?)\}', msg)) @@ -1109,7 +1109,7 @@ def __init__(self, auto_rule, action_type, data, msg, account_id, msg_type, msg_ self.init_notify(msg) def init_notify(self, msg): - account_dict = get_third_party_account(self.auto_rule.db_session, self.account_id) + account_dict = get_third_party_account(self.auto_rule.db_session, self.account_id, self.auto_rule.dtable_uuid) if not account_dict: raise RuleInvalidException('Send dingtalk no account') blanks = set(re.findall(r'\{([^{]*?)\}', msg)) @@ -1252,7 +1252,7 @@ def init_notify_images(self): self.image_cid_url_map[cid] = image_url def init_notify(self): - account_dict = get_third_party_account(self.auto_rule.db_session, self.account_id) + account_dict = get_third_party_account(self.auto_rule.db_session, self.account_id, self.auto_rule.dtable_uuid) if not account_dict: raise RuleInvalidException('Send email no account') self.col_name_dict = {col.get('name'): col for col in self.auto_rule.table_info['columns']} diff --git a/dtable_events/automations/general_actions.py b/dtable_events/automations/general_actions.py index e9b55adf..420a645d 100644 --- a/dtable_events/automations/general_actions.py +++ b/dtable_events/automations/general_actions.py @@ -15,10 +15,10 @@ from dtable_events.app.config import DTABLE_WEB_SERVICE_URL, DTABLE_PRIVATE_KEY, SEATABLE_FAAS_AUTH_TOKEN, \ SEATABLE_FAAS_URL, INNER_DTABLE_DB_URL -from dtable_events.automations.models import BoundThirdPartyAccounts +from dtable_events.automations.models import BoundThirdPartyAccounts, DTableBoundThirdPartyAccounts from dtable_events.dtable_io import send_wechat_msg, send_email_msg, send_dingtalk_msg from dtable_events.notification_rules.notification_rules_utils import fill_msg_blanks_with_sql_row, send_notification -from dtable_events.utils import is_valid_email, get_inner_dtable_server_url, uuid_str_to_36_chars +from dtable_events.utils import is_valid_email, get_inner_dtable_server_url, uuid_str_to_36_chars, uuid_str_to_32_chars from dtable_events.utils.constants import ColumnTypes from dtable_events.utils.dtable_server_api import DTableServerAPI, NotFoundException from dtable_events.utils.dtable_web_api import DTableWebAPI @@ -47,14 +47,19 @@ -def get_third_party_account(session, account_id): - stmt = select(BoundThirdPartyAccounts).where(BoundThirdPartyAccounts.id == account_id).limit(1) - account = session.scalars(stmt).first() - if account: - return account.to_dict() - else: - logger.warning("Third party account %s does not exists." % account_id) - return None +def get_third_party_account(session, account_id, dtable_uuid): + stmt_bound = select(DTableBoundThirdPartyAccounts).where( + DTableBoundThirdPartyAccounts.account_id == account_id, + dtable_uuid == uuid_str_to_32_chars(dtable_uuid) + ) + account_bound = session.scalars(stmt_bound).first() + if account_bound: + stmt = select(BoundThirdPartyAccounts).where(BoundThirdPartyAccounts.id == account_id).limit(1) + account = session.scalars(stmt).first() + if account: + return account.to_dict() + logger.warning("Third party account %s does not exists." % account_id) + return None def email2list(email_str, split_pattern='[,,]'): email_list = [value.strip() for value in re.split(split_pattern, email_str) if value.strip()] @@ -501,7 +506,7 @@ class SendEmailAction(BaseAction): def __init__(self, context: BaseContext, account_id, subject, msg, send_to, copy_to, send_from): super().__init__(context) - self.account_dict = get_third_party_account(self.context.db_session, account_id) + self.account_dict = get_third_party_account(self.context.db_session, account_id, self.context.dtable_uuid) if not self.account_dict: raise ActionInvalid('account_id: %s not found' % account_id) self.msg = msg @@ -557,7 +562,7 @@ class SendWechatAction(BaseAction): def __init__(self, context: BaseContext, account_id, msg, msg_type): super().__init__(context) - self.account_dict = get_third_party_account(self.context.db_session, account_id) + self.account_dict = get_third_party_account(self.context.db_session, account_id, self.context.dtable_uuid) if not self.account_dict: raise ActionInvalid('account_id: %s not found' % account_id) self.msg = msg @@ -587,7 +592,7 @@ def __init__(self, context: BaseContext, account_id, msg, msg_type, msg_title): self.msg = msg self.msg_type = msg_type self.msg_title = msg_title - self.account_dict = get_third_party_account(self.context.db_session, account_id) + self.account_dict = get_third_party_account(self.context.db_session, account_id, self.context.dtable_uuid) if not self.account_dict: raise ActionInvalid('account_id: %s not found' % account_id) diff --git a/dtable_events/automations/models.py b/dtable_events/automations/models.py index 6e9c386e..9fdbed2d 100644 --- a/dtable_events/automations/models.py +++ b/dtable_events/automations/models.py @@ -2,12 +2,14 @@ from copy import deepcopy from sqlalchemy.orm import mapped_column -from sqlalchemy import Integer, String, DateTime, Text, select +from sqlalchemy import Integer, String, DateTime, Text, select, ForeignKey from dtable_events.automations.hasher import AESPasswordHasher from dtable_events.db import Base import json +from dtable_events.utils import uuid_str_to_32_chars + logger = logging.getLogger(__name__) @@ -56,13 +58,28 @@ def to_dict(self): 'detail': _decrypt_detail(detail_dict) } return res + +class DTableBoundThirdPartyAccounts(Base): + id = mapped_column(Integer, primary_key=True, autoincrement=True) + dtable_uuid = mapped_column(String(length=255), nullable=False) + account_id = mapped_column(Integer, nullable=False) + __tablename__ = 'dtable_bound_third_party_accounts' + + + -def get_third_party_account(session, account_id): - stmt = select(BoundThirdPartyAccounts).where(BoundThirdPartyAccounts.id == account_id).limit(1) - account = session.scalars(stmt).first() - if account: - return account.to_dict() - else: - logger.warning("Third party account %s does not exists." % account_id) - return None + +def get_third_party_account(session, account_id, dtable_uuid): + stmt_bound = select(DTableBoundThirdPartyAccounts).where( + DTableBoundThirdPartyAccounts.account_id == account_id, + dtable_uuid == uuid_str_to_32_chars(dtable_uuid) + ) + account_bound = session.scalars(stmt_bound).first() + if account_bound: + stmt = select(BoundThirdPartyAccounts).where(BoundThirdPartyAccounts.id == account_id).limit(1) + account = session.scalars(stmt).first() + if account: + return account.to_dict() + logger.warning("Third party account %s does not exists." % account_id) + return None diff --git a/dtable_events/data_sync/data_sync_utils.py b/dtable_events/data_sync/data_sync_utils.py index 4dd2c6fc..24131009 100644 --- a/dtable_events/data_sync/data_sync_utils.py +++ b/dtable_events/data_sync/data_sync_utils.py @@ -401,7 +401,7 @@ def run_sync_emails(context): logger.error('send_date invalid.') return - account = get_third_party_account(db_session, account_id) + account = get_third_party_account(db_session, account_id, dtable_uuid) if not account or account.get('account_type') != 'email' or not account.get('detail'): set_data_sync_invalid(data_sync_id, db_session) logger.warning('third party account not found.')