Browse Source

implement the 'edit pages' functionality in web.lisp file.

stable
Craig Oates 2 years ago
parent
commit
1c6781c945
  1. 88
      src/web.lisp

88
src/web.lisp

@ -365,8 +365,7 @@
:title ,title
:data ,page-content))))
((storage:file-exists-p
"" "pages" (format nil "~a.html" (utils:slugify title)))
((storage:file-exists-p "" "pages" (utils:slugify title))
(render
"/user/create-page.html"
(append (auth:auth-user-data)
@ -377,13 +376,96 @@
(t (storage:store-text
"" ; `USERNAME' blank because it's not used/needed.
"pages"
(format nil "~a.html" (utils:slugify title))
(utils:slugify title)
page-content)
(utils:set-alert "Page created.")
(redirect "/dashboard")))
;; Not Authorised
(progn (utils:set-alert "You are not logged in.")
(redirect "/login")))))))
(defroute ("/pages" :method :GET) ()
(hermetic:auth (:logged-in)
;; Authorised
(let ((alert (utils:get-and-reset-alert)))
(render "/user/pages.html"
(append (auth:auth-user-data)
`(:alert ,alert
:pages ,(storage:get-file-names
(storage:get-files-in-directory
"" "pages"))))))
;; Not Authorised
(progn
(utils:set-alert "You are not logged in.")
(redirect "/login"))))
(defroute ("/edit/page/:slug" :method :GET) (&key slug)
(hermetic:auth (:logged-in)
;; Authorised
(let ((alert (utils:get-and-reset-alert)))
(render "/user/edit-page.html"
(append (auth:auth-user-data)
`(:alert ,alert
:title ,slug
:data ,(storage:open-text-file
"" "pages" slug)))))
;; Not Authorised
(progn
(utils:set-alert "You are not logged in.")
(redirect "/login"))))
(defroute ("/edit/page" :method :POST) ()
(destructuring-bind
(&key title page-content authenticity-token &allow-other-keys)
(utils:request-params (lack.request:request-body-parameters ningle:*request*))
(format t "~a" page-content)
(cond ((not (string= authenticity-token (auth:csrf-token)))
`(,+forbidden+ (:content-type "text/plain") ("Denied")))
(t (hermetic:auth
(:logged-in)
;; Authorised
(cond ((utils:string-is-nil-or-empty? title)
(utils:set-alert "Cannot find file. Unable to save page.")
(redirect "/pages"))
(t (storage:store-text
"" ; `USERNAME' blank because it's not used/needed.
"pages"
(utils:slugify title)
page-content)
(utils:set-alert "Page updated.")
(redirect "/pages")))
;; Not Authorised
(progn (utils:set-alert "You are not logged in.")
(redirect "/login")))))))
(defroute ("/rename/page" :method :POST) ()
(destructuring-bind
(&key title new-title authenticity-token &allow-other-keys)
(utils:request-params (lack.request:request-body-parameters ningle:*request*))
(cond ((not (string= authenticity-token (auth:csrf-token)))
`(,+forbidden+ (:content-type "text/plain") ("Denied")))
(t (hermetic:auth
(:logged-in)
;; Authorised
(cond ((utils:string-is-nil-or-empty? title)
(utils:set-alert "Cannot find file. Unable to save changes.")
(redirect "/pages"))
((utils:string-is-nil-or-empty? new-title)
(utils:set-alert "No title provided. Unable to save changes.")
(redirect (format nil "/edit/page/~a" title)))
(t (storage:rename-content-file
"" ; `USERNAME' blank because it's not used/needed.
"pages" title (utils:slugify new-title))
(utils:set-alert "File name changed.")
(redirect (format nil "/edit/page/~a"
(utils:slugify new-title)))))
;; Not Authorised
(progn (utils:set-alert "You are not logged in.")
(redirect "/login")))))))
;;
;; Error pages

Loading…
Cancel
Save