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.
442 lines
19 KiB
Go
442 lines
19 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"path"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"pageswiki/internal/schutzaria"
|
|
)
|
|
|
|
const MaxUserSpaceOverrides = 256
|
|
|
|
type UserSpacePathPolicy = schutzaria.UserSpacePathPolicy
|
|
type UserSpacePolicy = schutzaria.UserSpacePolicy
|
|
|
|
type UserSpacePage struct {
|
|
Path string
|
|
Visibility string
|
|
Override string
|
|
}
|
|
|
|
func (s *Store) EnsureUserSpaceSchema() error {
|
|
for _, query := range []string{
|
|
`CREATE TABLE IF NOT EXISTS user_space_policies(
|
|
domain_uuid TEXT NOT NULL, principal_uuid TEXT NOT NULL,
|
|
profile_visibility TEXT NOT NULL CHECK(profile_visibility IN ('private','domain','public')),
|
|
avatar_visibility TEXT NOT NULL CHECK(avatar_visibility IN ('private','domain','public')),
|
|
root_visibility TEXT NOT NULL CHECK(root_visibility IN ('private','domain','public')),
|
|
descendant_visibility TEXT NOT NULL CHECK(descendant_visibility IN ('private','domain','public')),
|
|
version INTEGER NOT NULL, actor_uuid TEXT NOT NULL, updated_at_ms INTEGER NOT NULL,
|
|
PRIMARY KEY(domain_uuid,principal_uuid))`,
|
|
`CREATE TABLE IF NOT EXISTS user_space_path_policies(
|
|
domain_uuid TEXT NOT NULL, principal_uuid TEXT NOT NULL, path TEXT NOT NULL,
|
|
visibility TEXT NOT NULL CHECK(visibility IN ('private','domain')),
|
|
actor_uuid TEXT NOT NULL, updated_at_ms INTEGER NOT NULL,
|
|
PRIMARY KEY(domain_uuid,principal_uuid,path))`,
|
|
`CREATE INDEX IF NOT EXISTS idx_user_space_path_policy ON user_space_path_policies(domain_uuid,path,principal_uuid)`,
|
|
`CREATE TABLE IF NOT EXISTS user_space_policy_events(
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT, domain_uuid TEXT NOT NULL, principal_uuid TEXT NOT NULL,
|
|
actor_uuid TEXT NOT NULL, before_json TEXT NOT NULL, after_json TEXT NOT NULL, created_at_ms INTEGER NOT NULL)`,
|
|
`CREATE INDEX IF NOT EXISTS idx_user_space_policy_events ON user_space_policy_events(domain_uuid,principal_uuid,id DESC)`,
|
|
`CREATE TRIGGER IF NOT EXISTS user_space_path_policy_move BEFORE UPDATE OF path,domain_uuid ON paths
|
|
WHEN EXISTS(SELECT 1 FROM user_space_path_policies usp WHERE usp.domain_uuid=OLD.domain_uuid AND usp.path=OLD.path)
|
|
AND NOT EXISTS(SELECT 1 FROM user_space_path_policies usp JOIN principal_identities i ON i.principal_uuid=usp.principal_uuid AND i.provider_type='password'
|
|
WHERE usp.domain_uuid=OLD.domain_uuid AND usp.path=OLD.path AND NEW.domain_uuid=OLD.domain_uuid AND substr(NEW.path,1,length('/u/'||i.subject||'/'))='/u/'||i.subject||'/')
|
|
BEGIN SELECT RAISE(ABORT,'user_space_policy_scope'); END`,
|
|
`CREATE TRIGGER IF NOT EXISTS user_space_path_policy_rename AFTER UPDATE OF path ON paths
|
|
BEGIN UPDATE user_space_path_policies SET path=NEW.path WHERE domain_uuid=OLD.domain_uuid AND path=OLD.path; END`,
|
|
`CREATE TRIGGER IF NOT EXISTS user_space_path_policy_delete AFTER DELETE ON paths
|
|
BEGIN DELETE FROM user_space_path_policies WHERE domain_uuid=OLD.domain_uuid AND path=OLD.path; END`,
|
|
} {
|
|
if _, err := s.DB.Exec(query); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return s.ensureNavigationSchema()
|
|
}
|
|
|
|
func defaultUserSpacePolicy(principal, username string) UserSpacePolicy {
|
|
return UserSpacePolicy{PrincipalUUID: principal, Username: username, ProfileVisibility: "domain", AvatarVisibility: "domain", RootVisibility: "domain", DescendantVisibility: "domain", Overrides: []UserSpacePathPolicy{}}
|
|
}
|
|
|
|
func userSpaceVisibilityRank(value string) int { return schutzaria.UserSpaceVisibilityRank(value) }
|
|
|
|
func userSpaceRoot(username string) string { return "/u/" + username }
|
|
|
|
func escapeUserSpaceLike(value string) string {
|
|
value = strings.ReplaceAll(value, "\\", "\\\\")
|
|
value = strings.ReplaceAll(value, "%", "\\%")
|
|
return strings.ReplaceAll(value, "_", "\\_")
|
|
}
|
|
|
|
func userSpaceDocumentPath(username, value string, descendant bool) bool {
|
|
return schutzaria.UserSpaceDocumentPath(username, value, descendant)
|
|
}
|
|
|
|
func (s *Store) domainAllowsUserSpacePublic(ctx context.Context, q interface {
|
|
QueryRowContext(context.Context, string, ...any) *sql.Row
|
|
}, domain string) (bool, error) {
|
|
var allowed bool
|
|
err := q.QueryRowContext(ctx, `SELECT EXISTS(
|
|
SELECT 1 FROM domains d JOIN campuses c ON c.uuid=d.campus_uuid
|
|
JOIN config cfg ON cfg.domain_uuid=d.uuid AND cfg.key='authorization_mode' AND cfg.value='enforcing'
|
|
JOIN locality_publication_policies p ON p.domain_uuid=d.uuid AND p.node_kind='domain' AND p.node_uuid=d.uuid AND p.policy='public'
|
|
WHERE d.uuid=? AND d.enabled=1 AND c.enabled=1)`, domain).Scan(&allowed)
|
|
return allowed, err
|
|
}
|
|
|
|
func loadUserSpacePolicy(ctx context.Context, q interface {
|
|
QueryRowContext(context.Context, string, ...any) *sql.Row
|
|
QueryContext(context.Context, string, ...any) (*sql.Rows, error)
|
|
}, domain, principal, username string) (UserSpacePolicy, error) {
|
|
p := defaultUserSpacePolicy(principal, username)
|
|
err := q.QueryRowContext(ctx, `SELECT profile_visibility,avatar_visibility,root_visibility,descendant_visibility,version FROM user_space_policies WHERE domain_uuid=? AND principal_uuid=?`, domain, principal).Scan(&p.ProfileVisibility, &p.AvatarVisibility, &p.RootVisibility, &p.DescendantVisibility, &p.Version)
|
|
if err != nil && err != sql.ErrNoRows {
|
|
return p, err
|
|
}
|
|
rows, err := q.QueryContext(ctx, `SELECT path,visibility FROM user_space_path_policies WHERE domain_uuid=? AND principal_uuid=? ORDER BY path LIMIT ?`, domain, principal, MaxUserSpaceOverrides+1)
|
|
if err != nil {
|
|
return p, err
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var item UserSpacePathPolicy
|
|
if err = rows.Scan(&item.Path, &item.Visibility); err != nil {
|
|
return p, err
|
|
}
|
|
p.Overrides = append(p.Overrides, item)
|
|
}
|
|
if err = rows.Err(); err != nil {
|
|
return p, err
|
|
}
|
|
if len(p.Overrides) > MaxUserSpaceOverrides {
|
|
return p, ErrGovernanceInvalid
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
func (s *Store) UserSpacePolicy(ctx context.Context, domain, principal string) (UserSpacePolicy, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
defer cancel()
|
|
var username string
|
|
if err := s.DB.QueryRowContext(ctx, `SELECT i.subject FROM principal_identities i JOIN domain_memberships m ON m.principal_uuid=i.principal_uuid AND m.domain_uuid=? AND m.state='active' JOIN principals p ON p.uuid=i.principal_uuid AND p.state IN ('active','readonly') WHERE i.principal_uuid=? AND i.provider_type='password'`, domain, principal).Scan(&username); err != nil {
|
|
return UserSpacePolicy{}, err
|
|
}
|
|
p, err := loadUserSpacePolicy(ctx, s.DB, domain, principal, username)
|
|
if err != nil {
|
|
return p, err
|
|
}
|
|
p.DomainAllowsPublic, err = s.domainAllowsUserSpacePublic(ctx, s.DB, domain)
|
|
return p, err
|
|
}
|
|
|
|
func userSpaceEffectiveVisibility(p UserSpacePolicy, target string) string {
|
|
return schutzaria.EffectiveUserSpaceVisibility(p, target)
|
|
}
|
|
|
|
func EffectiveUserSpaceVisibility(p UserSpacePolicy, target string) string {
|
|
return userSpaceEffectiveVisibility(p, target)
|
|
}
|
|
|
|
func renameUserSpacePathPoliciesTx(tx *sql.Tx, domain, oldPath, newPath string, recursive bool) error {
|
|
// Path mutation is also used by small maintenance/test stores that predate
|
|
// the optional authentication schemas. Keep the core rename primitive usable
|
|
// there; a fully opened application Store always creates this table.
|
|
var tableExists bool
|
|
if err := tx.QueryRow(`SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name='user_space_path_policies')`).Scan(&tableExists); err != nil {
|
|
return err
|
|
}
|
|
if !tableExists {
|
|
return nil
|
|
}
|
|
query := `SELECT usp.principal_uuid,usp.path,i.subject FROM user_space_path_policies usp JOIN principal_identities i ON i.principal_uuid=usp.principal_uuid AND i.provider_type='password' WHERE usp.domain_uuid=? AND usp.path=?`
|
|
args := []any{domain, oldPath}
|
|
if recursive {
|
|
query = `SELECT usp.principal_uuid,usp.path,i.subject FROM user_space_path_policies usp JOIN principal_identities i ON i.principal_uuid=usp.principal_uuid AND i.provider_type='password' WHERE usp.domain_uuid=? AND (usp.path=? OR substr(usp.path,1,length(?)+1)=?||'/') ORDER BY length(usp.path) DESC`
|
|
args = []any{domain, oldPath, oldPath, oldPath}
|
|
}
|
|
rows, err := tx.Query(query, args...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
type move struct{ principal, old, new, username string }
|
|
moves := []move{}
|
|
for rows.Next() {
|
|
var item move
|
|
if err = rows.Scan(&item.principal, &item.old, &item.username); err != nil {
|
|
rows.Close()
|
|
return err
|
|
}
|
|
item.new = newPath + strings.TrimPrefix(item.old, oldPath)
|
|
if !userSpaceDocumentPath(item.username, item.new, true) {
|
|
rows.Close()
|
|
return ErrGovernanceInvalid
|
|
}
|
|
moves = append(moves, item)
|
|
}
|
|
if err = rows.Err(); err != nil {
|
|
rows.Close()
|
|
return err
|
|
}
|
|
rows.Close()
|
|
for _, item := range moves {
|
|
if _, err = tx.Exec(`UPDATE user_space_path_policies SET path=? WHERE domain_uuid=? AND principal_uuid=? AND path=?`, item.new, domain, item.principal, item.old); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Store) SaveUserSpacePolicy(ctx context.Context, domain, actor string, proposed UserSpacePolicy) (UserSpacePolicy, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
|
defer cancel()
|
|
tx, err := s.DB.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return UserSpacePolicy{}, err
|
|
}
|
|
defer tx.Rollback()
|
|
// Reserve the writer before authority and ceiling checks.
|
|
if _, err = tx.ExecContext(ctx, `INSERT INTO navigation_generation(id,version) VALUES(1,1) ON CONFLICT(id) DO UPDATE SET version=version`); err != nil {
|
|
return UserSpacePolicy{}, err
|
|
}
|
|
var username string
|
|
if err = tx.QueryRowContext(ctx, `SELECT i.subject FROM principal_identities i JOIN domain_memberships m ON m.principal_uuid=i.principal_uuid AND m.domain_uuid=? AND m.state='active' JOIN principals p ON p.uuid=i.principal_uuid AND p.state='active' WHERE i.principal_uuid=? AND i.provider_type='password'`, domain, actor).Scan(&username); err != nil {
|
|
return UserSpacePolicy{}, ErrAccessDenied
|
|
}
|
|
if proposed.PrincipalUUID != "" && proposed.PrincipalUUID != actor || proposed.Username != "" && proposed.Username != username {
|
|
return UserSpacePolicy{}, ErrAccessDenied
|
|
}
|
|
current, err := loadUserSpacePolicy(ctx, tx, domain, actor, username)
|
|
if err != nil {
|
|
return UserSpacePolicy{}, err
|
|
}
|
|
if proposed.Version != current.Version {
|
|
return current, ErrGovernanceConflict
|
|
}
|
|
for _, value := range []string{proposed.ProfileVisibility, proposed.AvatarVisibility, proposed.RootVisibility, proposed.DescendantVisibility} {
|
|
if userSpaceVisibilityRank(value) < 0 {
|
|
return current, ErrGovernanceInvalid
|
|
}
|
|
}
|
|
allowed, err := s.domainAllowsUserSpacePublic(ctx, tx, domain)
|
|
if err != nil {
|
|
return current, err
|
|
}
|
|
if !allowed && (proposed.ProfileVisibility == "public" || proposed.AvatarVisibility == "public" || proposed.RootVisibility == "public" || proposed.DescendantVisibility == "public") {
|
|
return current, ErrGovernanceCeiling
|
|
}
|
|
if len(proposed.Overrides) > MaxUserSpaceOverrides {
|
|
return current, ErrGovernanceInvalid
|
|
}
|
|
seen := map[string]bool{}
|
|
for i := range proposed.Overrides {
|
|
item := &proposed.Overrides[i]
|
|
item.Path = path.Clean(item.Path)
|
|
if seen[item.Path] || !userSpaceDocumentPath(username, item.Path, true) || userSpaceVisibilityRank(item.Visibility) < 0 || userSpaceVisibilityRank(item.Visibility) > userSpaceVisibilityRank(proposed.DescendantVisibility) || item.Visibility == "public" {
|
|
return current, ErrGovernanceInvalid
|
|
}
|
|
seen[item.Path] = true
|
|
var exists bool
|
|
if err = tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM paths WHERE domain_uuid=? AND path=?)`, domain, item.Path).Scan(&exists); err != nil || !exists {
|
|
return current, ErrGovernanceInvalid
|
|
}
|
|
}
|
|
sort.Slice(proposed.Overrides, func(i, j int) bool { return proposed.Overrides[i].Path < proposed.Overrides[j].Path })
|
|
proposed.PrincipalUUID, proposed.Username, proposed.DomainAllowsPublic = actor, username, allowed
|
|
before, _ := json.Marshal(current)
|
|
now := time.Now().UnixMilli()
|
|
if _, err = tx.ExecContext(ctx, `INSERT INTO user_space_policies(domain_uuid,principal_uuid,profile_visibility,avatar_visibility,root_visibility,descendant_visibility,version,actor_uuid,updated_at_ms) VALUES(?,?,?,?,?,?,1,?,?) ON CONFLICT(domain_uuid,principal_uuid) DO UPDATE SET profile_visibility=excluded.profile_visibility,avatar_visibility=excluded.avatar_visibility,root_visibility=excluded.root_visibility,descendant_visibility=excluded.descendant_visibility,version=user_space_policies.version+1,actor_uuid=excluded.actor_uuid,updated_at_ms=excluded.updated_at_ms`, domain, actor, proposed.ProfileVisibility, proposed.AvatarVisibility, proposed.RootVisibility, proposed.DescendantVisibility, actor, now); err != nil {
|
|
return current, err
|
|
}
|
|
if _, err = tx.ExecContext(ctx, `DELETE FROM user_space_path_policies WHERE domain_uuid=? AND principal_uuid=?`, domain, actor); err != nil {
|
|
return current, err
|
|
}
|
|
for _, item := range proposed.Overrides {
|
|
if _, err = tx.ExecContext(ctx, `INSERT INTO user_space_path_policies VALUES(?,?,?,?,?,?)`, domain, actor, item.Path, item.Visibility, actor, now); err != nil {
|
|
return current, err
|
|
}
|
|
}
|
|
proposed.Version = current.Version + 1
|
|
after, _ := json.Marshal(proposed)
|
|
if _, err = tx.ExecContext(ctx, `INSERT INTO user_space_policy_events(domain_uuid,principal_uuid,actor_uuid,before_json,after_json,created_at_ms) VALUES(?,?,?,?,?,?)`, domain, actor, actor, string(before), string(after), now); err != nil {
|
|
return current, err
|
|
}
|
|
if err = tx.Commit(); err != nil {
|
|
return current, err
|
|
}
|
|
return proposed, nil
|
|
}
|
|
|
|
func (s *Store) ListUserSpacePages(ctx context.Context, domain, principal string, limit int) ([]UserSpacePage, error) {
|
|
if limit <= 0 || limit > MaxNavigationEntries {
|
|
limit = 512
|
|
}
|
|
p, err := s.UserSpacePolicy(ctx, domain, principal)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rows, err := s.DB.QueryContext(ctx, `SELECT path FROM paths WHERE domain_uuid=? AND path LIKE ? ESCAPE '\' ORDER BY path LIMIT ?`, domain, escapeUserSpaceLike(userSpaceRoot(p.Username))+"/%", limit+1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
result := []UserSpacePage{}
|
|
explicit := map[string]string{}
|
|
for _, item := range p.Overrides {
|
|
explicit[item.Path] = item.Visibility
|
|
}
|
|
for rows.Next() {
|
|
var item UserSpacePage
|
|
if err = rows.Scan(&item.Path); err != nil {
|
|
return nil, err
|
|
}
|
|
if !userSpaceDocumentPath(p.Username, item.Path, true) {
|
|
continue
|
|
}
|
|
item.Visibility = userSpaceEffectiveVisibility(p, item.Path)
|
|
item.Override = explicit[item.Path]
|
|
result = append(result, item)
|
|
}
|
|
if len(result) > limit {
|
|
return nil, ErrNavigationBudget
|
|
}
|
|
return result, rows.Err()
|
|
}
|
|
|
|
func (s *Store) UserSpaceCanReadAuthenticated(ctx context.Context, domain, viewer, target string) (bool, error) {
|
|
parts := strings.Split(strings.TrimPrefix(target, "/u/"), "/")
|
|
if len(parts) == 0 || parts[0] == "" {
|
|
return false, nil
|
|
}
|
|
owner, err := s.PrincipalByUsername(domain, parts[0])
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if viewer == owner.UUID || s.IsBuiltinGroupAdmin(domain, viewer, "domain_admin") {
|
|
return true, nil
|
|
}
|
|
p, err := s.UserSpacePolicy(ctx, domain, owner.UUID)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
root := userSpaceRoot(p.Username)
|
|
if target == root+"/_/avatar" {
|
|
return p.AvatarVisibility != "private", nil
|
|
}
|
|
if strings.HasPrefix(target, root+"/_/") {
|
|
return false, nil
|
|
}
|
|
if target == root {
|
|
return p.ProfileVisibility != "private" || p.RootVisibility != "private", nil
|
|
}
|
|
return userSpaceEffectiveVisibility(p, target) != "private", nil
|
|
}
|
|
|
|
func chooseRevisionLanguage(items []*Revision, languages []string) (*Revision, error) {
|
|
if len(items) == 0 || len(items) > 32 {
|
|
return nil, sql.ErrNoRows
|
|
}
|
|
for _, language := range languages {
|
|
for _, item := range items {
|
|
if item.Language == language {
|
|
return item, nil
|
|
}
|
|
}
|
|
}
|
|
return items[0], nil
|
|
}
|
|
|
|
func (s *Store) PublicUserSpaceRevision(ctx context.Context, domain, username, target string, languages []string) (*Revision, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
defer cancel()
|
|
owner, err := s.PrincipalByUsername(domain, username)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
p, err := s.UserSpacePolicy(ctx, domain, owner.UUID)
|
|
if err != nil || !p.DomainAllowsPublic || userSpaceEffectiveVisibility(p, target) != "public" {
|
|
return nil, sql.ErrNoRows
|
|
}
|
|
rows, err := s.DB.QueryContext(ctx, `SELECT r.uuid,r.object_uuid,r.document_hash,COALESCE(NULLIF(r.lang,''),'und'),COALESCE(NULLIF(r.content_type,''),'text/html; charset=utf-8'),COALESCE(r.filename,''),r.created_at_ms
|
|
FROM paths p JOIN revisions r ON r.object_uuid=p.object_uuid
|
|
WHERE p.domain_uuid=? AND p.path=?
|
|
AND NOT EXISTS(SELECT 1 FROM paths other WHERE other.object_uuid=p.object_uuid AND (other.domain_uuid!=p.domain_uuid OR other.path!=p.path))
|
|
AND r.uuid=(SELECT head.uuid FROM revisions head WHERE head.object_uuid=r.object_uuid AND COALESCE(NULLIF(head.lang,''),'und')=COALESCE(NULLIF(r.lang,''),'und') ORDER BY head.created_at_ms DESC,head.rowid DESC LIMIT 1)
|
|
ORDER BY r.created_at_ms DESC,r.uuid LIMIT 33`, domain, target)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []*Revision{}
|
|
for rows.Next() {
|
|
v := &Revision{}
|
|
if err = rows.Scan(&v.UUID, &v.ObjectUUID, &v.DocumentHash, &v.Language, &v.ContentType, &v.Filename, &v.CreatedAtMS); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, v)
|
|
}
|
|
if err = rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return chooseRevisionLanguage(items, languages)
|
|
}
|
|
|
|
func (s *Store) PublicUserSpaceNavigation(ctx context.Context, domain, username string) ([]PublishedNavigationPage, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
defer cancel()
|
|
owner, err := s.PrincipalByUsername(domain, username)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
p, err := s.UserSpacePolicy(ctx, domain, owner.UUID)
|
|
if err != nil || !p.DomainAllowsPublic {
|
|
return nil, sql.ErrNoRows
|
|
}
|
|
root := userSpaceRoot(username)
|
|
rows, err := s.DB.QueryContext(ctx, `SELECT p.path,r.uuid,r.object_uuid,r.document_hash,COALESCE(NULLIF(r.lang,''),'und'),COALESCE(NULLIF(r.content_type,''),'text/html; charset=utf-8'),COALESCE(r.filename,''),r.created_at_ms
|
|
FROM paths p JOIN revisions r ON r.object_uuid=p.object_uuid
|
|
WHERE p.domain_uuid=? AND (p.path=? OR p.path LIKE ? ESCAPE '\')
|
|
AND NOT EXISTS(SELECT 1 FROM paths other WHERE other.object_uuid=p.object_uuid AND (other.domain_uuid!=p.domain_uuid OR other.path!=p.path))
|
|
AND r.uuid=(SELECT head.uuid FROM revisions head WHERE head.object_uuid=r.object_uuid AND COALESCE(NULLIF(head.lang,''),'und')=COALESCE(NULLIF(r.lang,''),'und') ORDER BY head.created_at_ms DESC,head.rowid DESC LIMIT 1)
|
|
ORDER BY p.path,r.created_at_ms DESC,r.uuid LIMIT ?`, domain, root, escapeUserSpaceLike(root)+"/%", MaxNavigationEntries+1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
result := []PublishedNavigationPage{}
|
|
for rows.Next() {
|
|
var item PublishedNavigationPage
|
|
v := &item.Revision
|
|
if err = rows.Scan(&item.Path, &v.UUID, &v.ObjectUUID, &v.DocumentHash, &v.Language, &v.ContentType, &v.Filename, &v.CreatedAtMS); err != nil {
|
|
return nil, err
|
|
}
|
|
if userSpaceEffectiveVisibility(p, item.Path) == "public" && (item.Path == root || userSpaceDocumentPath(username, item.Path, true)) {
|
|
result = append(result, item)
|
|
}
|
|
}
|
|
if err = rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
if len(result) > MaxNavigationEntries {
|
|
return nil, ErrNavigationBudget
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func IsUserSpacePath(value string) bool {
|
|
parts := strings.Split(strings.TrimPrefix(value, "/u/"), "/")
|
|
return strings.HasPrefix(value, "/u/") && len(parts) > 0 && parts[0] != ""
|
|
}
|
|
|
|
func UserSpaceUsername(value string) string {
|
|
if !IsUserSpacePath(value) {
|
|
return ""
|
|
}
|
|
return strings.Split(strings.TrimPrefix(value, "/u/"), "/")[0]
|
|
}
|