Skip to content

Commit

Permalink
refactor: remove DKIM Key from agents
Browse files Browse the repository at this point in the history
  • Loading branch information
s-aga-r committed Jan 7, 2025
1 parent 6e747ee commit 32db82d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
33 changes: 17 additions & 16 deletions mail/mail/doctype/dkim_key/dkim_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ def autoname(self) -> None:
self.name = f"{self.domain_name.replace('.', '-')}-{random_string(length=10)}"

def validate(self) -> None:
self.validate_domain_name()
self.validate_rsa_key_size()
self.generate_dkim_keys()

def after_insert(self) -> None:
self.create_or_update_dns_record()
self.disable_existing_dkim_keys()
create_dkim_key_on_agents(self.domain_name, self.rsa_private_key, self.ed25519_private_key)
if self.is_new():
self.generate_dkim_keys()

def on_update(self) -> None:
if self.enabled:
if self.has_value_changed("enabled"):
self.create_or_update_dns_record()
self.disable_existing_dkim_keys()
create_dkim_key_on_agents(self.domain_name, self.rsa_private_key, self.ed25519_private_key)
elif self.has_value_changed("enabled"):
delete_dkim_key_from_agents(self.domain_name)

def on_trash(self) -> None:
if frappe.session.user != "Administrator":
frappe.throw(_("Only Administrator can delete DKIM Key."))

delete_dkim_key_from_agents(self.domain_name)

def validate_domain_name(self) -> None:
"""Validates the Domain Name."""

if not self.domain_name:
frappe.throw(_("Domain Name is mandatory"))
if self.enabled:
delete_dkim_key_from_agents(self.domain_name)

def validate_rsa_key_size(self) -> None:
"""Validates the Key Size."""
Expand Down Expand Up @@ -88,9 +88,10 @@ def disable_existing_dkim_keys(self) -> None:
"""Disables the existing DKIM Keys."""

filters = {"enabled": 1, "domain_name": self.domain_name, "name": ["!=", self.name]}
if frappe.db.exists("DKIM Key", filters):
frappe.db.set_value("DKIM Key", filters, "enabled", 0)
delete_dkim_key_from_agents(self.domain_name)
for dkim_key in frappe.db.get_all("DKIM Key", filters, pluck="name"):
dkim_key = frappe.get_doc("DKIM Key", dkim_key)
dkim_key.enabled = 0
dkim_key.save(ignore_permissions=True)


def create_dkim_key(domain_name: str, rsa_key_size: int | None = None) -> "DKIMKey":
Expand Down
4 changes: 3 additions & 1 deletion mail/mail/doctype/mail_account/mail_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ def on_update(self) -> None:

def on_trash(self) -> None:
frappe.cache.delete_value(f"user|{self.user}")
delete_account_from_agents(self.email)

if self.enabled:
delete_account_from_agents(self.email)

def validate_enabled(self) -> None:
"""Validates the enabled field."""
Expand Down
3 changes: 2 additions & 1 deletion mail/mail/doctype/mail_alias/mail_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def on_update(self) -> None:
delete_alias_from_agents(self.alias_for_name, self.email)

def on_trash(self) -> None:
delete_alias_from_agents(self.alias_for_name, self.email)
if self.enabled:
delete_alias_from_agents(self.alias_for_name, self.email)

def validate_alias_for_name(self) -> None:
"""Validates the alias for name."""
Expand Down
3 changes: 2 additions & 1 deletion mail/mail/doctype/mail_group/mail_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def on_update(self) -> None:
delete_group_from_agents(self.email)

def on_trash(self) -> None:
delete_group_from_agents(self.email)
if self.enabled:
delete_group_from_agents(self.email)

def validate_enabled(self) -> None:
"""Validates the enabled field."""
Expand Down

0 comments on commit 32db82d

Please sign in to comment.