module Hasura.RQL.Types.Session
  ( SessionVariable (..),
    SessionVariables (..),
    SessionVariableValue,
    parseSessionVariable,
    sessionVariablePrefix,
    sessionVariableToText,
    mkSessionVariable,
    mkSessionVariablesText,
    isSessionVariable,
    UserAdminSecret (..),
    BackendOnlyFieldAccess (..),
    UserInfo (..),
    UserInfoM (..),
    ExtraUserInfo (..),
    UserRoleBuild (..),
  )
where

import Data.Aeson
import Data.Aeson.Types (Parser, toJSONKeyText)
import Data.CaseInsensitive qualified as CI
import Data.HashMap.Strict qualified as HashMap
import Data.Text qualified as T
import Data.Text.Extended
import Hasura.Prelude
import Hasura.RQL.Types.Roles (RoleName)

newtype SessionVariable = SessionVariable {SessionVariable -> CI Text
unSessionVariable :: CI.CI Text}
  deriving (Int -> SessionVariable -> ShowS
[SessionVariable] -> ShowS
SessionVariable -> String
(Int -> SessionVariable -> ShowS)
-> (SessionVariable -> String)
-> ([SessionVariable] -> ShowS)
-> Show SessionVariable
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SessionVariable -> ShowS
showsPrec :: Int -> SessionVariable -> ShowS
$cshow :: SessionVariable -> String
show :: SessionVariable -> String
$cshowList :: [SessionVariable] -> ShowS
showList :: [SessionVariable] -> ShowS
Show, SessionVariable -> SessionVariable -> Bool
(SessionVariable -> SessionVariable -> Bool)
-> (SessionVariable -> SessionVariable -> Bool)
-> Eq SessionVariable
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SessionVariable -> SessionVariable -> Bool
== :: SessionVariable -> SessionVariable -> Bool
$c/= :: SessionVariable -> SessionVariable -> Bool
/= :: SessionVariable -> SessionVariable -> Bool
Eq, Eq SessionVariable
Eq SessionVariable
-> (Int -> SessionVariable -> Int)
-> (SessionVariable -> Int)
-> Hashable SessionVariable
Int -> SessionVariable -> Int
SessionVariable -> Int
forall a. Eq a -> (Int -> a -> Int) -> (a -> Int) -> Hashable a
$chashWithSalt :: Int -> SessionVariable -> Int
hashWithSalt :: Int -> SessionVariable -> Int
$chash :: SessionVariable -> Int
hash :: SessionVariable -> Int
Hashable, String -> SessionVariable
(String -> SessionVariable) -> IsString SessionVariable
forall a. (String -> a) -> IsString a
$cfromString :: String -> SessionVariable
fromString :: String -> SessionVariable
IsString, Typeable SessionVariable
Typeable SessionVariable
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> SessionVariable -> c SessionVariable)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c SessionVariable)
-> (SessionVariable -> Constr)
-> (SessionVariable -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c SessionVariable))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c SessionVariable))
-> ((forall b. Data b => b -> b)
    -> SessionVariable -> SessionVariable)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> SessionVariable -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> SessionVariable -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> SessionVariable -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> SessionVariable -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> SessionVariable -> m SessionVariable)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> SessionVariable -> m SessionVariable)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> SessionVariable -> m SessionVariable)
