One Rollen- und Befugnisdatensatz per company (docgen#1 plan 13781 §3.2
no. 2, owner addendum 13791): functions, who holds them, deputies with
their basis, the deciding function per decision type, immediate
authority, standard releases, named editors and the contact path for
critical security events.
- internal/roles: the catalogue in code (version r1-2026-10-26.1), the
union of A1 §2.3 and A4 §4 = 21 functions, 14 customer decision types;
only the three releases carry an owner default (management), the rest
must be set by the company.
- Rules: a deputy is wirksam only with a documented basis, within its
validity and, where it covers management authority, with management's
authorization (B7 wording: what management records it has authorized);
another release body for R1-D02 updates only with the R1-D02 provision
(B8); management and document owners write, the OWNER sign-in role may
only record the first management holder; recorded_*/authorized_* are
stamped from the token, never from the body; no self-authorization.
- Routes: GET/PUT /v1/tenants/{id}/role-dataset, GET …/revisions/{rev},
GET …/resolve. Writes need the user's token; user tokens on these
routes form route group role-dataset.user, so the portal's sign-in
client needs no tenants grant.
- Migration 0024: role_datasets and role_dataset_revisions, versioned as
the company profile (expected_revision, 409 on conflict).
- Audit role_dataset.saved with revision and changed entry ids only.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014aRnCGqELxoix5TfpX8Ejf
112 lines
3.6 KiB
Go
112 lines
3.6 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
func scanRoleDataset(row pgx.Row) (*RoleDataset, error) {
|
|
var d RoleDataset
|
|
var data []byte
|
|
if err := row.Scan(&d.TenantID, &data, &d.CatalogueVersion, &d.Revision, &d.UpdatedBy, &d.UpdatedAt); err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
d.Data = json.RawMessage(data)
|
|
return &d, nil
|
|
}
|
|
|
|
const roleDatasetSelect = `
|
|
SELECT tenant_id::text, data, catalogue_version, revision, updated_by, updated_at
|
|
FROM role_datasets `
|
|
|
|
func (p *Postgres) GetRoleDataset(ctx context.Context, tenantID string) (*RoleDataset, error) {
|
|
d, err := scanRoleDataset(p.pool.QueryRow(ctx, roleDatasetSelect+`WHERE tenant_id = $1::uuid`, tenantID))
|
|
if isInvalidUUID(err) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return d, err
|
|
}
|
|
|
|
func (p *Postgres) GetRoleDatasetRevision(ctx context.Context, tenantID string, revision int) (*RoleDataset, error) {
|
|
d, err := scanRoleDataset(p.pool.QueryRow(ctx, `
|
|
SELECT tenant_id::text, data, catalogue_version, revision, changed_by, changed_at
|
|
FROM role_dataset_revisions WHERE tenant_id = $1::uuid AND revision = $2`, tenantID, revision))
|
|
if isInvalidUUID(err) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return d, err
|
|
}
|
|
|
|
// SaveRoleDataset writes the dataset and its revision row in one
|
|
// transaction, with the row locked against a concurrent save.
|
|
func (p *Postgres) SaveRoleDataset(ctx context.Context, w RoleDatasetWrite) (*RoleDataset, bool, error) {
|
|
if !json.Valid(w.Data) || w.CatalogueVersion == "" || w.By == "" {
|
|
return nil, false, ErrInvalidInput
|
|
}
|
|
tx, err := p.pool.Begin(ctx)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
defer func() { _ = tx.Rollback(ctx) }()
|
|
cur, err := scanRoleDataset(tx.QueryRow(ctx, roleDatasetSelect+`WHERE tenant_id = $1::uuid FOR UPDATE`, w.TenantID))
|
|
switch {
|
|
case errors.Is(err, ErrNotFound):
|
|
cur = nil
|
|
case isInvalidUUID(err):
|
|
return nil, false, ErrNotFound
|
|
case err != nil:
|
|
return nil, false, err
|
|
}
|
|
current := 0
|
|
if cur != nil {
|
|
current = cur.Revision
|
|
}
|
|
if w.ExpectedRevision != current {
|
|
return nil, false, &RevisionConflict{Current: current}
|
|
}
|
|
if cur != nil && cur.CatalogueVersion == w.CatalogueVersion && sameJSON(cur.Data, w.Data) {
|
|
return cur, false, nil
|
|
}
|
|
next := current + 1
|
|
if cur == nil {
|
|
_, err = tx.Exec(ctx, `
|
|
INSERT INTO role_datasets (tenant_id, data, catalogue_version, revision, updated_by, updated_at)
|
|
VALUES ($1::uuid, $2::jsonb, $3, $4, $5, $6)`,
|
|
w.TenantID, string(w.Data), w.CatalogueVersion, next, w.By, w.At.UTC())
|
|
} else {
|
|
_, err = tx.Exec(ctx, `
|
|
UPDATE role_datasets SET data = $2::jsonb, catalogue_version = $3, revision = $4, updated_by = $5, updated_at = $6
|
|
WHERE tenant_id = $1::uuid`,
|
|
w.TenantID, string(w.Data), w.CatalogueVersion, next, w.By, w.At.UTC())
|
|
}
|
|
switch {
|
|
case err == nil:
|
|
case isFKViolation(err):
|
|
return nil, false, ErrNotFound
|
|
case isUniqueViolation(err):
|
|
// A concurrent first save won: report its revision.
|
|
return nil, false, &RevisionConflict{Current: 1}
|
|
case isCheckViolation(err):
|
|
return nil, false, ErrInvalidInput
|
|
default:
|
|
return nil, false, err
|
|
}
|
|
if _, err := tx.Exec(ctx, `
|
|
INSERT INTO role_dataset_revisions (tenant_id, revision, data, catalogue_version, changed_by, changed_at)
|
|
SELECT tenant_id, revision, data, catalogue_version, updated_by, updated_at
|
|
FROM role_datasets WHERE tenant_id = $1::uuid AND revision = $2`, w.TenantID, next); err != nil {
|
|
return nil, false, err
|
|
}
|
|
if err := tx.Commit(ctx); err != nil {
|
|
return nil, false, err
|
|
}
|
|
saved, err := p.GetRoleDataset(ctx, w.TenantID)
|
|
return saved, true, err
|
|
}
|