2018-06-27 16:11:32 +03:00
|
|
|
{-# LANGUAGE TypeFamilyDependencies #-}
|
|
|
|
|
|
|
|
module Hasura.RQL.DDL.Permission.Internal where
|
|
|
|
|
2020-08-27 19:36:39 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
|
2020-11-02 14:50:40 +03:00
|
|
|
import qualified Data.HashMap.Strict as M
|
|
|
|
import qualified Data.Text as T
|
2020-08-27 19:36:39 +03:00
|
|
|
|
2020-11-02 14:50:40 +03:00
|
|
|
import Control.Lens hiding ((.=))
|
2018-06-27 16:11:32 +03:00
|
|
|
import Data.Aeson.TH
|
|
|
|
import Data.Aeson.Types
|
2020-10-21 19:35:06 +03:00
|
|
|
import Data.Text.Extended
|
2020-10-27 16:53:49 +03:00
|
|
|
|
2020-11-02 14:50:40 +03:00
|
|
|
import Hasura.Backends.Postgres.Translate.BoolExp
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.RQL.Types
|
2020-10-21 19:35:06 +03:00
|
|
|
import Hasura.Server.Utils
|
|
|
|
import Hasura.Session
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
|
2021-02-14 09:07:52 +03:00
|
|
|
convColSpec :: FieldInfoMap (FieldInfo b) -> PermColSpec b -> [Column b]
|
2018-06-27 16:11:32 +03:00
|
|
|
convColSpec _ (PCCols cols) = cols
|
2019-09-19 07:47:36 +03:00
|
|
|
convColSpec cim PCStar = map pgiColumn $ getCols cim
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
permissionIsDefined
|
2020-10-22 23:42:27 +03:00
|
|
|
:: Maybe (RolePermInfo backend) -> PermAccessor backend a -> Bool
|
2018-06-27 16:11:32 +03:00
|
|
|
permissionIsDefined rpi pa =
|
2018-08-27 14:50:18 +03:00
|
|
|
isJust $ join $ rpi ^? _Just.permAccToLens pa
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
assertPermDefined
|
2020-12-01 18:50:18 +03:00
|
|
|
:: (Backend backend, MonadError QErr m)
|
2018-06-27 16:11:32 +03:00
|
|
|
=> RoleName
|
2020-10-22 23:42:27 +03:00
|
|
|
-> PermAccessor backend a
|
|
|
|
-> TableInfo backend
|
2018-06-27 16:11:32 +03:00
|
|
|
-> m ()
|
|
|
|
assertPermDefined roleName pa tableInfo =
|
|
|
|
unless (permissionIsDefined rpi pa) $ throw400 PermissionDenied $ mconcat
|
2020-12-17 14:37:16 +03:00
|
|
|
[ "'" <> tshow (permAccToType pa) <> "'"
|
2019-11-20 21:21:30 +03:00
|
|
|
, " permission on " <>> _tciName (_tiCoreInfo tableInfo)
|
2018-06-27 16:11:32 +03:00
|
|
|
, " for role " <>> roleName
|
|
|
|
, " does not exist"
|
|
|
|
]
|
|
|
|
where
|
2019-07-22 15:47:13 +03:00
|
|
|
rpi = M.lookup roleName $ _tiRolePermInfoMap tableInfo
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
askPermInfo
|
2020-12-01 18:50:18 +03:00
|
|
|
:: (Backend backend, MonadError QErr m)
|
2020-10-22 23:42:27 +03:00
|
|
|
=> TableInfo backend
|
2018-06-27 16:11:32 +03:00
|
|
|
-> RoleName
|
2020-10-22 23:42:27 +03:00
|
|
|
-> PermAccessor backend c
|
2018-06-27 16:11:32 +03:00
|
|
|
-> m c
|
|
|
|
askPermInfo tabInfo roleName pa =
|
2020-12-01 18:50:18 +03:00
|
|
|
(M.lookup roleName rpim >>= (^. permAccToLens pa))
|
2020-11-26 16:57:03 +03:00
|
|
|
`onNothing`
|
|
|
|
throw400 PermissionDenied
|
|
|
|
(mconcat
|
|
|
|
[ pt <> " permission on " <>> _tciName (_tiCoreInfo tabInfo)
|
|
|
|
, " for role " <>> roleName
|
|
|
|
, " does not exist"
|
|
|
|
])
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
|
|
|
pt = permTypeToCode $ permAccToType pa
|
2019-07-22 15:47:13 +03:00
|
|
|
rpim = _tiRolePermInfoMap tabInfo
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2021-02-14 09:07:52 +03:00
|
|
|
type CreatePerm b a = WithTable b (PermDef a)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
data CreatePermP1Res a
|
|
|
|
= CreatePermP1Res
|
|
|
|
{ cprInfo :: !a
|
|
|
|
, cprDeps :: ![SchemaDependency]
|
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
|
|
|
procBoolExp
|
2021-02-14 09:07:52 +03:00
|
|
|
:: (QErrM m, TableCoreInfoRM b m, BackendMetadata b)
|
2020-12-28 15:56:00 +03:00
|
|
|
=> SourceName
|
2021-02-14 09:07:52 +03:00
|
|
|
-> TableName b
|
|
|
|
-> FieldInfoMap (FieldInfo b)
|
|
|
|
-> BoolExp b
|
|
|
|
-> m (AnnBoolExpPartialSQL b, [SchemaDependency])
|
2020-12-28 15:56:00 +03:00
|
|
|
procBoolExp source tn fieldInfoMap be = do
|
2021-02-14 09:07:52 +03:00
|
|
|
abe <- annBoolExp parseCollectableType fieldInfoMap $ unBoolExp be
|
2020-12-28 15:56:00 +03:00
|
|
|
let deps = getBoolExpDeps source tn abe
|
2018-11-16 15:40:23 +03:00
|
|
|
return (abe, deps)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2020-10-27 16:53:49 +03:00
|
|
|
isReqUserId :: Text -> Bool
|
2018-08-29 08:47:13 +03:00
|
|
|
isReqUserId = (== "req_user_id") . T.toLower
|
|
|
|
|
2020-10-27 16:53:49 +03:00
|
|
|
getDepHeadersFromVal :: Value -> [Text]
|
2019-02-11 15:45:30 +03:00
|
|
|
getDepHeadersFromVal val = case val of
|
|
|
|
Object o -> parseObject o
|
|
|
|
_ -> parseOnlyString val
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
2019-02-11 15:45:30 +03:00
|
|
|
parseOnlyString v = case v of
|
2018-08-29 08:47:13 +03:00
|
|
|
(String t)
|
2020-04-24 12:10:53 +03:00
|
|
|
| isSessionVariable t -> [T.toLower t]
|
2018-08-29 08:47:13 +03:00
|
|
|
| isReqUserId t -> [userIdHeader]
|
|
|
|
| otherwise -> []
|
|
|
|
_ -> []
|
2018-11-16 15:40:23 +03:00
|
|
|
parseObject o =
|
2019-02-11 15:45:30 +03:00
|
|
|
concatMap getDepHeadersFromVal (M.elems o)
|
|
|
|
|
2020-11-02 14:50:40 +03:00
|
|
|
getDependentHeaders :: BoolExp b -> [Text]
|
2019-02-11 15:45:30 +03:00
|
|
|
getDependentHeaders (BoolExp boolExp) =
|
|
|
|
flip foldMap boolExp $ \(ColExp _ v) -> getDepHeadersFromVal v
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2021-02-14 09:07:52 +03:00
|
|
|
data DropPerm b a
|
2018-06-27 16:11:32 +03:00
|
|
|
= DropPerm
|
2020-12-28 15:56:00 +03:00
|
|
|
{ dipSource :: !SourceName
|
2021-02-14 09:07:52 +03:00
|
|
|
, dipTable :: !(TableName b)
|
2020-12-28 15:56:00 +03:00
|
|
|
, dipRole :: !RoleName
|
2021-02-14 09:07:52 +03:00
|
|
|
} deriving (Generic)
|
|
|
|
deriving instance (Backend b) => Show (DropPerm b a)
|
|
|
|
deriving instance (Backend b) => Eq (DropPerm b a)
|
|
|
|
instance (Backend b) => ToJSON (DropPerm b a) where
|
|
|
|
toJSON = genericToJSON hasuraJSON{omitNothingFields=True}
|
2020-12-28 15:56:00 +03:00
|
|
|
|
2021-02-14 09:07:52 +03:00
|
|
|
instance (Backend b) => FromJSON (DropPerm b a) where
|
2020-12-28 15:56:00 +03:00
|
|
|
parseJSON = withObject "DropPerm" $ \o ->
|
|
|
|
DropPerm
|
|
|
|
<$> o .:? "source" .!= defaultSource
|
|
|
|
<*> o .: "table"
|
|
|
|
<*> o .: "role"
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2021-02-14 09:07:52 +03:00
|
|
|
type family PermInfo (b :: BackendType) a = r | r -> a
|