-> Data SessionVariable
SessionVariable -> Constr
SessionVariable -> DataType
(forall b. Data b => b -> b) -> SessionVariable -> SessionVariable
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int -> (forall d. Data d => d -> u) -> SessionVariable -> u
forall u. (forall d. Data d => d -> u) -> SessionVariable -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> SessionVariable -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SessionVariable -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> SessionVariable -> m SessionVariable
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> SessionVariable -> m SessionVariable
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c SessionVariable
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SessionVariable -> c SessionVariable
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c SessionVariable)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c SessionVariable)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SessionVariable -> c SessionVariable
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SessionVariable -> c SessionVariable
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c SessionVariable
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c SessionVariable
$ctoConstr :: SessionVariable -> Constr
toConstr :: SessionVariable -> Constr
$cdataTypeOf :: SessionVariable -> DataType
dataTypeOf :: SessionVariable -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c SessionVariable)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c SessionVariable)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c SessionVariable)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c SessionVariable)
$cgmapT :: (forall b. Data b => b -> b) -> SessionVariable -> SessionVariable
gmapT :: (forall b. Data b => b -> b) -> SessionVariable -> SessionVariable
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> SessionVariable -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> SessionVariable -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SessionVariable -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SessionVariable -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> SessionVariable -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> SessionVariable -> [u]
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> SessionVariable -> u
gmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> SessionVariable -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> SessionVariable -> m SessionVariable
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> SessionVariable -> m SessionVariable
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> SessionVariable -> m SessionVariable
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> SessionVariable -> m SessionVariable
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> SessionVariable -> m SessionVariable
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> SessionVariable -> m SessionVariable
Data, SessionVariable -> ()
(SessionVariable -> ()) -> NFData SessionVariable
forall a. (a -> ()) -> NFData a
$crnf :: SessionVariable -> ()
rnf :: SessionVariable -> ()
NFData, Eq SessionVariable
Eq SessionVariable
-> (SessionVariable -> SessionVariable -> Ordering)
-> (SessionVariable -> SessionVariable -> Bool)
-> (SessionVariable -> SessionVariable -> Bool)
-> (SessionVariable -> SessionVariable -> Bool)
-> (SessionVariable -> SessionVariable -> Bool)
-> (SessionVariable -> SessionVariable -> SessionVariable)
-> (SessionVariable -> SessionVariable -> SessionVariable)
-> Ord SessionVariable
SessionVariable -> SessionVariable -> Bool
SessionVariable -> SessionVariable -> Ordering
SessionVariable -> SessionVariable -> SessionVariable
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: SessionVariable -> SessionVariable -> Ordering
compare :: SessionVariable -> SessionVariable -> Ordering
$c< :: SessionVariable -> SessionVariable -> Bool
< :: SessionVariable -> SessionVariable -> Bool
$c<= :: SessionVariable -> SessionVariable -> Bool
<= :: SessionVariable -> SessionVariable -> Bool
$c> :: SessionVariable -> SessionVariable -> Bool
> :: SessionVariable -> SessionVariable -> Bool
$c>= :: SessionVariable -> SessionVariable -> Bool
>= :: SessionVariable -> SessionVariable -> Bool
$cmax :: SessionVariable -> SessionVariable -> SessionVariable
max :: SessionVariable -> SessionVariable -> SessionVariable
$cmin :: SessionVariable -> SessionVariable -> SessionVariable
min :: SessionVariable -> SessionVariable -> SessionVariable
Ord)

instance ToJSON SessionVariable where
  toJSON :: SessionVariable -> Value
toJSON = Text -> Value
forall a. ToJSON a => a -> Value
toJSON (Text -> Value)
-> (SessionVariable -> Text) -> SessionVariable -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CI Text -> Text
forall s. CI s -> s
CI.original (CI Text -> Text)
-> (SessionVariable -> CI Text) -> SessionVariable -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SessionVariable -> CI Text
unSessionVariable

instance ToJSONKey SessionVariable where
  toJSONKey :: ToJSONKeyFunction SessionVariable
toJSONKey = (SessionVariable -> Text) -> ToJSONKeyFunction SessionVariable
forall a. (a -> Text) -> ToJSONKeyFunction a
toJSONKeyText SessionVariable -> Text
sessionVariableToText

instance ToTxt SessionVariable where
  toTxt :: SessionVariable -> Text
toTxt = SessionVariable -> Text
sessionVariableToText

type SessionVariableValue = Text

sessionVariablePrefix :: Text
sessionVariablePrefix :: Text
sessionVariablePrefix = Text
"x-hasura-"

