diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2024-10-31 22:06:00 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2024-10-31 22:06:00 +0300 |
| commit | 4210c9b38f9053f6720a6bebaadefd24c542eaa9 (patch) | |
| tree | 5b0061e23cf91291ed9e5f387766148d45103591 /backend/handlers/logs.go | |
| parent | chore: change repo name to lphub (diff) | |
| download | lphub-4210c9b38f9053f6720a6bebaadefd24c542eaa9.tar.gz lphub-4210c9b38f9053f6720a6bebaadefd24c542eaa9.tar.bz2 lphub-4210c9b38f9053f6720a6bebaadefd24c542eaa9.zip | |
backend: better auth check, audit logging
Diffstat (limited to 'backend/handlers/logs.go')
| -rw-r--r-- | backend/handlers/logs.go | 98 |
1 files changed, 0 insertions, 98 deletions
diff --git a/backend/handlers/logs.go b/backend/handlers/logs.go index 76ddac4..693c448 100644 --- a/backend/handlers/logs.go +++ b/backend/handlers/logs.go | |||
| @@ -1,7 +1,6 @@ | |||
| 1 | package handlers | 1 | package handlers |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "fmt" | ||
| 5 | "net/http" | 4 | "net/http" |
| 6 | "time" | 5 | "time" |
| 7 | 6 | ||
| @@ -11,42 +10,6 @@ import ( | |||
| 11 | "github.com/gin-gonic/gin" | 10 | "github.com/gin-gonic/gin" |
| 12 | ) | 11 | ) |
| 13 | 12 | ||
| 14 | const ( | ||
| 15 | LogTypeMod string = "Mod" | ||
| 16 | LogTypeUser string = "User" | ||
| 17 | LogTypeRecord string = "Record" | ||
| 18 | |||
| 19 | LogDescriptionUserLoginSuccess string = "LoginSuccess" | ||
| 20 | LogDescriptionUserLoginFailToken string = "LoginTokenFail" | ||
| 21 | LogDescriptionUserLoginFailValidate string = "LoginValidateFail" | ||
| 22 | LogDescriptionUserLoginFailSummary string = "LoginSummaryFail" | ||
| 23 | LogDescriptionUserUpdateSuccess string = "UpdateSuccess" | ||
| 24 | LogDescriptionUserUpdateFail string = "UpdateFail" | ||
| 25 | LogDescriptionUserUpdateSummaryFail string = "UpdateSummaryFail" | ||
| 26 | LogDescriptionUserUpdateCountrySuccess string = "UpdateCountrySuccess" | ||
| 27 | LogDescriptionUserUpdateCountryFail string = "UpdateCountryFail" | ||
| 28 | |||
| 29 | LogDescriptionMapSummaryCreateSuccess string = "MapSummaryCreateSuccess" | ||
| 30 | LogDescriptionMapSummaryCreateFail string = "MapSummaryCreateFail" | ||
| 31 | LogDescriptionMapSummaryEditSuccess string = "MapSummaryEditSuccess" | ||
| 32 | LogDescriptionMapSummaryEditFail string = "MapSummaryEditFail" | ||
| 33 | LogDescriptionMapSummaryEditImageSuccess string = "MapSummaryEditImageSuccess" | ||
| 34 | LogDescriptionMapSummaryEditImageFail string = "MapSummaryEditImageFail" | ||
| 35 | LogDescriptionMapSummaryDeleteSuccess string = "MapSummaryDeleteSuccess" | ||
| 36 | LogDescriptionMapSummaryDeleteFail string = "MapSummaryDeleteFail" | ||
| 37 | |||
| 38 | LogDescriptionCreateRecordSuccess string = "CreateRecordSuccess" | ||
| 39 | LogDescriptionCreateRecordInsertRecordFail string = "InsertRecordFail" | ||
| 40 | LogDescriptionCreateRecordInsertDemoFail string = "InsertDemoFail" | ||
| 41 | LogDescriptionCreateRecordProcessDemoFail string = "ProcessDemoFail" | ||
| 42 | LogDescriptionCreateRecordCreateDemoFail string = "CreateDemoFail" | ||
| 43 | LogDescriptionCreateRecordOpenDemoFail string = "OpenDemoFail" | ||
| 44 | LogDescriptionCreateRecordSaveDemoFail string = "SaveDemoFail" | ||
| 45 | LogDescriptionCreateRecordInvalidRequestFail string = "InvalidRequestFail" | ||
| 46 | LogDescriptionDeleteRecordSuccess string = "DeleteRecordSuccess" | ||
| 47 | LogDescriptionDeleteRecordFail string = "DeleteRecordFail" | ||
| 48 | ) | ||
| 49 | |||
| 50 | type Log struct { | 13 | type Log struct { |
| 51 | User models.UserShort `json:"user"` | 14 | User models.UserShort `json:"user"` |
| 52 | Type string `json:"type"` | 15 | Type string `json:"type"` |
| @@ -80,54 +43,6 @@ type ScoreLogsResponseDetails struct { | |||
| 80 | Date time.Time `json:"date"` | 43 | Date time.Time `json:"date"` |
| 81 | } | 44 | } |
| 82 | 45 | ||
| 83 | // GET Mod Logs | ||
| 84 | // | ||
| 85 | // @Description Get mod logs. | ||
| 86 | // @Tags logs | ||
| 87 | // @Produce json | ||
| 88 | // @Param Authorization header string true "JWT Token" | ||
| 89 | // @Success 200 {object} models.Response{data=LogsResponse} | ||
| 90 | // @Router /logs/mod [get] | ||
| 91 | func ModLogs(c *gin.Context) { | ||
| 92 | mod, exists := c.Get("mod") | ||
| 93 | if !exists || !mod.(bool) { | ||
| 94 | c.JSON(http.StatusOK, models.ErrorResponse("Insufficient permissions.")) | ||
| 95 | return | ||
| 96 | } | ||
| 97 | response := LogsResponse{Logs: []LogsResponseDetails{}} | ||
| 98 | sql := `SELECT u.user_name, l.user_id, l.type, l.description, l.message, l.date | ||
| 99 | FROM logs l INNER JOIN users u ON l.user_id = u.steam_id WHERE type != 'Score' | ||
| 100 | ORDER BY l.date DESC LIMIT 100;` | ||
| 101 | rows, err := database.DB.Query(sql) | ||
| 102 | if err != nil { | ||
| 103 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | ||
| 104 | return | ||
| 105 | } | ||
| 106 | for rows.Next() { | ||
| 107 | log := Log{} | ||
| 108 | err = rows.Scan(&log.User.UserName, &log.User.SteamID, &log.Type, &log.Description, &log.Message, &log.Date) | ||
| 109 | if err != nil { | ||
| 110 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | ||
| 111 | return | ||
| 112 | } | ||
| 113 | detail := fmt.Sprintf("%s.%s", log.Type, log.Description) | ||
| 114 | response.Logs = append(response.Logs, LogsResponseDetails{ | ||
| 115 | User: models.UserShort{ | ||
| 116 | SteamID: log.User.SteamID, | ||
| 117 | UserName: log.User.UserName, | ||
| 118 | }, | ||
| 119 | Log: detail, | ||
| 120 | Message: log.Message, | ||
| 121 | Date: log.Date, | ||
| 122 | }) | ||
| 123 | } | ||
| 124 | c.JSON(http.StatusOK, models.Response{ | ||
| 125 | Success: true, | ||
| 126 | Message: "Successfully retrieved logs.", | ||
| 127 | Data: response, | ||
| 128 | }) | ||
| 129 | } | ||
| 130 | |||
| 131 | // GET Score Logs | 46 | // GET Score Logs |
| 132 | // | 47 | // |
| 133 | // @Description Get score logs of every player. | 48 | // @Description Get score logs of every player. |
| @@ -186,16 +101,3 @@ func ScoreLogs(c *gin.Context) { | |||
| 186 | Data: response, | 101 | Data: response, |
| 187 | }) | 102 | }) |
| 188 | } | 103 | } |
| 189 | |||
| 190 | func CreateLog(userID string, logType string, logDescription string, logMessage ...string) (err error) { | ||
| 191 | message := "-" | ||
| 192 | if len(logMessage) == 1 { | ||
| 193 | message = logMessage[0] | ||
| 194 | } | ||
| 195 | sql := `INSERT INTO logs (user_id, "type", description, message) VALUES($1, $2, $3, $4)` | ||
| 196 | _, err = database.DB.Exec(sql, userID, logType, logDescription, message) | ||
| 197 | if err != nil { | ||
| 198 | return err | ||
| 199 | } | ||
| 200 | return nil | ||
| 201 | } | ||