Merge pull request #346 from CartoDB/username

Add CDB_Username function
This commit is contained in:
Gonzalo Riestra
2019-02-22 09:20:47 +01:00
committed by GitHub
7 changed files with 38 additions and 1 deletions

View File

@@ -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)

View File

@@ -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.

View File

@@ -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;

View File

@@ -0,0 +1 @@
../scripts-available/CDB_Username.sql

22
test/CDB_Username.sql Normal file
View File

@@ -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

4
test/CDB_Username_expect Normal file
View File

@@ -0,0 +1,4 @@
postgres
fulano
fulanito