@@ -87,10 +87,10 @@ int Password_encryption = PASSWORD_TYPE_SCRAM_SHA_256;
87
87
/* Hook to check passwords in CreateRole() and AlterRole() */
88
88
check_password_hook_type check_password_hook = NULL ;
89
89
90
- static void AddRoleMems (const char * rolename , Oid roleid ,
90
+ static void AddRoleMems (Oid currentUserId , const char * rolename , Oid roleid ,
91
91
List * memberSpecs , List * memberIds ,
92
92
Oid grantorId , GrantRoleOptions * popt );
93
- static void DelRoleMems (const char * rolename , Oid roleid ,
93
+ static void DelRoleMems (Oid currentUserId , const char * rolename , Oid roleid ,
94
94
List * memberSpecs , List * memberIds ,
95
95
Oid grantorId , GrantRoleOptions * popt ,
96
96
DropBehavior behavior );
@@ -133,6 +133,7 @@ CreateRole(ParseState *pstate, CreateRoleStmt *stmt)
133
133
HeapTuple tuple ;
134
134
Datum new_record [Natts_pg_authid ] = {0 };
135
135
bool new_record_nulls [Natts_pg_authid ] = {0 };
136
+ Oid currentUserId = GetUserId ();
136
137
Oid roleid ;
137
138
ListCell * item ;
138
139
ListCell * option ;
@@ -508,8 +509,8 @@ CreateRole(ParseState *pstate, CreateRoleStmt *stmt)
508
509
char * oldrolename = NameStr (oldroleform -> rolname );
509
510
510
511
/* can only add this role to roles for which you have rights */
511
- check_role_membership_authorization (GetUserId () , oldroleid , true);
512
- AddRoleMems (oldrolename , oldroleid ,
512
+ check_role_membership_authorization (currentUserId , oldroleid , true);
513
+ AddRoleMems (currentUserId , oldrolename , oldroleid ,
513
514
thisrole_list ,
514
515
thisrole_oidlist ,
515
516
InvalidOid , & popt );
@@ -525,12 +526,12 @@ CreateRole(ParseState *pstate, CreateRoleStmt *stmt)
525
526
* NB: No permissions check is required here. If you have enough rights
526
527
* to create a role, you can add any members you like.
527
528
*/
528
- AddRoleMems (stmt -> role , roleid ,
529
+ AddRoleMems (currentUserId , stmt -> role , roleid ,
529
530
rolemembers , roleSpecsToIds (rolemembers ),
530
531
InvalidOid , & popt );
531
532
popt .specified |= GRANT_ROLE_SPECIFIED_ADMIN ;
532
533
popt .admin = true;
533
- AddRoleMems (stmt -> role , roleid ,
534
+ AddRoleMems (currentUserId , stmt -> role , roleid ,
534
535
adminmembers , roleSpecsToIds (adminmembers ),
535
536
InvalidOid , & popt );
536
537
@@ -583,6 +584,7 @@ AlterRole(ParseState *pstate, AlterRoleStmt *stmt)
583
584
DefElem * dvalidUntil = NULL ;
584
585
DefElem * dbypassRLS = NULL ;
585
586
Oid roleid ;
587
+ Oid currentUserId = GetUserId ();
586
588
GrantRoleOptions popt ;
587
589
588
590
check_rolespec_name (stmt -> role ,
@@ -727,13 +729,13 @@ AlterRole(ParseState *pstate, AlterRoleStmt *stmt)
727
729
errmsg ("permission denied" )));
728
730
729
731
/* without CREATEROLE, can only change your own password */
730
- if (dpassword && roleid != GetUserId () )
732
+ if (dpassword && roleid != currentUserId )
731
733
ereport (ERROR ,
732
734
(errcode (ERRCODE_INSUFFICIENT_PRIVILEGE ),
733
735
errmsg ("must have CREATEROLE privilege to change another user's password" )));
734
736
735
737
/* without CREATEROLE, can only add members to roles you admin */
736
- if (drolemembers && !is_admin_of_role (GetUserId () , roleid ))
738
+ if (drolemembers && !is_admin_of_role (currentUserId , roleid ))
737
739
ereport (ERROR ,
738
740
(errcode (ERRCODE_INSUFFICIENT_PRIVILEGE ),
739
741
errmsg ("must have admin option on role \"%s\" to add members" ,
@@ -888,11 +890,11 @@ AlterRole(ParseState *pstate, AlterRoleStmt *stmt)
888
890
CommandCounterIncrement ();
889
891
890
892
if (stmt -> action == +1 ) /* add members to role */
891
- AddRoleMems (rolename , roleid ,
893
+ AddRoleMems (currentUserId , rolename , roleid ,
892
894
rolemembers , roleSpecsToIds (rolemembers ),
893
895
InvalidOid , & popt );
894
896
else if (stmt -> action == -1 ) /* drop members from role */
895
- DelRoleMems (rolename , roleid ,
897
+ DelRoleMems (currentUserId , rolename , roleid ,
896
898
rolemembers , roleSpecsToIds (rolemembers ),
897
899
InvalidOid , & popt , DROP_RESTRICT );
898
900
}
@@ -1378,6 +1380,7 @@ GrantRole(ParseState *pstate, GrantRoleStmt *stmt)
1378
1380
List * grantee_ids ;
1379
1381
ListCell * item ;
1380
1382
GrantRoleOptions popt ;
1383
+ Oid currentUserId = GetUserId ();
1381
1384
1382
1385
/* Parse options list. */
1383
1386
InitGrantRoleOptions (& popt );
@@ -1449,14 +1452,14 @@ GrantRole(ParseState *pstate, GrantRoleStmt *stmt)
1449
1452
errmsg ("column names cannot be included in GRANT/REVOKE ROLE" )));
1450
1453
1451
1454
roleid = get_role_oid (rolename , false);
1452
- check_role_membership_authorization (GetUserId (), roleid ,
1453
- stmt -> is_grant );
1455
+ check_role_membership_authorization (currentUserId ,
1456
+ roleid , stmt -> is_grant );
1454
1457
if (stmt -> is_grant )
1455
- AddRoleMems (rolename , roleid ,
1458
+ AddRoleMems (currentUserId , rolename , roleid ,
1456
1459
stmt -> grantee_roles , grantee_ids ,
1457
1460
grantor , & popt );
1458
1461
else
1459
- DelRoleMems (rolename , roleid ,
1462
+ DelRoleMems (currentUserId , rolename , roleid ,
1460
1463
stmt -> grantee_roles , grantee_ids ,
1461
1464
grantor , & popt , stmt -> behavior );
1462
1465
}
@@ -1555,23 +1558,24 @@ roleSpecsToIds(List *memberNames)
1555
1558
/*
1556
1559
* AddRoleMems -- Add given members to the specified role
1557
1560
*
1561
+ * currentUserId: OID of role performing the operation
1558
1562
* rolename: name of role to add to (used only for error messages)
1559
1563
* roleid: OID of role to add to
1560
1564
* memberSpecs: list of RoleSpec of roles to add (used only for error messages)
1561
1565
* memberIds: OIDs of roles to add
1562
- * grantorId: who is granting the membership (InvalidOid if not set explicitly)
1566
+ * grantorId: OID that should be recorded as having granted the membership
1567
+ * (InvalidOid if not set explicitly)
1563
1568
* popt: information about grant options
1564
1569
*/
1565
1570
static void
1566
- AddRoleMems (const char * rolename , Oid roleid ,
1571
+ AddRoleMems (Oid currentUserId , const char * rolename , Oid roleid ,
1567
1572
List * memberSpecs , List * memberIds ,
1568
1573
Oid grantorId , GrantRoleOptions * popt )
1569
1574
{
1570
1575
Relation pg_authmem_rel ;
1571
1576
TupleDesc pg_authmem_dsc ;
1572
1577
ListCell * specitem ;
1573
1578
ListCell * iditem ;
1574
- Oid currentUserId = GetUserId ();
1575
1579
1576
1580
Assert (list_length (memberSpecs ) == list_length (memberIds ));
1577
1581
@@ -1859,15 +1863,14 @@ AddRoleMems(const char *rolename, Oid roleid,
1859
1863
* behavior: RESTRICT or CASCADE behavior for recursive removal
1860
1864
*/
1861
1865
static void
1862
- DelRoleMems (const char * rolename , Oid roleid ,
1866
+ DelRoleMems (Oid currentUserId , const char * rolename , Oid roleid ,
1863
1867
List * memberSpecs , List * memberIds ,
1864
1868
Oid grantorId , GrantRoleOptions * popt , DropBehavior behavior )
1865
1869
{
1866
1870
Relation pg_authmem_rel ;
1867
1871
TupleDesc pg_authmem_dsc ;
1868
1872
ListCell * specitem ;
1869
1873
ListCell * iditem ;
1870
- Oid currentUserId = GetUserId ();
1871
1874
CatCList * memlist ;
1872
1875
RevokeRoleGrantAction * actions ;
1873
1876
int i ;
0 commit comments