Skip to content

Commit

Permalink
fix: Avoid permission check on unsaved doc (#26027) (#26031)
Browse files Browse the repository at this point in the history
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.

(cherry picked from commit 3c2bf77)

Co-authored-by: Ankush Menat <ankush@frappe.io>
  • Loading branch information
mergify[bot] and ankush committed Apr 18, 2024
1 parent f608498 commit 334d353
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
12 changes: 6 additions & 6 deletions frappe/core/doctype/file/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion frappe/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ def check_write_permission(doctype: str | None = None, name: str | None = None):
doc.has_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)
Expand Down

0 comments on commit 334d353

Please sign in to comment.