Skip to content

Commit

Permalink
fix(PWA): make fields readOnly based on permlevel
Browse files Browse the repository at this point in the history
  • Loading branch information
ruchamahabal committed Jun 12, 2024
1 parent 944a824 commit 7147fbf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
21 changes: 18 additions & 3 deletions frontend/src/components/FormView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
:options="field.options"
:linkFilters="field.linkFilters"
:documentList="field.documentList"
:readOnly="Boolean(field.read_only) || isFormReadOnly"
:readOnly="isFieldReadOnly(field)"
:reqd="Boolean(field.reqd)"
:hidden="Boolean(field.hidden)"
:errorMessage="field.error_message"
Expand Down Expand Up @@ -141,7 +141,7 @@
:options="field.options"
:linkFilters="field.linkFilters"
:documentList="field.documentList"
:readOnly="Boolean(field.read_only) || isFormReadOnly"
:readOnly="isFieldReadOnly(field)"
:reqd="Boolean(field.reqd)"
:hidden="Boolean(field.hidden)"
:errorMessage="field.error_message"
Expand Down Expand Up @@ -586,6 +586,12 @@ const documentResource = createDocumentResource({
const docPermissions = createResource({
url: "frappe.client.get_doc_permissions",
params: { doctype: props.doctype, docname: props.id },
})
const permittedWriteFields = createResource({
url: "hrms.api.get_permitted_fields_for_write",
params: { doctype: props.doctype },
})
const formButton = computed(() => {
Expand All @@ -610,6 +616,14 @@ function hasPermission(action) {
return docPermissions.data?.permissions[action]
}
function isFieldReadOnly(field) {
return (
Boolean(field.read_only)
|| isFormReadOnly.value
|| (props.id && !permittedWriteFields.data?.includes(field.fieldname))
)
}
function handleDocInsert() {
if (!validateMandatoryFields()) return
docList.insert.submit(formModel.value)
Expand Down Expand Up @@ -736,7 +750,8 @@ onMounted(async () => {
if (props.id) {
await documentResource.get.promise
formModel.value = { ...documentResource.doc }
await docPermissions.fetch({ doctype: props.doctype, docname: props.id })
await docPermissions.reload()
await permittedWriteFields.reload()
await attachedFiles.reload()
await setFormattedCurrency()
Expand Down
3 changes: 0 additions & 3 deletions frontend/src/composables/workflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ export default function useWorkflow(doctype) {
url: "hrms.api.get_workflow",
params: { doctype: doctype },
cache: ["hrms:workflow", doctype],
onSuccess: (data) => {
console.log("Workflow loaded successfully ✅", data)
},
})
workflowDoc.reload()

Expand Down
7 changes: 7 additions & 0 deletions hrms/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import frappe
from frappe import _
from frappe.model import get_permitted_fields
from frappe.model.workflow import get_workflow_name
from frappe.query_builder import Order
from frappe.utils import getdate, strip_html
Expand Down Expand Up @@ -641,3 +642,9 @@ def get_workflow_state_field(doctype: str) -> str | None:
def get_allowed_states_for_workflow(workflow: dict, user_id: str) -> list[str]:
user_roles = frappe.get_roles(user_id)
return [transition.state for transition in workflow.transitions if transition.allowed in user_roles]


# Permissions
@frappe.whitelist()
def get_permitted_fields_for_write(doctype: str) -> list[str]:
return get_permitted_fields(doctype, permission_type="write")

0 comments on commit 7147fbf

Please sign in to comment.