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:
2026-06-20 21:57:46 +08:00
parent 953c83d9aa
commit 9c843b79ba
28 changed files with 837 additions and 176 deletions
+63 -15
View File
@@ -24,7 +24,13 @@ func NewReposHandler(db *sql.DB, reposDir string) *ReposHandler {
}
func (h *ReposHandler) ListRepos(c *gin.Context) {
rows, err := h.db.Query(`SELECT id, url, local_path, size_bytes, cloned_at, last_used FROM repositories ORDER BY last_used DESC`)
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
return
}
rows, err := h.db.Query(`SELECT id, url, local_path, size_bytes, cloned_at, last_used FROM repositories WHERE user_id = ? ORDER BY last_used DESC`, user.ID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
@@ -54,9 +60,15 @@ func (h *ReposHandler) ListRepos(c *gin.Context) {
}
func (h *ReposHandler) DeleteRepo(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
return
}
id := c.Param("id")
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
@@ -69,11 +81,11 @@ func (h *ReposHandler) DeleteRepo(c *gin.Context) {
c.JSON(http.StatusInternalServerError, gin.H{"error": "remove repo dir: " + err.Error()})
return
}
if _, err := h.db.Exec(`DELETE FROM analyses WHERE repo_id = ?`, id); err != nil {
if _, err := h.db.Exec(`DELETE FROM analyses WHERE repo_id = ? AND user_id = ?`, id, user.ID); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "delete analyses: " + err.Error()})
return
}
if _, err := h.db.Exec(`DELETE FROM repositories WHERE id = ?`, id); err != nil {
if _, err := h.db.Exec(`DELETE FROM repositories WHERE id = ? AND user_id = ?`, id, user.ID); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "delete repository: " + err.Error()})
return
}
@@ -81,12 +93,18 @@ func (h *ReposHandler) DeleteRepo(c *gin.Context) {
}
func (h *ReposHandler) CleanupRepos(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
return
}
var maxAgeDays string
h.db.QueryRow(`SELECT value FROM settings WHERE key = 'cache.max_age_days'`).Scan(&maxAgeDays)
h.db.QueryRow(`SELECT value FROM user_settings WHERE user_id = ? AND key = 'cache.max_age_days'`, user.ID).Scan(&maxAgeDays)
if maxAgeDays == "" {
maxAgeDays = "7"
}
rows, err := h.db.Query(`SELECT id, local_path FROM repositories WHERE last_used < datetime('now', '-' || ? || ' days')`, maxAgeDays)
rows, err := h.db.Query(`SELECT id, local_path FROM repositories WHERE user_id = ? AND last_used < datetime('now', '-' || ? || ' days')`, user.ID, maxAgeDays)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
@@ -102,10 +120,10 @@ func (h *ReposHandler) CleanupRepos(c *gin.Context) {
errs = append(errs, fmt.Sprintf("remove %d: %s", id, err.Error()))
continue
}
if _, err := h.db.Exec(`DELETE FROM analyses WHERE repo_id = ?`, id); err != nil {
if _, err := h.db.Exec(`DELETE FROM analyses WHERE repo_id = ? AND user_id = ?`, id, user.ID); err != nil {
errs = append(errs, fmt.Sprintf("delete analyses %d: %s", id, err.Error()))
}
if _, err := h.db.Exec(`DELETE FROM repositories WHERE id = ?`, id); err != nil {
if _, err := h.db.Exec(`DELETE FROM repositories WHERE id = ? AND user_id = ?`, id, user.ID); err != nil {
errs = append(errs, fmt.Sprintf("delete repo %d: %s", id, err.Error()))
}
cleaned = append(cleaned, id)
@@ -124,6 +142,12 @@ func (h *ReposHandler) CleanupRepos(c *gin.Context) {
// CloneRepo handles POST /api/repos with SSE progress events.
func (h *ReposHandler) CloneRepo(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
return
}
var req struct {
URL string `json:"url" binding:"required"`
Username string `json:"username"`
@@ -179,10 +203,10 @@ func (h *ReposHandler) CloneRepo(c *gin.Context) {
return
}
// Save to database
// Save to database with user_id
now := time.Now().Format(time.RFC3339)
res, err := h.db.Exec(`INSERT INTO repositories (url, local_path, size_bytes, cloned_at, last_used) VALUES (?, ?, ?, ?, ?)`,
req.URL, repoDir, result.SizeBytes, now, now)
res, err := h.db.Exec(`INSERT INTO repositories (user_id, url, local_path, size_bytes, cloned_at, last_used) VALUES (?, ?, ?, ?, ?, ?)`,
user.ID, req.URL, repoDir, result.SizeBytes, now, now)
if err != nil {
sendEvent("error", map[string]interface{}{"message": "save to db: " + err.Error()})
return
@@ -201,9 +225,15 @@ func (h *ReposHandler) CloneRepo(c *gin.Context) {
// GetGraph handles GET /api/repos/:id/graph — returns D3.js-compatible data.
func (h *ReposHandler) GetGraph(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
return
}
id := c.Param("id")
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
@@ -240,6 +270,12 @@ func (h *ReposHandler) GetGraph(c *gin.Context) {
// GetDiff handles GET /api/repos/:id/diff — returns unified diff or per-file diffs.
func (h *ReposHandler) GetDiff(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
return
}
id := c.Param("id")
base := c.Query("base")
head := c.Query("head")
@@ -250,7 +286,7 @@ func (h *ReposHandler) GetDiff(c *gin.Context) {
}
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
@@ -291,9 +327,15 @@ func (h *ReposHandler) GetDiff(c *gin.Context) {
// GetRefs handles GET /api/repos/:id/refs — returns branches and tags.
func (h *ReposHandler) GetRefs(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
return
}
id := c.Param("id")
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
@@ -320,6 +362,12 @@ func (h *ReposHandler) GetRefs(c *gin.Context) {
// GetCommits handles GET /api/repos/:id/commits — returns commit log for a ref.
func (h *ReposHandler) GetCommits(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
return
}
id := c.Param("id")
refName := c.Query("ref")
if refName == "" {
@@ -333,7 +381,7 @@ func (h *ReposHandler) GetCommits(c *gin.Context) {
}
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