Skip to content

Commit

Permalink
fix(Address/Contact): validate perms on link removal (LAN-855)
Browse files Browse the repository at this point in the history
  • Loading branch information
barredterra committed Oct 25, 2024
1 parent 9b0aad4 commit e8ac01d
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions landa/address_and_contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def validate(doc, event):
"""

validate_member_link(doc)
validate_link_permissions(doc)

linked_doctypes = {link.link_doctype for link in doc.links}
mandatory_links = {
Expand All @@ -32,11 +33,6 @@ def validate(doc, event):

doc.organization = None
for link in doc.links:
if not doc.flags.ignore_permissions:
# Linking an Address or Contact should be treated like writing to the linked doc
linked_doc = frappe.get_doc(link.link_doctype, link.link_name)
linked_doc.check_permission("write")

if link.link_doctype == "Customer":
doc.organization = link.link_name

Expand All @@ -50,6 +46,27 @@ def validate(doc, event):
doc.organization = frappe.db.get_value("External Contact", link.link_name, "organization")


def validate_link_permissions(doc):
"""Linking an Address or Contact should be treated like writing to the linked doc."""
if doc.flags.ignore_permissions:
return

new_links = {(link.link_doctype, link.link_name) for link in doc.links}
for dt, name in new_links:
linked_doc = frappe.get_doc(dt, name)
linked_doc.check_permission("write")

doc_before_save = doc.get_doc_before_save()
if not doc_before_save:
return

old_links = {(link.link_doctype, link.link_name) for link in doc_before_save.links}
# Write permission is also necessary on removed links
for dt, name in old_links - new_links:
linked_doc = frappe.get_doc(dt, name)
linked_doc.check_permission("write")


def validate_member_link(doc):
if doc.doctype == "Contact" and doc.user:
member = frappe.get_value("User", doc.user, "landa_member")
Expand Down

0 comments on commit e8ac01d

Please sign in to comment.