Skip to content

Commit

Permalink
Refactoring approach
Browse files Browse the repository at this point in the history
Signed-off-by: P Aswini Kumar <[email protected]>
  • Loading branch information
P Aswini Kumar committed Sep 11, 2024
1 parent e9e2e29 commit 686fd0d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 73 deletions.
4 changes: 2 additions & 2 deletions contrib/babelfishpg_tsql/src/pl_exec-2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
88 changes: 19 additions & 69 deletions contrib/babelfishpg_tsql/src/rolecmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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 */
Expand Down
3 changes: 1 addition & 2 deletions contrib/babelfishpg_tsql/src/rolecmds.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 686fd0d

Please sign in to comment.