Add CDB_Username() function
This commit is contained in:
parent
3f4479fe12
commit
2a4ecd4850
13
scripts-available/CDB_Username.sql
Normal file
13
scripts-available/CDB_Username.sql
Normal file
@ -0,0 +1,13 @@
|
||||
-- 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
|
||||
CREATE OR REPLACE FUNCTION CDB_Username()
|
||||
RETURNS text
|
||||
AS $$
|
||||
SELECT _CDB_Username(current_user);
|
||||
$$ LANGUAGE SQL STABLE PARALLEL SAFE;
|
1
scripts-enabled/920-CDB_Username.sql
Symbolic link
1
scripts-enabled/920-CDB_Username.sql
Symbolic link
@ -0,0 +1 @@
|
||||
../scripts-available/CDB_Username.sql
|
17
test/CDB_Username.sql
Normal file
17
test/CDB_Username.sql
Normal file
@ -0,0 +1,17 @@
|
||||
SELECT current_user; -- postgres
|
||||
SELECT CDB_Username(); -- (NULL)
|
||||
|
||||
-- Connect with admin
|
||||
\set QUIET on
|
||||
\o log/test.log
|
||||
SELECT current_database() AS current_database;
|
||||
\gset
|
||||
SELECT SUBSTRING (:'current_database', 19, 36) AS user_id;
|
||||
\gset
|
||||
SELECT rolname AS admin_user FROM pg_roles where rolname LIKE ('%' || :'user_id');
|
||||
\gset
|
||||
\c :current_database :admin_user
|
||||
\o
|
||||
\set QUIET off
|
||||
|
||||
SELECT CDB_Username(); -- admin
|
3
test/CDB_Username_expect
Normal file
3
test/CDB_Username_expect
Normal file
@ -0,0 +1,3 @@
|
||||
postgres
|
||||
|
||||
admin
|
Loading…
Reference in New Issue
Block a user