diff --git a/Makefile b/Makefile index 0bc7ec6..fa9ffb6 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # cartodb/Makefile EXTENSION = cartodb -EXTVERSION = 0.24.1 +EXTVERSION = 0.25.0 SED = sed AWK = awk @@ -93,6 +93,7 @@ UPGRADABLE = \ 0.23.2 \ 0.24.0 \ 0.24.1 \ + 0.25.0 \ $(EXTVERSION)dev \ $(EXTVERSION)next \ $(END) diff --git a/NEWS.md b/NEWS.md index 17ec9c2..9c803b2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,6 @@ +0.25.0 (2019-02-22) +* Add `CDB_Username` to get the cartodb username from the current PostgreSQL user + 0.24.1 (2019-01-02) * Drop functions removed in 0.12 (#341) * Travis: Test with PostgreSQL 9.5, 10 and 11. diff --git a/scripts-available/CDB_Username.sql b/scripts-available/CDB_Username.sql new file mode 100644 index 0000000..9c6a5bc --- /dev/null +++ b/scripts-available/CDB_Username.sql @@ -0,0 +1,6 @@ +-- Returns the cartodb username of the current PostgreSQL session +CREATE OR REPLACE FUNCTION CDB_Username() +RETURNS text +AS $$ + SELECT CDB_Conf_GetConf(CONCAT('api_keys_', session_user))->>'username'; +$$ LANGUAGE SQL STABLE PARALLEL SAFE SECURITY DEFINER; diff --git a/scripts-enabled/CDB_Conf.sql b/scripts-enabled/910-CDB_Conf.sql similarity index 100% rename from scripts-enabled/CDB_Conf.sql rename to scripts-enabled/910-CDB_Conf.sql diff --git a/scripts-enabled/920-CDB_Username.sql b/scripts-enabled/920-CDB_Username.sql new file mode 120000 index 0000000..8c0abca --- /dev/null +++ b/scripts-enabled/920-CDB_Username.sql @@ -0,0 +1 @@ +../scripts-available/CDB_Username.sql \ No newline at end of file diff --git a/test/CDB_Username.sql b/test/CDB_Username.sql new file mode 100644 index 0000000..ed344d6 --- /dev/null +++ b/test/CDB_Username.sql @@ -0,0 +1,22 @@ +SELECT session_user; -- postgres +SELECT CDB_Username(); -- (NULL) + +-- Add the role fulano with api_key and connect with it +\set QUIET on +CREATE ROLE fulano LOGIN; +GRANT USAGE ON SCHEMA cartodb TO fulano; +GRANT EXECUTE ON FUNCTION CDB_Username() TO fulano; +INSERT INTO cdb_conf (key, value) VALUES ('api_keys_fulano', '{"username": "fulanito", "permissions":[]}'); +SET SESSION AUTHORIZATION fulano; +\set QUIET off + +SELECT session_user; -- fulano +SELECT CDB_Username(); -- fulanito + +-- Remove fulano +\set QUIET on +SET SESSION AUTHORIZATION postgres; +REVOKE USAGE ON SCHEMA cartodb FROM fulano; +REVOKE EXECUTE ON FUNCTION CDB_Username() FROM fulano; +DROP ROLE fulano; +\set QUIET off \ No newline at end of file diff --git a/test/CDB_Username_expect b/test/CDB_Username_expect new file mode 100644 index 0000000..fe1ea5b --- /dev/null +++ b/test/CDB_Username_expect @@ -0,0 +1,4 @@ +postgres + +fulano +fulanito