33{-# LANGUAGE FlexibleContexts #-}
44{-# LANGUAGE OverloadedStrings #-}
55{-# LANGUAGE MultiParamTypeClasses #-}
6+ {-# LANGUAGE RankNTypes #-}
67module ProjectM36.RelationalExpression where
78import ProjectM36.Relation
89import ProjectM36.Tuple
@@ -51,7 +52,7 @@ import Control.Monad.Trans.Except (except)
5152import ProjectM36.NormalizeExpr
5253import ProjectM36.WithNameExpr
5354import ProjectM36.Function
54- import ProjectM36.AccessControlList as ACL
55+ import ProjectM36.AccessControlList ( RoleId , AccessControlList , SomePermission ( .. ), relvarsACL , dbcFunctionsACL , schemaACL , transGraphACL , aclACL , allPermissionsForRole , addAccess , removeAccess )
5556import Test.QuickCheck
5657import Data.Functor (void )
5758import qualified Data.Functor.Foldable as Fold
@@ -65,8 +66,10 @@ import ProjectM36.Module
6566import GHC hiding (getContext )
6667import Control.Exception
6768import GHC.Paths
69+ -- import System.FilePath
6870-- import GHC.Unit.State (emptyUnitState)
6971-- import GHC.Driver.Ppr (showSDocForUser)
72+ -- import GHC.Utils.Outputable (ppr)
7073-- import GHC.Unit.Finder.Types (FindResult(..))
7174-- import GHC.Unit.Finder (findImportedModule)
7275import GHC.Types.Name.Occurrence (mkVarOcc , mkTcOcc )
@@ -77,7 +80,7 @@ import GHC.Builtin.Types (unitTy)
7780import GHC.Core.TyCo.Compare (eqType )
7881import Unsafe.Coerce
7982import Control.Monad (forM )
80- -- import GHC.Utils.Outputable (ppr)
83+
8184#endif
8285
8386data DatabaseContextExprDetails = CountUpdatedTuples
@@ -557,6 +560,7 @@ data DatabaseContextIOEvalEnv = DatabaseContextIOEvalEnv
557560 dbcio_graph :: TransactionGraph ,
558561 dbcio_mScriptSession :: Maybe ScriptSession ,
559562 dbcio_roleId :: RoleId ,
563+ resolveRoleNameACL :: forall x . AccessControlList RoleName x -> IO (Either RelationalError (AccessControlList RoleId x )),
560564 dbcio_mModulesDirectory :: Maybe FilePath , -- ^ when running in persistent mode, this must be a Just value to a directory containing .o/.so/.dynlib files which the user has placed there for access to compiled functions
561565 dbcio_dbcfunctionUtils :: DatabaseContextFunctionUtils
562566 }
@@ -660,7 +664,7 @@ evalGraphRefDatabaseContextIOExpr (AddDatabaseContextFunction funcName' funcType
660664 funcName = funcName',
661665 funcType = funcAtomType,
662666 funcBody = FunctionScriptBody script compiledFunc,
663- funcACL = allPermissionsForRoleId myRoleId
667+ funcACL = allPermissionsForRole myRoleId
664668 }
665669 -- check if the name is already in use
666670 if HS. member funcName' (HS. map funcName dbcFuncs) then
@@ -1888,14 +1892,15 @@ importModuleFromPath :: ScriptSession -> ModuleBody -> DatabaseContextIOEvalMona
18881892importModuleFromPath _scriptSession _moduleSource = throwError (ScriptError ScriptCompilationDisabledError )
18891893#else
18901894importModuleFromPath scriptSession moduleSource = do
1895+ resolveRoleNameACLF <- resolveRoleNameACL <$> ask
18911896 res <- liftIO $ try $ do
18921897 withSystemTempFile " pm36module" $ \ tempModulePath tempModuleHandle -> do
18931898 hClose tempModuleHandle
18941899 TIO. writeFile tempModulePath moduleSource
18951900 runGhc (Just libdir) $ do
18961901 -- GHC needs to see the module on disk, so we write it to a temporary location
18971902 setSession (hscEnv scriptSession)
1898- dflags <- getSessionDynFlags
1903+ dflags <- getSessionDynFlags
18991904 let target = Target {
19001905 targetId = TargetFile tempModulePath Nothing ,
19011906 targetAllowObjCode = False ,
@@ -1907,7 +1912,7 @@ importModuleFromPath scriptSession moduleSource = do
19071912 case loadSuccess of
19081913 Failed -> pure (Left (ScriptError ModuleLoadError ))
19091914 Succeeded -> do
1910- {- modRes <- liftIO $ findImportedModule (hscEnv scriptSession) (mkModuleName "ProjectM36.Base") NoPkgQual
1915+ {- modRes <- liftIO $ findImportedModule (hscEnv scriptSession) (mkModuleName "ProjectM36.Base") NoPkgQual
19111916 liftIO $ case modRes of
19121917 Found modLoc _mod -> do
19131918 let packageLoc = takeDirectory (takeDirectory (takeDirectory (ml_dyn_obj_file modLoc)))
@@ -1958,21 +1963,26 @@ importModuleFromPath scriptSession moduleSource = do
19581963 tyConv <- mkTypeConversions
19591964 mkFunctions <- forM funcDeclarations $ \ funcDecl -> do
19601965 case funcDecl of
1961- DeclareDatabaseContextFunction funcS acl' -> do
1962- fType <- exprType TM_Default (T. unpack funcS)
1963- dbcFuncMonadType <- exprType TM_Default " undefined :: DatabaseContextFunctionMonad ()"
1964- -- extract arguments for dbc function
1965- let eAtomFuncType = convertGhcTypeToDatabaseContextFunctionAtomType dflags tyConv dbcFuncMonadType fType
1966- case eAtomFuncType of
1967- Left err -> throw (OtherScriptCompilationError (show err))
1968- Right dbcFuncType -> do
1969- let interpretedFunc = wrapDatabaseContextFunction dbcFuncType funcS
1970- dbcFunc :: DatabaseContextFunctionBodyType <- unsafeCoerce <$> compileExpr interpretedFunc
1971- let newDBCFunc = Function { funcName = funcS,
1972- funcType = dbcFuncType,
1973- funcBody = FunctionScriptBody (T. pack interpretedFunc) dbcFunc,
1974- funcACL = acl' }
1975- pure (MkDatabaseContextFunction newDBCFunc)
1966+ DeclareDatabaseContextFunction funcS roleNameACL -> do
1967+ -- resolve role name ACL into role-id-based ACL
1968+ eACL <- liftIO $ resolveRoleNameACLF roleNameACL
1969+ case eACL of
1970+ Left err -> throw err
1971+ Right acl' -> do
1972+ fType <- exprType TM_Default (T. unpack funcS)
1973+ dbcFuncMonadType <- exprType TM_Default " undefined :: DatabaseContextFunctionMonad ()"
1974+ -- extract arguments for dbc function
1975+ let eAtomFuncType = convertGhcTypeToDatabaseContextFunctionAtomType dflags tyConv dbcFuncMonadType fType
1976+ case eAtomFuncType of
1977+ Left err -> throw (OtherScriptCompilationError (show err))
1978+ Right dbcFuncType -> do
1979+ let interpretedFunc = wrapDatabaseContextFunction dbcFuncType funcS
1980+ dbcFunc :: DatabaseContextFunctionBodyType <- unsafeCoerce <$> compileExpr interpretedFunc
1981+ let newDBCFunc = Function { funcName = funcS,
1982+ funcType = dbcFuncType,
1983+ funcBody = FunctionScriptBody (T. pack interpretedFunc) dbcFunc,
1984+ funcACL = acl' }
1985+ pure (MkDatabaseContextFunction newDBCFunc)
19761986 DeclareAtomFunction funcS -> do
19771987 -- extract type from function in script
19781988 fType <- exprType TM_Default (T. unpack funcS)
0 commit comments