Add new frontend pages for the multi-tenant OCDP platform: - Charts page (/charts): Browse Harbor OCI registries to list Helm chart repositories and versions, with deploy modal to launch charts on selected clusters - Monitoring page (/monitoring): Display cluster metrics (CPU/Memory/GPU usage) and per-node details with resource utilization bars - Chart References page (/chart-references): CRUD for chart metadata references - Values Templates page (/templates): CRUD for Helm values templates with version history and rollback support - Sidebar: Add Charts navigation, update Storage and Templates links - api.ts: Add all API client functions (clusterApi, registryApi, instanceApi, monitoringApi, storageApi, chartReferenceApi, valuesTemplateApi, workspaceApi, userApi) with full TypeScript types Note: deploy flow and values template rollback not yet end-to-end tested.
343 lines
7.7 KiB
Go
343 lines
7.7 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/ocdp/cluster-service/internal/domain/entity"
|
|
"github.com/ocdp/cluster-service/internal/domain/repository"
|
|
)
|
|
|
|
// UserRepository PostgreSQL 用户仓储实现
|
|
type UserRepository struct {
|
|
db *DB
|
|
}
|
|
|
|
// NewUserRepository 创建 PostgreSQL 用户仓储
|
|
func NewUserRepository(db *DB) repository.UserRepository {
|
|
return &UserRepository{db: db}
|
|
}
|
|
|
|
// Create 创建用户
|
|
func (r *UserRepository) Create(ctx context.Context, user *entity.User) error {
|
|
if user.ID == "" {
|
|
user.ID = uuid.New().String()
|
|
}
|
|
|
|
// 设置默认值
|
|
if user.IsActive {
|
|
user.IsActive = true
|
|
}
|
|
|
|
query := `
|
|
INSERT INTO users (id, username, password_hash, email, role, workspace_id, is_active, must_change_password, revoked_after, created_at, updated_at)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
|
`
|
|
|
|
_, err := r.db.conn.ExecContext(ctx, query,
|
|
user.ID,
|
|
user.Username,
|
|
user.PasswordHash,
|
|
user.Email,
|
|
user.Role,
|
|
user.WorkspaceID,
|
|
user.IsActive,
|
|
user.MustChangePassword,
|
|
user.RevokedAfter,
|
|
user.CreatedAt,
|
|
user.UpdatedAt,
|
|
)
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create user: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// GetByID 根据 ID 获取用户
|
|
func (r *UserRepository) GetByID(ctx context.Context, id string) (*entity.User, error) {
|
|
query := `
|
|
SELECT id, username, password_hash, email, role, workspace_id, is_active, must_change_password, revoked_after, created_at, updated_at
|
|
FROM users
|
|
WHERE id = $1
|
|
`
|
|
|
|
user := &entity.User{}
|
|
var workspaceID sql.NullString
|
|
err := r.db.conn.QueryRowContext(ctx, query, id).Scan(
|
|
&user.ID,
|
|
&user.Username,
|
|
&user.PasswordHash,
|
|
&user.Email,
|
|
&user.Role,
|
|
&workspaceID,
|
|
&user.IsActive,
|
|
&user.MustChangePassword,
|
|
&user.RevokedAfter,
|
|
&user.CreatedAt,
|
|
&user.UpdatedAt,
|
|
)
|
|
|
|
// Handle NULL workspace_id
|
|
if workspaceID.Valid {
|
|
user.WorkspaceID = workspaceID.String
|
|
} else {
|
|
user.WorkspaceID = ""
|
|
}
|
|
|
|
if err == sql.ErrNoRows {
|
|
return nil, entity.ErrUserNotFound
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get user: %w", err)
|
|
}
|
|
|
|
return user, nil
|
|
}
|
|
|
|
// GetByUsername 根据用户名获取用户
|
|
func (r *UserRepository) GetByUsername(ctx context.Context, username string) (*entity.User, error) {
|
|
log.Printf("[DEBUG] GetByUsername called with username: %q", username)
|
|
query := `
|
|
SELECT id, username, password_hash, email, role, workspace_id, is_active, must_change_password, revoked_after, created_at, updated_at
|
|
FROM users
|
|
WHERE username = $1
|
|
`
|
|
|
|
log.Printf("[DEBUG] Executing query: %s with param: %s", query, username)
|
|
|
|
user := &entity.User{}
|
|
var workspaceID sql.NullString
|
|
err := r.db.conn.QueryRowContext(ctx, query, username).Scan(
|
|
&user.ID,
|
|
&user.Username,
|
|
&user.PasswordHash,
|
|
&user.Email,
|
|
&user.Role,
|
|
&workspaceID,
|
|
&user.IsActive,
|
|
&user.MustChangePassword,
|
|
&user.RevokedAfter,
|
|
&user.CreatedAt,
|
|
&user.UpdatedAt,
|
|
)
|
|
|
|
// Handle NULL workspace_id
|
|
if workspaceID.Valid {
|
|
user.WorkspaceID = workspaceID.String
|
|
} else {
|
|
user.WorkspaceID = ""
|
|
}
|
|
|
|
log.Printf("[DEBUG] Query result - err: %v", err)
|
|
if err == sql.ErrNoRows {
|
|
log.Printf("[DEBUG] User not found in DB")
|
|
return nil, entity.ErrUserNotFound
|
|
}
|
|
if err != nil {
|
|
log.Printf("[DEBUG] Scan error: %v", err)
|
|
return nil, fmt.Errorf("failed to get user: %w", err)
|
|
}
|
|
|
|
log.Printf("[DEBUG] Found user: %+v", user)
|
|
|
|
return user, nil
|
|
}
|
|
|
|
// Update 更新用户
|
|
func (r *UserRepository) Update(ctx context.Context, user *entity.User) error {
|
|
user.UpdatedAt = time.Now()
|
|
|
|
query := `
|
|
UPDATE users
|
|
SET username = $1, password_hash = $2, email = $3, role = $4, workspace_id = $5, is_active = $6, must_change_password = $7, revoked_after = $8, updated_at = $9
|
|
WHERE id = $10
|
|
`
|
|
|
|
result, err := r.db.conn.ExecContext(ctx, query,
|
|
user.Username,
|
|
user.PasswordHash,
|
|
user.Email,
|
|
user.Role,
|
|
user.WorkspaceID,
|
|
user.IsActive,
|
|
user.MustChangePassword,
|
|
user.RevokedAfter,
|
|
user.UpdatedAt,
|
|
user.ID,
|
|
)
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("failed to update user: %w", err)
|
|
}
|
|
|
|
rows, err := result.RowsAffected()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get affected rows: %w", err)
|
|
}
|
|
|
|
if rows == 0 {
|
|
return entity.ErrUserNotFound
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Delete 删除用户
|
|
func (r *UserRepository) Delete(ctx context.Context, id string) error {
|
|
query := `DELETE FROM users WHERE id = $1`
|
|
|
|
result, err := r.db.conn.ExecContext(ctx, query, id)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to delete user: %w", err)
|
|
}
|
|
|
|
rows, err := result.RowsAffected()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get affected rows: %w", err)
|
|
}
|
|
|
|
if rows == 0 {
|
|
return entity.ErrUserNotFound
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// List 列出所有用户
|
|
func (r *UserRepository) List(ctx context.Context) ([]*entity.User, error) {
|
|
query := `
|
|
SELECT id, username, password_hash, email, role, workspace_id, is_active, must_change_password, revoked_after, created_at, updated_at
|
|
FROM users
|
|
ORDER BY created_at DESC
|
|
`
|
|
|
|
rows, err := r.db.conn.QueryContext(ctx, query)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to list users: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
users := make([]*entity.User, 0)
|
|
for rows.Next() {
|
|
user := &entity.User{}
|
|
err := rows.Scan(
|
|
&user.ID,
|
|
&user.Username,
|
|
&user.PasswordHash,
|
|
&user.Email,
|
|
&user.Role,
|
|
&user.WorkspaceID,
|
|
&user.IsActive,
|
|
&user.MustChangePassword,
|
|
&user.RevokedAfter,
|
|
&user.CreatedAt,
|
|
&user.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to scan user: %w", err)
|
|
}
|
|
users = append(users, user)
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("rows iteration error: %w", err)
|
|
}
|
|
|
|
return users, nil
|
|
}
|
|
|
|
// ListByWorkspace 列出指定 workspace 的用户
|
|
func (r *UserRepository) ListByWorkspace(ctx context.Context, workspaceID string) ([]*entity.User, error) {
|
|
query := `
|
|
SELECT id, username, password_hash, email, role, workspace_id, is_active, must_change_password, revoked_after, created_at, updated_at
|
|
FROM users
|
|
WHERE workspace_id = $1
|
|
ORDER BY created_at DESC
|
|
`
|
|
|
|
rows, err := r.db.conn.QueryContext(ctx, query, workspaceID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to list users by workspace: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
users := make([]*entity.User, 0)
|
|
for rows.Next() {
|
|
user := &entity.User{}
|
|
err := rows.Scan(
|
|
&user.ID,
|
|
&user.Username,
|
|
&user.PasswordHash,
|
|
&user.Email,
|
|
&user.Role,
|
|
&user.WorkspaceID,
|
|
&user.IsActive,
|
|
&user.MustChangePassword,
|
|
&user.RevokedAfter,
|
|
&user.CreatedAt,
|
|
&user.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to scan user: %w", err)
|
|
}
|
|
users = append(users, user)
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("rows iteration error: %w", err)
|
|
}
|
|
|
|
return users, nil
|
|
}
|
|
|
|
// ListActive 仅列出活跃用户
|
|
func (r *UserRepository) ListActive(ctx context.Context) ([]*entity.User, error) {
|
|
query := `
|
|
SELECT id, username, password_hash, email, role, workspace_id, is_active, must_change_password, revoked_after, created_at, updated_at
|
|
FROM users
|
|
WHERE is_active = TRUE
|
|
ORDER BY created_at DESC
|
|
`
|
|
|
|
rows, err := r.db.conn.QueryContext(ctx, query)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to list active users: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
users := make([]*entity.User, 0)
|
|
for rows.Next() {
|
|
user := &entity.User{}
|
|
err := rows.Scan(
|
|
&user.ID,
|
|
&user.Username,
|
|
&user.PasswordHash,
|
|
&user.Email,
|
|
&user.Role,
|
|
&user.WorkspaceID,
|
|
&user.IsActive,
|
|
&user.MustChangePassword,
|
|
&user.RevokedAfter,
|
|
&user.CreatedAt,
|
|
&user.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to scan user: %w", err)
|
|
}
|
|
users = append(users, user)
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("rows iteration error: %w", err)
|
|
}
|
|
|
|
return users, nil
|
|
}
|
|
|