fix: historial por usuario (user_id en query_history)
This commit is contained in:
@@ -119,7 +119,8 @@ func RunQuery(c *fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
|
||||
result := services.ExecuteSQL(conx, body.Database, body.SQL)
|
||||
uid := extractUserID(c)
|
||||
result := services.ExecuteSQL(conx, body.Database, body.SQL, uid)
|
||||
return c.JSON(result)
|
||||
}
|
||||
|
||||
@@ -128,6 +129,7 @@ func RunQuery(c *fiber.Ctx) error {
|
||||
// Body: { conx_db_id, database, sqls: ["...", "..."] }
|
||||
// También soporta multipart/form-data con file .sql
|
||||
func RunBatchQuery(c *fiber.Ctx) error {
|
||||
uid := extractUserID(c)
|
||||
conxDbIDStr := c.FormValue("conx_db_id", c.Query("conx_db_id"))
|
||||
database := c.FormValue("database", c.Query("database"))
|
||||
|
||||
@@ -195,7 +197,7 @@ func RunBatchQuery(c *fiber.Ctx) error {
|
||||
results := make([]batchResult, 0, len(statements))
|
||||
|
||||
for i, stmt := range statements {
|
||||
r := services.ExecuteSQL(conx, database, stmt)
|
||||
r := services.ExecuteSQL(conx, database, stmt, uid)
|
||||
br := batchResult{
|
||||
Index: i,
|
||||
SQL: stmt,
|
||||
@@ -244,7 +246,19 @@ func GetHistory(c *fiber.Ctx) error {
|
||||
limit := 50
|
||||
offset := (page - 1) * limit
|
||||
|
||||
items, total, err := models.GetQueryHistory(uint(conxID), limit, offset)
|
||||
user, _ := auth.User(c)
|
||||
var items []models.QueryHistory
|
||||
var total int64
|
||||
var err error
|
||||
if user != nil && user.IsAdmin {
|
||||
items, total, err = models.GetQueryHistoryAdmin(uint(conxID), limit, offset)
|
||||
} else {
|
||||
uid := uint(0)
|
||||
if user != nil {
|
||||
uid = user.ID
|
||||
}
|
||||
items, total, err = models.GetQueryHistory(uint(conxID), uid, limit, offset)
|
||||
}
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
@@ -262,7 +276,19 @@ func GetHistory(c *fiber.Ctx) error {
|
||||
func ClearHistory(c *fiber.Ctx) error {
|
||||
conxIDStr := c.Query("conx_db_id", "0")
|
||||
conxID, _ := strconv.ParseUint(conxIDStr, 10, 32)
|
||||
if err := models.DeleteQueryHistory(uint(conxID)); err != nil {
|
||||
|
||||
user, _ := auth.User(c)
|
||||
var err error
|
||||
if user != nil && user.IsAdmin {
|
||||
err = models.DeleteQueryHistoryAdmin(uint(conxID))
|
||||
} else {
|
||||
uid := uint(0)
|
||||
if user != nil {
|
||||
uid = user.ID
|
||||
}
|
||||
err = models.DeleteQueryHistory(uint(conxID), uid)
|
||||
}
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
return c.JSON(fiber.Map{"ok": true})
|
||||
@@ -284,7 +310,8 @@ func ExportCSV(c *fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
|
||||
result := services.ExecuteSQL(conx, body.Database, body.SQL)
|
||||
uid := extractUserID(c)
|
||||
result := services.ExecuteSQL(conx, body.Database, body.SQL, uid)
|
||||
if result.Error != "" {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": result.Error})
|
||||
}
|
||||
@@ -328,7 +355,8 @@ func ExportJSON(c *fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
|
||||
result := services.ExecuteSQL(conx, body.Database, body.SQL)
|
||||
uid := extractUserID(c)
|
||||
result := services.ExecuteSQL(conx, body.Database, body.SQL, uid)
|
||||
if result.Error != "" {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": result.Error})
|
||||
}
|
||||
@@ -423,7 +451,8 @@ func UpdateCellHandler(c *fiber.Ctx) error {
|
||||
sqlText := fmt.Sprintf("UPDATE %s SET %s = %s WHERE %s = %s",
|
||||
qTable, qCol, valueSQL, qPkCol, pkValueSQL)
|
||||
|
||||
result := services.ExecuteSQL(conx, body.Database, sqlText)
|
||||
uid := extractUserID(c)
|
||||
result := services.ExecuteSQL(conx, body.Database, sqlText, uid)
|
||||
if result.Error != "" {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": result.Error})
|
||||
}
|
||||
@@ -792,3 +821,11 @@ func loadConxDb(idStr string) (models.ConxDb, error) {
|
||||
}
|
||||
return conx, nil
|
||||
}
|
||||
|
||||
func extractUserID(c *fiber.Ctx) uint {
|
||||
user, err := auth.User(c)
|
||||
if err != nil || user == nil {
|
||||
return 0
|
||||
}
|
||||
return user.ID
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user