isSessionVariable :: Text -> Bool
isSessionVariable :: Text -> Bool
isSessionVariable = Text -> Text -> Bool
T.isPrefixOf Text
sessionVariablePrefix (Text -> Bool) -> (Text -> Text) -> Text -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
T.toLower

parseSessionVariable :: Text -> Parser SessionVariable
parseSessionVariable :: Text -> Parser SessionVariable
parseSessionVariable Text
t =
  if Text -> Bool
isSessionVariable Text
t
    then SessionVariable -> Parser SessionVariable
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SessionVariable -> Parser SessionVariable)
-> SessionVariable -> Parser SessionVariable
forall a b. (a -> b) -> a -> b
$ Text -> SessionVariable
mkSessionVariable Text
t
    else String -> Parser SessionVariable
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser SessionVariable)
-> String -> Parser SessionVariable
forall a b. (a -> b) -> a -> b
$ Text -> String
forall a. Show a => a -> String
show Text
t String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" is not a Hasura session variable"

instance FromJSON SessionVariable where
  parseJSON :: Value -> Parser SessionVariable
parseJSON = String
-> (Text -> Parser SessionVariable)
-> Value
-> Parser SessionVariable
forall a. String -> (Text -> Parser a) -> Value -> Parser a
withText String
"String" Text -> Parser SessionVariable
parseSessionVariable

instance FromJSONKey SessionVariable where
  fromJSONKey :: FromJSONKeyFunction SessionVariable
fromJSONKey = (Text -> Parser SessionVariable)
-> FromJSONKeyFunction SessionVariable
forall a. (Text -> Parser a) -> FromJSONKeyFunction a
FromJSONKeyTextParser Text -> Parser SessionVariable
parseSessionVariable

sessionVariableToText :: SessionVariable -> Text
sessionVariableToText :: SessionVariable -> Text
sessionVariableToText = Text -> Text
T.toLower (Text -> Text)
-> (SessionVariable -> Text) -> SessionVariable -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CI Text -> Text
forall s. CI s -> s
CI.original (CI Text -> Text)
-> (SessionVariable -> CI Text) -> SessionVariable -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SessionVariable -> CI Text
unSessionVariable

mkSessionVariable :: Text -> SessionVariable
mkSessionVariable :: Text -> SessionVariable
mkSessionVariable = CI Text -> SessionVariable
SessionVariable (CI Text -> SessionVariable)
-> (Text -> CI Text) -> Text -> SessionVariable
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> CI Text
forall s. FoldCase s => s -> CI s
CI.mk

