Line data Source code
1 4 : (ns matchscore.access.resolvers
2 : "Registries used by access.middleware to resolve authorization objects.
3 :
4 : `fetch-fns` — keyword → (ctx id opts) -> obj-map or nil.
5 : Covers resource types that require a DB lookup before Casbin
6 : can evaluate a policy.
7 :
8 : `via-fns` — keyword → (ctx parent-id) -> actual-resource-id.
9 : Used when the route exposes a parent-resource UUID (e.g.
10 : :class-uuid) and we need to walk back to the resource type
11 : that has a Casbin policy (e.g. :event)."
12 : (:require
13 : [clojure.string :as str]
14 : [matchscore.access.resource :as resource]
15 : [matchscore.domain.class-template.repository.postgres :as class-template-repo]
16 : [matchscore.domain.class.repository.postgres :as class-repo]
17 : [matchscore.domain.draw.repository.postgres :as draw-repo]
18 : [matchscore.domain.stage.repository.postgres :as stage-repo]))
19 :
20 :
21 4 : (defn require-or-404!
22 : "Returns `obj` if non-nil; otherwise throws an `:api-not-found` ex-info
23 : that `wrap-exception-handling` maps to a 404 response."
24 : [obj kind id]
25 20 : (or
26 4 : obj
27 : (throw
28 8 : (ex-info
29 32 : (str (str/capitalize (name kind)) " not found")
30 20 : {:type :api-not-found
31 4 : :resource kind
32 4 : :id id}))))
33 :
34 :
35 4 : (def fetch-fns
36 : "Maps resource-type keyword → (ctx id opts) -> obj-map or nil.
37 : Only types that require a DB lookup before authorization live here;
38 : create-only types (:admin, :account, :session) are built inline
39 : via resource/create-obj and do not appear here."
40 16 : {:template (fn [ctx id opts]
41 15 : (resource/template-obj ctx id opts))
42 4 : :event (fn [ctx id opts]
43 20 : (resource/event-obj ctx id opts))})
44 :
45 :
46 4 : (def via-fns
47 : "Maps :via keyword → (ctx parent-id) -> resource-id for the :obj fetch.
48 : Each step throws :api-not-found if the chain is broken."
49 24 : {:class (fn [ctx class-id]
50 9 : (let [class (class-repo/find-by-id
51 18 : (get-in ctx [:repos :class-postgres-repo])
52 3 : class-id)]
53 21 : (:event-id (require-or-404! class :class class-id))))
54 :
55 :
56 4 : :stage (fn [ctx stage-id]
57 8 : (let [repos (:repos ctx)
58 12 : stage (stage-repo/find-by-id (:stage-postgres-repo repos) stage-id)
59 10 : _ (require-or-404! stage :stage stage-id)
60 4 : class (class-repo/find-by-id
61 6 : (:class-postgres-repo repos)
62 6 : (:class-id stage))]
63 18 : (:event-id (require-or-404! class :class (:class-id stage)))))
64 :
65 :
66 4 : :draw (fn [ctx draw-id]
67 8 : (let [repos (:repos ctx)
68 12 : draw (draw-repo/find-by-id (:draw-postgres-repo repos) draw-id)
69 10 : _ (require-or-404! draw :draw draw-id)
70 4 : stage (stage-repo/find-by-id
71 6 : (:stage-postgres-repo repos)
72 6 : (:stage-id draw))
73 14 : _ (require-or-404! stage :stage (:stage-id draw))
74 4 : class (class-repo/find-by-id
75 6 : (:class-postgres-repo repos)
76 6 : (:class-id stage))]
77 18 : (:event-id (require-or-404! class :class (:class-id stage)))))
78 :
79 :
80 4 : :class-template (fn [ctx ct-id]
81 3 : (let [ct (class-template-repo/find-by-id
82 6 : (get-in ctx [:repos :class-template-postgres-repo])
83 1 : ct-id)]
84 7 : (:template-id (require-or-404! ct :class-template ct-id))))})
|