Line data Source code
1 4 : (ns matchscore.api.templates-access
2 : "Authorization helpers (`:obj-resolver` functions) for template endpoints.
3 :
4 : The resolver runs before the handler is invoked, so it must rely on
5 : request data that is already populated by the outer middleware:
6 : `:body`, `:route-params`, `:auth-headers`, and `:ctx` (with `:repos`).
7 :
8 : PR2 ships owner ABAC only (`r.obj.owner_id == r.sub`). Instance-role
9 : ABAC via `event_template_participants.roles` will be wired in the
10 : broader rollout (PR5)."
11 : (:require
12 : [matchscore.domain.template.repository.postgres :as template-postgres-repo]))
13 :
14 :
15 4 : (defn- body-uuid
16 : "Extracts the target template UUID from the update body."
17 : [request]
18 16 : (or (get-in request [:body :uuid]) (get-in request [:body "uuid"])))
19 :
20 :
21 4 : (defn- body-club-ids
22 : "Extracts the requested target club IDs from the update body. The resolver runs
23 : before body decoding/normalization, so it accepts both the plural `club-ids`
24 : vector and the singular (deprecated) `club-id`, under keyword or string keys."
25 : [request]
26 4 : (let [body (:body request)]
27 6 : (or
28 11 : (some #(get body %) [:club-ids :club_ids "club_ids" "club-ids"])
29 11 : (when-let [single (some #(get body %) [:club-id :club_id "club_id" "club-id"])] [single]))))
30 :
31 :
32 4 : (defn resolve-update-obj
33 : "Resolves the obj-map for `template-update-handler`. Returns a map with
34 : `:owner-id` (template's `created_by`) and `:club-ids` so the policy can
35 : evaluate owner-based ABAC and the club-level domain check.
36 : `:club-id` is kept for backward compatibility (first club)."
37 : [request]
38 4 : (let [tpl-uuid (body-uuid request)
39 6 : repos (get-in request [:ctx :repos])
40 3 : repo (:template-postgres-repo repos)
41 13 : template (when (and repo tpl-uuid) (template-postgres-repo/find-by-id repo tpl-uuid {}))]
42 6 : {:id tpl-uuid
43 3 : :owner-id (:created-by template)
44 : ;; Authorize against both the template's current clubs and the requested
45 : ;; target clubs, so moving a template to a new club is checked too.
46 17 : :club-ids (vec (distinct (concat (mapv :uuid (seq (:clubs template))) (body-club-ids request))))
47 19 : :club-id (or
48 3 : (:club-id template)
49 : (some->
50 3 : (:clubs template)
51 1 : first
52 : :uuid))}))
|