From 0374d4e6513728ff7c2fa0847a977f2e13f11f31 Mon Sep 17 00:00:00 2001 From: calebmer Date: Wed, 11 Nov 2015 09:31:36 -0500 Subject: [PATCH] Add tables back to DbStructure --- src/PostgREST/App.hs | 3 ++- src/PostgREST/DbStructure.hs | 23 ++++++++++++++++++++++- src/PostgREST/Types.hs | 3 ++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/PostgREST/App.hs b/src/PostgREST/App.hs index f3f914109..a51004cf9 100644 --- a/src/PostgREST/App.hs +++ b/src/PostgREST/App.hs @@ -147,7 +147,7 @@ app db conf reqBody req = -- select * from public.proc(a := "foo"::undefined) where whereT limit limitT ([], _) -> do - body <- encode <$> tables (cs schema) + body <- encode <$> accessibleTables (filter ((== cs schema) . tableSchema) allTabs) return $ responseLBS status200 [jsonH] $ cs body ([table], "OPTIONS") -> do @@ -160,6 +160,7 @@ app db conf reqBody req = return $ responseLBS status404 [] "" where + allTabs = tables db allRels = relations db allCols = columns db allPrKeys = primaryKeys db diff --git a/src/PostgREST/DbStructure.hs b/src/PostgREST/DbStructure.hs index 91d728c64..0798b1120 100644 --- a/src/PostgREST/DbStructure.hs +++ b/src/PostgREST/DbStructure.hs @@ -5,6 +5,7 @@ {-# LANGUAGE TypeSynonymInstances #-} module PostgREST.DbStructure ( createDbStructure +, accessibleTables , doesProcExist , doesProcReturnJWT ) where @@ -71,6 +72,26 @@ doesProcReturnJWT = doesProc [H.stmt| AND pg_catalog.pg_get_function_result(p.oid) like '%jwt_claims' |] +accessibleTables :: [Table] -> H.Tx P.Postgres s [Table] +accessibleTables allTabs = do + accessible <- H.listEx $ [H.stmt| + SELECT + n.nspname AS table_schema, + c.relname AS table_name + FROM pg_class c + JOIN pg_namespace n ON n.oid = c.relnamespace + WHERE + c.relkind IN ('v','r','m') AND + n.nspname NOT IN ('pg_catalog', 'information_schema') AND ( + pg_has_role(c.relowner, 'USAGE'::text) OR + has_table_privilege(c.oid, 'SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER'::text) OR + has_any_column_privilege(c.oid, 'SELECT, INSERT, UPDATE, REFERENCES'::text) + ) + ORDER BY table_schema, table_name + |] + let isAccessible table = isJust $ find (\(s,n) -> tableSchema table == s && tableName table == n) accessible + return $ filter isAccessible allTabs + synonymousColumns :: [(Column,Column)] -> [Column] -> [[Column]] synonymousColumns allSyns cols = synCols' where @@ -159,7 +180,7 @@ allTables = do WHERE c.relkind IN ('v','r','m') AND n.nspname NOT IN ('pg_catalog', 'information_schema') GROUP BY table_schema, table_name, insertable - ORDER BY table_schema, table_name; + ORDER BY table_schema, table_name |] return $ map tableFromRow rows diff --git a/src/PostgREST/Types.hs b/src/PostgREST/Types.hs index 9bb4d4cab..ae9e8e819 100644 --- a/src/PostgREST/Types.hs +++ b/src/PostgREST/Types.hs @@ -6,7 +6,8 @@ import Data.Aeson import Data.Map data DbStructure = DbStructure { - columns :: [Column] + tables :: [Table] +, columns :: [Column] , relations :: [Relation] , primaryKeys :: [PrimaryKey] } deriving (Show, Eq)