LCOV - code coverage report
Current view: top level - matchscore/access - resource.clj Coverage Total Hit
Test: lcov.info Lines: 90.1 % 141 127
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.access.resource
       2              :   "Resource authorization helpers for Casbin ABAC policies.
       3              : 
       4              :   Global roles come from users.roles. Resource roles come from
       5              :   template_participants.roles and event_participants.roles. Casbin domains are
       6              :   intentionally fixed to \"*\"."
       7              :   (:require
       8              :    [cats.core :as c]
       9              :    [matchscore.access.core :as access]
      10              :    [matchscore.domain.event-participant.repository.postgres :as event-participant-repo]
      11              :    [matchscore.domain.event.repository.postgres :as event-repo]
      12              :    [matchscore.domain.participant.repository.postgres :as participant-repo]
      13              :    [matchscore.domain.telegram-chat-member.repository.postgres :as telegram-chat-member-repo]
      14              :    [matchscore.domain.template-participant.repository.postgres :as template-participant-repo]
      15              :    [matchscore.domain.template.repository.postgres :as template-repo]
      16              :    [matchscore.domain.user.repository.postgres :as user-repo]))
      17              : 
      18              : 
      19           20 : (def ^:private valid-global-roles #{"root" "admin" "player"})
      20              : 
      21              : 
      22            4 : (defn normalize-roles
      23              :  [roles]
      24            8 :  (->>
      25            4 :    roles
      26           43 :    (map #(if (keyword? %) (name %) (str %)))
      27           12 :    (filter valid-global-roles)
      28            4 :    distinct
      29            4 :    vec))
      30              : 
      31              : 
      32           28 : (defn- current-user-uuid [ctx] (get-in ctx [:auth-headers :user/uuid]))
      33              : 
      34              : 
      35            4 : (defn current-user
      36              :  [ctx]
      37           36 :  (when-let [uuid (current-user-uuid ctx)]
      38           64 :    (when-let [repo (get-in ctx [:repos :user-postgres-repo])] (user-repo/find-by-id repo uuid))))
      39              : 
      40              : 
      41            4 : (defn- base-obj
      42              :   [ctx obj-type id owner-id participant]
      43           16 :   (let [user             (current-user ctx)
      44           20 :         global-roles     (normalize-roles (:roles user))
      45           38 :         participant-roles (vec (or (:roles participant) []))
      46           12 :         has-global?      (set global-roles)
      47           12 :         has-participant? (set participant-roles)]
      48           52 :     {:type          obj-type
      49            4 :      :id            id
      50            4 :      :owner-id      owner-id
      51            4 :      :global-roles  global-roles
      52            4 :      :participant-roles participant-roles
      53           16 :      :global-root   (contains? has-global? "root")
      54           16 :      :global-admin  (contains? has-global? "admin")
      55           16 :      :global-player (contains? has-global? "player")
      56           16 :      :participant-admin (contains? has-participant? "admin")
      57           16 :      :participant-player (contains? has-participant? "player")
      58           12 :      :participates  (some? participant)}))
      59              : 
      60              : 
      61            4 : (defn template-obj
      62              :  [ctx
      63              :   template-id
      64              :   &
      65              :   [{:keys [participant?]
      66              :     :or   {participant? true}}]]
      67           16 :  (let [repos       (:repos ctx)
      68            8 :        template    (template-repo/find-by-id
      69           12 :                     (:template-postgres-repo repos)
      70            4 :                     template-id
      71           12 :                     {:joins #{}})
      72           29 :        participant (when (and participant? template)
      73            8 :                      (template-participant-repo/find-by-template-id-and-user-id
      74           12 :                       (:template-participant-postgres-repo repos)
      75            4 :                       template-id
      76           12 :                       (current-user-uuid ctx)))]
      77           48 :    (when template (base-obj ctx "template" template-id (:created-by template) participant))))
      78              : 
      79              : 
      80            4 : (defn event-obj
      81              :  [ctx
      82              :   event-id
      83              :   &
      84              :   [{:keys [participant?]
      85              :     :or   {participant? true}}]]
      86           16 :  (let [repos       (:repos ctx)
      87           36 :        event       (event-repo/find-by-id (:event-postgres-repo repos) event-id {:joins #{}})
      88           29 :        participant (when (and participant? event)
      89            8 :                      (event-participant-repo/find-by-event-id-and-user-id
      90           12 :                       (:event-participant-postgres-repo repos)
      91            4 :                       event-id
      92           12 :                       (current-user-uuid ctx)))]
      93           12 :    (when event
      94           52 :      (assoc (base-obj ctx "event" event-id (:created-by event) participant)
      95           12 :       :source     (:source event)
      96           12 :       :created-by (:created-by event)))))
      97              : 
      98              : 
      99            4 : (defn template-event-management-obj
     100              :  "Builds an event authorization object that also recognizes the linked
     101              :    template's owner and current participant admins. This keeps management of
     102              :    older child events in sync when template admins are added after creation."
     103              :  [ctx event-id]
     104           16 :  (when-let [event (event-repo/find-by-id
     105           12 :                    (get-in ctx [:repos :event-postgres-repo])
     106            2 :                    event-id
     107            6 :                    {:joins #{}})]
     108           10 :    (let [obj (event-obj ctx event-id)
     109           26 :          template-obj (when-let [template-id (:template-id event)] (template-obj ctx template-id))
     110              :          template-manager?
     111           15 :          (or
     112            6 :           (:participant-admin template-obj)
     113            8 :           (and template-obj (= (current-user-uuid ctx) (:owner-id template-obj))))]
     114           15 :      (cond-> obj
     115            3 :        template-manager? (assoc :participant-admin
     116              :                           true :participates
     117              :                           true :participant-roles
     118           10 :                           (vec (distinct (conj (:participant-roles obj) "admin"))))))))
     119              : 
     120              : 
     121            4 : (defn create-obj
     122              :  [ctx obj-type]
     123           28 :  (base-obj ctx obj-type nil nil nil))
     124              : 
     125              : 
     126            4 : (defn- template-readable-obj
     127              :   [ctx template]
     128            3 :   (when template
     129            4 :     (let [template-id (:uuid template)
     130            2 :           participant (template-participant-repo/find-by-template-id-and-user-id
     131            6 :                        (get-in ctx [:repos :template-participant-postgres-repo])
     132            1 :                        template-id
     133            3 :                        (current-user-uuid ctx))]
     134            9 :       (base-obj ctx "template" template-id (:created-by template) participant))))
     135              : 
     136              : 
     137            4 : (defn- event-readable-obj
     138              :   [ctx event]
     139            0 :   (when event
     140            0 :     (let [event-id    (:uuid event)
     141            0 :           participant (event-participant-repo/find-by-event-id-and-user-id
     142            0 :                        (get-in ctx [:repos :event-participant-postgres-repo])
     143            0 :                        event-id
     144            0 :                        (current-user-uuid ctx))]
     145            0 :       (base-obj ctx "event" event-id (:created-by event) participant))))
     146              : 
     147              : 
     148           10 : (defn- tcm-repo [ctx] (get-in ctx [:repos :telegram-chat-member-postgres-repo]))
     149              : 
     150              : 
     151            4 : (defn- event-telegram-member?
     152              :   "True when the current user is a cached member of a Telegram chat linked to the event."
     153              :   [ctx event]
     154            0 :   (boolean
     155            0 :    (when-let [repo (tcm-repo ctx)]
     156            0 :      (when-let [user-id (current-user-uuid ctx)]
     157            0 :        (telegram-chat-member-repo/member-of-event? repo (:uuid event) user-id)))))
     158              : 
     159              : 
     160            4 : (defn- template-telegram-member?
     161              :   "True when the current user is a cached member of a Telegram chat linked to the template."
     162              :   [ctx template]
     163            1 :   (boolean
     164            9 :    (when-let [repo (tcm-repo ctx)]
     165            9 :      (when-let [user-id (current-user-uuid ctx)]
     166            7 :        (telegram-chat-member-repo/member-of-template? repo (:uuid template) user-id)))))
     167              : 
     168              : 
     169            4 : (defn template-readable?
     170              :  [ctx template]
     171            3 :  (boolean
     172           28 :   (and
     173            3 :    template
     174              :    (or
     175           18 :     (not= "private" (:visibility template))
     176           11 :     (when-let [obj (and (current-user-uuid ctx) (template-readable-obj ctx template))]
     177            5 :       (or
     178           13 :        (true? (access/allowed? (:enforcer ctx) (current-user-uuid ctx) "*" obj :read))
     179            3 :        (template-telegram-member? ctx template)))))))
     180              : 
     181              : 
     182            4 : (defn event-readable?
     183              :  "Public events are readable by everyone. Private events require the normal
     184              :   owner/participant/global-role policy or cached Telegram membership."
     185              :  [ctx event]
     186            4 :  (boolean
     187           35 :   (and
     188            4 :    event
     189              :    (or
     190           24 :     (not= "private" (:visibility event))
     191           14 :     (when-let [obj (and (current-user-uuid ctx) (event-readable-obj ctx event))]
     192            0 :       (or
     193            0 :        (true? (access/allowed? (:enforcer ctx) (current-user-uuid ctx) "*" obj :read))
     194            0 :        (event-telegram-member? ctx event)))))))
     195              : 
     196              : 
     197            4 : (defn template-manageable?
     198              :  "True when the current user may update the template: its owner, a participant
     199              :   admin, or a platform root."
     200              :  [ctx template-id]
     201            1 :  (boolean
     202            9 :   (when-let [user-id (current-user-uuid ctx)]
     203           10 :     (when-let [obj (template-obj ctx template-id)]
     204           11 :       (true? (access/allowed? (:enforcer ctx) user-id "*" obj :update))))))
     205              : 
     206              : 
     207            4 : (defn event-manageable?
     208              :  "True when the current user may update the event: its owner, a participant
     209              :   admin, an owner or participant admin of the linked template, or a platform root."
     210              :  [ctx event-id]
     211            1 :  (boolean
     212            9 :   (when-let [user-id (current-user-uuid ctx)]
     213           10 :     (when-let [obj (template-event-management-obj ctx event-id)]
     214           11 :       (true? (access/allowed? (:enforcer ctx) user-id "*" obj :update))))))
     215              : 
     216              : 
     217            4 : (defn ensure-current-participant!
     218              :  [ctx]
     219            4 :  (participant-repo/ensure-person-participant-for-user!
     220           12 :   (get-in ctx [:repos :participant-postgres-repo])
     221            6 :   (current-user-uuid ctx)))
     222              : 
     223              : 
     224            4 : (defn join-template!
     225              :  [ctx template-id]
     226           20 :  (c/mlet [participant (ensure-current-participant! ctx)]
     227            4 :    (template-participant-repo/save!
     228           12 :     (get-in ctx [:repos :template-participant-postgres-repo])
     229           20 :     {:template-id    template-id
     230            6 :      :participant-id (:uuid participant)
     231              :      :status         "registered"
     232            4 :      :roles          ["player"]
     233            2 :      :extra          {}
     234              :      :source         "matchscore"})))
     235              : 
     236              : 
     237            4 : (defn leave-template!
     238              :  [ctx template-id]
     239            4 :  (template-participant-repo/delete-by-template-id-and-user-id!
     240           12 :   (get-in ctx [:repos :template-participant-postgres-repo])
     241            2 :   template-id
     242            6 :   (current-user-uuid ctx)))
        

Generated by: LCOV version 2.0-1