feat(roles): company rule for separate determination and approval #129

Merged
sharang merged 1 commits from feat/company-rules into main 2026-11-12 17:17:58 +00:00
5 changed files with 131 additions and 0 deletions

No files matched your search

+38
View File
@@ -23,6 +23,22 @@ type Dataset struct {
StandardReleases []StandardRelease `json:"standard_releases"`
Editors []Editors `json:"editors"`
CriticalContact []Contact `json:"critical_contact"`
// CompanyRules are rules the company sets for its own decisions. A
// dataset without them has every rule off.
CompanyRules *CompanyRules `json:"company_rules,omitempty"`
}
// CompanyRules (docgen#4 Nr. 31, AK-R3-030): rules a company switches on in
// its role dataset, maintained by whoever maintains the dataset (AM-3) and
// historised with its revisions. A rule not set is off; "not set" never
// counts as switched on.
type CompanyRules struct {
// SeparateApproval: "Festlegung und Freigabe durch verschiedene
// Personen". When on, nobody approves what they determined themselves
// (Werkmeister checks it); when off, the same authorised person may do
// both.
SeparateApproval bool `json:"separate_approval"`
Stamps
}
// Holder kinds.
@@ -620,6 +636,17 @@ func Prepare(current, incoming *Dataset, authorize []string, w Writer, now time.
}
}
// The rules keep their stamps while unchanged; a change is recorded now.
if incoming.CompanyRules != nil {
r := *incoming.CompanyRules
if old := prev.CompanyRules; old != nil && old.SeparateApproval == r.SeparateApproval {
r.Stamps = old.Stamps
} else {
r.Stamps = Stamps{RecordedBy: w.Sub, RecordedAt: &at}
}
out.CompanyRules = &r
}
for _, id := range authorize {
if !writerIsManagement {
return nil, &Error{Status: http.StatusForbidden, Code: "not_authorizer",
@@ -689,9 +716,20 @@ func withoutStamps(ds *Dataset) *Dataset {
for i := range c.StandardReleases {
c.StandardReleases[i].Stamps, c.StandardReleases[i].Authorization = Stamps{}, Authorization{}
}
if ds.CompanyRules != nil {
r := *ds.CompanyRules
r.Stamps = Stamps{}
c.CompanyRules = &r
}
return &c
}
// SeparateApproval: the company rule "Festlegung und Freigabe durch
// verschiedene Personen" is on.
func (ds *Dataset) SeparateApproval() bool {
return ds != nil && ds.CompanyRules != nil && ds.CompanyRules.SeparateApproval
}
func findAssignment(list []Assignment, id string) (Assignment, bool) {
for _, a := range list {
if a.ID == id {
+35
View File
@@ -412,3 +412,38 @@ func TestHoldersOf(t *testing.T) {
t.Fatalf("not applicable: %+v", h)
}
}
// Nr. 31: the company rule for separate persons is off until set, stamped
// by whoever switches it, and keeps its stamps while unchanged.
func TestPrepare_companyRules(t *testing.T) {
ds := base(t)
if ds.SeparateApproval() {
t.Fatal("a dataset without company rules has the rule on")
}
old, err := Decode([]byte(`{"assignments":[]}`))
if err != nil || old.SeparateApproval() || old.CompanyRules != nil {
t.Fatalf("an older document: %+v %v", old, err)
}
next := *ds
next.CompanyRules = &CompanyRules{SeparateApproval: true, Stamps: Stamps{RecordedBy: "u-forged"}}
on, perr := Prepare(ds, &next, nil, Writer{Sub: owner}, now)
if perr != nil {
t.Fatal(perr)
}
if !on.SeparateApproval() || on.CompanyRules.RecordedBy != owner || on.CompanyRules.RecordedAt == nil {
t.Fatalf("switched on: %+v", on.CompanyRules)
}
again := *on
again.CompanyRules = &CompanyRules{SeparateApproval: true}
same, perr := Prepare(on, &again, nil, Writer{Sub: ceo}, now.Add(time.Hour))
if perr != nil || same.CompanyRules.RecordedBy != owner || !same.CompanyRules.RecordedAt.Equal(now) {
t.Fatalf("unchanged keeps its stamps: %+v %v", same.CompanyRules, perr)
}
back, err := Decode([]byte(Canonical(same)))
if err != nil || !back.SeparateApproval() {
t.Fatalf("stored and read back: %+v %v", back, err)
}
if strings.Contains(Canonical(ds), "company_rules") {
t.Fatal("a dataset without rules stores none")
}
}
+35
View File
@@ -24,6 +24,7 @@ import (
// PUT /v1/tenants/{id}/role-dataset save (user token only)
// GET /v1/tenants/{id}/role-dataset/revisions/{rev} the dataset a decision was made under
// GET /v1/tenants/{id}/role-dataset/resolve who may decide a decision type now
// GET /v1/tenants/{id}/role-dataset/rules the company rules now (docgen#4 Nr. 31)
//
// Reads: a user token of the tenant, or a service with tenants.read
// (Werkmeister). Writes come only from a user token: the writer is the
@@ -61,6 +62,15 @@ type roleDatasetHoldersResp struct {
*roles.FunctionHolders
}
type roleDatasetRulesResp struct {
Revision int `json:"revision"`
CatalogueVersion string `json:"catalogue_version"`
// SeparateApproval: "Festlegung und Freigabe durch verschiedene
// Personen" is on. Off when the company never set it.
SeparateApproval bool `json:"separate_approval"`
RecordedAt *time.Time `json:"recorded_at,omitempty"`
}
type roleDatasetResolveResp struct {
Revision int `json:"revision"`
CatalogueVersion string `json:"catalogue_version"`
@@ -285,6 +295,31 @@ func (s *Server) roleDatasetHolders(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, out)
}
// roleDatasetRules is GET /v1/tenants/{id}/role-dataset/rules: the
// company rules now, for a Werkmeister check that applies only while a rule
// is on.
func (s *Server) roleDatasetRules(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
defer cancel()
t, ok := s.tenantInScope(ctx, w, r)
if !ok {
return
}
stored, ds, err := s.currentRoleDataset(ctx, t.ID)
if err != nil {
writeError(w, http.StatusInternalServerError, "internal", err.Error())
return
}
out := roleDatasetRulesResp{CatalogueVersion: roles.CatalogueVersion, SeparateApproval: ds.SeparateApproval()}
if stored != nil {
out.Revision, out.CatalogueVersion = stored.Revision, stored.CatalogueVersion
}
if ds.CompanyRules != nil {
out.RecordedAt = ds.CompanyRules.RecordedAt
}
writeJSON(w, http.StatusOK, out)
}
// resolveRoleDataset is GET /v1/tenants/{id}/role-dataset/resolve
// ?decision_type=…[&document=…|&product=…|&area=…].
func (s *Server) resolveRoleDataset(w http.ResponseWriter, r *http.Request) {
+22
View File
@@ -256,6 +256,7 @@ func TestRoleDatasetPath(t *testing.T) {
"/v1/tenants/x/role-dataset": true,
"/v1/tenants/x/role-dataset/resolve": true,
"/v1/tenants/x/role-dataset/holders": true,
"/v1/tenants/x/role-dataset/rules": true,
"/v1/tenants/x/role-dataset/revisions/2": true,
"/v1/tenants/x/members": false,
"/v1/tenants/role-dataset": false,
@@ -267,3 +268,24 @@ func TestRoleDatasetPath(t *testing.T) {
}
}
}
// Nr. 31: Werkmeister reads the company rules; off until management or a
// maintainer switches the rule on.
func TestRoleDataset_rules(t *testing.T) {
f := newRoleFixture(t)
code, out, raw := f.do(t, "werkmeister", "GET", f.path("/rules"), "")
if code != http.StatusOK || out["separate_approval"] != false || out["revision"].(float64) != 0 {
t.Fatalf("no dataset: %d %s", code, raw)
}
if code, _, raw := f.do(t, "signup", "PUT", f.path(""), saveBody(mgmtOnly, 0)); code != http.StatusOK {
t.Fatalf("bootstrap: %d %s", code, raw)
}
on := strings.TrimSuffix(mgmtOnly, "}") + `,"company_rules":{"separate_approval":true}}`
if code, _, raw := f.do(t, "ceo", "PUT", f.path(""), saveBody(on, 1)); code != http.StatusOK {
t.Fatalf("switch on: %d %s", code, raw)
}
code, out, raw = f.do(t, "werkmeister", "GET", f.path("/rules"), "")
if code != http.StatusOK || out["separate_approval"] != true || out["revision"].(float64) != 2 || out["recorded_at"] == nil {
t.Fatalf("switched on: %d %s", code, raw)
}
}
+1
View File
@@ -169,6 +169,7 @@ func NewRouter(s *Server) http.Handler {
mux.HandleFunc("GET /v1/tenants/{id}/role-dataset/revisions/{rev}", s.getRoleDatasetRevision)
mux.HandleFunc("GET /v1/tenants/{id}/role-dataset/resolve", s.resolveRoleDataset)
mux.HandleFunc("GET /v1/tenants/{id}/role-dataset/holders", s.roleDatasetHolders)
mux.HandleFunc("GET /v1/tenants/{id}/role-dataset/rules", s.roleDatasetRules)
// Prüfwerk settings (0025, t72): the workspace's Prüftag cap.
// R1 intake (0028, docgen#6 F01): intake.go.
mux.HandleFunc("POST /v1/tenants/{id}/intake/reports", s.createIntakeReport)