2014-06-25 18:38:14 +08:00
|
|
|
CREATE OR REPLACE
|
|
|
|
FUNCTION cartodb.CDB_Organization_Member_Group_Role_Member_Name()
|
|
|
|
RETURNS TEXT
|
|
|
|
AS 'SELECT ''cdb_org_member''::text || ''_'' || md5(current_database());'
|
|
|
|
LANGUAGE SQL IMMUTABLE;
|
|
|
|
|
2014-06-25 01:56:17 +08:00
|
|
|
DO LANGUAGE 'plpgsql' $$
|
2014-06-25 18:38:14 +08:00
|
|
|
DECLARE
|
|
|
|
cdb_org_member_role_name TEXT;
|
2014-06-25 01:56:17 +08:00
|
|
|
BEGIN
|
2014-06-25 18:38:14 +08:00
|
|
|
cdb_org_member_role_name := cartodb.CDB_Organization_Member_Group_Role_Member_Name();
|
|
|
|
IF NOT EXISTS ( SELECT * FROM pg_roles WHERE rolname= cdb_org_member_role_name )
|
2014-06-25 01:56:17 +08:00
|
|
|
THEN
|
2014-06-25 18:38:14 +08:00
|
|
|
EXECUTE 'CREATE ROLE "' || cdb_org_member_role_name || '" NOLOGIN;';
|
2014-06-25 01:56:17 +08:00
|
|
|
END IF;
|
|
|
|
END
|
|
|
|
$$;
|
|
|
|
|
|
|
|
CREATE OR REPLACE
|
|
|
|
FUNCTION cartodb.CDB_Organization_Create_Member(role_name text)
|
|
|
|
RETURNS void
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
2014-06-25 18:38:14 +08:00
|
|
|
EXECUTE 'GRANT "' || cartodb.CDB_Organization_Member_Group_Role_Member_Name() || '" TO "' || role_name || '"';
|
2014-06-25 01:56:17 +08:00
|
|
|
END
|
|
|
|
$$ LANGUAGE PLPGSQL VOLATILE;
|
|
|
|
|
|
|
|
|
2014-06-16 22:10:53 +08:00
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- Sharing tables
|
|
|
|
-------------------------------------------------------------------------------
|
2014-06-20 00:44:00 +08:00
|
|
|
CREATE OR REPLACE
|
|
|
|
FUNCTION cartodb.CDB_Organization_Add_Table_Read_Permission(from_schema text, table_name text, to_role_name text)
|
2014-06-16 22:10:53 +08:00
|
|
|
RETURNS void
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
2014-06-20 00:44:00 +08:00
|
|
|
EXECUTE 'GRANT USAGE ON SCHEMA ' || from_schema || ' TO "' || to_role_name || '"';
|
|
|
|
EXECUTE 'GRANT SELECT ON ' || from_schema || '.' || table_name || ' TO "' || to_role_name || '"';
|
2014-06-16 22:10:53 +08:00
|
|
|
END
|
|
|
|
$$ LANGUAGE PLPGSQL VOLATILE;
|
|
|
|
|
2014-06-25 01:56:17 +08:00
|
|
|
CREATE OR REPLACE
|
|
|
|
FUNCTION cartodb.CDB_Organization_Add_Table_Organization_Read_Permission(from_schema text, table_name text)
|
|
|
|
RETURNS void
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
2014-06-25 18:38:14 +08:00
|
|
|
EXECUTE 'SELECT cartodb.CDB_Organization_Add_Table_Read_Permission(''' || from_schema || ''', ''' || table_name || ''', ''' || cartodb.CDB_Organization_Member_Group_Role_Member_Name() || ''');';
|
2014-06-25 01:56:17 +08:00
|
|
|
END
|
|
|
|
$$ LANGUAGE PLPGSQL VOLATILE;
|
2014-06-16 22:10:53 +08:00
|
|
|
|
2014-06-20 00:44:00 +08:00
|
|
|
CREATE OR REPLACE
|
|
|
|
FUNCTION cartodb.CDB_Organization_Add_Table_Read_Write_Permission(from_schema text, table_name text, to_role_name text)
|
2014-06-16 22:10:53 +08:00
|
|
|
RETURNS void
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
2014-06-20 00:44:00 +08:00
|
|
|
EXECUTE 'GRANT USAGE ON SCHEMA ' || from_schema || ' TO "' || to_role_name || '"';
|
2014-06-26 21:50:56 +08:00
|
|
|
EXECUTE 'GRANT SELECT, INSERT, UPDATE, DELETE ON ' || from_schema || '.' || table_name || ' TO "' || to_role_name || '"';
|
2014-06-16 22:10:53 +08:00
|
|
|
END
|
|
|
|
$$ LANGUAGE PLPGSQL VOLATILE;
|
|
|
|
|
2014-06-25 01:56:17 +08:00
|
|
|
CREATE OR REPLACE
|
|
|
|
FUNCTION cartodb.CDB_Organization_Add_Table_Organization_Read_Write_Permission(from_schema text, table_name text)
|
|
|
|
RETURNS void
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
2014-06-25 18:38:14 +08:00
|
|
|
EXECUTE 'SELECT cartodb.CDB_Organization_Add_Table_Read_Write_Permission(''' || from_schema || ''', ''' || table_name || ''', ''' || cartodb.CDB_Organization_Member_Group_Role_Member_Name() || ''');';
|
2014-06-25 01:56:17 +08:00
|
|
|
END
|
|
|
|
$$ LANGUAGE PLPGSQL VOLATILE;
|
|
|
|
|
2014-06-16 22:10:53 +08:00
|
|
|
|
2014-06-20 00:44:00 +08:00
|
|
|
CREATE OR REPLACE
|
|
|
|
FUNCTION cartodb.CDB_Organization_Remove_Access_Permission(from_schema text, table_name text, to_role_name text)
|
2014-06-16 22:10:53 +08:00
|
|
|
RETURNS void
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
2014-06-20 00:44:00 +08:00
|
|
|
EXECUTE 'REVOKE ALL PRIVILEGES ON TABLE ' || from_schema || '.' || table_name || ' FROM "' || to_role_name || '"';
|
|
|
|
-- EXECUTE 'REVOKE USAGE ON SCHEMA ' || from_schema || ' FROM "' || to_role_name || '"';
|
|
|
|
-- We need to revoke usage on schema only if we are revoking privileges from the last table where to_role_name has
|
|
|
|
-- any permission granted within the schema from_schema
|
2014-06-16 22:10:53 +08:00
|
|
|
END
|
|
|
|
$$ LANGUAGE PLPGSQL VOLATILE;
|
2014-06-25 01:56:17 +08:00
|
|
|
|
|
|
|
CREATE OR REPLACE
|
|
|
|
FUNCTION cartodb.CDB_Organization_Remove_Organization_Access_Permission(from_schema text, table_name text)
|
|
|
|
RETURNS void
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
2014-06-25 18:38:14 +08:00
|
|
|
EXECUTE 'SELECT cartodb.CDB_Organization_Remove_Access_Permission(''' || from_schema || ''', ''' || table_name || ''', ''' || cartodb.CDB_Organization_Member_Group_Role_Member_Name() || ''');';
|
2014-06-25 01:56:17 +08:00
|
|
|
END
|
|
|
|
$$ LANGUAGE PLPGSQL VOLATILE;
|