(ns wanijo.schema.middleware (:require [ring.util.response :as resp] [wanijo.infra.routing :refer [path]] [wanijo.schema.db :as db])) (defn wrap-user-schemas! [handler] (fn [req] (if-let [uuid (get-in req [:session :uuid])] (handler (assoc-in req [:session :schemas] (db/accessible-schemas! uuid))) (handler req)))) (defn write-permission-middleware! [schema-fn] (fn [handler] (fn [req] (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-read! ([schema-fn] (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)] (if (db/has-user-read-permissions? uuid (-> req :session :uuid)) (handler req) (not-allowed-fn req)))))))