@@ -501,20 +501,12 @@ AlterRole(ParseState *pstate, AlterRoleStmt *stmt)
501
501
new_tuple ;
502
502
Form_pg_authid authform ;
503
503
ListCell * option ;
504
- char * rolename = NULL ;
504
+ char * rolename ;
505
505
char * password = NULL ; /* user password */
506
- int issuper = -1 ; /* Make the user a superuser? */
507
- int inherit = -1 ; /* Auto inherit privileges? */
508
- int createrole = -1 ; /* Can this user create roles? */
509
- int createdb = -1 ; /* Can the user create databases? */
510
- int canlogin = -1 ; /* Can this user login? */
511
- int isreplication = -1 ; /* Is this a replication role? */
512
506
int connlimit = -1 ; /* maximum connections allowed */
513
- List * rolemembers = NIL ; /* roles to be added/removed */
514
507
char * validUntil = NULL ; /* time the login is valid until */
515
508
Datum validUntil_datum ; /* same, as timestamptz Datum */
516
509
bool validUntil_null ;
517
- int bypassrls = -1 ;
518
510
DefElem * dpassword = NULL ;
519
511
DefElem * dissuper = NULL ;
520
512
DefElem * dinherit = NULL ;
@@ -610,18 +602,6 @@ AlterRole(ParseState *pstate, AlterRoleStmt *stmt)
610
602
611
603
if (dpassword && dpassword -> arg )
612
604
password = strVal (dpassword -> arg );
613
- if (dissuper )
614
- issuper = intVal (dissuper -> arg );
615
- if (dinherit )
616
- inherit = intVal (dinherit -> arg );
617
- if (dcreaterole )
618
- createrole = intVal (dcreaterole -> arg );
619
- if (dcreatedb )
620
- createdb = intVal (dcreatedb -> arg );
621
- if (dcanlogin )
622
- canlogin = intVal (dcanlogin -> arg );
623
- if (disreplication )
624
- isreplication = intVal (disreplication -> arg );
625
605
if (dconnlimit )
626
606
{
627
607
connlimit = intVal (dconnlimit -> arg );
@@ -630,12 +610,8 @@ AlterRole(ParseState *pstate, AlterRoleStmt *stmt)
630
610
(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
631
611
errmsg ("invalid connection limit: %d" , connlimit )));
632
612
}
633
- if (drolemembers )
634
- rolemembers = (List * ) drolemembers -> arg ;
635
613
if (dvalidUntil )
636
614
validUntil = strVal (dvalidUntil -> arg );
637
- if (dbypassRLS )
638
- bypassrls = intVal (dbypassRLS -> arg );
639
615
640
616
/*
641
617
* Scan the pg_authid relation to be certain the user exists.
@@ -654,21 +630,21 @@ AlterRole(ParseState *pstate, AlterRoleStmt *stmt)
654
630
* property. Otherwise, if you don't have createrole, you're only allowed
655
631
* to change your own password.
656
632
*/
657
- if (authform -> rolsuper || issuper >= 0 )
633
+ if (authform -> rolsuper || dissuper )
658
634
{
659
635
if (!superuser ())
660
636
ereport (ERROR ,
661
637
(errcode (ERRCODE_INSUFFICIENT_PRIVILEGE ),
662
638
errmsg ("must be superuser to alter superuser roles or change superuser attribute" )));
663
639
}
664
- else if (authform -> rolreplication || isreplication >= 0 )
640
+ else if (authform -> rolreplication || disreplication )
665
641
{
666
642
if (!superuser ())
667
643
ereport (ERROR ,
668
644
(errcode (ERRCODE_INSUFFICIENT_PRIVILEGE ),
669
645
errmsg ("must be superuser to alter replication roles or change replication attribute" )));
670
646
}
671
- else if (bypassrls >= 0 )
647
+ else if (dbypassRLS )
672
648
{
673
649
if (!superuser ())
674
650
ereport (ERROR ,
@@ -677,23 +653,16 @@ AlterRole(ParseState *pstate, AlterRoleStmt *stmt)
677
653
}
678
654
else if (!have_createrole_privilege ())
679
655
{
680
- /* We already checked issuper, isreplication, and bypassrls */
681
- if (!(inherit < 0 &&
682
- createrole < 0 &&
683
- createdb < 0 &&
684
- canlogin < 0 &&
685
- !dconnlimit &&
686
- !rolemembers &&
687
- !validUntil &&
688
- dpassword &&
689
- roleid == GetUserId ()))
656
+ /* check the rest */
657
+ if (dinherit || dcreaterole || dcreatedb || dcanlogin || dconnlimit ||
658
+ drolemembers || dvalidUntil || !dpassword || roleid != GetUserId ())
690
659
ereport (ERROR ,
691
660
(errcode (ERRCODE_INSUFFICIENT_PRIVILEGE ),
692
661
errmsg ("permission denied" )));
693
662
}
694
663
695
664
/* Convert validuntil to internal form */
696
- if (validUntil )
665
+ if (dvalidUntil )
697
666
{
698
667
validUntil_datum = DirectFunctionCall3 (timestamptz_in ,
699
668
CStringGetDatum (validUntil ),
@@ -729,39 +698,39 @@ AlterRole(ParseState *pstate, AlterRoleStmt *stmt)
729
698
/*
730
699
* issuper/createrole/etc
731
700
*/
732
- if (issuper >= 0 )
701
+ if (dissuper )
733
702
{
734
- new_record [Anum_pg_authid_rolsuper - 1 ] = BoolGetDatum (issuper > 0 );
703
+ new_record [Anum_pg_authid_rolsuper - 1 ] = BoolGetDatum (intVal ( dissuper -> arg ) );
735
704
new_record_repl [Anum_pg_authid_rolsuper - 1 ] = true;
736
705
}
737
706
738
- if (inherit >= 0 )
707
+ if (dinherit )
739
708
{
740
- new_record [Anum_pg_authid_rolinherit - 1 ] = BoolGetDatum (inherit > 0 );
709
+ new_record [Anum_pg_authid_rolinherit - 1 ] = BoolGetDatum (intVal ( dinherit -> arg ) );
741
710
new_record_repl [Anum_pg_authid_rolinherit - 1 ] = true;
742
711
}
743
712
744
- if (createrole >= 0 )
713
+ if (dcreaterole )
745
714
{
746
- new_record [Anum_pg_authid_rolcreaterole - 1 ] = BoolGetDatum (createrole > 0 );
715
+ new_record [Anum_pg_authid_rolcreaterole - 1 ] = BoolGetDatum (intVal ( dcreaterole -> arg ) );
747
716
new_record_repl [Anum_pg_authid_rolcreaterole - 1 ] = true;
748
717
}
749
718
750
- if (createdb >= 0 )
719
+ if (dcreatedb )
751
720
{
752
- new_record [Anum_pg_authid_rolcreatedb - 1 ] = BoolGetDatum (createdb > 0 );
721
+ new_record [Anum_pg_authid_rolcreatedb - 1 ] = BoolGetDatum (intVal ( dcreatedb -> arg ) );
753
722
new_record_repl [Anum_pg_authid_rolcreatedb - 1 ] = true;
754
723
}
755
724
756
- if (canlogin >= 0 )
725
+ if (dcanlogin )
757
726
{
758
- new_record [Anum_pg_authid_rolcanlogin - 1 ] = BoolGetDatum (canlogin > 0 );
727
+ new_record [Anum_pg_authid_rolcanlogin - 1 ] = BoolGetDatum (intVal ( dcanlogin -> arg ) );
759
728
new_record_repl [Anum_pg_authid_rolcanlogin - 1 ] = true;
760
729
}
761
730
762
- if (isreplication >= 0 )
731
+ if (disreplication )
763
732
{
764
- new_record [Anum_pg_authid_rolreplication - 1 ] = BoolGetDatum (isreplication > 0 );
733
+ new_record [Anum_pg_authid_rolreplication - 1 ] = BoolGetDatum (intVal ( disreplication -> arg ) );
765
734
new_record_repl [Anum_pg_authid_rolreplication - 1 ] = true;
766
735
}
767
736
@@ -808,9 +777,9 @@ AlterRole(ParseState *pstate, AlterRoleStmt *stmt)
808
777
new_record_nulls [Anum_pg_authid_rolvaliduntil - 1 ] = validUntil_null ;
809
778
new_record_repl [Anum_pg_authid_rolvaliduntil - 1 ] = true;
810
779
811
- if (bypassrls >= 0 )
780
+ if (dbypassRLS )
812
781
{
813
- new_record [Anum_pg_authid_rolbypassrls - 1 ] = BoolGetDatum (bypassrls > 0 );
782
+ new_record [Anum_pg_authid_rolbypassrls - 1 ] = BoolGetDatum (intVal ( dbypassRLS -> arg ) );
814
783
new_record_repl [Anum_pg_authid_rolbypassrls - 1 ] = true;
815
784
}
816
785
@@ -827,17 +796,21 @@ AlterRole(ParseState *pstate, AlterRoleStmt *stmt)
827
796
* Advance command counter so we can see new record; else tests in
828
797
* AddRoleMems may fail.
829
798
*/
830
- if (rolemembers )
799
+ if (drolemembers )
800
+ {
801
+ List * rolemembers = (List * ) drolemembers -> arg ;
802
+
831
803
CommandCounterIncrement ();
832
804
833
- if (stmt -> action == +1 ) /* add members to role */
834
- AddRoleMems (rolename , roleid ,
835
- rolemembers , roleSpecsToIds (rolemembers ),
836
- GetUserId (), false);
837
- else if (stmt -> action == -1 ) /* drop members from role */
838
- DelRoleMems (rolename , roleid ,
839
- rolemembers , roleSpecsToIds (rolemembers ),
840
- false);
805
+ if (stmt -> action == +1 ) /* add members to role */
806
+ AddRoleMems (rolename , roleid ,
807
+ rolemembers , roleSpecsToIds (rolemembers ),
808
+ GetUserId (), false);
809
+ else if (stmt -> action == -1 ) /* drop members from role */
810
+ DelRoleMems (rolename , roleid ,
811
+ rolemembers , roleSpecsToIds (rolemembers ),
812
+ false);
813
+ }
841
814
842
815
/*
843
816
* Close pg_authid, but keep lock till commit.
0 commit comments