Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin tools update #34

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions server/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,49 @@ func adminChangeUsername(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
}

func adminTempBanUser(w http.ResponseWriter, r *http.Request) {
uuid, _, rank, _, _, _ := getPlayerDataFromToken(r.Header.Get("Authorization"))
if rank == 0 {
handleError(w, r, "access denied")
return
}

targetUuid, duration := r.URL.Query().Get("uuid"), r.URL.Query().Get("duration")
if targetUuid == "" {
user := r.URL.Query().Get("user")
if user == "" {
handleError(w, r, "uuid or user not specified")
return
}

uuid, err := getUuidFromName(user)
if err != nil {
handleInternalError(w, r, err)
return
}

if uuid == "" {
handleError(w, r, "invalid user specified")
return
}

targetUuid = uuid
}

if duration = 0 {
handleError(w, r, "duration not specified")
return
}

duration , err := handleTempBan(uuid, targetUuid)
if err != nil {
handleInternalError(w, r, err)
return
}

w.Write([]byte(duration))
}

func adminResetPw(w http.ResponseWriter, r *http.Request) {
_, _, rank, _, _, _ := getPlayerDataFromToken(r.Header.Get("Authorization"))
if rank == 0 {
Expand Down
1 change: 1 addition & 0 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func initApi() {
http.HandleFunc("/admin/unban", adminBanMute)
http.HandleFunc("/admin/unmute", adminBanMute)
http.HandleFunc("/admin/changeusername", adminChangeUsername)
http.HandleFunc("/admin/temporaryban", adminTempBanUser)
http.HandleFunc("/admin/resetpw", adminResetPw)
http.HandleFunc("/admin/grantbadge", adminManageBadge)
http.HandleFunc("/admin/revokebadge", adminManageBadge)
Expand Down
32 changes: 32 additions & 0 deletions server/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,38 @@ func tryChangePlayerUsername(senderUuid string, recipientUuid string, newUsernam
return nil
}

func handleTempBan(uuid string, targetUuid string) (duration int, err error) {
var userExists int
db.QueryRow("SELECT EXISTS (SELECT * FROM accounts WHERE uuid = ?)", uuid).Scan(&userExists)

if userExists == 0 {
return "", errors.New("user not found")
}

err = tryBanPlayer(uuid, targetUuid)

if err != nil {
handleInternalError(http.ResponseWriter, *http.Request, err)
return
}

_, err := db.Exec("INSERT INTO playerModerationAction (uuid, action, expiry) VALUES (?, 'tempban', ?)", uuid, duration)
if err != nil {
return err
}

tempBan := time.NewTimer(duration * 60 * time.Second)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause the HTTP responder to hang without giving a response. You might want to set up a timer to be called later instead, push notifications are already doing this so you can refer to it.


<-tempBan.C
err = tryUnbanPlayer(uuid, targetUuid)
if err != nil {
handleInternalError(http.ResponseWriter, *http.Request, err)
return
}

w.Write([]byte("ok"))
}

func getPlayerMedals(uuid string) (medals [5]int) {
if client, ok := clients.Load(uuid); ok {
return client.medals // return medals from session if client is connected
Expand Down