Files
gooseek/backend/internal/db/space_repo.go
home a0e3748dde feat: auth service + security audit fixes + cleanup legacy services
Major changes:
- Add auth-svc: JWT auth, register/login/refresh, password reset
- Add auth UI: modals, pages (/login, /register, /forgot-password)
- Add usage tracking (usage_metrics table, daily limits)
- Add tiered rate limiting (free/pro/business)
- Add LLM usage limits per tier

Security fixes:
- All repos now require userID for Update/Delete operations
- JWT middleware in chat-svc, llm-svc, agent-svc, discover-svc
- ErrNotFound/ErrForbidden errors for proper access control

Cleanup:
- Remove legacy TypeScript services/ directory
- Remove computer-svc (to be reimplemented)
- Remove old deploy/docker configs

New files:
- backend/cmd/auth-svc/main.go
- backend/internal/auth/{types,repository}.go
- backend/internal/usage/{types,repository}.go
- backend/pkg/middleware/{llm_limits,ratelimit_tiered}.go
- backend/webui/src/components/auth/*
- backend/webui/src/app/(auth)/*

Made-with: Cursor
2026-02-28 01:33:49 +03:00

178 lines
5.2 KiB
Go

package db
import (
"context"
"database/sql"
"encoding/json"
"time"
)
type Space struct {
ID string `json:"id"`
UserID string `json:"userId"`
Name string `json:"name"`
Description string `json:"description"`
Icon string `json:"icon"`
Color string `json:"color"`
CustomInstructions string `json:"customInstructions"`
DefaultFocusMode string `json:"defaultFocusMode"`
DefaultModel string `json:"defaultModel"`
IsPublic bool `json:"isPublic"`
Settings map[string]interface{} `json:"settings"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
ThreadCount int `json:"threadCount,omitempty"`
}
type SpaceRepository struct {
db *PostgresDB
}
func NewSpaceRepository(db *PostgresDB) *SpaceRepository {
return &SpaceRepository{db: db}
}
func (r *SpaceRepository) RunMigrations(ctx context.Context) error {
migrations := []string{
`CREATE TABLE IF NOT EXISTS spaces (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL,
name VARCHAR(255) NOT NULL,
description TEXT,
icon VARCHAR(50),
color VARCHAR(20),
custom_instructions TEXT,
default_focus_mode VARCHAR(50) DEFAULT 'all',
default_model VARCHAR(100),
is_public BOOLEAN DEFAULT FALSE,
settings JSONB DEFAULT '{}',
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
)`,
`CREATE INDEX IF NOT EXISTS idx_spaces_user ON spaces(user_id)`,
}
for _, m := range migrations {
if _, err := r.db.db.ExecContext(ctx, m); err != nil {
return err
}
}
return nil
}
func (r *SpaceRepository) Create(ctx context.Context, s *Space) error {
settingsJSON, _ := json.Marshal(s.Settings)
query := `
INSERT INTO spaces (user_id, name, description, icon, color, custom_instructions, default_focus_mode, default_model, is_public, settings)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
RETURNING id, created_at, updated_at
`
return r.db.db.QueryRowContext(ctx, query,
s.UserID, s.Name, s.Description, s.Icon, s.Color,
s.CustomInstructions, s.DefaultFocusMode, s.DefaultModel,
s.IsPublic, settingsJSON,
).Scan(&s.ID, &s.CreatedAt, &s.UpdatedAt)
}
func (r *SpaceRepository) GetByID(ctx context.Context, id string) (*Space, error) {
query := `
SELECT id, user_id, name, description, icon, color, custom_instructions,
default_focus_mode, default_model, is_public, settings, created_at, updated_at,
(SELECT COUNT(*) FROM threads WHERE space_id = spaces.id) as thread_count
FROM spaces
WHERE id = $1
`
var s Space
var settingsJSON []byte
err := r.db.db.QueryRowContext(ctx, query, id).Scan(
&s.ID, &s.UserID, &s.Name, &s.Description, &s.Icon, &s.Color,
&s.CustomInstructions, &s.DefaultFocusMode, &s.DefaultModel,
&s.IsPublic, &settingsJSON, &s.CreatedAt, &s.UpdatedAt, &s.ThreadCount,
)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
json.Unmarshal(settingsJSON, &s.Settings)
return &s, nil
}
func (r *SpaceRepository) GetByUserID(ctx context.Context, userID string) ([]*Space, error) {
query := `
SELECT id, user_id, name, description, icon, color, custom_instructions,
default_focus_mode, default_model, is_public, settings, created_at, updated_at,
(SELECT COUNT(*) FROM threads WHERE space_id = spaces.id) as thread_count
FROM spaces
WHERE user_id = $1
ORDER BY updated_at DESC
`
rows, err := r.db.db.QueryContext(ctx, query, userID)
if err != nil {
return nil, err
}
defer rows.Close()
var spaces []*Space
for rows.Next() {
var s Space
var settingsJSON []byte
if err := rows.Scan(
&s.ID, &s.UserID, &s.Name, &s.Description, &s.Icon, &s.Color,
&s.CustomInstructions, &s.DefaultFocusMode, &s.DefaultModel,
&s.IsPublic, &settingsJSON, &s.CreatedAt, &s.UpdatedAt, &s.ThreadCount,
); err != nil {
return nil, err
}
json.Unmarshal(settingsJSON, &s.Settings)
spaces = append(spaces, &s)
}
return spaces, nil
}
func (r *SpaceRepository) Update(ctx context.Context, s *Space, userID string) error {
settingsJSON, _ := json.Marshal(s.Settings)
query := `
UPDATE spaces
SET name = $2, description = $3, icon = $4, color = $5,
custom_instructions = $6, default_focus_mode = $7, default_model = $8,
is_public = $9, settings = $10, updated_at = NOW()
WHERE id = $1 AND user_id = $11
`
result, err := r.db.db.ExecContext(ctx, query,
s.ID, s.Name, s.Description, s.Icon, s.Color,
s.CustomInstructions, s.DefaultFocusMode, s.DefaultModel,
s.IsPublic, settingsJSON, userID,
)
if err != nil {
return err
}
rows, _ := result.RowsAffected()
if rows == 0 {
return ErrNotFound
}
return nil
}
func (r *SpaceRepository) Delete(ctx context.Context, id, userID string) error {
result, err := r.db.db.ExecContext(ctx, "DELETE FROM spaces WHERE id = $1 AND user_id = $2", id, userID)
if err != nil {
return err
}
rows, _ := result.RowsAffected()
if rows == 0 {
return ErrNotFound
}
return nil
}