Skip to content

Commit

Permalink
Replaced physical rolename with logical rolename in error message
Browse files Browse the repository at this point in the history
Signed-off-by: ANJU BHARTI <[email protected]>
  • Loading branch information
ANJU BHARTI committed Aug 16, 2024
1 parent 799df76 commit 675f72e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
50 changes: 25 additions & 25 deletions contrib/babelfishpg_tsql/src/pl_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -3286,26 +3286,45 @@ bbf_ProcessUtility(PlannedStmt *pstmt,
RoleSpec *rolspec = lfirst(item);
char *user_name;
char *db_principal;
const char *db_owner_name;
int role_oid;
int rolename_len;
char *logical_role_name = NULL;

user_name = get_physical_user_name(db_name, rolspec->rolename, false);
role_name = rolspec->rolename;
db_owner_name = get_db_owner_name(get_cur_db_name());
role_oid = get_role_oid(user_name, true);
logical_role_name = rolspec->rolename;
rolename_len = strlen(logical_role_name);

if (drop_user)
db_principal = "user";
else
db_principal = "role";

/* If user is dbo or role is db_owner, restrict dropping */
if ((drop_user && strncmp(role_name, "dbo", 3) == 0) || (drop_role && strncmp(role_name, "db_owner", 8) == 0))
if ((drop_user && strncmp(logical_role_name, "dbo", rolename_len) == 0) ||
(drop_role && strncmp(logical_role_name, "db_owner", rolename_len) == 0))
ereport(ERROR,
(errcode(ERRCODE_CHECK_VIOLATION),
errmsg("Cannot drop the %s '%s'.", db_principal, role_name)));
errmsg("Cannot drop the %s '%s'.", db_principal, logical_role_name)));

/*
* Check for current_user's privileges
* must be database owner to drop user/role
*/
if ((!stmt->missing_ok && !OidIsValid(role_oid)) ||
!is_member_of_role(GetUserId(), get_role_oid(db_owner_name, false)))
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("Cannot drop the %s '%s', because it does not exist or you do not have permission.", db_principal, logical_role_name)));

/*
* If a role has members, do not drop it.
* Note that here we don't handle invalid
* roles.
*/
if (drop_role && !is_empty_role(get_role_oid(user_name, true)))
if (drop_role && !is_empty_role(role_oid))
ereport(ERROR,
(errcode(ERRCODE_CHECK_VIOLATION),
errmsg("The role has members. It must be empty before it can be dropped.")));
Expand All @@ -3318,7 +3337,7 @@ bbf_ProcessUtility(PlannedStmt *pstmt,
* if enabled. 3. Otherwise throw an
* error.
*/
if (drop_user && strcmp(rolspec->rolename, "guest") == 0)
if (drop_user && strcmp(logical_role_name, "guest") == 0)
{
if (guest_has_dbaccess(db_name))
{
Expand All @@ -3327,7 +3346,7 @@ bbf_ProcessUtility(PlannedStmt *pstmt,
(errcode(ERRCODE_CHECK_VIOLATION),
errmsg("Cannot disable access to the guest user in master or tempdb.")));

alter_user_can_connect(false, rolspec->rolename, db_name);
alter_user_can_connect(false, logical_role_name, db_name);
return;
}
else
Expand Down Expand Up @@ -3442,25 +3461,6 @@ bbf_ProcessUtility(PlannedStmt *pstmt,
(errcode(ERRCODE_OBJECT_IN_USE),
errmsg("Could not drop login '%s' as the user is currently logged in.", role_name)));
}
/* If user/role, check for current_user's privileges */
else if (drop_user || drop_role)
{
const char *db_owner_name;
char *db_principal;
int role_oid = get_role_oid(role_name, true);

if (drop_user)
db_principal = "user";
else
db_principal = "role";

/* must be database owner to drop user/role */
db_owner_name = get_db_owner_name(get_cur_db_name());
if ((!stmt->missing_ok && !OidIsValid(role_oid)) || !is_member_of_role(GetUserId(), get_role_oid(db_owner_name, false)))
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("Cannot drop the %s '%s', because it does not exist or you do not have permission.", db_principal, role_name)));
}

/*
* We have performed all the permissions checks.
Expand Down
2 changes: 1 addition & 1 deletion test/JDBC/expected/BABEL-3844-vu-cleanup.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ drop user test_user;
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Cannot drop the user 'master_test_user', because it does not exist or you do not have permission.)~~
~~ERROR (Message: Cannot drop the user 'test_user', because it does not exist or you do not have permission.)~~


drop login [babel\aduser1];
Expand Down
12 changes: 6 additions & 6 deletions test/JDBC/expected/restrict_drop_user_role.out
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ drop role dont_drop_role
go
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Cannot drop the role 'master_dont_drop_role', because it does not exist or you do not have permission.)~~
~~ERROR (Message: Cannot drop the role 'dont_drop_role', because it does not exist or you do not have permission.)~~


drop user no_priv_user2
go
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Cannot drop the user 'master_no_priv_user2', because it does not exist or you do not have permission.)~~
~~ERROR (Message: Cannot drop the user 'no_priv_user2', because it does not exist or you do not have permission.)~~


-- tsql
Expand Down Expand Up @@ -81,14 +81,14 @@ drop role dont_drop_role
go
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Cannot drop the role 'master_dont_drop_role', because it does not exist or you do not have permission.)~~
~~ERROR (Message: Cannot drop the role 'dont_drop_role', because it does not exist or you do not have permission.)~~


drop user no_priv_user2
go
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Cannot drop the user 'master_no_priv_user2', because it does not exist or you do not have permission.)~~
~~ERROR (Message: Cannot drop the user 'no_priv_user2', because it does not exist or you do not have permission.)~~


-- tsql
Expand Down Expand Up @@ -120,14 +120,14 @@ drop role fake_role
go
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Cannot drop the role 'restrict_user_db1_fake_role', because it does not exist or you do not have permission.)~~
~~ERROR (Message: Cannot drop the role 'fake_role', because it does not exist or you do not have permission.)~~


drop user fake_user
go
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Cannot drop the user 'restrict_user_db1_fake_user', because it does not exist or you do not have permission.)~~
~~ERROR (Message: Cannot drop the user 'fake_user', because it does not exist or you do not have permission.)~~


-- should deny
Expand Down

0 comments on commit 675f72e

Please sign in to comment.