From c96f5ca3ce67300e5484905630cf218649b304ec Mon Sep 17 00:00:00 2001 From: ANJU BHARTI Date: Tue, 31 Dec 2024 14:59:26 +0000 Subject: [PATCH] Add hook has_bbf_role_direct_membership_with_admin_true Signed-off-by: ANJU BHARTI --- contrib/babelfishpg_tsql/src/catalog.c | 34 ++++++++++++++++++++++++++ contrib/babelfishpg_tsql/src/catalog.h | 1 + contrib/babelfishpg_tsql/src/hooks.c | 6 +++++ 3 files changed, 41 insertions(+) diff --git a/contrib/babelfishpg_tsql/src/catalog.c b/contrib/babelfishpg_tsql/src/catalog.c index a9209fe2d8..3b6b454e6e 100644 --- a/contrib/babelfishpg_tsql/src/catalog.c +++ b/contrib/babelfishpg_tsql/src/catalog.c @@ -9,6 +9,7 @@ #include "catalog/catalog.h" #include "catalog/heap.h" #include "catalog/indexing.h" +#include "catalog/pg_auth_members.h" #include "catalog/pg_namespace.h" #include "catalog/pg_authid.h" #include "catalog/pg_proc.h" @@ -6287,3 +6288,36 @@ get_tvp_typename_typeschemaname(char *proc_name, char *target_arg_name, char **t if(!xactStarted) CommitTransactionCommand(); } + +/* + * Returns true if given role has direct member bbf_role_admin + * whose admin_option is true, else returns false. + */ +bool +has_bbf_role_direct_membership_with_admin_true(Oid role) +{ + CatCList *memlist; + + /* + * Find all existing tuples for given role + * whose member is bbf_role_admin + */ + memlist = SearchSysCacheList2(AUTHMEMROLEMEM, + ObjectIdGetDatum(role), + ObjectIdGetDatum(get_bbf_role_admin_oid())); + for (int i = 0; i < memlist->n_members; i++) + { + HeapTuple tup = &memlist->members[i]->tuple; + Form_pg_auth_members form = (Form_pg_auth_members) GETSTRUCT(tup); + + /* Return true if admin_option is true */ + if (form->admin_option) + { + ReleaseSysCacheList(memlist); + return true; + } + } + + ReleaseSysCacheList(memlist); + return false; +} \ No newline at end of file diff --git a/contrib/babelfishpg_tsql/src/catalog.h b/contrib/babelfishpg_tsql/src/catalog.h index c6ecfab882..b3e3ba05fe 100644 --- a/contrib/babelfishpg_tsql/src/catalog.h +++ b/contrib/babelfishpg_tsql/src/catalog.h @@ -28,6 +28,7 @@ extern bool IsPLtsqlExtendedCatalog(Oid relationId); extern bool IsPltsqlToastRelationHook(Relation relation); extern bool IsPltsqlToastClassHook(Form_pg_class pg_class_tup); extern void pltsql_drop_relation_refcnt_hook(Relation relation); +extern bool has_bbf_role_direct_membership_with_admin_true(Oid role); /***************************************** * SYS schema diff --git a/contrib/babelfishpg_tsql/src/hooks.c b/contrib/babelfishpg_tsql/src/hooks.c index fe618a9e9d..6cade5cede 100644 --- a/contrib/babelfishpg_tsql/src/hooks.c +++ b/contrib/babelfishpg_tsql/src/hooks.c @@ -302,6 +302,7 @@ static pltsql_replace_non_determinstic_hook_type prev_pltsql_replace_non_determi static pltsql_is_partitioned_table_reloptions_allowed_hook_type prev_pltsql_is_partitioned_table_reloptions_allowed_hook = NULL; static ExecFuncProc_AclCheck_hook_type prev_ExecFuncProc_AclCheck_hook = NULL; static bbf_execute_grantstmt_as_dbsecadmin_hook_type prev_bbf_execute_grantstmt_as_dbsecadmin_hook = NULL; +static has_bbf_role_direct_membership_with_admin_true_hook_type prev_has_bbf_role_direct_membership_with_admin_true_hook = NULL; /***************************************** * Install / Uninstall @@ -517,6 +518,9 @@ InstallExtendedHooks(void) prev_bbf_execute_grantstmt_as_dbsecadmin_hook = bbf_execute_grantstmt_as_dbsecadmin_hook; bbf_execute_grantstmt_as_dbsecadmin_hook = handle_grantstmt_for_dbsecadmin; + + prev_has_bbf_role_direct_membership_with_admin_true_hook = has_bbf_role_direct_membership_with_admin_true_hook; + has_bbf_role_direct_membership_with_admin_true_hook = has_bbf_role_direct_membership_with_admin_true; pltsql_get_object_identity_event_trigger_hook = pltsql_get_object_identity_event_trigger; @@ -594,6 +598,8 @@ UninstallExtendedHooks(void) pltsql_is_partitioned_table_reloptions_allowed_hook = prev_pltsql_is_partitioned_table_reloptions_allowed_hook; ExecFuncProc_AclCheck_hook = prev_ExecFuncProc_AclCheck_hook; bbf_execute_grantstmt_as_dbsecadmin_hook = prev_bbf_execute_grantstmt_as_dbsecadmin_hook; + has_bbf_role_direct_membership_with_admin_true_hook = prev_has_bbf_role_direct_membership_with_admin_true_hook; + bbf_InitializeParallelDSM_hook = NULL; bbf_ParallelWorkerMain_hook = NULL;