LCOV - code coverage report
Current view: top level - matchscore/domain/permission - command.clj Coverage Total Hit
Test: lcov.info Lines: 100.0 % 59 59
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.domain.permission.command
       2              :   "Write-side of the permission aggregate.
       3              : 
       4              :   Two concerns are routed differently:
       5              :   - Role catalog (`roles` table) goes through `RolesPostgresRepository`.
       6              :   - Assignments + policies (`casbin_rule`) go through `access.core`,
       7              :     which delegates to jcasbin's JDBCAdapter and publishes a reload."
       8              :   (:require
       9              :    [cats.monad.either :as either]
      10              :    [matchscore.access.core :as access]
      11              :    [matchscore.domain.permission.repository.postgres :as roles-repo]
      12              :    [matchscore.utils.errors :as utils.errors]))
      13              : 
      14              : 
      15              : ;; ---------------------------------------------------------------------------
      16              : ;; Roles catalog
      17              : ;; ---------------------------------------------------------------------------
      18              : 
      19              : 
      20            4 : (defn create-role!
      21              :  "Insert a role into the lookup table. Returns Either<Error, Role>."
      22              :  [{:keys [repos]} role]
      23            8 :  (let [repo (:roles-postgres-repo repos)]
      24            4 :    (if repo
      25            8 :      (roles-repo/save! repo role)
      26            2 :      (either/left
      27            3 :       (utils.errors/build-error
      28              :        :missing-dependency
      29            5 :        {:code    :error.permission/no-roles-repo
      30              :         :message ":roles-postgres-repo not bound in ctx :repos"})))))
      31              : 
      32              : 
      33            4 : (defn update-role!
      34              :  "Upsert a role (same call shape as create)."
      35              :  [ctx role]
      36            8 :  (create-role! ctx role))
      37              : 
      38              : 
      39            4 : (defn delete-role!
      40              :  [{:keys [repos]} role-name]
      41            8 :  (let [repo (:roles-postgres-repo repos)]
      42            4 :    (if repo
      43            8 :      (roles-repo/delete! repo role-name)
      44            2 :      (either/left
      45            3 :       (utils.errors/build-error
      46              :        :missing-dependency
      47            5 :        {:code    :error.permission/no-roles-repo
      48              :         :message ":roles-postgres-repo not bound in ctx :repos"})))))
      49              : 
      50              : 
      51              : ;; ---------------------------------------------------------------------------
      52              : ;; Assignments
      53              : ;; ---------------------------------------------------------------------------
      54              : 
      55              : 
      56            4 : (defn assign-role!
      57              :  "Grant `role` to `user-uuid` within `domain`. Returns Either<Error, bool>
      58              :   where bool = whether the row was new."
      59              :  [ctx user-uuid role domain]
      60            1 :  (try
      61            8 :    (either/right (access/add-role-for-user-in-domain! ctx user-uuid role domain))
      62              :    (catch Exception e
      63            2 :      (either/left
      64            3 :       (utils.errors/build-error
      65              :        :casbin
      66            5 :        {:code    :error.permission/assign-failed
      67            1 :         :message (.getMessage e)
      68            5 :         :context {:user-uuid user-uuid
      69            1 :                   :role      role
      70            1 :                   :domain    domain}})))))
      71              : 
      72              : 
      73            4 : (defn revoke-role!
      74              :  [ctx user-uuid role domain]
      75            1 :  (try
      76            8 :    (either/right (access/remove-role-for-user-in-domain! ctx user-uuid role domain))
      77              :    (catch Exception e
      78            2 :      (either/left
      79            3 :       (utils.errors/build-error
      80              :        :casbin
      81            5 :        {:code    :error.permission/revoke-failed
      82            1 :         :message (.getMessage e)
      83            5 :         :context {:user-uuid user-uuid
      84            1 :                   :role      role
      85            1 :                   :domain    domain}})))))
      86              : 
      87              : 
      88              : ;; ---------------------------------------------------------------------------
      89              : ;; Policies
      90              : ;; ---------------------------------------------------------------------------
      91              : 
      92              : 
      93            4 : (defn add-permission!
      94              :  "Add a Casbin `p` row. `cond` defaults to \"true\", `effect` to \"allow\"."
      95              :  [ctx
      96              :   {:keys [role domain obj-type action cond effect]
      97              :    :or   {cond   "true"
      98              :           effect "allow"}}]
      99            2 :  (try
     100           22 :    (either/right (access/add-policy! ctx role domain obj-type action cond effect))
     101              :    (catch Exception e
     102            2 :      (either/left
     103            3 :       (utils.errors/build-error
     104              :        :casbin
     105            5 :        {:code    :error.permission/add-policy-failed
     106            1 :         :message (.getMessage e)
     107            6 :         :context {:role     role
     108            1 :                   :domain   domain
     109            1 :                   :obj-type obj-type
     110            1 :                   :action   action}})))))
     111              : 
     112              : 
     113            4 : (defn remove-permission!
     114              :  [ctx
     115              :   {:keys [role domain obj-type action cond effect]
     116              :    :or   {cond   "true"
     117              :           effect "allow"}}]
     118            2 :  (try
     119           22 :    (either/right (access/remove-policy! ctx role domain obj-type action cond effect))
     120              :    (catch Exception e
     121            2 :      (either/left
     122            3 :       (utils.errors/build-error
     123              :        :casbin
     124            5 :        {:code    :error.permission/remove-policy-failed
     125            1 :         :message (.getMessage e)
     126            6 :         :context {:role     role
     127            1 :                   :domain   domain
     128            1 :                   :obj-type obj-type
     129            1 :                   :action   action}})))))
        

Generated by: LCOV version 2.0-1