Skip to content

Commit

Permalink
fix: get correct doc when checking child table permission
Browse files Browse the repository at this point in the history
(cherry picked from commit 05c1cb3)
  • Loading branch information
sagarvora authored and mergify[bot] committed Aug 9, 2022
1 parent e5b8a47 commit 9b89dc6
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions frappe/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ def has_permission(
if not user:
user = frappe.session.user

if user == "Administrator":
return True

if not doc and hasattr(doctype, "doctype"):
# first argument can be doc or doctype
doc = doctype
doctype = doc.doctype

if user == "Administrator":
return True

if frappe.is_table(doctype):
return has_child_table_permission(
doctype, ptype, doc, verbose, user, raise_exception, parent_doctype
Expand Down Expand Up @@ -675,43 +675,36 @@ def has_child_table_permission(
raise_exception=True,
parent_doctype=None,
):
parent_doc = None

if child_doc:
parent_doctype = child_doc.get("parenttype")
parent_doc = frappe.get_cached_doc(
{"doctype": parent_doctype, "docname": child_doc.get("parent")}
)
parent_doctype = child_doc.parenttype

if parent_doctype:
if not is_parent_valid(child_doctype, parent_doctype):
frappe.throw(
_("{0} is not a valid parent DocType for {1}").format(
frappe.bold(parent_doctype), frappe.bold(child_doctype)
),
title=_("Invalid Parent DocType"),
)
else:
if not parent_doctype:
frappe.throw(
_("Please specify a valid parent DocType for {0}").format(frappe.bold(child_doctype)),
title=_("Parent DocType Required"),
)

if not is_parent_valid(child_doctype, parent_doctype):
frappe.throw(
_("{0} is not a valid parent DocType for {1}").format(
frappe.bold(parent_doctype), frappe.bold(child_doctype)
),
title=_("Invalid Parent DocType"),
)

return has_permission(
parent_doctype,
ptype=ptype,
doc=parent_doc,
doc=getattr(child_doc, "parent_doc", child_doc.parent),
verbose=verbose,
user=user,
raise_exception=raise_exception,
)


def is_parent_valid(child_doctype, parent_doctype):
from frappe.core.utils import find

parent_meta = frappe.get_meta(parent_doctype)
child_table_field_exists = find(
parent_meta.get_table_fields(), lambda d: d.options == child_doctype

return not parent_meta.istable and any(
df.options == child_doctype for df in parent_meta.get_table_fields()
)
return not parent_meta.istable and child_table_field_exists

0 comments on commit 9b89dc6

Please sign in to comment.