newtype SessionVariables = SessionVariables {SessionVariables -> HashMap SessionVariable Text
unSessionVariables :: HashMap.HashMap SessionVariable SessionVariableValue}
  deriving (Int -> SessionVariables -> ShowS
[SessionVariables] -> ShowS
SessionVariables -> String
(Int -> SessionVariables -> ShowS)
-> (SessionVariables -> String)
-> ([SessionVariables] -> ShowS)
-> Show SessionVariables
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SessionVariables -> ShowS
showsPrec :: Int -> SessionVariables -> ShowS
$cshow :: SessionVariables -> String
show :: SessionVariables -> String
$cshowList :: [SessionVariables] -> ShowS
showList :: [SessionVariables] -> ShowS
Show, SessionVariables -> SessionVariables -> Bool
(SessionVariables -> SessionVariables -> Bool)
-> (SessionVariables -> SessionVariables -> Bool)
-> Eq SessionVariables
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SessionVariables -> SessionVariables -> Bool
== :: SessionVariables -> SessionVariables -> Bool
$c/= :: SessionVariables -> SessionVariables -> Bool
/= :: SessionVariables -> SessionVariables -> Bool
Eq, Eq SessionVariables
Eq SessionVariables
-> (Int -> SessionVariables -> Int)
-> (SessionVariables -> Int)
-> Hashable SessionVariables
Int -> SessionVariables -> Int
SessionVariables -> Int
forall a. Eq a -> (Int -> a -> Int) -> (a -> Int) -> Hashable a
$chashWithSalt :: Int -> SessionVariables -> Int
hashWithSalt :: Int -> SessionVariables -> Int
$chash :: SessionVariables -> Int
hash :: SessionVariables -> Int
Hashable, NonEmpty SessionVariables -> SessionVariables
SessionVariables -> SessionVariables -> SessionVariables
(SessionVariables -> SessionVariables -> SessionVariables)
-> (NonEmpty SessionVariables -> SessionVariables)
-> (forall b.
    Integral b =>
    b -> SessionVariables -> SessionVariables)
-> Semigroup SessionVariables
forall b. Integral b => b -> SessionVariables -> SessionVariables
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
$c<> :: SessionVariables -> SessionVariables -> SessionVariables
<> :: SessionVariables -> SessionVariables -> SessionVariables
$csconcat :: NonEmpty SessionVariables -> SessionVariables
sconcat :: NonEmpty SessionVariables -> SessionVariables
$cstimes :: forall b. Integral b => b -> SessionVariables -> SessionVariables
stimes :: forall b. Integral b => b -> SessionVariables -> SessionVariables
Semigroup, Semigroup SessionVariables
SessionVariables
Semigroup SessionVariables
-> SessionVariables
-> (SessionVariables -> SessionVariables -> SessionVariables)
-> ([SessionVariables] -> SessionVariables)
-> Monoid SessionVariables
[SessionVariables] -> SessionVariables
SessionVariables -> SessionVariables -> SessionVariables
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
$cmempty :: SessionVariables
mempty :: SessionVariables
$cmappend :: SessionVariables -> SessionVariables -> SessionVariables
mappend :: SessionVariables -> SessionVariables -> SessionVariables
$cmconcat :: [SessionVariables] -> SessionVariables
mconcat :: [SessionVariables] -> SessionVariables
Monoid)

instance ToJSON SessionVariables where
  toJSON :: SessionVariables -> Value
toJSON (SessionVariables HashMap SessionVariable Text
varMap) =
    HashMap Text Text -> Value
forall a. ToJSON a => a -> Value
toJSON (HashMap Text Text -> Value) -> HashMap Text Text -> Value
forall a b. (a -> b) -> a -> b
$ (SessionVariable -> Text)
-> HashMap SessionVariable Text -> HashMap Text Text
forall k2 k1 v.
(Eq k2, Hashable k2) =>
(k1 -> k2) -> HashMap k1 v -> HashMap k2 v
mapKeys SessionVariable -> Text
sessionVariableToText HashMap SessionVariable Text
varMap

instance FromJSON SessionVariables where
  parseJSON :: Value -> Parser SessionVariables
parseJSON Value
v = HashMap Text Text -> SessionVariables
mkSessionVariablesText (HashMap Text Text -> SessionVariables)
-> Parser (HashMap Text Text) -> Parser SessionVariables
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser (HashMap Text Text)
forall a. FromJSON a => Value -> Parser a
parseJSON Value
v

mkSessionVariablesText :: HashMap.HashMap Text Text -> SessionVariables
mkSessionVariablesText :: HashMap Text Text -> SessionVariables
mkSessionVariablesText = HashMap SessionVariable Text -> SessionVariables
SessionVariables (HashMap SessionVariable Text -> SessionVariables)
-> (HashMap Text Text -> HashMap SessionVariable Text)
-> HashMap Text Text
-> SessionVariables
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> SessionVariable)
-> HashMap Text Text -> HashMap SessionVariable Text
forall k2 k1 v.
(Eq k2, Hashable k2) =>
(k1 -> k2) -> HashMap k1 v -> HashMap k2 v
mapKeys Text -> SessionVariable
mkSessionVariable

