use session_user instead of current_user

This commit is contained in:
Gonzalo Riestra
2019-02-20 14:45:57 +01:00
parent 6d122462bb
commit 0568b36a90
2 changed files with 9 additions and 18 deletions

View File

@@ -1,13 +1,6 @@
-- Function returning the username of the provided user
CREATE OR REPLACE FUNCTION _CDB_Username(pg_user TEXT)
RETURNS text
AS $$
SELECT CDB_Conf_GetConf(CONCAT('api_keys_', pg_user))->>'username';
$$ LANGUAGE SQL STRICT IMMUTABLE PARALLEL SAFE SECURITY DEFINER;
-- Function returning the username of the current user
-- Returns the cartodb username of the current PostgreSQL session
CREATE OR REPLACE FUNCTION CDB_Username()
RETURNS text
AS $$
SELECT _CDB_Username(current_user);
$$ LANGUAGE SQL STABLE PARALLEL SAFE;
SELECT CDB_Conf_GetConf(CONCAT('api_keys_', session_user))->>'username';
$$ LANGUAGE SQL STABLE PARALLEL SAFE SECURITY DEFINER;

View File

@@ -1,24 +1,22 @@
SELECT current_user; -- postgres
SELECT session_user; -- postgres
SELECT CDB_Username(); -- (NULL)
-- Add the role fulano with an api_key and connect with it
-- 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;
GRANT EXECUTE ON FUNCTION _CDB_Username(text) TO fulano;
INSERT INTO cdb_conf (key, value) VALUES ('api_keys_fulano', '{"username": "fulanito", "permissions":[]}');
SET ROLE fulano;
SET SESSION AUTHORIZATION fulano;
\set QUIET off
SELECT current_user; -- fulano
SELECT session_user; -- fulano
SELECT CDB_Username(); -- fulanito
-- Remove fulano
\set QUIET on
SET ROLE postgres;
SET SESSION AUTHORIZATION postgres;
REVOKE USAGE ON SCHEMA cartodb FROM fulano;
REVOKE EXECUTE ON FUNCTION CDB_Username() FROM fulano;
REVOKE EXECUTE ON FUNCTION _CDB_Username(text) FROM fulano;
DROP ROLE fulano;
\set QUIET off
\set QUIET off