chore: trim spaces in the received data

This commit is contained in:
Ivan R. 2024-03-26 01:15:51 +05:00
parent 6a71891478
commit 101202b128
No known key found for this signature in database
GPG key ID: 56C7BAAE859B302C
4 changed files with 17 additions and 12 deletions

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"github.com/ordinary-dev/phoenix/database" "github.com/ordinary-dev/phoenix/database"
) )
@ -11,7 +12,7 @@ import (
func CreateGroup(w http.ResponseWriter, r *http.Request) { func CreateGroup(w http.ResponseWriter, r *http.Request) {
// Save new group to the database. // Save new group to the database.
group := database.Group{ group := database.Group{
Name: r.FormValue("groupName"), Name: strings.TrimSpace(r.FormValue("groupName")),
} }
if err := database.CreateGroup(&group); err != nil { if err := database.CreateGroup(&group); err != nil {
@ -30,7 +31,8 @@ func UpdateGroup(w http.ResponseWriter, r *http.Request) {
return return
} }
if err := database.UpdateGroup(int(id), r.FormValue("groupName")); err != nil { newName := strings.TrimSpace(r.FormValue("groupName"))
if err := database.UpdateGroup(int(id), newName); err != nil {
ShowError(w, http.StatusInternalServerError, err) ShowError(w, http.StatusInternalServerError, err)
return return
} }

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"github.com/ordinary-dev/phoenix/database" "github.com/ordinary-dev/phoenix/database"
) )
@ -16,11 +17,11 @@ func CreateLink(w http.ResponseWriter, r *http.Request) {
} }
link := database.Link{ link := database.Link{
Name: r.FormValue("linkName"), Name: strings.TrimSpace(r.FormValue("linkName")),
Href: r.FormValue("href"), Href: strings.TrimSpace(r.FormValue("href")),
GroupID: groupID, GroupID: groupID,
} }
icon := r.FormValue("icon") icon := strings.TrimSpace(r.FormValue("icon"))
if icon == "" { if icon == "" {
link.Icon = nil link.Icon = nil
} else { } else {
@ -48,9 +49,9 @@ func UpdateLink(w http.ResponseWriter, r *http.Request) {
return return
} }
link.Name = r.FormValue("linkName") link.Name = strings.TrimSpace(r.FormValue("linkName"))
link.Href = r.FormValue("href") link.Href = strings.TrimSpace(r.FormValue("href"))
icon := r.FormValue("icon") icon := strings.TrimSpace(r.FormValue("icon"))
if icon == "" { if icon == "" {
link.Icon = nil link.Icon = nil
} else { } else {

View file

@ -3,6 +3,7 @@ package pages
import ( import (
"errors" "errors"
"net/http" "net/http"
"strings"
"github.com/ordinary-dev/phoenix/database" "github.com/ordinary-dev/phoenix/database"
"github.com/ordinary-dev/phoenix/jwttoken" "github.com/ordinary-dev/phoenix/jwttoken"
@ -41,8 +42,8 @@ func CreateUser(w http.ResponseWriter, r *http.Request) {
} }
// Try to create a user. // Try to create a user.
username := r.FormValue("username") username := strings.TrimSpace(r.FormValue("username"))
password := r.FormValue("password") password := strings.TrimSpace(r.FormValue("password"))
_, err = database.CreateAdmin(username, password) _, err = database.CreateAdmin(username, password)
if err != nil { if err != nil {
ShowError(w, http.StatusInternalServerError, err) ShowError(w, http.StatusInternalServerError, err)

View file

@ -2,6 +2,7 @@ package pages
import ( import (
"net/http" "net/http"
"strings"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -23,8 +24,8 @@ func ShowSignInForm(w http.ResponseWriter, _ *http.Request) {
func AuthorizeUser(w http.ResponseWriter, r *http.Request) { func AuthorizeUser(w http.ResponseWriter, r *http.Request) {
// Check credentials. // Check credentials.
username := r.FormValue("username") username := strings.TrimSpace(r.FormValue("username"))
password := r.FormValue("password") password := strings.TrimSpace(r.FormValue("password"))
_, err := database.GetAdminIfPasswordMatches(username, password) _, err := database.GetAdminIfPasswordMatches(username, password)
if err != nil { if err != nil {
ShowError(w, http.StatusUnauthorized, err) ShowError(w, http.StatusUnauthorized, err)