refactor: pass AuthResult to txVarQuery

Saves one parameter to the function.
This commit is contained in:
steve-chavez
2025-09-10 09:04:12 -05:00
committed by Steve Chavez
parent 966a611d7f
commit 1d2a3e8501
2 changed files with 8 additions and 9 deletions
+2 -2
View File
@@ -106,7 +106,7 @@ data ResultSet
query :: AppConfig -> AuthResult -> ApiRequest -> ActionPlan -> SchemaCache -> Query
query _ _ _ (NoDb x) _ = NoDbQuery $ NoDbResult x
query conf@AppConfig{..} AuthResult{..} apiReq (Db plan) sCache =
query conf@AppConfig{..} auth@AuthResult{..} apiReq (Db plan) sCache =
DbQuery isoLvl txMode dbHandler transaction mainSQLQuery
where
transaction = if configDbPreparedStatements then SQL.transaction else SQL.unpreparedTransaction
@@ -115,7 +115,7 @@ query conf@AppConfig{..} AuthResult{..} apiReq (Db plan) sCache =
(mainActionQuery, mainSQLQuery) = actionQuery plan conf apiReq sCache
dbHandler = do
lift $ SQL.statement mempty $ SQL.dynamicallyParameterized
(PreQuery.txVarQuery plan conf authClaims authRole apiReq)
(PreQuery.txVarQuery plan conf auth apiReq)
HD.noResult configDbPreparedStatements
lift $ whenJust configDbPreRequest $ \prereq -> do
SQL.statement mempty $ SQL.dynamicallyParameterized (PreQuery.preReqQuery prereq) HD.noResult configDbPreparedStatements
+6 -7
View File
@@ -10,14 +10,13 @@ module PostgREST.Query.PreQuery
) where
import qualified Data.Aeson as JSON
import qualified Data.Aeson.KeyMap as KM
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy.Char8 as LBS
import qualified Data.HashMap.Strict as HM
import qualified Hasql.DynamicStatements.Snippet as SQL hiding (sql)
import PostgREST.Auth.Types (AuthResult (..))
import PostgREST.ApiRequest (ApiRequest (..))
import PostgREST.ApiRequest.Preferences (PreferTimezone (..),
Preferences (..))
@@ -35,8 +34,8 @@ import PostgREST.SchemaCache.Routine (Routine (..))
import Protolude hiding (Handler)
-- sets transaction variables
txVarQuery :: DbActionPlan -> AppConfig -> KM.KeyMap JSON.Value -> BS.ByteString -> ApiRequest -> SQL.Snippet
txVarQuery dbActPlan AppConfig{..} claims role ApiRequest{..} =
txVarQuery :: DbActionPlan -> AppConfig -> AuthResult -> ApiRequest -> SQL.Snippet
txVarQuery dbActPlan AppConfig{..} AuthResult{..} ApiRequest{..} =
-- To ensure `GRANT SET ON PARAMETER <superuser_setting> TO authenticator` works, the role settings must be set before the impersonated role.
-- Otherwise the GRANT SET would have to be applied to the impersonated role. See https://github.com/PostgREST/postgrest/issues/3045
"select " <> intercalateSnippet ", " (
@@ -47,9 +46,9 @@ txVarQuery dbActPlan AppConfig{..} claims role ApiRequest{..} =
pathSql = setConfigWithConstantName ("request.path", iPath)
headersSql = setConfigWithConstantNameJSON "request.headers" iHeaders
cookiesSql = setConfigWithConstantNameJSON "request.cookies" iCookies
claimsSql = [setConfigWithConstantName ("request.jwt.claims", LBS.toStrict $ JSON.encode claims)]
roleSql = [setConfigWithConstantName ("role", role)]
roleSettingsSql = setConfigWithDynamicName <$> HM.toList (fromMaybe mempty $ HM.lookup role configRoleSettings)
claimsSql = [setConfigWithConstantName ("request.jwt.claims", LBS.toStrict $ JSON.encode authClaims)]
roleSql = [setConfigWithConstantName ("role", authRole)]
roleSettingsSql = setConfigWithDynamicName <$> HM.toList (fromMaybe mempty $ HM.lookup authRole configRoleSettings)
appSettingsSql = setConfigWithDynamicName . join bimap toUtf8 <$> configAppSettings
timezoneSql = maybe mempty (\(PreferTimezone tz) -> [setConfigWithConstantName ("timezone", tz)]) $ preferTimezone iPreferences
funcSettingsSql = setConfigWithDynamicName . join bimap toUtf8 <$> funcSettings