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"
"net/http"
"strconv"
"strings"
"github.com/ordinary-dev/phoenix/database"
)
@ -11,7 +12,7 @@ import (
func CreateGroup(w http.ResponseWriter, r *http.Request) {
// Save new group to the database.
group := database.Group{
Name: r.FormValue("groupName"),
Name: strings.TrimSpace(r.FormValue("groupName")),
}
if err := database.CreateGroup(&group); err != nil {
@ -30,7 +31,8 @@ func UpdateGroup(w http.ResponseWriter, r *http.Request) {
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)
return
}

View file

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

View file

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

View file

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