From ce313bd4e8a3dd1347a1c765cb6777af6c306b69 Mon Sep 17 00:00:00 2001 From: Anju Bharti <66729219+anju15bharti@users.noreply.github.com> Date: Fri, 20 Dec 2024 20:52:05 +0530 Subject: [PATCH] Fix is_srvrolememberoutput for login same as specified server role (#3293) Earlier is_srvrolemember was returning 1 for login same as specified server role. With this commit, now it will return expected output. Task: BABEL-5515 Signed-off-by: ANJU BHARTI --- .../babelfishpg_tsql/sql/babelfishpg_tsql.sql | 3 +++ .../babelfishpg_tsql--4.4.0--5.0.0.sql | 4 +++ .../expected/dbcreator_role-vu-verify.out | 25 +++++++++++++++++++ .../expected/securityadmin_role-vu-verify.out | 8 ++++++ .../single_db/dbcreator_role-vu-verify.out | 25 +++++++++++++++++++ test/JDBC/input/dbcreator_role-vu-verify.mix | 10 ++++++++ .../input/securityadmin_role-vu-verify.mix | 3 +++ 7 files changed, 78 insertions(+) diff --git a/contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql b/contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql index 4a2617e7a4..fa07aee525 100644 --- a/contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql +++ b/contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql @@ -2190,6 +2190,9 @@ BEGIN ELSIF role = 'public' COLLATE sys.database_default THEN RETURN 1; + + ELSEIF role = login THEN + RETURN 0; ELSIF role COLLATE sys.database_default IN ('sysadmin', 'securityadmin', 'dbcreator') THEN has_role = (pg_has_role(login::TEXT, role::TEXT, 'MEMBER') diff --git a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--4.4.0--5.0.0.sql b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--4.4.0--5.0.0.sql index d2db5a4bf0..141fbf0a43 100644 --- a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--4.4.0--5.0.0.sql +++ b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--4.4.0--5.0.0.sql @@ -382,6 +382,7 @@ FROM pg_catalog.pg_roles AS Base INNER JOIN sys.babelfish_authid_login_ext AS Ex WHERE Ext.type = 'R' AND bbf_is_member_of_role_nosuper(sys.suser_id(), Base.oid); GRANT SELECT ON sys.login_token TO PUBLIC; + CREATE OR REPLACE FUNCTION is_srvrolemember(role sys.SYSNAME, login sys.SYSNAME DEFAULT suser_name()) RETURNS INTEGER AS $$ @@ -402,6 +403,9 @@ BEGIN ELSIF role = 'public' COLLATE sys.database_default THEN RETURN 1; + + ELSEIF role = login THEN + RETURN 0; ELSIF role COLLATE sys.database_default IN ('sysadmin', 'securityadmin', 'dbcreator') THEN has_role = (pg_has_role(login::TEXT, role::TEXT, 'MEMBER') diff --git a/test/JDBC/expected/dbcreator_role-vu-verify.out b/test/JDBC/expected/dbcreator_role-vu-verify.out index e8f0a98cd6..800402c13a 100644 --- a/test/JDBC/expected/dbcreator_role-vu-verify.out +++ b/test/JDBC/expected/dbcreator_role-vu-verify.out @@ -32,6 +32,31 @@ int ~~END~~ +select is_srvrolemember('dbcreator','dbcreator') +go +~~START~~ +int +0 +~~END~~ + + +select is_srvrolemember('sysadmin','sysadmin') +go +~~START~~ +int +0 +~~END~~ + + +-- should return NULL for invalid login +select is_srvrolemember('l1', 'l1') +go +~~START~~ +int + +~~END~~ + + alter login dbcreator_login1 with password='123' go diff --git a/test/JDBC/expected/securityadmin_role-vu-verify.out b/test/JDBC/expected/securityadmin_role-vu-verify.out index ccabd5967a..97c6c10cc1 100644 --- a/test/JDBC/expected/securityadmin_role-vu-verify.out +++ b/test/JDBC/expected/securityadmin_role-vu-verify.out @@ -15,6 +15,14 @@ int ~~END~~ +select is_srvrolemember('securityadmin','securityadmin') +go +~~START~~ +int +0 +~~END~~ + + alter login securityadmin_login1 with password='123' go diff --git a/test/JDBC/expected/single_db/dbcreator_role-vu-verify.out b/test/JDBC/expected/single_db/dbcreator_role-vu-verify.out index f845656db2..5e997fc9da 100644 --- a/test/JDBC/expected/single_db/dbcreator_role-vu-verify.out +++ b/test/JDBC/expected/single_db/dbcreator_role-vu-verify.out @@ -32,6 +32,31 @@ int ~~END~~ +select is_srvrolemember('dbcreator','dbcreator') +go +~~START~~ +int +0 +~~END~~ + + +select is_srvrolemember('sysadmin','sysadmin') +go +~~START~~ +int +0 +~~END~~ + + +-- should return NULL for invalid login +select is_srvrolemember('l1', 'l1') +go +~~START~~ +int + +~~END~~ + + alter login dbcreator_login1 with password='123' go diff --git a/test/JDBC/input/dbcreator_role-vu-verify.mix b/test/JDBC/input/dbcreator_role-vu-verify.mix index e5c8dafaa0..397cb44366 100644 --- a/test/JDBC/input/dbcreator_role-vu-verify.mix +++ b/test/JDBC/input/dbcreator_role-vu-verify.mix @@ -13,6 +13,16 @@ go select is_srvrolemember('dbcreator', 'securityadmin') go +select is_srvrolemember('dbcreator','dbcreator') +go + +select is_srvrolemember('sysadmin','sysadmin') +go + +-- should return NULL for invalid login +select is_srvrolemember('l1', 'l1') +go + alter login dbcreator_login1 with password='123' go diff --git a/test/JDBC/input/securityadmin_role-vu-verify.mix b/test/JDBC/input/securityadmin_role-vu-verify.mix index e764c9e726..60c56562b8 100644 --- a/test/JDBC/input/securityadmin_role-vu-verify.mix +++ b/test/JDBC/input/securityadmin_role-vu-verify.mix @@ -5,6 +5,9 @@ go select is_srvrolemember('securityadmin', 'sysadmin') go +select is_srvrolemember('securityadmin','securityadmin') +go + alter login securityadmin_login1 with password='123' go