mirror of
https://github.com/WiiLink24/wfc-server.git
synced 2026-03-21 17:44:58 -05:00
Add /api/clear to drop user from database
This commit is contained in:
parent
3099499868
commit
64e007631f
75
api/clear.go
Normal file
75
api/clear.go
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"wwfc/database"
|
||||
)
|
||||
|
||||
func HandleClear(w http.ResponseWriter, r *http.Request) {
|
||||
var user *database.User
|
||||
var success bool
|
||||
var err string
|
||||
var statusCode int
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodHead:
|
||||
statusCode = http.StatusOK
|
||||
case http.MethodPost:
|
||||
user, success, err, statusCode = handleClearImpl(w, r)
|
||||
default:
|
||||
err = "Incorrect request. POST or HEAD only."
|
||||
statusCode = http.StatusBadRequest
|
||||
}
|
||||
|
||||
if user == nil {
|
||||
user = &database.User{}
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
|
||||
if r.Method == http.MethodHead {
|
||||
w.WriteHeader(statusCode)
|
||||
} else {
|
||||
json, _ := json.Marshal(UserActionResponse{*user, success, err})
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(json)))
|
||||
w.WriteHeader(statusCode)
|
||||
w.Write(json)
|
||||
}
|
||||
}
|
||||
|
||||
type ClearRequestSpec struct {
|
||||
Secret string
|
||||
Pid uint32
|
||||
}
|
||||
|
||||
func handleClearImpl(w http.ResponseWriter, r *http.Request) (*database.User, bool, string, int) {
|
||||
// TODO: Actual authentication rather than a fixed secret
|
||||
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
return nil, false, "Unable to read request body", http.StatusBadRequest
|
||||
}
|
||||
|
||||
var req ClearRequestSpec
|
||||
err = json.Unmarshal(body, &req)
|
||||
if err != nil {
|
||||
return nil, false, err.Error(), http.StatusBadRequest
|
||||
}
|
||||
|
||||
if apiSecret == "" || req.Secret != apiSecret {
|
||||
return nil, false, "Invalid API secret in request", http.StatusUnauthorized
|
||||
}
|
||||
|
||||
user, success := database.ClearProfile(pool, ctx, req.Pid)
|
||||
|
||||
if !success {
|
||||
return nil, false, "Unable to query user data from the database", http.StatusInternalServerError
|
||||
}
|
||||
|
||||
// Don't return empty JSON, this is placeholder for now.
|
||||
return &user, true, "", http.StatusOK
|
||||
}
|
||||
|
|
@ -16,6 +16,7 @@ const (
|
|||
UpdateUserProfileID = `UPDATE users SET profile_id = $3 WHERE user_id = $1 AND gsbrcd = $2`
|
||||
UpdateUserNGDeviceID = `UPDATE users SET ng_device_id = $2 WHERE profile_id = $1`
|
||||
GetUser = `SELECT user_id, gsbrcd, email, unique_nick, firstname, lastname, open_host, last_ip_address, last_ingamesn FROM users WHERE profile_id = $1`
|
||||
DeleteUser = `DELETE FROM users WHERE profile_id = $1 RETURNING user_id, gsbrcd, email, unique_nick, firstname, lastname, open_host, last_ip_address, last_ingamesn`
|
||||
DoesUserExist = `SELECT EXISTS(SELECT 1 FROM users WHERE user_id = $1 AND gsbrcd = $2)`
|
||||
IsProfileIDInUse = `SELECT EXISTS(SELECT 1 FROM users WHERE profile_id = $1)`
|
||||
DeleteUserSession = `DELETE FROM sessions WHERE profile_id = $1`
|
||||
|
|
@ -139,6 +140,19 @@ func GetProfile(pool *pgxpool.Pool, ctx context.Context, profileId uint32) (User
|
|||
return user, true
|
||||
}
|
||||
|
||||
func ClearProfile(pool *pgxpool.Pool, ctx context.Context, profileId uint32) (User, bool) {
|
||||
user := User{}
|
||||
row := pool.QueryRow(ctx, DeleteUser, profileId)
|
||||
err := row.Scan(&user.UserId, &user.GsbrCode, &user.Email, &user.UniqueNick, &user.FirstName, &user.LastName, &user.OpenHost, &user.LastIPAddress, &user.LastInGameSn)
|
||||
|
||||
if err != nil {
|
||||
return User{}, false
|
||||
}
|
||||
|
||||
user.ProfileId = profileId
|
||||
return user, true
|
||||
}
|
||||
|
||||
func BanUser(pool *pgxpool.Pool, ctx context.Context, profileId uint32, tos bool, length time.Duration, reason string, reasonHidden string, moderator string) bool {
|
||||
_, err := pool.Exec(ctx, UpdateUserBan, profileId, time.Now(), time.Now().Add(length), reason, reasonHidden, moderator, tos)
|
||||
return err == nil
|
||||
|
|
|
|||
|
|
@ -173,6 +173,11 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
if r.URL.Path == "/api/clear" {
|
||||
api.HandleClear(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
logging.Info("NAS", aurora.Yellow(r.Method), aurora.Cyan(r.URL), "via", aurora.Cyan(r.Host), "from", aurora.BrightCyan(r.RemoteAddr))
|
||||
replyHTTPError(w, 404, "404 Not Found")
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user