From 686fd0d67b47be94be6d5bf2b22ee8d5188e5283 Mon Sep 17 00:00:00 2001 From: P Aswini Kumar Date: Wed, 11 Sep 2024 12:11:07 +0000 Subject: [PATCH] Refactoring approach Signed-off-by: P Aswini Kumar --- contrib/babelfishpg_tsql/src/pl_exec-2.c | 4 +- contrib/babelfishpg_tsql/src/rolecmds.c | 88 +++++------------------- contrib/babelfishpg_tsql/src/rolecmds.h | 3 +- 3 files changed, 22 insertions(+), 73 deletions(-) diff --git a/contrib/babelfishpg_tsql/src/pl_exec-2.c b/contrib/babelfishpg_tsql/src/pl_exec-2.c index 1df723144b..51dd39f500 100644 --- a/contrib/babelfishpg_tsql/src/pl_exec-2.c +++ b/contrib/babelfishpg_tsql/src/pl_exec-2.c @@ -3921,10 +3921,10 @@ exec_stmt_change_dbowner(PLtsql_execstate *estate, PLtsql_stmt_change_dbowner *s SetUserIdAndSecContext(get_bbf_role_admin_oid(), save_sec_context | SECURITY_LOCAL_USERID_CHANGE); /* Revoke dbo role from the previous owner */ - revoke_dbo_from_login(get_owner_of_db(stmt->db_name), stmt->db_name); + grant_dbo_to_login(get_owner_of_db(stmt->db_name), stmt->db_name, false); /* Grant dbo role to the new owner */ - grant_dbo_to_login(stmt->new_owner_name, stmt->db_name); + grant_dbo_to_login(stmt->new_owner_name, stmt->db_name, true); update_db_owner(stmt->new_owner_name, stmt->db_name); } PG_FINALLY(); diff --git a/contrib/babelfishpg_tsql/src/rolecmds.c b/contrib/babelfishpg_tsql/src/rolecmds.c index 6e97cc827e..d6275fbfec 100644 --- a/contrib/babelfishpg_tsql/src/rolecmds.c +++ b/contrib/babelfishpg_tsql/src/rolecmds.c @@ -586,7 +586,7 @@ grant_guests_to_login(const char *login) } void -grant_dbo_to_login(const char* login, const char* db_name) +grant_dbo_to_login(const char* login, const char* db_name, bool is_grant) { StringInfoData query; List *parsetree_list; @@ -605,73 +605,16 @@ grant_dbo_to_login(const char* login, const char* db_name) acc->cols = NIL; dbo = lappend(dbo, acc); - /* Build dummy GRANT statement to grant membership to login */ - appendStringInfo(&query, "GRANT dummy TO dummy; "); - - parsetree_list = raw_parser(query.data, RAW_PARSE_DEFAULT); - - if (list_length(parsetree_list) != 1) - ereport(ERROR, - (errcode(ERRCODE_SYNTAX_ERROR), - errmsg("Expected 1 statement but get %d statements after parsing", - list_length(parsetree_list)))); - - /* Update the dummy statement with real values */ - stmt = parsetree_nth_stmt(parsetree_list, 0); - tmp = makeNode(RoleSpec); - tmp->roletype = ROLESPEC_CSTRING; - tmp->location = -1; - tmp->rolename = pstrdup(login); - - update_GrantRoleStmt(stmt, dbo, list_make1(tmp)); - - /* Run the built query */ - /* need to make a wrapper PlannedStmt */ - wrapper = makeNode(PlannedStmt); - wrapper->commandType = CMD_UTILITY; - wrapper->canSetTag = false; - wrapper->utilityStmt = stmt; - wrapper->stmt_location = 0; - wrapper->stmt_len = 18; - - /* do this step */ - ProcessUtility(wrapper, - "(CREATE DATABASE )", - false, - PROCESS_UTILITY_SUBCOMMAND, - NULL, - NULL, - None_Receiver, - NULL); - - /* make sure later steps can see the object created here */ - CommandCounterIncrement(); - - pfree(query.data); -} - -void -revoke_dbo_from_login(const char* login, const char* db_name) -{ - StringInfoData query; - List *parsetree_list; - List *dbo = NIL; - Node *stmt; - RoleSpec *tmp; - PlannedStmt *wrapper; - AccessPriv *acc; - - const char *dbo_role_name = get_dbo_role_name(db_name); - - initStringInfo(&query); - - acc = makeNode(AccessPriv); - acc->priv_name = pstrdup(dbo_role_name); - acc->cols = NIL; - dbo = lappend(dbo, acc); - - /* Build dummy REVOKE statement to revoke membership from login */ - appendStringInfo(&query, "REVOKE dummy FROM dummy; "); + if (is_grant) + { + /* Build dummy GRANT statement to grant membership to login */ + appendStringInfo(&query, "GRANT dummy TO dummy; "); + } + else + { + /* Build dummy REVOKE statement to revoke membership from login */ + appendStringInfo(&query, "REVOKE dummy FROM dummy; "); + } parsetree_list = raw_parser(query.data, RAW_PARSE_DEFAULT); @@ -688,7 +631,14 @@ revoke_dbo_from_login(const char* login, const char* db_name) tmp->location = -1; tmp->rolename = pstrdup(login); - update_RevokeRoleStmt(stmt, dbo, list_make1(tmp)); + if (is_grant) + { + update_GrantRoleStmt(stmt, dbo, list_make1(tmp)); + } + else + { + update_RevokeRoleStmt(stmt, dbo, list_make1(tmp)); + } /* Run the built query */ /* need to make a wrapper PlannedStmt */ diff --git a/contrib/babelfishpg_tsql/src/rolecmds.h b/contrib/babelfishpg_tsql/src/rolecmds.h index bfb147168c..608b67657c 100644 --- a/contrib/babelfishpg_tsql/src/rolecmds.h +++ b/contrib/babelfishpg_tsql/src/rolecmds.h @@ -83,7 +83,6 @@ extern bool windows_domain_contains_invalid_chars(char *input); extern bool check_windows_logon_length(char *input); extern char* get_windows_domain_name(char* input); extern bool windows_domain_is_not_supported(char* domain_name); -extern void grant_dbo_to_login(const char* login, const char* db_name); -extern void revoke_dbo_from_login(const char* login, const char* db_name); +extern void grant_dbo_to_login(const char* login, const char* db_name, bool is_grant); #endif