fix: Avoid permission check on unsaved doc (#26027)

Use case:
- User has "if owner" perm
- Doc isn't created
- We skip doc perm check because doc doesn't exist
- We check if user has write perm to doctype, which isn't available
  because it's only "if owner"

Fix: We can avoid perm check entirely here, files are only re-attached
if doc saves successfully which implies that reference doc was indeed
saved after perm check.
This commit is contained in:
Ankush Menat 2024-04-18 13:47:39 +05:30 committed by GitHub
parent 1dcfadf5ca
commit 3c2bf77c81
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 7 deletions

View file

@ -361,15 +361,15 @@ def attach_files_to_document(doc: "Document", event) -> None:
def relink_files(doc, fieldname, temp_doc_name):
if not temp_doc_name:
return
from frappe.utils.data import add_to_date, now_datetime
"""
Relink files attached to incorrect document name to the new document name
by check if file with temp name exists that was created in last 60 minutes
"""
mislinked_file = frappe.db.exists(
if not temp_doc_name:
return
from frappe.utils.data import add_to_date, now_datetime
mislinked_file = frappe.db.get_value(
"File",
{
"file_url": doc.get(fieldname),
@ -382,7 +382,7 @@ def relink_files(doc, fieldname, temp_doc_name):
),
},
)
"""If file exists, attach it to the new docname"""
# If file exists, attach it to the new docname
if mislinked_file:
frappe.db.set_value(
"File",

View file

@ -252,7 +252,8 @@ def check_write_permission(doctype: str | None = None, name: str | None = None):
doc.check_permission("write")
except frappe.DoesNotExistError:
# doc has not been inserted yet, name is set to "new-some-doctype"
check_doctype = True
# If doc inserts fine then only this attachment will be linked see file/utils.py:relink_mismatched_files
return
if check_doctype:
frappe.has_permission(doctype, "write", throw=True)