{-# LANGUAGE TemplateHaskell #-}

module Hasura.RQL.Types.Roles
  ( DropInheritedRole (..),
    InheritedRole,
    ParentRoles (..),
    Role (..),
  )
where

import Data.Aeson
import Data.Aeson.Casing
import Data.Aeson.TH
import Hasura.Incremental (Cacheable)
import Hasura.Prelude
import Hasura.Session

newtype ParentRoles = ParentRoles {ParentRoles -> HashSet RoleName
_unParentRoles :: HashSet RoleName}
  deriving (Int -> ParentRoles -> ShowS
[ParentRoles] -> ShowS
ParentRoles -> String
(Int -> ParentRoles -> ShowS)
-> (ParentRoles -> String)
-> ([ParentRoles] -> ShowS)
-> Show ParentRoles
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ParentRoles] -> ShowS
$cshowList :: [ParentRoles] -> ShowS
show :: ParentRoles -> String
$cshow :: ParentRoles -> String
showsPrec :: Int -> ParentRoles -> ShowS
$cshowsPrec :: Int -> ParentRoles -> ShowS
Show, ParentRoles -> ParentRoles -> Bool
(ParentRoles -> ParentRoles -> Bool)
-> (ParentRoles -> ParentRoles -> Bool) -> Eq ParentRoles
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ParentRoles -> ParentRoles -> Bool
$c/= :: ParentRoles -> ParentRoles -> Bool
== :: ParentRoles -> ParentRoles -> Bool
$c== :: ParentRoles -> ParentRoles -> Bool
Eq, [ParentRoles] -> Value
[ParentRoles] -> Encoding
ParentRoles -> Value
ParentRoles -> Encoding
(ParentRoles -> Value)
-> (ParentRoles -> Encoding)
-> ([ParentRoles] -> Value)
-> ([ParentRoles] -> Encoding)
-> ToJSON ParentRoles
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [ParentRoles] -> Encoding
$ctoEncodingList :: [ParentRoles] -> Encoding
toJSONList :: [ParentRoles] -> Value
$ctoJSONList :: [ParentRoles] -> Value
toEncoding :: ParentRoles -> Encoding
$ctoEncoding :: ParentRoles -> Encoding
toJSON :: ParentRoles -> Value
$ctoJSON :: ParentRoles -> Value
ToJSON, Value -> Parser [ParentRoles]
Value -> Parser ParentRoles
(Value -> Parser ParentRoles)
-> (Value -> Parser [ParentRoles]) -> FromJSON ParentRoles
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [ParentRoles]
$cparseJSONList :: Value -> Parser [ParentRoles]
parseJSON :: Value -> Parser ParentRoles
$cparseJSON :: Value -> Parser ParentRoles
FromJSON, (forall x. ParentRoles -> Rep ParentRoles x)
-> (forall x. Rep ParentRoles x -> ParentRoles)
-> Generic ParentRoles
forall x. Rep ParentRoles x -> ParentRoles
forall x. ParentRoles -> Rep ParentRoles x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ParentRoles x -> ParentRoles
$cfrom :: forall x. ParentRoles -> Rep ParentRoles x
Generic)

instance Hashable ParentRoles

instance Cacheable ParentRoles

-- | The `Role` type represents a role by
--   containing its name and the names of its parent roles.
--   This type is used externally in the `add_inherited_role`
--   metadata API and is also used internally
--   in the permission building
--   part of the schema cache building process
data Role = Role
  { Role -> RoleName
_rRoleName :: RoleName,
    -- | set of the parent role names, in case of
    -- non-inherited roles it will be an empty set
    Role -> ParentRoles
_rParentRoles :: ParentRoles
  }
  deriving (Int -> Role -> ShowS
[Role] -> ShowS
Role -> String
(Int -> Role -> ShowS)
-> (Role -> String) -> ([Role] -> ShowS) -> Show Role
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Role] -> ShowS
$cshowList :: [Role] -> ShowS
show :: Role -> String
$cshow :: Role -> String
showsPrec :: Int -> Role -> ShowS
$cshowsPrec :: Int -> Role -> ShowS
Show, Role -> Role -> Bool
(Role -> Role -> Bool) -> (Role -> Role -> Bool) -> Eq Role
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Role -> Role -> Bool
$c/= :: Role -> Role -> Bool
== :: Role -> Role -> Bool
$c== :: Role -> Role -> Bool
Eq, (forall x. Role -> Rep Role x)
-> (forall x. Rep Role x -> Role) -> Generic Role
forall x. Rep Role x -> Role
forall x. Role -> Rep Role x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Role x -> Role
$cfrom :: forall x. Role -> Rep Role x
Generic)

instance Hashable Role

instance Cacheable Role

instance ToJSON Role where
  toJSON :: Role -> Value
toJSON (Role RoleName
roleName ParentRoles
parentRoles) =
    [Pair] -> Value
object
      [ Key
"role_name" Key -> RoleName -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= RoleName
roleName,
        -- the key for parent roles is "role_set"
        -- in the JSON encoding of the `Role` type
        -- is because when this feature
        -- was introduced, it was added as "role_set"
        Key
"role_set" Key -> ParentRoles -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= ParentRoles
parentRoles
      ]

instance FromJSON Role where
  parseJSON :: Value -> Parser Role
parseJSON = String -> (Object -> Parser Role) -> Value -> Parser Role
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Role" ((Object -> Parser Role) -> Value -> Parser Role)
-> (Object -> Parser Role) -> Value -> Parser Role
forall a b. (a -> b) -> a -> b
$ \Object
o ->
    RoleName -> ParentRoles -> Role
Role (RoleName -> ParentRoles -> Role)
-> Parser RoleName -> Parser (ParentRoles -> Role)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser RoleName
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"role_name" Parser (ParentRoles -> Role) -> Parser ParentRoles -> Parser Role
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser ParentRoles
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"role_set"

type InheritedRole = Role

newtype DropInheritedRole = DropInheritedRole
  { DropInheritedRole -> RoleName
_ddrRoleName :: RoleName
  }
  deriving (Int -> DropInheritedRole -> ShowS
[DropInheritedRole] -> ShowS
DropInheritedRole -> String
(Int -> DropInheritedRole -> ShowS)
-> (DropInheritedRole -> String)
-> ([DropInheritedRole] -> ShowS)
-> Show DropInheritedRole
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DropInheritedRole] -> ShowS
$cshowList :: [DropInheritedRole] -> ShowS
show :: DropInheritedRole -> String
$cshow :: DropInheritedRole -> String
showsPrec :: Int -> DropInheritedRole -> ShowS
$cshowsPrec :: Int -> DropInheritedRole -> ShowS
Show, DropInheritedRole -> DropInheritedRole -> Bool
(DropInheritedRole -> DropInheritedRole -> Bool)
-> (DropInheritedRole -> DropInheritedRole -> Bool)
-> Eq DropInheritedRole
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DropInheritedRole -> DropInheritedRole -> Bool
$c/= :: DropInheritedRole -> DropInheritedRole -> Bool
== :: DropInheritedRole -> DropInheritedRole -> Bool
$c== :: DropInheritedRole -> DropInheritedRole -> Bool
Eq)

$(deriveJSON (aesonDrop 4 snakeCase) ''DropInheritedRole)