feat: 添加用户注册登录功能,实现数据和配置的用户隔离
- 新增 users 和 user_settings 表,repositories/analyses/review_notes 添加 user_id 列 - 实现基于邮箱+密码的注册登录,密码使用 bcrypt 哈希 - 使用 gin-contrib/sessions cookie-based session 管理 - 所有仓库、分析记录、review notes 按用户隔离 - 用户设置(LLM 配置、review 参数、缓存配置)独立存储 - 新增登录/注册页面,导航栏显示用户邮箱和退出按钮 - 前端 fetch 请求统一添加 credentials: 'same-origin' - 支持 SESSION_SECRET 环境变量配置会话密钥
This commit is contained in:
+82
-22
@@ -22,11 +22,17 @@ func NewReviewHandler(db *sql.DB) *ReviewHandler {
|
||||
|
||||
// Review handles POST /api/repos/:id/review — SSE streaming AI code review.
|
||||
func (h *ReviewHandler) Review(c *gin.Context) {
|
||||
user := GetCurrentUser(c)
|
||||
if user == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
|
||||
return
|
||||
}
|
||||
|
||||
id := c.Param("id")
|
||||
|
||||
// Get repo info
|
||||
// Get repo info (scoped to user)
|
||||
var localPath string
|
||||
err := h.db.QueryRow(`SELECT local_path FROM repositories WHERE id = ?`, id).Scan(&localPath)
|
||||
err := h.db.QueryRow(`SELECT local_path FROM repositories WHERE id = ? AND user_id = ?`, id, user.ID).Scan(&localPath)
|
||||
if err == sql.ErrNoRows {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "repository not found"})
|
||||
return
|
||||
@@ -54,7 +60,7 @@ func (h *ReviewHandler) Review(c *gin.Context) {
|
||||
topN = *req.TopN
|
||||
} else {
|
||||
var topNStr string
|
||||
h.db.QueryRow(`SELECT value FROM settings WHERE key = 'review.top_n'`).Scan(&topNStr)
|
||||
h.db.QueryRow(`SELECT value FROM user_settings WHERE user_id = ? AND key = 'review.top_n'`, user.ID).Scan(&topNStr)
|
||||
if topNStr != "" {
|
||||
if n, err := strconv.Atoi(topNStr); err == nil && n > 0 {
|
||||
topN = n
|
||||
@@ -68,7 +74,7 @@ func (h *ReviewHandler) Review(c *gin.Context) {
|
||||
concurrency = *req.Concurrency
|
||||
} else {
|
||||
var concStr string
|
||||
h.db.QueryRow(`SELECT value FROM settings WHERE key = 'review.concurrency'`).Scan(&concStr)
|
||||
h.db.QueryRow(`SELECT value FROM user_settings WHERE user_id = ? AND key = 'review.concurrency'`, user.ID).Scan(&concStr)
|
||||
if concStr != "" {
|
||||
if n, err := strconv.Atoi(concStr); err == nil && n > 0 {
|
||||
concurrency = n
|
||||
@@ -101,21 +107,21 @@ func (h *ReviewHandler) Review(c *gin.Context) {
|
||||
// Update last_used
|
||||
h.db.Exec(`UPDATE repositories SET last_used = datetime('now') WHERE id = ?`, id)
|
||||
|
||||
// Run AI review
|
||||
reviewResult, err := services.GenerateReview(h.db, localPath, req.Base, req.Head, topN, concurrency, sendEvent)
|
||||
// Run AI review (pass user ID for per-user LLM config)
|
||||
reviewResult, err := services.GenerateReview(h.db, localPath, req.Base, req.Head, topN, concurrency, user.ID, sendEvent)
|
||||
if err != nil {
|
||||
sendEvent("error", map[string]interface{}{"message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// Save analysis to DB with full review result
|
||||
// Save analysis to DB with user_id
|
||||
resultJSON, err := json.Marshal(reviewResult)
|
||||
if err != nil {
|
||||
sendEvent("error", map[string]interface{}{"message": "marshal result: " + err.Error()})
|
||||
return
|
||||
}
|
||||
res, err := h.db.Exec(`INSERT INTO analyses (repo_id, type, base_ref, head_ref, result) VALUES (?, 'code_review', ?, ?, ?)`,
|
||||
id, req.Base, req.Head, string(resultJSON))
|
||||
res, err := h.db.Exec(`INSERT INTO analyses (user_id, repo_id, type, base_ref, head_ref, result) VALUES (?, ?, 'code_review', ?, ?, ?)`,
|
||||
user.ID, id, req.Base, req.Head, string(resultJSON))
|
||||
if err != nil {
|
||||
sendEvent("error", map[string]interface{}{"message": "save analysis: " + err.Error()})
|
||||
return
|
||||
@@ -132,6 +138,12 @@ func (h *ReviewHandler) Review(c *gin.Context) {
|
||||
|
||||
// SaveNotes handles POST /api/repos/:id/review/notes — upsert a review note.
|
||||
func (h *ReviewHandler) SaveNotes(c *gin.Context) {
|
||||
user := GetCurrentUser(c)
|
||||
if user == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
AnalysisID int64 `json:"analysis_id" binding:"required"`
|
||||
Scope string `json:"scope" binding:"required"`
|
||||
@@ -152,6 +164,18 @@ func (h *ReviewHandler) SaveNotes(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Verify analysis belongs to user
|
||||
var analysisOwnerID int64
|
||||
err := h.db.QueryRow(`SELECT user_id FROM analyses WHERE id = ?`, req.AnalysisID).Scan(&analysisOwnerID)
|
||||
if err == sql.ErrNoRows || analysisOwnerID != user.ID {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "analysis not found"})
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
note, err := services.SaveNote(h.db, req.AnalysisID, req.Scope, req.ScopeKey, req.Content)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
@@ -163,6 +187,12 @@ func (h *ReviewHandler) SaveNotes(c *gin.Context) {
|
||||
|
||||
// GetNotes handles GET /api/repos/:id/review/notes — list review notes for an analysis.
|
||||
func (h *ReviewHandler) GetNotes(c *gin.Context) {
|
||||
user := GetCurrentUser(c)
|
||||
if user == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
|
||||
return
|
||||
}
|
||||
|
||||
analysisIDStr := c.Query("analysis_id")
|
||||
if analysisIDStr == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "analysis_id query parameter is required"})
|
||||
@@ -175,6 +205,18 @@ func (h *ReviewHandler) GetNotes(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Verify analysis belongs to user
|
||||
var analysisOwnerID int64
|
||||
err = h.db.QueryRow(`SELECT user_id FROM analyses WHERE id = ?`, analysisID).Scan(&analysisOwnerID)
|
||||
if err == sql.ErrNoRows || analysisOwnerID != user.ID {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "analysis not found"})
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
scope := c.Query("scope") // optional filter
|
||||
|
||||
notes, err := services.GetNotes(h.db, analysisID, scope)
|
||||
@@ -191,9 +233,15 @@ func (h *ReviewHandler) GetNotes(c *gin.Context) {
|
||||
|
||||
// ListReviews handles GET /api/repos/:id/review/analyses — list past code review analyses.
|
||||
func (h *ReviewHandler) ListReviews(c *gin.Context) {
|
||||
user := GetCurrentUser(c)
|
||||
if user == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
|
||||
return
|
||||
}
|
||||
|
||||
id := c.Param("id")
|
||||
|
||||
rows, err := h.db.Query(`SELECT id, base_ref, head_ref, result, created_at FROM analyses WHERE repo_id = ? AND type = 'code_review' ORDER BY created_at DESC`, id)
|
||||
rows, err := h.db.Query(`SELECT id, base_ref, head_ref, result, created_at FROM analyses WHERE repo_id = ? AND user_id = ? AND type = 'code_review' ORDER BY created_at DESC`, id, user.ID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
@@ -228,10 +276,16 @@ func (h *ReviewHandler) ListReviews(c *gin.Context) {
|
||||
|
||||
// GetReview handles GET /api/repos/:id/review/analyses/:aid — get a single review with full result.
|
||||
func (h *ReviewHandler) GetReview(c *gin.Context) {
|
||||
user := GetCurrentUser(c)
|
||||
if user == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
|
||||
return
|
||||
}
|
||||
|
||||
aid := c.Param("aid")
|
||||
|
||||
var result, baseRef, headRef, createdAt string
|
||||
err := h.db.QueryRow(`SELECT result, base_ref, head_ref, created_at FROM analyses WHERE id = ? AND type = 'code_review'`, aid).Scan(&result, &baseRef, &headRef, &createdAt)
|
||||
err := h.db.QueryRow(`SELECT result, base_ref, head_ref, created_at FROM analyses WHERE id = ? AND user_id = ? AND type = 'code_review'`, aid, user.ID).Scan(&result, &baseRef, &headRef, &createdAt)
|
||||
if err == sql.ErrNoRows {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "analysis not found"})
|
||||
return
|
||||
@@ -258,11 +312,17 @@ func (h *ReviewHandler) GetReview(c *gin.Context) {
|
||||
|
||||
// GeneratePDF handles POST /api/repos/:id/review/pdf — generate and download a PDF report.
|
||||
func (h *ReviewHandler) GeneratePDF(c *gin.Context) {
|
||||
user := GetCurrentUser(c)
|
||||
if user == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
|
||||
return
|
||||
}
|
||||
|
||||
id := c.Param("id")
|
||||
|
||||
// Get repo info
|
||||
// Get repo info (scoped to user)
|
||||
var repoURL string
|
||||
err := h.db.QueryRow(`SELECT url FROM repositories WHERE id = ?`, id).Scan(&repoURL)
|
||||
err := h.db.QueryRow(`SELECT url FROM repositories WHERE id = ? AND user_id = ?`, id, user.ID).Scan(&repoURL)
|
||||
if err == sql.ErrNoRows {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "repository not found"})
|
||||
return
|
||||
@@ -283,10 +343,10 @@ func (h *ReviewHandler) GeneratePDF(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Load the analysis result
|
||||
// Verify analysis belongs to user
|
||||
var analysisResult, baseRef, headRef string
|
||||
var createdAt string
|
||||
err = h.db.QueryRow(`SELECT result, base_ref, head_ref, created_at FROM analyses WHERE id = ?`, req.AnalysisID).Scan(&analysisResult, &baseRef, &headRef, &createdAt)
|
||||
err = h.db.QueryRow(`SELECT result, base_ref, head_ref, created_at FROM analyses WHERE id = ? AND user_id = ?`, req.AnalysisID, user.ID).Scan(&analysisResult, &baseRef, &headRef, &createdAt)
|
||||
if err == sql.ErrNoRows {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "analysis not found"})
|
||||
return
|
||||
@@ -305,13 +365,13 @@ func (h *ReviewHandler) GeneratePDF(c *gin.Context) {
|
||||
|
||||
// Build report data
|
||||
reportData := services.ReportData{
|
||||
RepoURL: repoURL,
|
||||
BaseRef: baseRef,
|
||||
HeadRef: headRef,
|
||||
ReviewedAt: createdAt,
|
||||
AnalysisID: req.AnalysisID,
|
||||
Result: analysisResult,
|
||||
Notes: notes,
|
||||
RepoURL: repoURL,
|
||||
BaseRef: baseRef,
|
||||
HeadRef: headRef,
|
||||
ReviewedAt: createdAt,
|
||||
AnalysisID: req.AnalysisID,
|
||||
Result: analysisResult,
|
||||
Notes: notes,
|
||||
}
|
||||
|
||||
// Generate PDF
|
||||
|
||||
Reference in New Issue
Block a user