diff --git a/db/sql/00_msar.sql b/db/sql/00_msar.sql index 78b37c80a5..9aa2976a31 100644 --- a/db/sql/00_msar.sql +++ b/db/sql/00_msar.sql @@ -1287,7 +1287,7 @@ SELECT jsonb_build_object( 'current_role', msar.get_role(current_role), 'parent_roles', COALESCE(array_remove( array_agg( - CASE WHEN pg_has_role(role_data.name, current_role, 'USAGE') + CASE WHEN pg_has_role(current_role, role_data.name, 'USAGE') THEN msar.get_role(role_data.name) END ), NULL ), ARRAY[]::jsonb[]) diff --git a/db/sql/test_00_msar.sql b/db/sql/test_00_msar.sql index 99c607f29c..ad2a7a3cad 100644 --- a/db/sql/test_00_msar.sql +++ b/db/sql/test_00_msar.sql @@ -3466,6 +3466,77 @@ END; $$ LANGUAGE plpgsql; +-- msar.get_current_role --------------------------------------------------------------------------- + +CREATE OR REPLACE FUNCTION __setup_get_current_role() RETURNS SETOF TEXT AS $$ +BEGIN + CREATE USER parent1; + CREATE ROLE parent2; + CREATE ROLE child_role; + GRANT parent1 TO child_role; + GRANT parent2 TO child_role; + GRANT USAGE ON SCHEMA msar, __msar TO child_role; + -- GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA msar, __msar TO intern_no_pkey; +END; +$$ LANGUAGE plpgsql; + + +CREATE OR REPLACE FUNCTION test_get_current_role() RETURNS SETOF TEXT AS $$ +DECLARE + child_role_oid oid; + parent1_oid oid; + parent2_oid oid; +BEGIN + PERFORM __setup_get_current_role(); + SET ROLE child_role; + child_role_oid := 'child_role'::regrole::oid; + parent1_oid := 'parent1'::regrole::oid; + parent2_oid := 'parent2'::regrole::oid; + RETURN NEXT is( + msar.get_current_role(), + format($j${ + "current_role":{ + "oid": %1$s, + "name": "child_role", + "login": false, + "super": false, + "members": null, + "inherits": true, + "create_db": false, + "create_role": false, + "description": null + }, + "parent_roles": [ + { + "oid": %2$s, + "name": "parent1", + "login": true, + "super": false, + "members": [{"oid": %1$s, "admin": false}], + "inherits": true, + "create_db": false, + "create_role": false, + "description": null + }, + { + "oid": %3$s, + "name": "parent2", + "login": false, + "super": false, + "members": [{"oid": %1$s, "admin": false}], + "inherits": true, + "create_db": false, + "create_role": false, + "description": null + } + ] + } + $j$, child_role_oid, parent1_oid, parent2_oid + )::jsonb + ); +END; +$$ LANGUAGE plpgsql; + -- msar.build_order_by_expr ------------------------------------------------------------------------ CREATE OR REPLACE FUNCTION test_build_order_by_expr() RETURNS SETOF TEXT AS $$