Files
pages/internal/store/read_policy.go
T
Anya Tokugawa d5d21b7841
Build and publish container images / images (push) Successful in 9m34s
Release container images / release (push) Successful in 36s
Refactor architecture into eight components and consolidate reliability work
Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts.

Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation.

Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
2026-11-01 15:23:31 +09:00

80 lines
2.7 KiB
Go

package store
import (
"errors"
"strings"
"pageswiki/internal/schutzaria"
)
// ReadPathPolicy is a request-scoped, read-only policy snapshot.
type ReadPathPolicy = schutzaria.ReadPathPolicy
// LoadReadPathPolicy uses one bounded query, independent of the number of
// directory entries. It deliberately excludes Object grants: an Object grant
// does not reveal all of that Object's semantic aliases.
func (s *Store) LoadReadPathPolicy(domain, principal string) (*ReadPathPolicy, error) {
return loadReadPathPolicy(s.DB, domain, principal)
}
func loadReadPathPolicy(q pathQueryer, domain, principal string) (*ReadPathPolicy, error) {
var all bool
var exact, subtrees []string
if domain == "" || principal == "" {
return &ReadPathPolicy{}, ErrAccessDenied
}
rows, err := q.Query(`
SELECT 'role',g.target_type,g.target_id FROM principals p
JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=? AND m.state='active'
JOIN auth_role_bindings b ON b.membership_uuid=m.uuid
JOIN auth_roles r ON r.uuid=b.role_uuid AND r.domain_uuid=m.domain_uuid
JOIN auth_capability_grants g ON g.role_uuid=r.uuid AND g.capability='read'
WHERE p.uuid=? AND p.state IN ('active','readonly') AND g.target_type IN ('domain','path')
UNION ALL
SELECT 'group','path',gp.scope_path FROM principals p
JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=? AND m.state='active'
JOIN group_members gm ON gm.membership_uuid=m.uuid
JOIN groups g ON g.uuid=gm.group_uuid AND g.domain_uuid=m.domain_uuid AND g.state='active'
JOIN group_permissions gp ON gp.group_uuid=g.uuid AND gp.permission IN ('read','read_write','read_write_once')
WHERE p.uuid=? AND p.state IN ('active','readonly')
LIMIT 4097`, domain, principal, domain, principal)
if err != nil {
return nil, err
}
defer rows.Close()
count := 0
for rows.Next() {
count++
if count > 4096 {
return nil, errors.New("too many read grants for bounded projection")
}
var source, kind, target string
if err := rows.Scan(&source, &kind, &target); err != nil {
return nil, err
}
if source == "role" {
if kind == "domain" && target == domain || kind == "path" && target == "*" {
all = true
} else if kind == "path" {
exact = append(exact, target)
}
} else if target == "/" {
all = true
} else if strings.HasPrefix(target, "/") {
exact = append(exact, target)
subtrees = append(subtrees, strings.TrimRight(target, "/"))
}
}
if err = rows.Err(); err != nil {
return nil, err
}
if err = rows.Close(); err != nil {
return nil, err
}
access, err := loadLocalityAccess(q, domain, principal)
if err != nil {
return nil, err
}
return schutzaria.NewReadPathPolicy(all, exact, subtrees, access), nil
}