-- | Represent the admin secret state; whether the secret is sent
-- in the request or if actually authorization is not configured.
data UserAdminSecret
  = UAdminSecretSent
  | UAdminSecretNotSent
  | UAuthNotSet
  deriving (Int -> UserAdminSecret -> ShowS
[UserAdminSecret] -> ShowS
UserAdminSecret -> String
(Int -> UserAdminSecret -> ShowS)
-> (UserAdminSecret -> String)
-> ([UserAdminSecret] -> ShowS)
-> Show UserAdminSecret
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UserAdminSecret -> ShowS
showsPrec :: Int -> UserAdminSecret -> ShowS
$cshow :: UserAdminSecret -> String
show :: UserAdminSecret -> String
$cshowList :: [UserAdminSecret] -> ShowS
showList :: [UserAdminSecret] -> ShowS
Show, UserAdminSecret -> UserAdminSecret -> Bool
(UserAdminSecret -> UserAdminSecret -> Bool)
-> (UserAdminSecret -> UserAdminSecret -> Bool)
-> Eq UserAdminSecret
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UserAdminSecret -> UserAdminSecret -> Bool
== :: UserAdminSecret -> UserAdminSecret -> Bool
$c/= :: UserAdminSecret -> UserAdminSecret -> Bool
/= :: UserAdminSecret -> UserAdminSecret -> Bool
Eq)

-- | Represents the 'X-Hasura-Use-Backend-Only-Permissions' session variable
-- and request made with 'X-Hasura-Admin-Secret' if any auth configured.
-- For more details see Note [Backend only permissions]
data BackendOnlyFieldAccess
  = BOFAAllowed
  | BOFADisallowed
  deriving (Int -> BackendOnlyFieldAccess -> ShowS
[BackendOnlyFieldAccess] -> ShowS
BackendOnlyFieldAccess -> String
(Int -> BackendOnlyFieldAccess -> ShowS)
-> (BackendOnlyFieldAccess -> String)
-> ([BackendOnlyFieldAccess] -> ShowS)
-> Show BackendOnlyFieldAccess
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BackendOnlyFieldAccess -> ShowS
showsPrec :: Int -> BackendOnlyFieldAccess -> ShowS
$cshow :: BackendOnlyFieldAccess -> String
show :: BackendOnlyFieldAccess -> String
$cshowList :: [BackendOnlyFieldAccess] -> ShowS
showList :: [BackendOnlyFieldAccess] -> ShowS
Show, BackendOnlyFieldAccess -> BackendOnlyFieldAccess -> Bool
(BackendOnlyFieldAccess -> BackendOnlyFieldAccess -> Bool)
-> (BackendOnlyFieldAccess -> BackendOnlyFieldAccess -> Bool)
-> Eq BackendOnlyFieldAccess
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BackendOnlyFieldAccess -> BackendOnlyFieldAccess -> Bool
== :: BackendOnlyFieldAccess -> BackendOnlyFieldAccess -> Bool
$c/= :: BackendOnlyFieldAccess -> BackendOnlyFieldAccess -> Bool
/= :: BackendOnlyFieldAccess -> BackendOnlyFieldAccess -> Bool
Eq, (forall x. BackendOnlyFieldAccess -> Rep BackendOnlyFieldAccess x)
-> (forall x.
    Rep BackendOnlyFieldAccess x -> BackendOnlyFieldAccess)
-> Generic BackendOnlyFieldAccess
forall x. Rep BackendOnlyFieldAccess x -> BackendOnlyFieldAccess
forall x. BackendOnlyFieldAccess -> Rep BackendOnlyFieldAccess x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. BackendOnlyFieldAccess -> Rep BackendOnlyFieldAccess x
from :: forall x. BackendOnlyFieldAccess -> Rep BackendOnlyFieldAccess x
$cto :: forall x. Rep BackendOnlyFieldAccess x -> BackendOnlyFieldAccess
to :: forall x. Rep BackendOnlyFieldAccess x -> BackendOnlyFieldAccess
Generic)

instance FromJSON BackendOnlyFieldAccess where
  parseJSON :: Value -> Parser BackendOnlyFieldAccess
