feat(system-admin): implement bootstrap for system admin promotion and enhance system settings management

- Added WEKNORA_BOOTSTRAP_SYSTEM_ADMIN_EMAIL environment variable to promote a specified user to system admin on startup.
- Introduced a new bootstrap process in `bootstrap.go` to handle the promotion logic.
- Updated `.env.example` to document the new environment variable and its behavior.
- Created new views for managing system administrators and system settings, including listing, promoting, and revoking admin privileges.
- Enhanced the frontend to reflect the new system admin features, including UI elements for admin management and settings configuration.
- Updated API interfaces to support system admin functionalities, ensuring proper data handling and user management.
This commit is contained in:
wizardchen
2026-05-24 21:38:05 +08:00
committed by lyingbug
parent e6ee87759d
commit 47a183aa65
43 changed files with 3322 additions and 146 deletions
@@ -0,0 +1,76 @@
package repository
import (
"context"
"errors"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
// systemSettingRepository implements interfaces.SystemSettingRepository
// against the system_settings table (migration 000053). The table is
// system-scoped (no tenant_id column) and intentionally tiny — single-
// digit rows in P1 — so List does not paginate.
type systemSettingRepository struct {
db *gorm.DB
}
// NewSystemSettingRepository wires the repo into the dig container.
// Receives the shared *gorm.DB; no other deps.
func NewSystemSettingRepository(db *gorm.DB) interfaces.SystemSettingRepository {
return &systemSettingRepository{db: db}
}
// Get fetches a system setting by key. Returns (nil, nil) when the row
// does not exist — the resolver service treats "missing" as "fall back
// to ENV / default", so a 404 here is a normal control-flow signal,
// not an error. Real DB errors (connection lost, etc.) surface up.
func (r *systemSettingRepository) Get(ctx context.Context, key string) (*types.SystemSetting, error) {
var s types.SystemSetting
err := r.db.WithContext(ctx).Where("key = ?", key).First(&s).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, err
}
return &s, nil
}
// List returns every system_settings row, ordered by category then key
// for stable management-UI rendering. No pagination — see type comment.
func (r *systemSettingRepository) List(ctx context.Context) ([]*types.SystemSetting, error) {
var rows []*types.SystemSetting
err := r.db.WithContext(ctx).Order("category ASC, key ASC").Find(&rows).Error
if err != nil {
return nil, err
}
return rows, nil
}
// Upsert writes the row keyed by Key. We use ON CONFLICT (key) DO UPDATE
// rather than the naive Save() because (a) the natural key is `key`, not
// `id`, and (b) the seeded migration row already has an id; a Save with
// id=0 would re-insert and trip the UNIQUE constraint. Updating only
// the mutable columns prevents the migration's seeded id/created_at
// from being overwritten.
func (r *systemSettingRepository) Upsert(ctx context.Context, s *types.SystemSetting) error {
return r.db.WithContext(ctx).
Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "key"}},
DoUpdates: clause.AssignmentColumns([]string{
"value",
"value_type",
"category",
"description",
"is_secret",
"requires_restart",
"last_modified_by",
"updated_at",
}),
}).
Create(s).Error
}
+33
View File
@@ -127,6 +127,39 @@ func (r *userRepository) ListUsers(ctx context.Context, offset, limit int) ([]*t
return users, nil
}
// ListSystemAdmins lists users where is_system_admin = true.
//
// Walks idx_users_is_system_admin (created in migration 000052), so the
// query stays cheap even on a large users table — only the small subset
// of system admins is scanned. Returns total count alongside the page so
// the management UI can render pagination without a second roundtrip.
//
// Ordered by created_at DESC for stable, newest-first listing; ties are
// further broken by id to keep paging deterministic across boundaries.
// limit <= 0 means "no limit" (matches ListUsers semantics); callers in
// production pass a sane page size.
func (r *userRepository) ListSystemAdmins(ctx context.Context, offset, limit int) ([]*types.User, int64, error) {
var users []*types.User
var total int64
base := r.db.WithContext(ctx).Model(&types.User{}).Where("is_system_admin = ?", true)
if err := base.Count(&total).Error; err != nil {
return nil, 0, err
}
query := base.Order("created_at DESC, id ASC")
if limit > 0 {
query = query.Limit(limit)
}
if offset > 0 {
query = query.Offset(offset)
}
if err := query.Find(&users).Error; err != nil {
return nil, 0, err
}
return users, total, nil
}
// SearchUsers searches users by username or email
func (r *userRepository) SearchUsers(ctx context.Context, query string, limit int) ([]*types.User, error) {
var users []*types.User