mirror of
https://github.com/pagefaultgames/rogueserver.git
synced 2025-04-02 19:17:14 +08:00
Changes to allow linking and unlinking of google Id from DB
This commit is contained in:
parent
759a748010
commit
6d4d2a10ad
BIN
.vs/slnx.sqlite
BIN
.vs/slnx.sqlite
Binary file not shown.
@ -70,6 +70,8 @@ func Init(mux *http.ServeMux) error {
|
|||||||
// admin
|
// admin
|
||||||
mux.HandleFunc("POST /admin/account/discord-link", handleAdminDiscordLink)
|
mux.HandleFunc("POST /admin/account/discord-link", handleAdminDiscordLink)
|
||||||
mux.HandleFunc("POST /admin/account/discord-unlink", handleAdminDiscordUnlink)
|
mux.HandleFunc("POST /admin/account/discord-unlink", handleAdminDiscordUnlink)
|
||||||
|
mux.HandleFunc("POST /admin/account/google-link", handleAdminGoogleLink)
|
||||||
|
mux.HandleFunc("POST /admin/account/google-unlink", handleAdminGoogleUnlink)
|
||||||
mux.HandleFunc("GET /admin/account/admin-search", handleAdminSearch)
|
mux.HandleFunc("GET /admin/account/admin-search", handleAdminSearch)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
121
api/endpoints.go
121
api/endpoints.go
@ -697,10 +697,10 @@ func handleAdminDiscordLink(w http.ResponseWriter, r *http.Request) {
|
|||||||
discordId := r.Form.Get("discordId")
|
discordId := r.Form.Get("discordId")
|
||||||
|
|
||||||
// this does a quick call to make sure the username exists on the server before allowing the rest of the code to run
|
// this does a quick call to make sure the username exists on the server before allowing the rest of the code to run
|
||||||
// this calls error value 204 (StatusNoContent) if there's no data; this means the username does not exist in the server
|
// this calls error value 404 (StatusNotFound) if there's no data; this means the username does not exist in the server
|
||||||
_, err = db.CheckUsernameExists(username)
|
_, err = db.CheckUsernameExists(username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpError(w, r, fmt.Errorf("username does not exist on the server"), http.StatusNoContent)
|
httpError(w, r, fmt.Errorf("username does not exist on the server"), http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -751,10 +751,10 @@ func handleAdminDiscordUnlink(w http.ResponseWriter, r *http.Request) {
|
|||||||
if username != "" {
|
if username != "" {
|
||||||
log.Printf("Username given, removing discordId")
|
log.Printf("Username given, removing discordId")
|
||||||
// this does a quick call to make sure the username exists on the server before allowing the rest of the code to run
|
// this does a quick call to make sure the username exists on the server before allowing the rest of the code to run
|
||||||
// this calls error value 204 (StatusNoContent) if there's no data; this means the username does not exist in the server
|
// this calls error value 404 (StatusNotFound) if there's no data; this means the username does not exist in the server
|
||||||
_, err = db.CheckUsernameExists(username)
|
_, err = db.CheckUsernameExists(username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpError(w, r, fmt.Errorf("username does not exist on the server"), http.StatusNoContent)
|
httpError(w, r, fmt.Errorf("username does not exist on the server"), http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = db.RemoveDiscordIdByUsername(username)
|
err = db.RemoveDiscordIdByUsername(username)
|
||||||
@ -777,6 +777,115 @@ func handleAdminDiscordUnlink(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleAdminGoogleLink(w http.ResponseWriter, r *http.Request) {
|
||||||
|
err := r.ParseForm()
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, r, fmt.Errorf("failed to parse request form: %s", err), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uuid, err := uuidFromRequest(r)
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, r, err, http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
userDiscordId, err := db.FetchDiscordIdByUUID(uuid)
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, r, err, http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
hasRole, err := account.IsUserDiscordAdmin(userDiscordId, account.DiscordGuildID)
|
||||||
|
if !hasRole || err != nil {
|
||||||
|
httpError(w, r, fmt.Errorf("user does not have the required role"), http.StatusForbidden)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
username := r.Form.Get("username")
|
||||||
|
googleId := r.Form.Get("googleId")
|
||||||
|
|
||||||
|
// this does a quick call to make sure the username exists on the server before allowing the rest of the code to run
|
||||||
|
// this calls error value 404 (StatusNotFound) if there's no data; this means the username does not exist in the server
|
||||||
|
_, err = db.CheckUsernameExists(username)
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, r, fmt.Errorf("username does not exist on the server"), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = db.AddGoogleIdByUsername(googleId, username)
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, r, err, http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("%s: %s added google id %s to username %s", r.URL.Path, userDiscordId, googleId, username)
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleAdminGoogleUnlink(w http.ResponseWriter, r *http.Request) {
|
||||||
|
err := r.ParseForm()
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, r, fmt.Errorf("failed to parse request form: %s", err), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uuid, err := uuidFromRequest(r)
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, r, err, http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
userDiscordId, err := db.FetchDiscordIdByUUID(uuid)
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, r, err, http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
hasRole, err := account.IsUserDiscordAdmin(userDiscordId, account.DiscordGuildID)
|
||||||
|
if !hasRole || err != nil {
|
||||||
|
httpError(w, r, fmt.Errorf("user does not have the required role"), http.StatusForbidden)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, r, err, http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
username := r.Form.Get("username")
|
||||||
|
googleId := r.Form.Get("googleId")
|
||||||
|
|
||||||
|
if username != "" {
|
||||||
|
log.Printf("Username given, removing googleId")
|
||||||
|
// this does a quick call to make sure the username exists on the server before allowing the rest of the code to run
|
||||||
|
// this calls error value 404 (StatusNotFound) if there's no data; this means the username does not exist in the server
|
||||||
|
_, err = db.CheckUsernameExists(username)
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, r, fmt.Errorf("username does not exist on the server"), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = db.RemoveGoogleIdByUsername(username)
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, r, err, http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if googleId != "" {
|
||||||
|
log.Printf("DiscordID given, removing googleId")
|
||||||
|
err = db.RemoveGoogleIdByDiscordId(googleId)
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, r, err, http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("%s: %s removed google id %s from username %s", userDiscordId, r.URL.Path, r.Form.Get("googleId"), r.Form.Get("username"))
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
func handleAdminSearch(w http.ResponseWriter, r *http.Request) {
|
func handleAdminSearch(w http.ResponseWriter, r *http.Request) {
|
||||||
err := r.ParseForm()
|
err := r.ParseForm()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -810,10 +919,10 @@ func handleAdminSearch(w http.ResponseWriter, r *http.Request) {
|
|||||||
username := r.Form.Get("username")
|
username := r.Form.Get("username")
|
||||||
|
|
||||||
// this does a quick call to make sure the username exists on the server before allowing the rest of the code to run
|
// this does a quick call to make sure the username exists on the server before allowing the rest of the code to run
|
||||||
// this calls error value 204 (StatusNoContent) if there's no data; this means the username does not exist in the server
|
// this calls error value 404 (StatusNotFound) if there's no data; this means the username does not exist in the server
|
||||||
_, err = db.CheckUsernameExists(username)
|
_, err = db.CheckUsernameExists(username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpError(w, r, fmt.Errorf("username does not exist on the server"), http.StatusNoContent)
|
httpError(w, r, fmt.Errorf("username does not exist on the server"), http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -183,14 +183,15 @@ type AdminSearchResponse struct {
|
|||||||
DiscordId string `json:"discordId"`
|
DiscordId string `json:"discordId"`
|
||||||
GoogleId string `json:"googleId"`
|
GoogleId string `json:"googleId"`
|
||||||
LastLoggedIn string `json:"lastLoggedIn"`
|
LastLoggedIn string `json:"lastLoggedIn"`
|
||||||
|
Registered string `json:"registered"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchAdminDetailsByUsername(dbUsername string) (AdminSearchResponse, error) {
|
func FetchAdminDetailsByUsername(dbUsername string) (AdminSearchResponse, error) {
|
||||||
var resultUsername, resultDiscordId, resultGoogleId, resultLastLoggedIn sql.NullString
|
var resultUsername, resultDiscordId, resultGoogleId, resultLastLoggedIn, resultRegistered sql.NullString
|
||||||
var username, discordId, googleId, lastLoggedIn string
|
var username, discordId, googleId, lastLoggedIn, registered string
|
||||||
var adminResponse AdminSearchResponse
|
var adminResponse AdminSearchResponse
|
||||||
|
|
||||||
err := handle.QueryRow("SELECT username, discordId, googleId, lastLoggedIn from accounts WHERE username = ?", dbUsername).Scan(&resultUsername, &resultDiscordId, &resultGoogleId, &resultLastLoggedIn)
|
err := handle.QueryRow("SELECT username, discordId, googleId, lastLoggedIn, registered from accounts WHERE username = ?", dbUsername).Scan(&resultUsername, &resultDiscordId, &resultGoogleId, &resultLastLoggedIn, &resultRegistered)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return adminResponse, err
|
return adminResponse, err
|
||||||
}
|
}
|
||||||
@ -219,11 +220,18 @@ func FetchAdminDetailsByUsername(dbUsername string) (AdminSearchResponse, error)
|
|||||||
lastLoggedIn = ""
|
lastLoggedIn = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if resultRegistered.Valid {
|
||||||
|
registered = resultRegistered.String
|
||||||
|
} else {
|
||||||
|
registered = ""
|
||||||
|
}
|
||||||
|
|
||||||
adminResponse = AdminSearchResponse{
|
adminResponse = AdminSearchResponse{
|
||||||
Username: username,
|
Username: username,
|
||||||
DiscordId: discordId,
|
DiscordId: discordId,
|
||||||
GoogleId: googleId,
|
GoogleId: googleId,
|
||||||
LastLoggedIn: lastLoggedIn,
|
LastLoggedIn: lastLoggedIn,
|
||||||
|
Registered: registered,
|
||||||
}
|
}
|
||||||
|
|
||||||
return adminResponse, nil
|
return adminResponse, nil
|
||||||
@ -436,6 +444,15 @@ func RemoveGoogleIdByUUID(uuid []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RemoveGoogleIdByUsername(username string) error {
|
||||||
|
_, err := handle.Exec("UPDATE accounts SET googleId = NULL WHERE username = ?", username)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func RemoveDiscordIdByUsername(username string) error {
|
func RemoveDiscordIdByUsername(username string) error {
|
||||||
_, err := handle.Exec("UPDATE accounts SET discordId = NULL WHERE username = ?", username)
|
_, err := handle.Exec("UPDATE accounts SET discordId = NULL WHERE username = ?", username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -451,5 +468,14 @@ func RemoveDiscordIdByDiscordId(discordId string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func RemoveGoogleIdByDiscordId(discordId string) error {
|
||||||
|
_, err := handle.Exec("UPDATE accounts SET googleId = NULL WHERE discordId = ?", discordId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user