Skip to content

Commit

Permalink
Add validation on slug edit
Browse files Browse the repository at this point in the history
- validation on frontend using js, prevent submit empty slug
- validation on backend, use existing slug if new slug is empty string
- update code to be more readable
  • Loading branch information
junnotantra committed Aug 8, 2019
1 parent aecca64 commit 9465f07
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 38 deletions.
12 changes: 10 additions & 2 deletions management/editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,14 @@ func Form(post Editable, fields ...Field) ([]byte, error) {
external = form.find('.post-controls.external'),
id = form.find('input[name=id]'),
timestamp = $('.__ponzu.content-only'),
slug = $('input[name=slug]');
slug = $('input[name=slug]'),
allowEmptySlug = false;
// hide if this is a new post, or a non-post editor page
if (id.val() === '-1' || form.attr('action') !== '/admin/edit') {
del.hide();
external.hide();
allowEmptySlug = true;
}
// hide approval if not on a pending content item
Expand All @@ -167,11 +169,17 @@ func Form(post Editable, fields ...Field) ([]byte, error) {
if (form.attr('action') === '/admin/addon') {
timestamp.hide();
slug.parent().hide();
allowEmptySlug = true;
}
save.on('click', function(e) {
e.preventDefault();
if (!allowEmptySlug) {
alert("Slug cannot be empty");
return;
}
if (getParam('status') === 'pending') {
var action = form.attr('action');
form.attr('action', action + '?status=pending')
Expand Down Expand Up @@ -238,7 +246,7 @@ func addPostDefaultFieldsToEditorView(p Editable, e *Editor) error {
View: Input("Slug", p, map[string]string{
"label": "URL Slug",
"type": "text",
"placeholder": "Can be left empty for new item",
"placeholder": "Can be left empty, only for new item",
}),
},
{
Expand Down
89 changes: 53 additions & 36 deletions system/db/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,33 +108,38 @@ func update(ns, id string, data url.Values, existingContent *[]byte) (int, error
return err
}

// update the slug,type:id in contentIndex if public content
// get slugs data
var (
existingSlug, newSlug string
)
existingSlug = data.Get("slug")
if len(data["slug"]) > 1 {
newSlug = data["slug"][1]
}

// update the slug (type:id) in contentIndex if public content
if specifier == "" {
if len(data["slug"]) > 1 {
slug := data["slug"][0]
customSlug := data["slug"][1]

target := fmt.Sprintf("%s:%d", ns, cid)
// if slug changed
if slug != customSlug {
ci := tx.Bucket([]byte("__contentIndex"))
if ci == nil {
return bolt.ErrBucketNotFound
}

// remove existing slug from __contentIndex
err = ci.Delete([]byte(fmt.Sprintf("%s", slug)))
if err != nil {
return err
}
target := fmt.Sprintf("%s:%d", ns, cid)
// if slug changed and valid
if existingSlug != newSlug && newSlug != "" {
ci := tx.Bucket([]byte("__contentIndex"))
if ci == nil {
return bolt.ErrBucketNotFound
}

// insert new slug to __contentIndex
k := []byte(customSlug)
v := []byte(target)
err := ci.Put(k, v)
if err != nil {
return err
}
// remove existing slug from __contentIndex
err = ci.Delete([]byte(fmt.Sprintf("%s", existingSlug)))
if err != nil {
return err
}

// insert new slug to __contentIndex
k := []byte(newSlug)
v := []byte(target)
err := ci.Put(k, v)
if err != nil {
return err
}
}
}
Expand Down Expand Up @@ -801,20 +806,22 @@ func postToJSON(ns string, data url.Values) ([]byte, error) {
return nil, err
}

// get slugs data
var (
existingSlug, newSlug string
)
existingSlug = data.Get("slug")
if len(data["slug"]) > 1 {
newSlug = data["slug"][1]
}

// for new item
// if the content has no slug, and has no specifier, create a slug, check it
// for duplicates, and add it to our values
if data.Get("slug") == "" && data.Get("__specifier") == "" {
// check custom slug
slugsData := data["slug"]
if len(slugsData) > 1 && data["slug"][1] != "" {
customSlug := data["slug"][1]

if customSlug != "" {
post.(item.Sluggable).SetSlug(customSlug)
data.Set("slug", customSlug)
}
} else {
// generate default slug
if existingSlug == "" && data.Get("__specifier") == "" {

// if no new slug specified, generate slug
if newSlug == "" {
slug, err := item.Slug(post.(item.Identifiable))
if err != nil {
return nil, err
Expand All @@ -827,9 +834,19 @@ func postToJSON(ns string, data url.Values) ([]byte, error) {

post.(item.Sluggable).SetSlug(slug)
data.Set("slug", slug)
} else {
post.(item.Sluggable).SetSlug(newSlug)
data.Set("slug", newSlug)
}
}

// for editing item
// prevent edit to empty slug
if existingSlug != "" && newSlug == "" {
post.(item.Sluggable).SetSlug(existingSlug)
data.Set("slug", existingSlug)
}

// marshall content struct to json for db storage
j, err := json.Marshal(post)
if err != nil {
Expand Down

0 comments on commit 9465f07

Please sign in to comment.