This commit is contained in:
2023-08-31 23:24:42 -07:00
parent 1d82ec29e0
commit 7d251c8398
40 changed files with 333 additions and 286 deletions

View File

@@ -2,7 +2,9 @@
(:require
[amazonica.core :refer [defcredential]]
[auto-ap.client-routes :as client-routes]
[auto-ap.ssr-routes :as ssr-routes]
[auto-ap.datomic :refer [conn pull-many]]
[auto-ap.datomic.clients :as d-clients]
[auto-ap.graphql.utils :refer [assert-can-see-client limited-clients]]
[auto-ap.logging :as alog]
[auto-ap.routes.auth :as auth]
[auto-ap.routes.exports :as exports]
@@ -12,15 +14,21 @@
[auto-ap.routes.invoices :as invoices]
[auto-ap.routes.queries :as queries]
[auto-ap.routes.yodlee2 :as yodlee2]
[auto-ap.ssr-routes :as ssr-routes]
[auto-ap.ssr.core :as ssr]
[bidi.bidi :as bidi]
[bidi.ring :refer [->ResourcesMaybe make-handler]]
[buddy.auth.backends.session :refer [session-backend]]
[buddy.auth.backends.token :refer [jws-backend]]
[buddy.auth.middleware :refer [wrap-authentication wrap-authorization]]
[cemerick.url :as url]
[clj-time.coerce :as coerce]
[clj-time.core :as time]
[clojure.string :as str]
[clojure.edn :as edn]
[com.brunobonacci.mulog :as mu]
[config.core :refer [env]]
[datomic.api :as dc]
[ring.middleware.edn :refer [wrap-edn-params]]
[ring.middleware.multipart-params :as mp]
[ring.middleware.params :refer [wrap-params]]
@@ -29,9 +37,7 @@
[ring.middleware.session.cookie :refer [cookie-store]]
[ring.util.response :as response]
[unilog.context :as lc]
[clj-time.coerce :as coerce]
[clj-time.core :as time]
[cemerick.url :as url]))
[clojure.set :as set]))
(when (:aws-access-key-id env)
(defcredential (:aws-access-key-id env) (:aws-secret-access-key env) (:aws-region env)))
@@ -169,13 +175,67 @@
request (assoc request :hx-query-params query-params)]
(handler request))))
(defn wrap-hydrate-clients
[handler]
(fn [request]
(let [x-clients (-> request :session :client-selection)
identity (-> request :session :identity)
ideal-ids (set (cond
(or (= :all x-clients)
(nil? x-clients))
(->> (dc/q '[:find ?c
:where [?c :client/code]]
(dc/db conn))
(map first))
(= :mine x-clients)
(map :db/id (:user/clients identity))
(seq x-clients)
x-clients))
limited-clients (some->> (limited-clients identity)
(map :db/id )
set)
client-ids (if (set? limited-clients)
(set/intersection ideal-ids
limited-clients)
ideal-ids)
clients (pull-many (dc/db conn)
d-clients/full-read
client-ids)]
(lc/with-context {:clients (map :client/code clients)}
(handler (assoc request
:clients clients
:client (when (= 1 (count clients))
(first clients))))))))
(defn wrap-store-client-in-session
[handler]
(fn [{:keys [headers identity] :as request}]
(let [x-clients (edn/read-string (get headers "x-clients"))
_ (when-let [client-id (and x-clients
(sequential? x-clients)
(first x-clients))]
(assert-can-see-client identity client-id))
new-request (if x-clients
(assoc-in request [:session :client-selection] x-clients)
request)]
(cond-> (handler new-request)
x-clients (update :session
(fn [new-session]
(-> (:session request)
(into new-session)
(assoc :client-selection x-clients))))))))
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(def app
(-> route-handler
(wrap-hx-current-url-params)
(wrap-guess-route)
(wrap-authorization auth-backend
)
(wrap-hydrate-clients)
(wrap-store-client-in-session)
(wrap-authorization auth-backend)
(wrap-authentication auth-backend
(session-backend {:authfn (fn [auth]
(dissoc auth :exp))}))