Reorganised routes so that routes/wiki.clj is less overcrowded and messy.

This commit is contained in:
simon 2016-09-11 17:38:46 +01:00
parent 09fe67a26e
commit 44263c3fa0
8 changed files with 178 additions and 95 deletions

View file

@ -1 +1 @@
{:admin {:admin true, :email "info@weft.scot", password "admin"}} {:admin {:admin true, :email "info@weft.scot", :password "admin"}}

View file

@ -1 +1 @@
This is the page linked to from the left bar. This is the page linked to from the [[Introduction]] page.

View file

@ -2,11 +2,20 @@
{% block content %} {% block content %}
<div id="content"> <div id="content">
<ul> <table>
<tr>
<th>Edit</th><th>Delete</th>
</tr>
{% for user in users %} {% for user in users %}
<li><a href="edit-user?target={{user}}">{{user}}</a></li> <tr>
<td><a href="edit-user?target={{user}}">Edit {{user}}</a></td>
<td><a href="delete-user?target={{user}}">Delete {{user}}</a></td>
</tr>
{% endfor %} {% endfor %}
<li><a href="edit-user">Add new user</a></li> <tr>
</ul> <td><a href="edit-user">Add new user</a></td>
<td></td>
</tr>
</table>
</div> </div>
{% endblock %} {% endblock %}

View file

@ -101,6 +101,7 @@
{keywd {keywd
(merge user (merge user
{:password (password/encrypt newpass)})}))) {:password (password/encrypt newpass)})})))
(timbre/info (str "Successfully changed password for user " username))
true)) true))
(catch Exception any (catch Exception any
(timbre/error (timbre/error
@ -145,11 +146,27 @@
(spit password-file-path (spit password-file-path
(merge users (merge users
{(keyword username) (merge user full-details)})) {(keyword username) (merge user full-details)}))
(timbre/info (str "Successfully added user " username))
true) true)
(catch Exception any (catch Exception any
(timbre/error (timbre/error
(format "Changing password failed for user %s failed: %s (%s)" (format "Adding user %s failed: %s (%s)"
username (.getName (.getClass any)) (.getMessage any))) username (.getName (.getClass any)) (.getMessage any)))
false)))) false))))
(defn delete-user
"Delete the user with this `username` from the password file."
[username]
(let [users (get-users)]
(try
(locking password-file-path
(spit password-file-path
(dissoc users (keyword username)))
(timbre/info (str "Successfully deleted user " username))
true)
(catch Exception any
(timbre/error
(format "Deleting user %s failed: %s (%s)"
username (.getName (.getClass any)) (.getMessage any)))
false))))

View file

