mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
enhance: validate settings value before save
This commit is contained in:
parent
d70e37c857
commit
d67b8428e4
8 changed files with 83 additions and 26 deletions
73
api/api.go
73
api/api.go
|
@ -4,11 +4,28 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/0xJacky/Nginx-UI/internal/logger"
|
"github.com/0xJacky/Nginx-UI/internal/logger"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/gin-gonic/gin/binding"
|
||||||
val "github.com/go-playground/validator/v10"
|
val "github.com/go-playground/validator/v10"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
if v, ok := binding.Validator.Engine().(*val.Validate); ok {
|
||||||
|
err := v.RegisterValidation("alphanumdash", func(fl val.FieldLevel) bool {
|
||||||
|
return regexp.MustCompile(`^[a-zA-Z0-9-]+$`).MatchString(fl.Field().String())
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
logger.Fatal("binding validator engine is not initialized")
|
||||||
|
}
|
||||||
|
|
||||||
func ErrHandler(c *gin.Context, err error) {
|
func ErrHandler(c *gin.Context, err error) {
|
||||||
logger.GetLogger().Errorln(err)
|
logger.GetLogger().Errorln(err)
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
c.JSON(http.StatusInternalServerError, gin.H{
|
||||||
|
@ -22,7 +39,6 @@ type ValidError struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func BindAndValid(c *gin.Context, target interface{}) bool {
|
func BindAndValid(c *gin.Context, target interface{}) bool {
|
||||||
errs := make(map[string]string)
|
|
||||||
err := c.ShouldBindJSON(target)
|
err := c.ShouldBindJSON(target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("bind err", err)
|
logger.Error("bind err", err)
|
||||||
|
@ -31,24 +47,23 @@ func BindAndValid(c *gin.Context, target interface{}) bool {
|
||||||
ok := errors.As(err, &verrs)
|
ok := errors.As(err, &verrs)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
logger.Error("valid err", verrs)
|
|
||||||
c.JSON(http.StatusNotAcceptable, gin.H{
|
c.JSON(http.StatusNotAcceptable, gin.H{
|
||||||
"message": "Requested with wrong parameters",
|
"message": "Requested with wrong parameters",
|
||||||
"code": http.StatusNotAcceptable,
|
"code": http.StatusNotAcceptable,
|
||||||
"error": verrs,
|
|
||||||
})
|
})
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t := reflect.TypeOf(target).Elem()
|
||||||
|
errorsMap := make(map[string]interface{})
|
||||||
for _, value := range verrs {
|
for _, value := range verrs {
|
||||||
t := reflect.ValueOf(target)
|
var path []string
|
||||||
realType := t.Type().Elem()
|
getJsonPath(t, value.StructNamespace(), &path)
|
||||||
field, _ := realType.FieldByName(value.StructField())
|
insertError(errorsMap, path, value.Tag())
|
||||||
errs[field.Tag.Get("json")] = value.Tag()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusNotAcceptable, gin.H{
|
c.JSON(http.StatusNotAcceptable, gin.H{
|
||||||
"errors": errs,
|
"errors": errorsMap,
|
||||||
"message": "Requested with wrong parameters",
|
"message": "Requested with wrong parameters",
|
||||||
"code": http.StatusNotAcceptable,
|
"code": http.StatusNotAcceptable,
|
||||||
})
|
})
|
||||||
|
@ -58,3 +73,45 @@ func BindAndValid(c *gin.Context, target interface{}) bool {
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// findField recursively finds the field in a nested struct
|
||||||
|
func getJsonPath(t reflect.Type, namespace string, path *[]string) {
|
||||||
|
fields := strings.Split(namespace, ".")
|
||||||
|
if len(fields) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
f, ok := t.FieldByName(fields[0])
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
*path = append(*path, f.Tag.Get("json"))
|
||||||
|
|
||||||
|
if len(fields) > 1 {
|
||||||
|
subFields := strings.Join(fields[1:], ".")
|
||||||
|
getJsonPath(f.Type, subFields, path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// insertError inserts an error into the errors map
|
||||||
|
func insertError(errorsMap map[string]interface{}, path []string, errorTag string) {
|
||||||
|
if len(path) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonTag := path[0]
|
||||||
|
if len(path) == 1 {
|
||||||
|
// Last element in the path, set the error
|
||||||
|
errorsMap[jsonTag] = errorTag
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new map if necessary
|
||||||
|
if _, ok := errorsMap[jsonTag]; !ok {
|
||||||
|
errorsMap[jsonTag] = make(map[string]interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recursively insert into the nested map
|
||||||
|
subMap, _ := errorsMap[jsonTag].(map[string]interface{})
|
||||||
|
insertError(subMap, path[1:], errorTag)
|
||||||
|
}
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
{"version":"2.0.0-beta.11","build_id":110,"total_build":314}
|
{"version":"2.0.0-beta.11","build_id":111,"total_build":315}
|
|
@ -43,7 +43,7 @@ const issueCert = () => {
|
||||||
step.value++
|
step.value++
|
||||||
modalVisible.value = true
|
modalVisible.value = true
|
||||||
|
|
||||||
refObtainCertLive.value.issue_cert(computedDomain.value, [computedDomain.value], () => {
|
refObtainCertLive.value.issue_cert(computedDomain.value, [computedDomain.value, domain.value], () => {
|
||||||
message.success($gettext('Renew successfully'))
|
message.success($gettext('Renew successfully'))
|
||||||
emit('issued')
|
emit('issued')
|
||||||
})
|
})
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
{"version":"2.0.0-beta.11","build_id":110,"total_build":314}
|
{"version":"2.0.0-beta.11","build_id":111,"total_build":315}
|
|
@ -1,13 +1,13 @@
|
||||||
package settings
|
package settings
|
||||||
|
|
||||||
type Casdoor struct {
|
type Casdoor struct {
|
||||||
Endpoint string `json:"endpoint"`
|
Endpoint string `json:"endpoint" protected:"true"`
|
||||||
ClientId string `json:"client_id"`
|
ClientId string `json:"client_id" protected:"true"`
|
||||||
ClientSecret string `json:"client_secret"`
|
ClientSecret string `json:"client_secret" protected:"true"`
|
||||||
Certificate string `json:"certificate"`
|
Certificate string `json:"certificate" protected:"true"`
|
||||||
Organization string `json:"organization"`
|
Organization string `json:"organization" protected:"true"`
|
||||||
Application string `json:"application"`
|
Application string `json:"application" protected:"true"`
|
||||||
RedirectUri string `json:"redirect_uri"`
|
RedirectUri string `json:"redirect_uri" protected:"true"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var CasdoorSettings = Casdoor{
|
var CasdoorSettings = Casdoor{
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package settings
|
package settings
|
||||||
|
|
||||||
type Nginx struct {
|
type Nginx struct {
|
||||||
AccessLogPath string `json:"access_log_path"`
|
AccessLogPath string `json:"access_log_path" binding:"omitempty,file"`
|
||||||
ErrorLogPath string `json:"error_log_path"`
|
ErrorLogPath string `json:"error_log_path" binding:"omitempty,file"`
|
||||||
ConfigDir string `json:"config_dir" protected:"true"`
|
ConfigDir string `json:"config_dir" protected:"true"`
|
||||||
PIDPath string `json:"pid_path" protected:"true"`
|
PIDPath string `json:"pid_path" protected:"true"`
|
||||||
TestConfigCmd string `json:"test_config_cmd" protected:"true"`
|
TestConfigCmd string `json:"test_config_cmd" protected:"true"`
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
package settings
|
package settings
|
||||||
|
|
||||||
type OpenAI struct {
|
type OpenAI struct {
|
||||||
BaseUrl string `json:"base_url"`
|
BaseUrl string `json:"base_url" binding:"omitempty,url"`
|
||||||
Token string `json:"token"`
|
Token string `json:"token" binding:"omitempty,alphanumdash"`
|
||||||
Proxy string `json:"proxy"`
|
Proxy string `json:"proxy" binding:"omitempty,url"`
|
||||||
Model string `json:"model"`
|
Model string `json:"model" binding:"omitempty,alphanumdash"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var OpenAISettings = OpenAI{}
|
var OpenAISettings = OpenAI{}
|
||||||
|
|
|
@ -10,10 +10,10 @@ type Server struct {
|
||||||
Email string `json:"email" protected:"true"`
|
Email string `json:"email" protected:"true"`
|
||||||
Database string `json:"database" protected:"true"`
|
Database string `json:"database" protected:"true"`
|
||||||
StartCmd string `json:"start_cmd" protected:"true"`
|
StartCmd string `json:"start_cmd" protected:"true"`
|
||||||
CADir string `json:"ca_dir"`
|
CADir string `json:"ca_dir" binding:"omitempty,url"`
|
||||||
Demo bool `json:"demo" protected:"true"`
|
Demo bool `json:"demo" protected:"true"`
|
||||||
PageSize int `json:"page_size" protected:"true"`
|
PageSize int `json:"page_size" protected:"true"`
|
||||||
GithubProxy string `json:"github_proxy"`
|
GithubProxy string `json:"github_proxy" binding:"omitempty,url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var ServerSettings = Server{
|
var ServerSettings = Server{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue