Skip to content

Instantly share code, notes, and snippets.

@MrEbbinghaus
Last active July 7, 2021 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrEbbinghaus/eb14937a0b0909530cbfa85c26c79139 to your computer and use it in GitHub Desktop.
Save MrEbbinghaus/eb14937a0b0909530cbfa85c26c79139 to your computer and use it in GitHub Desktop.
Access plugin for pathom
(defmulti check-access! (fn [_env [k v]] k))
(defmethod check-access! :decide.models.proposal/id
[env input]
; Add code to check access and add input to access-cache
false)
(defmethod check-access! :default [_ _] true)
(defn has-access? [env input]
(or
(contains? @(:access-cache env) input)
(check-access! env input)))
(def access-plugin
{::p/wrap-parser
(fn [parser]
(fn [env tx]
(parser (assoc env :access-cache (atom #{})) tx)))
::pc/wrap-resolve
(fn [resolve]
(fn [env input]
(let [allowed?
(if (seq? input) ; batched?
(every? (fn [input] (every? #(has-access? env %) input)) input)
(every? #(has-access? env %) input))]
(when allowed?
(resolve env input)))))})
;; To optimize access-checks resolvers can add access prematurely
(defresolver resolve-proposals [{:keys [db access-cache] :as env} {::process/keys [slug]}]
{::pc/output [{::process/proposals [::proposal/id]}]}
(if-let [response (d/pull db [{::process/proposals [::proposal/id]}] [::process/slug slug])]
(do
(swap! access-cache (fn [state] (apply conj state (map #(find % ::proposal/id) (::process/proposals response)))))
response)
{::process/proposals []}))
@MrEbbinghaus
Copy link
Author

I could generalize the plugin to make it reusable.

@MrEbbinghaus
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment