Merge pull request #83 from CartoDB/64-usertables

Replace CDB_UserTables with something that can handle multi-user accounts
This commit is contained in:
Raul Ochoa 2015-06-30 11:33:32 +02:00
commit 8190edb461
4 changed files with 21 additions and 28 deletions

View File

@ -11,7 +11,7 @@ See https://github.com/CartoDB/cartodb/wiki/CartoDB-PostgreSQL-extension
Dependencies Dependencies
------------ ------------
* PostgreSQL 9.3+ (with plpythonu extension) * PostgreSQL 9.3+ (with plpythonu extension and xml support)
* [PostGIS extension](http://postgis.net) * [PostGIS extension](http://postgis.net)
* [Schema triggers extension] * [Schema triggers extension]
(https://bitbucket.org/malloclabs/pg_schema_triggers) (https://bitbucket.org/malloclabs/pg_schema_triggers)

View File

@ -6,34 +6,23 @@
-- Currently accepted permissions are: 'public', 'private' or 'all' -- Currently accepted permissions are: 'public', 'private' or 'all'
-- --
CREATE OR REPLACE FUNCTION CDB_UserTables(perm text DEFAULT 'all') CREATE OR REPLACE FUNCTION CDB_UserTables(perm text DEFAULT 'all')
RETURNS SETOF information_schema.sql_identifier RETURNS SETOF name
AS $$ AS $$
WITH usertables AS (
-- TODO: query CDB_TableMetadata for this ? SELECT c.relname
-- See http://github.com/CartoDB/cartodb/issues/254#issuecomment-26044777 FROM pg_class c
SELECT table_name as t JOIN pg_roles r ON r.oid = c.relowner
FROM information_schema.tables JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE WHERE r.rolname = current_user
table_type='BASE TABLE' AND c.relkind = 'r'
AND table_schema='public' AND c.relname NOT IN ('cdb_tablemetadata', 'spatial_ref_sys')
AND table_name NOT IN ( AND n.nspname NOT IN ('pg_catalog', 'information_schema')
'cdb_tablemetadata', AND CASE WHEN perm = 'public' THEN has_table_privilege('public', c.oid, 'SELECT')
'spatial_ref_sys' WHEN perm = 'private' THEN has_table_privilege(c.oid, 'SELECT') AND NOT
) has_table_privilege('public', c.oid, 'SELECT')
), perms AS ( WHEN perm = 'all' THEN has_table_privilege(c.oid, 'SELECT')
SELECT t, has_table_privilege('public', 'public'||'.'||t, 'SELECT') as p ELSE false END;
FROM usertables
)
SELECT t FROM perms
WHERE (
p = CASE WHEN $1 = 'private' THEN false
WHEN $1 = 'public' THEN true
ELSE not p -- none
END
OR $1 = 'all'
)
AND has_table_privilege('public'||'.'||t, 'SELECT')
;
$$ LANGUAGE 'sql'; $$ LANGUAGE 'sql';
-- This is to migrate from pre-0.2.0 version -- This is to migrate from pre-0.2.0 version

View File

@ -31,6 +31,8 @@ create table sc.test (a int);
insert into sc.test values (1); insert into sc.test values (1);
WITH inp AS ( select 'select * from sc.test'::text as q ) WITH inp AS ( select 'select * from sc.test'::text as q )
SELECT q, CDB_QueryTables(q) from inp; SELECT q, CDB_QueryTables(q) from inp;
DROP TABLE sc.test;
DROP SCHEMA sc;
WITH inp AS ( select 'SELECT WITH inp AS ( select 'SELECT
* FROM geometry_columns'::text as q ) * FROM geometry_columns'::text as q )

View File

@ -17,5 +17,7 @@ CREATE SCHEMA
CREATE TABLE CREATE TABLE
INSERT 0 1 INSERT 0 1
select * from sc.test|{sc.test} select * from sc.test|{sc.test}
DROP TABLE
DROP SCHEMA
SELECT SELECT
* FROM geometry_columns|{pg_catalog.pg_attribute,pg_catalog.pg_class,pg_catalog.pg_namespace,pg_catalog.pg_type} * FROM geometry_columns|{pg_catalog.pg_attribute,pg_catalog.pg_class,pg_catalog.pg_namespace,pg_catalog.pg_type}