From 81a33bb68f25f40c4935567dbad3df212dcb1542 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Thu, 5 Sep 2019 14:44:47 +0200 Subject: [PATCH] naming conventions regarding side effects --- src/wanijo/attribute/routes.clj | 2 +- src/wanijo/handler.clj | 4 +-- src/wanijo/home/view.clj | 2 +- src/wanijo/infrastructure/auth.clj | 2 +- src/wanijo/infrastructure/view.clj | 2 +- src/wanijo/instance/routes.clj | 2 +- src/wanijo/instance/view/edit.clj | 2 +- src/wanijo/instance/view/instances.clj | 2 +- src/wanijo/instance/view/link_selection.clj | 2 +- src/wanijo/instance/view/show.clj | 2 +- src/wanijo/instance/view/starred.clj | 2 +- src/wanijo/schema/middleware.clj | 32 ++++++++++----------- src/wanijo/schema/routes.clj | 4 +-- src/wanijo/schema/view.clj | 4 +-- src/wanijo/user/view.clj | 2 +- src/wanijo/visualisation/routes.clj | 2 +- src/wanijo/visualisation/view.clj | 2 +- 17 files changed, 34 insertions(+), 36 deletions(-) diff --git a/src/wanijo/attribute/routes.clj b/src/wanijo/attribute/routes.clj index 5b7489c..976f779 100644 --- a/src/wanijo/attribute/routes.clj +++ b/src/wanijo/attribute/routes.clj @@ -33,7 +33,7 @@ (resp/redirect (path :schema-show {:uuid (get-in req [:params :schema])}))) (defn wrap-allowed-to-write [] - (schema-middleware/write-permission-middleware + (schema-middleware/write-permission-middleware! #(or (get-in % [:params :schema]) (get-in % [:route-params :schema]) (-> (get-in % [:route-params :uuid]) diff --git a/src/wanijo/handler.clj b/src/wanijo/handler.clj index a60cbef..0dbb64e 100644 --- a/src/wanijo/handler.clj +++ b/src/wanijo/handler.clj @@ -42,7 +42,7 @@ (def standalone-app (-> app-routes - schema-middleware/wrap-user-schemas + schema-middleware/wrap-user-schemas! ring-json/wrap-json-response (wrap-defaults site-defaults))) @@ -55,7 +55,7 @@ (-> app-routes wrap-spec-asserts devmode/wrap-devmode - schema-middleware/wrap-user-schemas + schema-middleware/wrap-user-schemas! ring-json/wrap-json-response (wrap-defaults (assoc-in site-defaults diff --git a/src/wanijo/home/view.clj b/src/wanijo/home/view.clj index 31c28d4..171019f 100644 --- a/src/wanijo/home/view.clj +++ b/src/wanijo/home/view.clj @@ -2,4 +2,4 @@ (:require [wanijo.infrastructure.view :as view])) (defn root! [req] - (view/layout! :session (:session req))) + (view/layout :session (:session req))) diff --git a/src/wanijo/infrastructure/auth.clj b/src/wanijo/infrastructure/auth.clj index ec7fd6b..8369057 100644 --- a/src/wanijo/infrastructure/auth.clj +++ b/src/wanijo/infrastructure/auth.clj @@ -21,7 +21,7 @@ :flash :invalid-credentials)))) (defn login! [req] - (view/layout! + (view/layout :content [[:h1 "Login"] (hform/form-to diff --git a/src/wanijo/infrastructure/view.clj b/src/wanijo/infrastructure/view.clj index c2a289e..73399ae 100644 --- a/src/wanijo/infrastructure/view.clj +++ b/src/wanijo/infrastructure/view.clj @@ -23,7 +23,7 @@ [:h2.flash__heading--error "Warning"] content]) -(defn layout! +(defn layout [& {:keys [content title session diff --git a/src/wanijo/instance/routes.clj b/src/wanijo/instance/routes.clj index ce47b09..f7a994b 100644 --- a/src/wanijo/instance/routes.clj +++ b/src/wanijo/instance/routes.clj @@ -127,7 +127,7 @@ (GET (register! :instance-list "/instance/list/:schema-uuid") [schema-uuid :as req] (list! schema-uuid req)) - (middleware-schema/wrap-allowed-to-read + (middleware-schema/wrap-allowed-to-read! #(get-in % [:params :schema-uuid]))) (POST (register! :instance-new "/instance/new") [] new!) diff --git a/src/wanijo/instance/view/edit.clj b/src/wanijo/instance/view/edit.clj index 4405d27..874811c 100644 --- a/src/wanijo/instance/view/edit.clj +++ b/src/wanijo/instance/view/edit.clj @@ -9,7 +9,7 @@ [formulare.core :as form])) (defn edit [instance form form-data schemas req] - (view/layout! + (view/layout :request req :content [[:h1 diff --git a/src/wanijo/instance/view/instances.clj b/src/wanijo/instance/view/instances.clj index c8e4fda..aa7390b 100644 --- a/src/wanijo/instance/view/instances.clj +++ b/src/wanijo/instance/view/instances.clj @@ -8,7 +8,7 @@ [wanijo.infrastructure.time :refer [prettify-dt]])) (defn instances [schema instances new-form req] - (view/layout! + (view/layout :request req :content [[:h1 "All Instances of schema " diff --git a/src/wanijo/instance/view/link_selection.clj b/src/wanijo/instance/view/link_selection.clj index ccdf25f..3b845f2 100644 --- a/src/wanijo/instance/view/link_selection.clj +++ b/src/wanijo/instance/view/link_selection.clj @@ -6,7 +6,7 @@ [formulare.core :as form])) (defn link-selection [instance schema form req] - (view/layout! + (view/layout :request req :content [[:h1 diff --git a/src/wanijo/instance/view/show.clj b/src/wanijo/instance/view/show.clj index 2f4a29a..585db9e 100644 --- a/src/wanijo/instance/view/show.clj +++ b/src/wanijo/instance/view/show.clj @@ -11,7 +11,7 @@ [wanijo.visualisation.viz :as viz])) (defn show [instance schemas req] - (view/layout! + (view/layout :request req :content [[:h1 diff --git a/src/wanijo/instance/view/starred.clj b/src/wanijo/instance/view/starred.clj index cedc18c..dbe8e42 100644 --- a/src/wanijo/instance/view/starred.clj +++ b/src/wanijo/instance/view/starred.clj @@ -5,7 +5,7 @@ [wanijo.infrastructure.time :refer [prettify-dt]])) (defn starred [instances req] - (view/layout! + (view/layout :request req :content [[:h1 "Starred instances"] diff --git a/src/wanijo/schema/middleware.clj b/src/wanijo/schema/middleware.clj index e571007..c1f4610 100644 --- a/src/wanijo/schema/middleware.clj +++ b/src/wanijo/schema/middleware.clj @@ -1,41 +1,39 @@ (ns wanijo.schema.middleware (:require [ring.util.response :as resp] [wanijo.infrastructure.routing :refer [path]] - [wanijo.schema.db :as domain])) + [wanijo.schema.db :as db])) -(defn wrap-user-schemas [handler] +(defn wrap-user-schemas! [handler] (fn [req] (if-let [uuid (get-in req [:session :uuid])] (handler (assoc-in req [:session :schemas] - (domain/accessible-schemas! uuid))) + (db/accessible-schemas! uuid))) (handler req)))) -(defn write-permission-middleware [schema-fn] +(defn write-permission-middleware! [schema-fn] (fn [handler] (fn [req] - (let [uuid (schema-fn req) - check-fn domain/has-user-write-permissions?] - (if (check-fn uuid (get-in req [:session :uuid])) + (let [uuid (schema-fn req)] + (if (db/has-user-write-permissions? uuid (-> req :session :uuid)) (handler req) (assoc (resp/redirect (path :schema-show {:uuid uuid})) :flash ["No write permission for schema"])))))) -(defn wrap-allowed-to-write [] - (write-permission-middleware #(get-in % [:params :uuid]))) +(defn wrap-allowed-to-write! [] + (write-permission-middleware! #(get-in % [:params :uuid]))) -(defn wrap-allowed-to-read +(defn wrap-allowed-to-read! ([schema-fn] - (wrap-allowed-to-read schema-fn - (fn [_] - (assoc (resp/redirect (path :schema-overview)) - :flash ["No read permission for schema"])))) + (wrap-allowed-to-read! schema-fn + (fn [_] + (assoc (resp/redirect (path :schema-overview)) + :flash ["No read permission for schema"])))) ([schema-fn not-allowed-fn] (fn [handler] (fn [req] - (let [uuid (schema-fn req) - check-fn domain/has-user-read-permissions?] - (if (check-fn uuid (get-in req [:session :uuid])) + (let [uuid (schema-fn req)] + (if (db/has-user-read-permissions? uuid (-> req :session :uuid)) (handler req) (not-allowed-fn req))))))) diff --git a/src/wanijo/schema/routes.clj b/src/wanijo/schema/routes.clj index e1dcb65..3ccf066 100644 --- a/src/wanijo/schema/routes.clj +++ b/src/wanijo/schema/routes.clj @@ -86,7 +86,7 @@ (POST (register! :schema-new "/schema/new") [] new!) (comp/wrap-routes read-routes - (middleware/wrap-allowed-to-read + (middleware/wrap-allowed-to-read! #(get-in % [:route-params :uuid]))) (comp/wrap-routes write-routes - (middleware/wrap-allowed-to-write))) + (middleware/wrap-allowed-to-write!))) diff --git a/src/wanijo/schema/view.clj b/src/wanijo/schema/view.clj index 5e8b8d7..695e842 100644 --- a/src/wanijo/schema/view.clj +++ b/src/wanijo/schema/view.clj @@ -14,7 +14,7 @@ (let [session (:session req) uuid (:uuid session) schemas (domain/all-created-by! uuid)] - (view/layout! + (view/layout :request req :content [[:h1 "All schemas"] @@ -37,7 +37,7 @@ (hform/submit-button "Create"))]))) (defn show-schema! [schema attrs assign-form conn-form req] - (view/layout! + (view/layout :request req :content [[:h1 "Schema " diff --git a/src/wanijo/user/view.clj b/src/wanijo/user/view.clj index accf0ff..253596d 100644 --- a/src/wanijo/user/view.clj +++ b/src/wanijo/user/view.clj @@ -12,7 +12,7 @@ :spec ::domain/ident}}}) (defn profile! [req user] - (view/layout! + (view/layout :session (:session req) :content [[:h1 "Hi, " (get-in req [:session :ident])] diff --git a/src/wanijo/visualisation/routes.clj b/src/wanijo/visualisation/routes.clj index 122e72b..0c71232 100644 --- a/src/wanijo/visualisation/routes.clj +++ b/src/wanijo/visualisation/routes.clj @@ -12,7 +12,7 @@ [view :as vis-view]])) (defn all-instances [req] - (view/layout! + (view/layout :request req :content [[:h1 "All Instances"] diff --git a/src/wanijo/visualisation/view.clj b/src/wanijo/visualisation/view.clj index f891ce0..73414cd 100644 --- a/src/wanijo/visualisation/view.clj +++ b/src/wanijo/visualisation/view.clj @@ -6,7 +6,7 @@ [wanijo.infrastructure.view :as view])) (defn index [instance req] - (view/layout! + (view/layout :request req :head (list (include-css "/css/vis-network.min.css") (include-js "/js/vis-network.min.js"