Line data Source code
1 4 : (ns matchscore.access.match-results
2 : (:require
3 : [cats.monad.either :as either]
4 : [matchscore.access.core :as access]
5 : [matchscore.access.resource :as access-resource]
6 : [matchscore.db.postgres.telegram-match-notifications :as notification-db]
7 : [matchscore.domain.event.command :as event-command]))
8 :
9 :
10 24 : (def ^:private writable-match-states #{"scheduled" "in_progress" "completed" "postponed"})
11 :
12 :
13 4 : (defn authorize
14 : "Authorizes result mutations for the currently assigned players and existing
15 : event managers. Links and client-supplied participant IDs grant no access.
16 :
17 : Active events accept writes from players and managers. Completed events stay
18 : correctable by managers only, so a wrong score can be fixed without reopening
19 : the event (which may conflict with another active event of the template)."
20 : [ctx user-id match-id]
21 11 : (if-let [match (notification-db/match-context (:pg ctx) match-id)]
22 4 : (let [event-id (:event-id match)
23 8 : event-state (:state (notification-db/event-context (:pg ctx) event-id))
24 5 : player? (and
25 1 : user-id
26 1 : (contains?
27 9 : #{(some->
28 3 : (:home-user-id match)
29 1 : str)
30 8 : (some->
31 3 : (:away-user-id match)
32 1 : str)}
33 3 : (str user-id)))
34 7 : auth-ctx (assoc ctx :auth-headers {:user/uuid user-id})
35 : ;; Load the caller's event membership: event admins manage results
36 : ;; through participant roles, not only through ownership.
37 4 : event-obj (access-resource/event-obj auth-ctx event-id)
38 2 : manager? (delay
39 1 : (boolean
40 7 : (and
41 1 : event-obj
42 6 : (access/allowed? (:enforcer ctx) user-id "*" event-obj :update))))]
43 8 : (cond (event-command/imported-event? event-obj)
44 4 : (either/left (event-command/imported-event-read-only-error))
45 :
46 23 : (not (or (= "active" event-state) (and (= "completed" event-state) user-id @manager?)))
47 2 : (either/left
48 5 : {:code :error.match-result/event-not-writable
49 : :message "The event is not active."})
50 :
51 8 : (not (contains? writable-match-states (:state match)))
52 0 : (either/left
53 0 : {:code :error.match-result/match-not-writable
54 : :message "The match cannot be edited in its current state."})
55 :
56 11 : (or player? @manager?) (either/right match)
57 :
58 : :else
59 2 : (either/left
60 5 : {:code :error.match-result/forbidden
61 : :message "You are not allowed to edit this match result."})))
62 0 : (either/left
63 0 : {:code :error.match-result/match-not-found
64 : :message "Match not found."})))
|