parseJSON = Options -> Value -> Parser BackendOnlyFieldAccess
forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON Options
hasuraJSON

instance ToJSON BackendOnlyFieldAccess where
  toJSON :: BackendOnlyFieldAccess -> Value
toJSON = Options -> BackendOnlyFieldAccess -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON Options
hasuraJSON
  toEncoding :: BackendOnlyFieldAccess -> Encoding
toEncoding = Options -> BackendOnlyFieldAccess -> Encoding
forall a.
(Generic a, GToJSON' Encoding Zero (Rep a)) =>
Options -> a -> Encoding
genericToEncoding Options
hasuraJSON

instance Hashable BackendOnlyFieldAccess

data UserInfo = UserInfo
  { UserInfo -> RoleName
_uiRole :: !RoleName,
    UserInfo -> SessionVariables
_uiSession :: !SessionVariables,
    UserInfo -> BackendOnlyFieldAccess
_uiBackendOnlyFieldAccess :: !BackendOnlyFieldAccess
  }
  deriving (Int -> UserInfo -> ShowS
[UserInfo] -> ShowS
UserInfo -> String
(Int -> UserInfo -> ShowS)
-> (UserInfo -> String) -> ([UserInfo] -> ShowS) -> Show UserInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UserInfo -> ShowS
showsPrec :: Int -> UserInfo -> ShowS
$cshow :: UserInfo -> String
show :: UserInfo -> String
$cshowList :: [UserInfo] -> ShowS
showList :: [UserInfo] -> ShowS
Show, UserInfo -> UserInfo -> Bool
(UserInfo -> UserInfo -> Bool)
-> (UserInfo -> UserInfo -> Bool) -> Eq UserInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UserInfo -> UserInfo -> Bool
== :: UserInfo -> UserInfo -> Bool
$c/= :: UserInfo -> UserInfo -> Bool
/= :: UserInfo -> UserInfo -> Bool
Eq, (forall x. UserInfo -> Rep UserInfo x)
-> (forall x. Rep UserInfo x -> UserInfo) -> Generic UserInfo
forall x. Rep UserInfo x -> UserInfo
forall x. UserInfo -> Rep UserInfo x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. UserInfo -> Rep UserInfo x
from :: forall x. UserInfo -> Rep UserInfo x
$cto :: forall x. Rep UserInfo x -> UserInfo
to :: forall x. Rep UserInfo x -> UserInfo
Generic)

instance Hashable UserInfo

instance FromJSON UserInfo where
  parseJSON :: Value -> Parser UserInfo
parseJSON = Options -> Value -> Parser UserInfo
forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON Options
hasuraJSON

instance ToJSON UserInfo where
  toJSON :: UserInfo -> Value
toJSON = Options -> UserInfo -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON Options
hasuraJSON
  toEncoding :: UserInfo -> Encoding
toEncoding = Options -> UserInfo -> Encoding
forall a.
(Generic a, GToJSON' Encoding Zero (Rep a)) =>
Options -> a -> Encoding
genericToEncoding Options
hasuraJSON

class (Monad m) => UserInfoM m where
  askUserInfo :: m UserInfo

instance (UserInfoM m) => UserInfoM (ReaderT r m) where
  askUserInfo :: ReaderT r m UserInfo
askUserInfo = m UserInfo -> ReaderT r m UserInfo
forall (m :: * -> *) a. Monad m => m a -> ReaderT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m UserInfo
forall (m :: * -> *). UserInfoM m => m UserInfo
askUserInfo

instance (UserInfoM m) => UserInfoM (ExceptT r m) where
  askUserInfo :: ExceptT r m UserInfo
askUserInfo = m UserInfo -> ExceptT r m UserInfo
forall (m :: * -> *) a. Monad m => m a -> ExceptT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m UserInfo
forall (m :: * -> *). UserInfoM m => m UserInfo
askUserInfo

instance (UserInfoM m) => UserInfoM (StateT s m) where
  askUserInfo :: StateT s m UserInfo
askUserInfo = m UserInfo -> StateT s m UserInfo
forall (m :: * -> *) a. Monad m => m a -> StateT s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m UserInfo
forall (m :: * -> *). UserInfoM m => m UserInfo
askUserInfo

-- | extra information used to identify a Hasura User
data ExtraUserInfo = ExtraUserInfo {ExtraUserInfo -> Maybe Text
_euiUserId :: Maybe Text}
  deriving (Int -> ExtraUserInfo -> ShowS
[ExtraUserInfo] -> ShowS
ExtraUserInfo -> String
(Int -> ExtraUserInfo -> ShowS)
-> (ExtraUserInfo -> String)
-> ([ExtraUserInfo] -> ShowS)
-> Show ExtraUserInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ExtraUserInfo -> ShowS
showsPrec :: Int -> ExtraUserInfo -> ShowS
$cshow :: ExtraUserInfo -> String
show :: ExtraUserInfo -> String
$cshowList :: [ExtraUserInfo] -> ShowS
showList :: [ExtraUserInfo] -> ShowS
Show, ExtraUserInfo -> ExtraUserInfo -> Bool
(ExtraUserInfo -> ExtraUserInfo -> Bool)
-> (ExtraUserInfo -> ExtraUserInfo -> Bool) -> Eq ExtraUserInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ExtraUserInfo -> ExtraUserInfo -> Bool
== :: ExtraUserInfo -> ExtraUserInfo -> Bool
$c/= :: ExtraUserInfo -> ExtraUserInfo -> Bool
/= :: ExtraUserInfo -> ExtraUserInfo -> Bool
Eq, (forall x. ExtraUserInfo -> Rep ExtraUserInfo x)
-> (forall x. Rep ExtraUserInfo x -> ExtraUserInfo)
-> Generic ExtraUserInfo
forall x. Rep ExtraUserInfo x -> ExtraUserInfo
forall x. ExtraUserInfo -> Rep ExtraUserInfo x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ExtraUserInfo -> Rep ExtraUserInfo x
from :: forall x. ExtraUserInfo -> Rep ExtraUserInfo x
$cto :: forall x. Rep ExtraUserInfo x -> ExtraUserInfo
to :: forall x. Rep ExtraUserInfo x -> ExtraUserInfo
Generic)

-- | Represents how to build a role from the session variables
data UserRoleBuild
  = -- | Look for `x-hasura-role` session variable value and absence will raise an exception
    URBFromSessionVariables
  | -- | Look for `x-hasura-role` session variable value, if absent fall back to given role
    URBFromSessionVariablesFallback !RoleName
  | -- | Use only the pre-determined role
    URBPreDetermined !RoleName
  deriving (Int -> UserRoleBuild -> ShowS
[UserRoleBuild] -> ShowS
UserRoleBuild -> String
(Int -> UserRoleBuild -> ShowS)
-> (UserRoleBuild -> String)
-> ([UserRoleBuild] -> ShowS)
-> Show UserRoleBuild
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UserRoleBuild -> ShowS
showsPrec :: Int -> UserRoleBuild -> ShowS
$cshow :: UserRoleBuild -> String
show :: UserRoleBuild -> String
$cshowList :: [UserRoleBuild] -> ShowS
showList :: [UserRoleBuild] -> ShowS
Show, UserRoleBuild -> UserRoleBuild -> Bool
(UserRoleBuild -> UserRoleBuild -> Bool)
-> (UserRoleBuild -> UserRoleBuild -> Bool) -> Eq UserRoleBuild
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UserRoleBuild -> UserRoleBuild -> Bool
== :: UserRoleBuild -> UserRoleBuild -> Bool
$c/= :: UserRoleBuild -> UserRoleBuild -> Bool
/= :: UserRoleBuild -> UserRoleBuild -> Bool
Eq)