From b0a3989ef4a66e89eb2e15a43c62ae2cbae5a04e Mon Sep 17 00:00:00 2001 From: 0xJacky Date: Sun, 28 Jan 2024 14:33:21 +0800 Subject: [PATCH] enhance: show the form error details of CertificateEditor --- api/certificate/certificate.go | 244 +++++++++--------- app/package.json | 2 +- .../views/certificate/CertificateEditor.vue | 41 ++- internal/cert/helper.go | 7 + 4 files changed, 166 insertions(+), 128 deletions(-) diff --git a/api/certificate/certificate.go b/api/certificate/certificate.go index b5ff78cd..2bb69d0a 100644 --- a/api/certificate/certificate.go +++ b/api/certificate/certificate.go @@ -1,174 +1,174 @@ package certificate import ( - "github.com/0xJacky/Nginx-UI/api" - "github.com/0xJacky/Nginx-UI/api/cosy" - "github.com/0xJacky/Nginx-UI/internal/cert" - "github.com/0xJacky/Nginx-UI/model" - "github.com/0xJacky/Nginx-UI/query" - "github.com/gin-gonic/gin" - "github.com/spf13/cast" - "net/http" - "os" + "github.com/0xJacky/Nginx-UI/api" + "github.com/0xJacky/Nginx-UI/api/cosy" + "github.com/0xJacky/Nginx-UI/internal/cert" + "github.com/0xJacky/Nginx-UI/model" + "github.com/0xJacky/Nginx-UI/query" + "github.com/gin-gonic/gin" + "github.com/spf13/cast" + "net/http" + "os" ) type APICertificate struct { - *model.Cert - SSLCertificate string `json:"ssl_certificate,omitempty"` - SSLCertificateKey string `json:"ssl_certificate_key,omitempty"` - CertificateInfo *cert.Info `json:"certificate_info,omitempty"` + *model.Cert + SSLCertificate string `json:"ssl_certificate,omitempty"` + SSLCertificateKey string `json:"ssl_certificate_key,omitempty"` + CertificateInfo *cert.Info `json:"certificate_info,omitempty"` } func Transformer(certModel *model.Cert) (certificate *APICertificate) { - var sslCertificationBytes, sslCertificationKeyBytes []byte - var certificateInfo *cert.Info - if certModel.SSLCertificatePath != "" { - if _, err := os.Stat(certModel.SSLCertificatePath); err == nil { - sslCertificationBytes, _ = os.ReadFile(certModel.SSLCertificatePath) - if !cert.IsPublicKey(string(sslCertificationBytes)) { - sslCertificationBytes = []byte{} - } - } + var sslCertificationBytes, sslCertificationKeyBytes []byte + var certificateInfo *cert.Info + if certModel.SSLCertificatePath != "" { + if _, err := os.Stat(certModel.SSLCertificatePath); err == nil { + sslCertificationBytes, _ = os.ReadFile(certModel.SSLCertificatePath) + if !cert.IsPublicKey(string(sslCertificationBytes)) { + sslCertificationBytes = []byte{} + } + } - certificateInfo, _ = cert.GetCertInfo(certModel.SSLCertificatePath) - } + certificateInfo, _ = cert.GetCertInfo(certModel.SSLCertificatePath) + } - if certModel.SSLCertificateKeyPath != "" { - if _, err := os.Stat(certModel.SSLCertificateKeyPath); err == nil { - sslCertificationKeyBytes, _ = os.ReadFile(certModel.SSLCertificateKeyPath) - if !cert.IsPrivateKey(string(sslCertificationKeyBytes)) { - sslCertificationKeyBytes = []byte{} - } - } - } + if certModel.SSLCertificateKeyPath != "" { + if _, err := os.Stat(certModel.SSLCertificateKeyPath); err == nil { + sslCertificationKeyBytes, _ = os.ReadFile(certModel.SSLCertificateKeyPath) + if !cert.IsPrivateKey(string(sslCertificationKeyBytes)) { + sslCertificationKeyBytes = []byte{} + } + } + } - return &APICertificate{ - Cert: certModel, - SSLCertificate: string(sslCertificationBytes), - SSLCertificateKey: string(sslCertificationKeyBytes), - CertificateInfo: certificateInfo, - } + return &APICertificate{ + Cert: certModel, + SSLCertificate: string(sslCertificationBytes), + SSLCertificateKey: string(sslCertificationKeyBytes), + CertificateInfo: certificateInfo, + } } func GetCertList(c *gin.Context) { - cosy.Core[model.Cert](c).SetFussy("name", "domain").SetTransformer(func(m *model.Cert) any { + cosy.Core[model.Cert](c).SetFussy("name", "domain").SetTransformer(func(m *model.Cert) any { - info, _ := cert.GetCertInfo(m.SSLCertificatePath) + info, _ := cert.GetCertInfo(m.SSLCertificatePath) - return APICertificate{ - Cert: m, - CertificateInfo: info, - } - }).PagingList() + return APICertificate{ + Cert: m, + CertificateInfo: info, + } + }).PagingList() } func GetCert(c *gin.Context) { - q := query.Cert + q := query.Cert - certModel, err := q.FirstByID(cast.ToInt(c.Param("id"))) + certModel, err := q.FirstByID(cast.ToInt(c.Param("id"))) - if err != nil { - api.ErrHandler(c, err) - return - } + if err != nil { + api.ErrHandler(c, err) + return + } - c.JSON(http.StatusOK, Transformer(certModel)) + c.JSON(http.StatusOK, Transformer(certModel)) } type certJson struct { - Name string `json:"name"` - SSLCertificatePath string `json:"ssl_certificate_path" binding:"publickey_path"` - SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"privatekey_path"` - SSLCertificate string `json:"ssl_certificate" binding:"omitempty,publickey"` - SSLCertificateKey string `json:"ssl_certificate_key" binding:"omitempty,privatekey"` - ChallengeMethod string `json:"challenge_method"` - DnsCredentialID int `json:"dns_credential_id"` + Name string `json:"name" binding:"required"` + SSLCertificatePath string `json:"ssl_certificate_path" binding:"required,publickey_path"` + SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required,privatekey_path"` + SSLCertificate string `json:"ssl_certificate" binding:"omitempty,publickey"` + SSLCertificateKey string `json:"ssl_certificate_key" binding:"omitempty,privatekey"` + ChallengeMethod string `json:"challenge_method"` + DnsCredentialID int `json:"dns_credential_id"` } func AddCert(c *gin.Context) { - var json certJson - if !api.BindAndValid(c, &json) { - return - } - certModel := &model.Cert{ - Name: json.Name, - SSLCertificatePath: json.SSLCertificatePath, - SSLCertificateKeyPath: json.SSLCertificateKeyPath, - ChallengeMethod: json.ChallengeMethod, - DnsCredentialID: json.DnsCredentialID, - } + var json certJson + if !api.BindAndValid(c, &json) { + return + } + certModel := &model.Cert{ + Name: json.Name, + SSLCertificatePath: json.SSLCertificatePath, + SSLCertificateKeyPath: json.SSLCertificateKeyPath, + ChallengeMethod: json.ChallengeMethod, + DnsCredentialID: json.DnsCredentialID, + } - err := certModel.Insert() + err := certModel.Insert() - if err != nil { - api.ErrHandler(c, err) - return - } + if err != nil { + api.ErrHandler(c, err) + return + } - content := &cert.Content{ - SSLCertificatePath: json.SSLCertificatePath, - SSLCertificateKeyPath: json.SSLCertificateKeyPath, - SSLCertificate: json.SSLCertificate, - SSLCertificateKey: json.SSLCertificateKey, - } + content := &cert.Content{ + SSLCertificatePath: json.SSLCertificatePath, + SSLCertificateKeyPath: json.SSLCertificateKeyPath, + SSLCertificate: json.SSLCertificate, + SSLCertificateKey: json.SSLCertificateKey, + } - err = content.WriteFile() + err = content.WriteFile() - if err != nil { - api.ErrHandler(c, err) - return - } + if err != nil { + api.ErrHandler(c, err) + return + } - c.JSON(http.StatusOK, Transformer(certModel)) + c.JSON(http.StatusOK, Transformer(certModel)) } func ModifyCert(c *gin.Context) { - id := cast.ToInt(c.Param("id")) + id := cast.ToInt(c.Param("id")) - var json certJson + var json certJson - if !api.BindAndValid(c, &json) { - return - } + if !api.BindAndValid(c, &json) { + return + } - q := query.Cert + q := query.Cert - certModel, err := q.FirstByID(id) - if err != nil { - api.ErrHandler(c, err) - return - } + certModel, err := q.FirstByID(id) + if err != nil { + api.ErrHandler(c, err) + return + } - err = certModel.Updates(&model.Cert{ - Name: json.Name, - SSLCertificatePath: json.SSLCertificatePath, - SSLCertificateKeyPath: json.SSLCertificateKeyPath, - ChallengeMethod: json.ChallengeMethod, - DnsCredentialID: json.DnsCredentialID, - }) + err = certModel.Updates(&model.Cert{ + Name: json.Name, + SSLCertificatePath: json.SSLCertificatePath, + SSLCertificateKeyPath: json.SSLCertificateKeyPath, + ChallengeMethod: json.ChallengeMethod, + DnsCredentialID: json.DnsCredentialID, + }) - if err != nil { - api.ErrHandler(c, err) - return - } + if err != nil { + api.ErrHandler(c, err) + return + } - content := &cert.Content{ - SSLCertificatePath: json.SSLCertificatePath, - SSLCertificateKeyPath: json.SSLCertificateKeyPath, - SSLCertificate: json.SSLCertificate, - SSLCertificateKey: json.SSLCertificateKey, - } + content := &cert.Content{ + SSLCertificatePath: json.SSLCertificatePath, + SSLCertificateKeyPath: json.SSLCertificateKeyPath, + SSLCertificate: json.SSLCertificate, + SSLCertificateKey: json.SSLCertificateKey, + } - err = content.WriteFile() + err = content.WriteFile() - if err != nil { - api.ErrHandler(c, err) - return - } + if err != nil { + api.ErrHandler(c, err) + return + } - GetCert(c) + GetCert(c) } func RemoveCert(c *gin.Context) { - cosy.Core[model.Cert](c).Destroy() + cosy.Core[model.Cert](c).Destroy() } diff --git a/app/package.json b/app/package.json index 9c7a9687..5cea5b02 100644 --- a/app/package.json +++ b/app/package.json @@ -1,6 +1,6 @@ { "name": "nginx-ui-app-next", - "version": "2.0.0-beta.11", + "version": "2.0.0-beta.12", "type": "module", "scripts": { "dev": "vite", diff --git a/app/src/views/certificate/CertificateEditor.vue b/app/src/views/certificate/CertificateEditor.vue index 2c234172..158d9748 100644 --- a/app/src/views/certificate/CertificateEditor.vue +++ b/app/src/views/certificate/CertificateEditor.vue @@ -41,12 +41,15 @@ onMounted(() => { }) const router = useRouter() +const errors = ref({}) as Ref> function save() { cert.save(data.value.id, data.value).then(r => { data.value = r message.success($gettext('Save successfully')) router.push(`/certificates/${r.id}`) + errors.value = {} }).catch(e => { + errors.value = e.errors message.error($gettext(e?.message ?? 'Server error')) }) } @@ -142,7 +145,13 @@ const isManaged = computed(() => { layout="vertical" style="max-width: 600px" > - +

{{ data.name }}

@@ -151,7 +160,13 @@ const isManaged = computed(() => { v-model:value="data.name" />
- +

{{ data.ssl_certificate_path }}

@@ -160,7 +175,13 @@ const isManaged = computed(() => { v-model:value="data.ssl_certificate_path" />
- +

{{ data.ssl_certificate_key_path }}

@@ -169,7 +190,12 @@ const isManaged = computed(() => { v-model:value="data.ssl_certificate_key_path" />
- + { :placeholder="$gettext('Leave blank will not change anything')" /> - +