Эх сурвалжийг харах

Fix pb invalid slug panic

Chenyang 1 жил өмнө
parent
commit
3718cee5fb
2 өөрчлөгдсөн 25 нэмэгдсэн , 0 устгасан
  1. 13 0
      pagebuilder/builder.go
  2. 12 0
      presets/api.go

+ 13 - 0
pagebuilder/builder.go

@@ -255,6 +255,19 @@ func (b *Builder) Configure(pb *presets.Builder, db *gorm.DB, l10nB *l10n.Builde
 				dmb = templateM
 			}
 			obj = dmb.NewModel()
+			if sd, ok := obj.(presets.SlugDecoder); ok {
+				vs, err := presets.RecoverPrimaryColumnValuesBySlug(sd, id)
+				if err != nil {
+					return pb.DefaultNotFoundPageFunc(ctx)
+				}
+				if _, err := strconv.Atoi(vs["id"]); err != nil {
+					return pb.DefaultNotFoundPageFunc(ctx)
+				}
+			} else {
+				if _, err := strconv.Atoi(id); err != nil {
+					return pb.DefaultNotFoundPageFunc(ctx)
+				}
+			}
 			obj, err = dmb.Detailing().GetFetchFunc()(obj, id, ctx)
 			if err != nil {
 				if errors.Is(err, presets.ErrRecordNotFound) {

+ 12 - 0
presets/api.go

@@ -1,6 +1,7 @@
 package presets
 
 import (
+	"fmt"
 	"net/http"
 	"net/url"
 
@@ -62,6 +63,17 @@ type SlugDecoder interface {
 	PrimaryColumnValuesBySlug(slug string) map[string]string
 }
 
+func RecoverPrimaryColumnValuesBySlug(dec SlugDecoder, slug string) (r map[string]string, err error) {
+	defer func() {
+		if e := recover(); e != nil {
+			r = nil
+			err = fmt.Errorf("wrong slug: %v", slug)
+		}
+	}()
+	r = dec.PrimaryColumnValuesBySlug(slug)
+	return r, nil
+}
+
 type SlugEncoder interface {
 	PrimarySlug() string
 }