package erpsetup import ( "context" "encoding/json" "fmt" "net/http" "net/url" ) // registryRole is the role of the registry's API user (platform-registry@): // it appends usage records and reads invoices. It is not Accounts User, which // could also create and submit bookings and payments. const registryRole = "Sales User" // registryReads are the doctypes the registry must read beyond what its role // gets by default. Dunning: the late-payment ladder counts from the Mahnung // (AGB ยง 10, tenant-registry #134); ERPNext gives read only to the Accounts // roles. The child table (Overdue Payment) follows its parent. func registryReads() []string { return []string{"Dunning"} } // grantFlags are the rights a read-only rule must not carry. var grantFlags = []string{"write", "create", "submit", "cancel", "delete", "amend"} // ensureReadPerm makes sure `role` may read `doctype` and nothing more, at // permission level 0. A missing rule is added through Frappe's permission // manager, which first copies the doctype's standard rules into Custom DocPerm // (as the Role Permissions Manager UI does), so the other roles keep theirs. // An existing rule is corrected to read-only. func (r *runner) ensureReadPerm(ctx context.Context, doctype, role string) error { what := fmt.Sprintf("permission %s: %s may read (level 0, read only)", doctype, role) filters, _ := json.Marshal([][]any{{"parent", "=", doctype}, {"role", "=", role}, {"permlevel", "=", 0}}) fields, _ := json.Marshal(append([]string{"name", "read"}, grantFlags...)) q := url.Values{"filters": {string(filters)}, "fields": {string(fields)}, "limit_page_length": {"1"}} status, body, err := r.call(ctx, http.MethodGet, "/api/resource/"+url.PathEscape("Custom DocPerm")+"?"+q.Encode(), nil) if err != nil { return err } if status != http.StatusOK { return fmt.Errorf("erpsetup: list Custom DocPerm: HTTP %d %s", status, truncate(body)) } var list struct { Data []map[string]any `json:"data"` } if err := json.Unmarshal(body, &list); err != nil { return fmt.Errorf("erpsetup: decode Custom DocPerm list: %w", err) } if len(list.Data) == 0 { if r.cfg.DryRun { r.record(what, "would create") return nil } status, body, err = r.call(ctx, http.MethodPost, "/api/method/frappe.core.page.permission_manager.permission_manager.add", map[string]any{"parent": doctype, "role": role, "permlevel": 0}) if err != nil { return err } if status/100 != 2 { return fmt.Errorf("erpsetup: create %s: HTTP %d %s", what, status, truncate(body)) } r.record(what, "created") return nil } row := list.Data[0] name := fmt.Sprint(row["name"]) patch := map[string]any{} var changes []string if fmt.Sprint(row["read"]) != "1" { patch["read"] = 1 changes = append(changes, change("read", row["read"], 1)) } for _, f := range grantFlags { if v := fmt.Sprint(row[f]); v != "0" && v != "" { patch[f] = 0 changes = append(changes, change(f, row[f], 0)) } } return r.applyPatch(ctx, "/api/resource/"+url.PathEscape("Custom DocPerm")+"/"+url.PathEscape(name), what, " ("+name+")", patch, changes) }