Add Stock Viewer role: read-only access including cost/value figures

For stakeholders who need visibility (a board member, an investor) but
should never be able to alter stock records -- confirmed the hard way
this was a real gap when reviewing Technician's default Stock User
access: nothing in that role actually prevented self-issuing stock,
which led to dropping Technician system accounts entirely in favor of
phone-based reporting through the Dispatcher instead.

Grants read/report/export/print (never create/write/submit/cancel/
delete/amend) on Stock Entry, Material Request, Serial No, Item,
Warehouse, and Stock Ledger Entry, plus adds the role to both the
Damage & Cost Report and Serial No Ledger report's own role lists --
cost/value figures included, not redacted, per explicit request.

Verified idempotent (two full provisioning runs against local dev) and
confirmed the exact permission flags directly against the live Custom
DocPerm records, not just the script's own print statements.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012pc2x7vyuUofdT3kLuqF3Y
This commit is contained in:
JasonFraser 2026-10-03 22:55:19 -04:00
parent dc763613c7
commit 1fc08dd9ec
1 changed files with 69 additions and 1 deletions

View File

@ -24,7 +24,11 @@ Entry read access, and Stock Manager's addition to the standard Serial No
Ledger report's role list — the last one needs a brief in-process
frappe.conf.developer_mode toggle, since Frappe refuses to edit a standard
report's roles otherwise; it's restored immediately after and never
touches site_config.json), the entry-time serial-availability Client
touches site_config.json), the Stock Viewer role (read-only across Stock
Entry/Material Request/Serial No/Item/Warehouse/Stock Ledger Entry, plus
both reports — for someone who needs visibility, e.g. a board member,
with no ability to create/write/submit/cancel/delete anything), the
entry-time serial-availability Client
Script (catches a serial that's already elsewhere/inactive the moment it's
added to a row, instead of only at submit), the two audit-hardening
settings, the "Trial Expired" lockout Role, the Custom Field System Settings.
@ -60,6 +64,7 @@ def run():
_damage_cost_report()
_role_permissions()
_serial_no_ledger_permission()
_stock_viewer_role()
_audit_hardening()
_lockout_role()
_trial_check_scheduler()
@ -751,6 +756,69 @@ def _serial_no_ledger_permission():
print(" added Stock Manager")
# --- Stage 8: Stock Viewer role (read-only, e.g. a board member) ------------
def _stock_viewer_role():
print("-- Role: Stock Viewer (read-only — board/investor access, values included)")
if not frappe.db.exists("Role", "Stock Viewer"):
frappe.get_doc({
"doctype": "Role", "role_name": "Stock Viewer", "desk_access": 1,
}).insert(ignore_permissions=True)
print(" created role")
else:
print(" role already exists")
from frappe.permissions import add_permission, update_permission_property
# Read + report/export/print only — explicitly zeroing every write-side
# flag rather than relying on Custom DocPerm's own defaults, since the
# entire point of this role is that nothing here can be altered.
read_only_doctypes = (
"Stock Entry", "Material Request", "Serial No", "Item", "Warehouse",
"Stock Ledger Entry",
)
for dt in read_only_doctypes:
if frappe.db.exists("Custom DocPerm", {"parent": dt, "role": "Stock Viewer"}):
continue
add_permission(dt, "Stock Viewer", 0)
for prop, value in (
("read", 1), ("report", 1), ("export", 1), ("print", 1),
("create", 0), ("write", 0), ("submit", 0), ("cancel", 0),
("delete", 0), ("amend", 0),
):
update_permission_property(dt, "Stock Viewer", 0, prop, value)
print(f" read-only granted on: {', '.join(read_only_doctypes)}")
# A Report's own `roles` list is a separate access gate on top of its
# ref_doctype's permissions above — same mechanism as Stock Manager's
# own addition to Serial No Ledger. Damage & Cost Report is a custom
# report (is_standard=No) and saves normally; Serial No Ledger ships
# with ERPNext and needs the same brief developer_mode toggle Stock
# Manager's own grant above uses, since Frappe refuses to edit a
# standard report's roles otherwise.
damage_report = frappe.get_doc("Report", "Damage & Cost Report")
if "Stock Viewer" not in [d.role for d in damage_report.roles]:
damage_report.append("roles", {"role": "Stock Viewer"})
damage_report.save(ignore_permissions=True)
print(" added Stock Viewer to Damage & Cost Report (cost/value figures included)")
else:
print(" Damage & Cost Report already correct")
serial_report = frappe.get_doc("Report", "Serial No Ledger")
if "Stock Viewer" not in [d.role for d in serial_report.roles]:
was_on = bool(frappe.conf.get("developer_mode"))
frappe.conf.developer_mode = 1
try:
serial_report.append("roles", {"role": "Stock Viewer"})
serial_report.save(ignore_permissions=True)
finally:
frappe.conf.developer_mode = 1 if was_on else 0
print(" added Stock Viewer to Serial No Ledger")
else:
print(" Serial No Ledger already correct")
# --- Stage 10: audit trail hardening -----------------------------------------