feat(erpsetup): registry user may read dunning, read only
The late-payment ladder (#134) reads Dunnings, which ERPNext lets only the Accounts roles read. The registry's API user holds Sales User only. erpsetup now adds a level-0 read rule for Sales User on Dunning through Frappe's permission manager (which first copies the standard rules, as the Role Permissions Manager UI does) and cuts a broader rule back to read only. The spec.go comment no longer claims Accounts User. Live ERP: the same rule was added by hand on 17.11.; the next run reports it as existing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f6bb84933c
commit
b02bf254bc
4 files changed
+155
-2
No files matched your search
@@ -205,6 +205,12 @@ func Run(ctx context.Context, cfg Config) ([]Step, error) {
|
||||
if err := r.reisekosten(ctx); err != nil {
|
||||
return r.steps, err
|
||||
}
|
||||
// What the registry's API user must read beyond its role (permissions.go).
|
||||
for _, dt := range registryReads() {
|
||||
if err := r.ensureReadPerm(ctx, dt, registryRole); err != nil {
|
||||
return r.steps, err
|
||||
}
|
||||
}
|
||||
for _, w := range webhooks(cfg.WebhookURL, cfg.WebhookSecret) {
|
||||
if err := r.ensureWebhook(ctx, w); err != nil {
|
||||
return r.steps, err
|
||||
|
||||
@@ -67,6 +67,20 @@ func (f *fakeFrappe) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/api/method/frappe.core.page.permission_manager.permission_manager.add" && r.Method == http.MethodPost {
|
||||
// Frappe copies the standard rules first, then adds the role with read only.
|
||||
var in map[string]any
|
||||
_ = json.NewDecoder(r.Body).Decode(&in)
|
||||
if f.docs["Custom DocPerm"] == nil {
|
||||
f.docs["Custom DocPerm"] = map[string]map[string]any{}
|
||||
}
|
||||
name := fmt.Sprintf("perm-%d", len(f.docs["Custom DocPerm"])+1)
|
||||
f.docs["Custom DocPerm"][name] = map[string]any{"name": name, "parent": in["parent"], "role": in["role"], "permlevel": in["permlevel"],
|
||||
"read": 1, "write": 0, "create": 0, "submit": 0, "cancel": 0, "delete": 0, "amend": 0}
|
||||
f.posts++
|
||||
_, _ = io.WriteString(w, `{"message":"`+name+`"}`)
|
||||
return
|
||||
}
|
||||
rest := strings.TrimPrefix(r.URL.Path, "/api/resource/")
|
||||
parts := strings.SplitN(rest, "/", 2)
|
||||
doctype, _ := url.PathUnescape(parts[0])
|
||||
@@ -809,3 +823,56 @@ func TestCustomerTextSpellsBreakpilot(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The registry's API user may read Dunning (Mahnungen) and nothing more: a
|
||||
// missing rule is created through the permission manager, a broader one is
|
||||
// cut back to read only, a correct one is left alone.
|
||||
func TestRegistryReadPermission(t *testing.T) {
|
||||
f := newFake()
|
||||
srv := httptest.NewServer(f)
|
||||
defer srv.Close()
|
||||
find := func() map[string]any {
|
||||
for _, d := range f.docs["Custom DocPerm"] {
|
||||
if d["parent"] == "Dunning" && d["role"] == registryRole {
|
||||
return d
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
result := func(steps []Step) string {
|
||||
for _, s := range steps {
|
||||
if strings.HasPrefix(s.What, "permission Dunning") {
|
||||
return s.Result
|
||||
}
|
||||
}
|
||||
return "(no step)"
|
||||
}
|
||||
|
||||
dry := cfg(srv.URL)
|
||||
dry.DryRun = true
|
||||
steps, err := Run(t.Context(), dry)
|
||||
if err != nil || result(steps) != "would create" || find() != nil {
|
||||
t.Fatalf("dry run: %v %q", err, result(steps))
|
||||
}
|
||||
steps, err = Run(t.Context(), cfg(srv.URL))
|
||||
if err != nil || result(steps) != "created" {
|
||||
t.Fatalf("first run: %v %q", err, result(steps))
|
||||
}
|
||||
if d := find(); d == nil || fmt.Sprint(d["read"]) != "1" || fmt.Sprint(d["write"]) != "0" {
|
||||
t.Fatalf("rule: %v", d)
|
||||
}
|
||||
steps, _ = Run(t.Context(), cfg(srv.URL))
|
||||
if !strings.HasPrefix(result(steps), "exists") {
|
||||
t.Fatalf("second run: %q", result(steps))
|
||||
}
|
||||
// someone ticked more in the UI: the next run cuts it back to read only
|
||||
d := find()
|
||||
d["write"], d["create"] = 1, 1
|
||||
steps, _ = Run(t.Context(), cfg(srv.URL))
|
||||
if r := result(steps); !strings.HasPrefix(r, "updated") || !strings.Contains(r, "write 1 → 0") || !strings.Contains(r, "create 1 → 0") {
|
||||
t.Fatalf("broader rule: %q", r)
|
||||
}
|
||||
if fmt.Sprint(find()["write"]) != "0" || fmt.Sprint(find()["read"]) != "1" {
|
||||
t.Fatalf("after cut back: %v", find())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
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 != "<nil>" {
|
||||
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)
|
||||
}
|
||||
@@ -60,8 +60,9 @@ func usageRecordDoctype() map[string]any {
|
||||
{"role": "System Manager", "read": 1, "write": 1, "create": 1, "delete": 1, "report": 1, "export": 1},
|
||||
{"role": "Accounts Manager", "read": 1, "report": 1, "export": 1},
|
||||
{"role": "Sales Manager", "read": 1, "report": 1, "export": 1},
|
||||
// The registry's API user (platform-registry) holds Sales User and
|
||||
// Accounts User, not System Manager: it only appends usage.
|
||||
// The registry's API user (platform-registry) holds Sales User
|
||||
// only, not System Manager: it appends usage. Its extra reads are
|
||||
// in permissions.go.
|
||||
{"role": "Sales User", "read": 1, "create": 1},
|
||||
},
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user