@ -0,0 +1,84 @@
(ns ^{:doc "Render all the main pages of a very simple Wiki engine."
:author "Simon Brooke"}
smeagol.routes.admin
(:require [clojure.walk :refer :all]
[noir.session :as session]
[taoensso.timbre :as timbre]
[smeagol.authenticate :as auth]
[smeagol.layout :as layout]
[smeagol.util :as util]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;
;;;; Smeagol: a very simple Wiki engine.
;;;;
;;;; This program is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU General Public License
;;;; as published by the Free Software Foundation; either version 2
;;;; of the License, or (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
;;;; USA.
;;;;
;;;; Copyright (C) 2016 Simon Brooke
;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn edit-users
"Put a list of users on-screen for editing."
[request]
(let [params (keywordize-keys (:params request))
user (session/get :user)]
(layout/render "edit-users.html"
(merge (util/standard-params request)
{:title "Select user to edit"
:users (auth/list-users)}))))
(defn delete-user
"Delete a user."
[request]
(let [params (keywordize-keys (:params request))
target (:target params)
deleted (auth/delete-user target)
message (if deleted (str "Successfully deleted user " target))
error (if (not deleted) (str "Could not delete user " target))]
(layout/render "edit-users.html"
(merge (util/standard-params request)
{:title "Select user to edit"
:message message
:error error
:users (auth/list-users)}))))
(defn edit-user
"Put an individual user's details on screen for editing."
[request]
(let [params (keywordize-keys (:params request))
target (:target params)
pass1 (:pass1 params)
password (if (and pass1 (auth/evaluate-password pass1 (:pass2 params))) pass1)
stored (if (:email params)
(auth/add-user target password (:email params) (:admin params)))
message (if stored (str "User " target " was stored successfully."))
error (if (and (:email params) (not stored))
(str "User " target " was not stored."))
details (auth/fetch-user-details target)]
(if message
(timbre/info message))
(if error
(timbre/warn error))
(layout/render "edit-user.html"
(merge (util/standard-params request)
{:title (str "Edit user " target)
:message message
:error error
:target target
:details details}))))

View file

View file

@ -3,10 +3,9 @@
smeagol.routes.wiki smeagol.routes.wiki
(:require [clojure.walk :refer :all] (:require [clojure.walk :refer :all]
[clojure.java.io :as cjio] [clojure.java.io :as cjio]
[clojure.string :as cs] [cemerick.url :refer (url url-encode url-decode)]
[compojure.core :refer :all] [compojure.core :refer :all]
[clj-jgit.porcelain :as git] [clj-jgit.porcelain :as git]
[cemerick.url :refer (url url-encode url-decode)]
[markdown.core :as md] [markdown.core :as md]
[noir.io :as io] [noir.io :as io]
[noir.response :as response] [noir.response :as response]
@ -17,7 +16,8 @@
[smeagol.diff2html :as d2h] [smeagol.diff2html :as d2h]
[smeagol.layout :as layout] [smeagol.layout :as layout]
[smeagol.util :as util] [smeagol.util :as util]
[smeagol.history :as hist])) [smeagol.history :as hist]
[smeagol.routes.admin :as admin]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; ;;;;
@ -42,19 +42,6 @@
;;;; ;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn local-links
"Rewrite text in `html-src` surrounded by double square brackets as a local link into this wiki."
[^String html-src]
(cs/replace html-src #"\[\[[^\[\]]*\]\]"
#(let [text (clojure.string/replace %1 #"[\[\]]" "")
encoded (url-encode text)
;; I use '\_' to represent '_' in wiki markup, because
;; '_' is meaningful in Markdown. However, this needs to
;; be stripped out when interpreting local links.
munged (cs/replace encoded #"%26%2395%3B" "_")]
(format "<a href='wiki?page=%s'>%s</a>" munged text))))
(defn get-git-repo (defn get-git-repo
"Get the git repository for my content, creating it if necessary" "Get the git repository for my content, creating it if necessary"
[] []
@ -64,17 +51,6 @@
(git/git-init path)))) (git/git-init path))))
(defn standard-params
"Return a map of standard parameters to pass to the template renderer."
[request]
(let [user (session/get :user)]
{:user user
:admin (auth/get-admin user)
:side-bar (local-links (util/md->html "/content/_side-bar.md"))
:header (local-links (util/md->html "/content/_header.md"))
:version (System/getProperty "smeagol.version")}))
(defn process-source (defn process-source
"Process `source-text` and save it to the specified `file-path`, committing it "Process `source-text` and save it to the specified `file-path`, committing it
to Git and finally redirecting to wiki-page." to Git and finally redirecting to wiki-page."
@ -113,14 +89,16 @@
file-path (str (io/resource-path) "content/" page suffix) file-path (str (io/resource-path) "content/" page suffix)
exists? (.exists (cjio/as-file file-path)) exists? (.exists (cjio/as-file file-path))
user (session/get :user)] user (session/get :user)]
(if (not exists?) (timbre/info (format "File '%s' not found; creating a new file" file-path))) (if (not exists?)
(timbre/info (format "File '%s' not found; creating a new file" file-path))
(timbre/info (format "Opening '%s' for editing" file-path)))
(cond src-text (process-source params suffix) (cond src-text (process-source params suffix)
true true
(layout/render template (layout/render template
(merge (standard-params request) (merge (util/standard-params request)
{:title (str "Edit " page) {:title (str "Edit " page)
:page page :page page
:side-bar (local-links (util/md->html side-bar)) :side-bar (util/local-links (util/md->html side-bar))
:content (if exists? (io/slurp-resource (str "/content/" page suffix)) "") :content (if exists? (io/slurp-resource (str "/content/" page suffix)) "")
:exists exists?})))))) :exists exists?}))))))
@ -131,43 +109,6 @@
(edit-page request "stylesheet" ".css" "edit-css.html" "/content/_edit-side-bar.md")) (edit-page request "stylesheet" ".css" "edit-css.html" "/content/_edit-side-bar.md"))
(defn edit-users
"Put a list of users on-screen for editing."
[request]
(let [params (keywordize-keys (:params request))
user (session/get :user)]
(layout/render "edit-users.html"
(merge (standard-params request)
{:title "Select user to edit"
:users (auth/list-users)}))))
(defn edit-user
"Put an individual user's details on screen for editing."
[request]
(let [params (keywordize-keys (:params request))
target (:target params)
pass1 (:pass1 params)
password (if (and pass1 (auth/evaluate-password pass1 (:pass2 params))) pass1)
stored (if (:email params)
(auth/add-user target password (:email params) (:admin params)))
message (if stored (str "User " target " was stored successfully."))
error (if (and (:email params) (not stored))
(str "User " target " was not stored."))
details (auth/fetch-user-details target)]
(if message
(timbre/info message))
(if error
(timbre/warn error))
(layout/render "edit-user.html"
(merge (standard-params request)
{:title (str "Edit user " target)
:message message
:error error
:target target
:details details}))))
(defn wiki-page (defn wiki-page
"Render the markdown page specified in this `request`, if any. If none found, redirect to edit-page" "Render the markdown page specified in this `request`, if any. If none found, redirect to edit-page"
[request] [request]
@ -177,12 +118,14 @@
file-path (str (io/resource-path) file-name) file-path (str (io/resource-path) file-name)
exists? (.exists (clojure.java.io/as-file file-path))] exists? (.exists (clojure.java.io/as-file file-path))]
(cond exists? (cond exists?
(do
(timbre/info (format "Showing page '%s'" page))
(layout/render "wiki.html" (layout/render "wiki.html"
(merge (standard-params request) (merge (util/standard-params request)
{:title page {:title page
:page page :page page
:content (local-links (util/md->html file-name)) :content (util/local-links (util/md->html file-name))
:editable true})) :editable true})))
true (response/redirect (str "edit?page=" page))))) true (response/redirect (str "edit?page=" page)))))
@ -195,7 +138,7 @@
file-name (str page ".md") file-name (str page ".md")
repo-path (str (io/resource-path) "/content/")] repo-path (str (io/resource-path) "/content/")]
(layout/render "history.html" (layout/render "history.html"
(merge (standard-params request) (merge (util/standard-params request)
{:title (str "History of " page) {:title (str "History of " page)
:page page :page page
:history (hist/find-history repo-path file-name)})))) :history (hist/find-history repo-path file-name)}))))
@ -210,10 +153,10 @@
file-name (str page ".md") file-name (str page ".md")
repo-path (str (io/resource-path) "/content/")] repo-path (str (io/resource-path) "/content/")]
(layout/render "wiki.html" (layout/render "wiki.html"
(merge (standard-params request) (merge (util/standard-params request)
{:title (str "Version " version " of " page) {:title (str "Version " version " of " page)
:page page :page page
:content (local-links :content (util/local-links
(md/md-to-html-string (md/md-to-html-string
(hist/fetch-version (hist/fetch-version
repo-path file-name version)))})))) repo-path file-name version)))}))))
@ -228,7 +171,7 @@
file-name (str page ".md") file-name (str page ".md")
repo-path (str (io/resource-path) "/content/")] repo-path (str (io/resource-path) "/content/")]
(layout/render "wiki.html" (layout/render "wiki.html"
(merge (standard-params request) (merge (util/standard-params request)
{:title (str "Changes since version " version " of " page) {:title (str "Changes since version " version " of " page)
:page page :page page
:content (d2h/diff2html (hist/diff repo-path file-name version))})))) :content (d2h/diff2html (hist/diff repo-path file-name version))}))))
@ -255,11 +198,11 @@
(response/redirect redirect-to)) (response/redirect redirect-to))
true true
(layout/render "auth.html" (layout/render "auth.html"
(merge (standard-params request) (merge (util/standard-params request)
{:title (if user (str "Logout " user) "Log in") {:title (if user (str "Logout " user) "Log in")
:redirect-to ((:headers request) "referer") :redirect-to ((:headers request) "referer")
:side-bar (local-links (util/md->html "/content/_side-bar.md")) :side-bar (util/local-links (util/md->html "/content/_side-bar.md"))
:header (local-links (util/md->html "/content/_header.md")) :header (util/local-links (util/md->html "/content/_header.md"))
:user user}))))) :user user})))))
@ -279,23 +222,24 @@
(not (= pass1 pass2)) "Your proposed passwords don't match" (not (= pass1 pass2)) "Your proposed passwords don't match"
true "Your password was not changed")] ;; but I don't know why... true "Your password was not changed")] ;; but I don't know why...
(layout/render "passwd.html" (layout/render "passwd.html"
(merge (standard-params request) (merge (util/standard-params request)
{:title (str "Change passord for " user) {:title (str "Change passord for " user)
:side-bar (local-links (util/md->html "/content/_side-bar.md")) :side-bar (util/local-links (util/md->html "/content/_side-bar.md"))
:header (local-links (util/md->html "/content/_header.md")) :header (util/local-links (util/md->html "/content/_header.md"))
:message message})))) :message message}))))
(defroutes wiki-routes (defroutes wiki-routes
(GET "/wiki" request (wiki-page request)) (GET "/wiki" request (wiki-page request))
(GET "/" request (wiki-page request)) (GET "/" request (wiki-page request))
(GET "/delete-user" request (route/restricted (admin/delete-user request)))
(GET "/edit" request (route/restricted (edit-page request))) (GET "/edit" request (route/restricted (edit-page request)))
(POST "/edit" request (route/restricted (edit-page request))) (POST "/edit" request (route/restricted (edit-page request)))
(GET "/edit-css" request (route/restricted (edit-css-page request))) (GET "/edit-css" request (route/restricted (edit-css-page request)))
(POST "/edit-css" request (route/restricted (edit-css-page request))) (POST "/edit-css" request (route/restricted (edit-css-page request)))
(GET "/edit-users" request (route/restricted (edit-users request))) (GET "/edit-users" request (route/restricted (admin/edit-users request)))
(GET "/edit-user" request (route/restricted (edit-user request))) (GET "/edit-user" request (route/restricted (admin/edit-user request)))
(POST "/edit-user" request (route/restricted (edit-user request))) (POST "/edit-user" request (route/restricted (admin/edit-user request)))
(GET "/history" request (history-page request)) (GET "/history" request (history-page request))
(GET "/version" request (version-page request)) (GET "/version" request (version-page request))
(GET "/changes" request (diff-page request)) (GET "/changes" request (diff-page request))

View file

@ -1,8 +1,12 @@
(ns ^{:doc "Miscellaneous utility functions supporting Smeagol." (ns ^{:doc "Miscellaneous utility functions supporting Smeagol."
:author "Simon Brooke"} :author "Simon Brooke"}
smeagol.util smeagol.util
(:require [noir.io :as io] (:require [clojure.string :as cs]
[markdown.core :as md])) [cemerick.url :refer (url url-encode url-decode)]
[noir.io :as io]
[noir.session :as session]
[markdown.core :as md]
[smeagol.authenticate :as auth]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; ;;;;
@ -31,3 +35,28 @@
"reads a markdown file from public/md and returns an HTML string" "reads a markdown file from public/md and returns an HTML string"
[filename] [filename]
(md/md-to-html-string (io/slurp-resource filename))) (md/md-to-html-string (io/slurp-resource filename)))
(defn local-links
"Rewrite text in `html-src` surrounded by double square brackets as a local link into this wiki."
[^String html-src]
(cs/replace html-src #"\[\[[^\[\]]*\]\]"
#(let [text (clojure.string/replace %1 #"[\[\]]" "")
encoded (url-encode text)
;; I use '\_' to represent '_' in wiki markup, because
;; '_' is meaningful in Markdown. However, this needs to
;; be stripped out when interpreting local links.
munged (cs/replace encoded #"%26%2395%3B" "_")]
(format "<a href='wiki?page=%s'>%s</a>" munged text))))
(defn standard-params
"Return a map of standard parameters to pass to the template renderer."
[request]
(let [user (session/get :user)]
{:user user
:admin (auth/get-admin user)
:side-bar (local-links (md->html "/content/_side-bar.md"))
:header (local-links (md->html "/content/_header.md"))
:version (System/getProperty "smeagol.version")}))