Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RoleTrait.php « databasetraits « database « src - github.com/HuasoFoundries/phpPgAdmin6.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d3203c0a325d496601c775925d71b68736c6b7fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
<?php

/**
 * PHPPgAdmin 6.1.3
 */

namespace PHPPgAdmin\Database\Traits;

/**
 * Common trait for roles and users manipulation.
 */
trait RoleTrait
{
    /**
     * Returns all roles in the database cluster.
     *
     * @param string $rolename (optional) The role name to exclude from the select
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function getRoles($rolename = '')
    {
        $sql = '
			SELECT
                r.rolname,
                r1.rolname as group,
                r.rolsuper,
                r.rolcreatedb,
                r.rolcreaterole,
                r.rolinherit,
                r.rolcanlogin,
                r.rolconnlimit,
                r.rolvaliduntil,
                r.rolconfig
            FROM pg_catalog.pg_roles r
            LEFT JOIN pg_catalog.pg_auth_members m ON (m.member = r.oid)
            LEFT JOIN pg_roles r1 ON (m.roleid=r1.oid)
            ';

        if ($rolename) {
            $sql .= " WHERE r.rolname!='{$rolename}'";
        }

        $sql .= ' ORDER BY r.rolname';

        return $this->selectSet($sql);
    }

    /**
     * Returns information about a single role.
     *
     * @param string $rolename The name of the role to retrieve
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function getRole($rolename)
    {
        $this->clean($rolename);

        $sql = "
            SELECT
                r.rolname,
                r1.rolname as group,
                r.rolsuper,
                r.rolcreatedb,
                r.rolcreaterole,
                r.rolinherit,
                r.rolcanlogin,
                r.rolconnlimit,
                r.rolvaliduntil,
                r.rolconfig
            FROM pg_catalog.pg_roles r
            LEFT JOIN pg_catalog.pg_auth_members m ON (m.member = r.oid)
            LEFT JOIN pg_roles r1 ON (m.roleid=r1.oid)
            WHERE r.rolname='{$rolename}'";

        return $this->selectSet($sql);
    }

    /**
     * Returns all users in the database cluster.
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function getUsers()
    {
        $sql = 'SELECT
                r.usename,
                r1.rolname as group,
                r.usesuper,
                r.valuntil AS useexpires,
                r.useconfig
            FROM pg_catalog.pg_user r
            LEFT JOIN pg_catalog.pg_auth_members m ON (m.member = r.usesysid)
            LEFT JOIN pg_roles r1 ON (m.roleid=r1.oid)';

        return $this->selectSet($sql);
    }

    /**
     * Returns information about a single user.
     *
     * @param string $username The username of the user to retrieve
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function getUser($username)
    {
        $this->clean($username);

        $sql = "SELECT
                r.usename,
                r1.rolname as group,
                r.usesuper,
                r.valuntil AS useexpires,
                r.useconfig
            FROM pg_catalog.pg_user r
            LEFT JOIN pg_catalog.pg_auth_members m ON (m.member = r.usesysid)
            LEFT JOIN pg_roles r1 ON (m.roleid=r1.oid)
			WHERE r.usename='{$username}'";

        return $this->selectSet($sql);
    }

    /**
     * Creates a new role.
     *
     * @param string $rolename            The name of the role to create
     * @param string $password            A password for the role
     * @param bool   $superuser           Boolean whether or not the role is a superuser
     * @param bool   $createdb            Boolean whether or not the role can create databases
     * @param bool   $createrole          Boolean whether or not the role can create other roles
     * @param bool   $inherits            Boolean whether or not the role inherits the privileges from parent roles
     * @param bool   $login               Boolean whether or not the role will be allowed to login
     * @param int    $connlimit           Number of concurrent connections the role can make
     * @param string $expiry              String Format 'YYYY-MM-DD HH:MM:SS'.  '' means never expire
     * @param array  $new_roles_to_add    (array) Roles to which the new role will be immediately added as a new member
     * @param array  $new_members_of_role (array) Roles which are automatically added as members of the new role
     * @param array  $new_admins_of_role  (array) Roles which are automatically added as admin members of the new role
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function createRole(
        $rolename,
        $password,
        $superuser,
        $createdb,
        $createrole,
        $inherits,
        $login,
        $connlimit,
        $expiry,
        $new_roles_to_add,
        $new_members_of_role,
        $new_admins_of_role
    ) {
        $enc = $this->_encryptPassword($rolename, $password);
        $this->fieldClean($rolename);
        $this->clean($enc);
        $this->clean($connlimit);
        $this->clean($expiry);
        $this->fieldArrayClean($new_roles_to_add);
        $this->fieldArrayClean($new_members_of_role);
        $this->fieldArrayClean($new_admins_of_role);

        $sql = "CREATE ROLE \"{$rolename}\"";

        if ('' !== $password) {
            $sql .= " WITH ENCRYPTED PASSWORD '{$enc}'";
        }

        $sql .= $superuser ? ' SUPERUSER' : ' NOSUPERUSER';
        $sql .= $createdb ? ' CREATEDB' : ' NOCREATEDB';
        $sql .= $createrole ? ' CREATEROLE' : ' NOCREATEROLE';
        $sql .= $inherits ? ' INHERIT' : ' NOINHERIT';
        $sql .= $login ? ' LOGIN' : ' NOLOGIN';

        if ('' !== $connlimit) {
            $sql .= " CONNECTION LIMIT {$connlimit}";
        } else {
            $sql .= ' CONNECTION LIMIT -1';
        }

        if ('' !== $expiry) {
            $sql .= " VALID UNTIL '{$expiry}'";
        } else {
            $sql .= " VALID UNTIL 'infinity'";
        }

        if (\is_array($new_roles_to_add) && 0 < \count($new_roles_to_add)) {
            $sql .= ' IN ROLE "' . \implode('", "', $new_roles_to_add) . '"';
        }

        if (\is_array($new_members_of_role) && 0 < \count($new_members_of_role)) {
            $sql .= ' ROLE "' . \implode('", "', $new_members_of_role) . '"';
        }

        if (\is_array($new_admins_of_role) && 0 < \count($new_admins_of_role)) {
            $sql .= ' ADMIN "' . \implode('", "', $new_admins_of_role) . '"';
        }

        return $this->execute($sql);
    }

    /**
     * Helper function that computes encypted PostgreSQL passwords.
     *
     * @param string $username The username
     * @param string $password The password
     *
     * @return string
     */
    public function _encryptPassword($username, $password)
    {
        return 'md5' . \md5($password . $username);
    }

    /**
     * Adjusts a role's info and renames it.
     *
     * @param string $rolename              The name of the role to adjust
     * @param string $password              A password for the role
     * @param bool   $superuser             Boolean whether or not the role is a superuser
     * @param bool   $createdb              Boolean whether or not the role can create databases
     * @param bool   $createrole            Boolean whether or not the role can create other roles
     * @param bool   $inherits              Boolean whether or not the role inherits the privileges from parent roles
     * @param bool   $login                 Boolean whether or not the role will be allowed to login
     * @param int    $connlimit             Number of concurrent connections the role can make
     * @param string $expiry                string Format 'YYYY-MM-DD HH:MM:SS'.  '' means never expire
     * @param array  $new_roles_to_add      (array) Roles to which the role will be immediately added as a new member
     * @param array  $new_members_of_role   (array) Roles which are automatically added as members of the role
     * @param array  $new_admins_of_role    (array) Roles which are automatically added as admin members of the role
     * @param string $original_parent_roles Original roles whose the role belongs to, comma separated
     * @param string $original_members      Original roles that are members of the role, comma separated
     * @param string $original_admins       Original roles that are admin members of the role, comma separated
     * @param string $newrolename           The new name of the role
     *
     * @return bool|int 0 success
     */
    public function setRenameRole(
        $rolename,
        $password,
        $superuser,
        $createdb,
        $createrole,
        $inherits,
        $login,
        $connlimit,
        $expiry,
        $new_roles_to_add,
        $new_members_of_role,
        $new_admins_of_role,
        $original_parent_roles,
        $original_members,
        $original_admins,
        $newrolename
    ) {
        $status = $this->beginTransaction();

        if (0 !== $status) {
            return -1;
        }

        if ($rolename !== $newrolename) {
            $status = $this->renameRole($rolename, $newrolename);

            if (0 !== $status) {
                $this->rollbackTransaction();

                return -3;
            }
            $rolename = $newrolename;
        }

        $status = $this->setRole(
            $rolename,
            $password,
            $superuser,
            $createdb,
            $createrole,
            $inherits,
            $login,
            $connlimit,
            $expiry,
            $new_roles_to_add,
            $new_members_of_role,
            $new_admins_of_role,
            $original_parent_roles,
            $original_members,
            $original_admins
        );

        if (0 !== $status) {
            $this->rollbackTransaction();

            return -2;
        }

        return $this->endTransaction();
    }

    /**
     * Renames a role.
     *
     * @param string $rolename    The name of the role to rename
     * @param string $newrolename The new name of the role
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function renameRole($rolename, $newrolename)
    {
        $this->fieldClean($rolename);
        $this->fieldClean($newrolename);

        $sql = "ALTER ROLE \"{$rolename}\" RENAME TO \"{$newrolename}\"";

        return $this->execute($sql);
    }

    /**
     * Adjusts a role's info.
     *
     * @param string $rolename              The name of the role to adjust
     * @param string $password              A password for the role
     * @param bool   $superuser             Boolean whether or not the role is a superuser
     * @param bool   $createdb              Boolean whether or not the role can create databases
     * @param bool   $createrole            Boolean whether or not the role can create other roles
     * @param bool   $inherits              Boolean whether or not the role inherits the privileges from parent roles
     * @param bool   $login                 Boolean whether or not the role will be allowed to login
     * @param int    $connlimit             Number of concurrent connections the role can make
     * @param string $expiry                string Format 'YYYY-MM-DD HH:MM:SS'.  '' means never expire
     * @param array  $new_roles_to_add      (array) Roles to which the role will be immediately added as a new member
     * @param array  $new_members_of_role   (array) Roles which are automatically added as members of the role
     * @param array  $new_admins_of_role    (array) Roles which are automatically added as admin members of the role
     * @param string $original_parent_roles Original roles whose the role belongs to, comma separated
     * @param string $original_members      Original roles that are members of the role, comma separated
     * @param string $original_admins       Original roles that are admin members of the role, comma separated
     *
     * @return int 0 if operation was successful
     */
    public function setRole(
        $rolename,
        $password,
        $superuser,
        $createdb,
        $createrole,
        $inherits,
        $login,
        $connlimit,
        $expiry,
        $new_roles_to_add,
        $new_members_of_role,
        $new_admins_of_role,
        $original_parent_roles,
        $original_members,
        $original_admins
    ) {
        $this->fieldClean($rolename);

        $this->fieldArrayClean($new_roles_to_add);
        $this->fieldArrayClean($new_members_of_role);
        $this->fieldArrayClean($new_admins_of_role);

        $status = $this->_alterRole($rolename, $password, $connlimit, $expiry, $superuser, $createdb, $createrole, $inherits, $login);

        if (0 !== $status) {
            return -1;
        }

        // If there were existing users with the requested role,
        // assign their roles to the new user, and remove said
        // role from them if they are not among the new authorized members
        if ($original_parent_roles) {
            $status = $this->_dealWithOldParentRoles($original_parent_roles, $new_roles_to_add, $rolename);

            if (0 !== $status) {
                return -1;
            }
        }

        if ($original_members) {
            $status = $this->_dealWithOriginalMembers($original_members, $new_members_of_role, $rolename);

            if (0 !== $status) {
                return -1;
            }
        }

        if ($original_admins) {
            $status = $this->_dealWithOriginalAdmins($original_admins, $new_admins_of_role, $rolename);

            if (0 !== $status) {
                return -1;
            }
        }

        return $status;
    }

    /**
     * Grants membership in a role.
     *
     * @param string $role     The name of the target role
     * @param string $rolename The name of the role that will belong to the target role
     * @param int    $admin    (optional) Flag to grant the admin option
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function grantRole($role, $rolename, $admin = 0)
    {
        $this->fieldClean($role);
        $this->fieldClean($rolename);

        $sql = "GRANT \"{$role}\" TO \"{$rolename}\"";

        if (1 === $admin) {
            $sql .= ' WITH ADMIN OPTION';
        }

        return $this->execute($sql);
    }

    /**
     * Revokes membership in a role.
     *
     * @param string $role     The name of the target role
     * @param string $rolename The name of the role that will not belong to the target role
     * @param int    $admin    (optional) Flag to revoke only the admin option
     * @param string $type     (optional) Type of revoke: RESTRICT | CASCADE
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function revokeRole($role, $rolename, $admin = 0, $type = 'RESTRICT')
    {
        $this->fieldClean($role);
        $this->fieldClean($rolename);

        $sql = 'REVOKE ';

        if (1 === $admin) {
            $sql .= 'ADMIN OPTION FOR ';
        }

        $sql .= "\"{$role}\" FROM \"{$rolename}\" {$type}";

        return $this->execute($sql);
    }

    /**
     * Removes a role.
     *
     * @param string $rolename The name of the role to drop
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function dropRole($rolename)
    {
        $this->fieldClean($rolename);

        $sql = "DROP ROLE \"{$rolename}\"";

        return $this->execute($sql);
    }

    /**
     * Creates a new user.
     *
     * @param string $username   The username of the user to create
     * @param string $password   A password for the user
     * @param bool   $createdb   boolean Whether or not the user can create databases
     * @param bool   $createuser boolean Whether or not the user can create other users
     * @param string $expiry     string Format 'YYYY-MM-DD HH:MM:SS'.  '' means never expire
     * @param array  $groups     The groups to create the user in
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     *
     * @internal param $group (array) The groups to create the user in
     */
    public function createUser($username, $password, $createdb, $createuser, $expiry, $groups)
    {
        $enc = $this->_encryptPassword($username, $password);
        $this->fieldClean($username);
        $this->clean($enc);
        $this->clean($expiry);
        $this->fieldArrayClean($groups);

        $sql = "CREATE USER \"{$username}\"";

        if ('' !== $password) {
            $sql .= " WITH ENCRYPTED PASSWORD '{$enc}'";
        }

        $sql .= $createdb ? ' CREATEDB' : ' NOCREATEDB';
        $sql .= $createuser ? ' CREATEUSER' : ' NOCREATEUSER';

        if (\is_array($groups) && 0 < \count($groups)) {
            $sql .= ' IN GROUP "' . \implode('", "', $groups) . '"';
        }

        if ('' !== $expiry) {
            $sql .= " VALID UNTIL '{$expiry}'";
        } else {
            $sql .= " VALID UNTIL 'infinity'";
        }

        return $this->execute($sql);
    }

    /**
     * Adjusts a user's info and renames the user.
     *
     * @param string $username   The username of the user to modify
     * @param string $password   A new password for the user
     * @param bool   $createdb   boolean Whether or not the user can create databases
     * @param bool   $createuser boolean Whether or not the user can create other users
     * @param string $expiry     string Format 'YYYY-MM-DD HH:MM:SS'.  '' means never expire.
     * @param string $newname    The new name of the user
     *
     * @return bool|int 0 success
     */
    public function setRenameUser($username, $password, $createdb, $createuser, $expiry, $newname)
    {
        $status = $this->beginTransaction();

        if (0 !== $status) {
            return -1;
        }

        if ($username !== $newname) {
            $status = $this->renameUser($username, $newname);

            if (0 !== $status) {
                $this->rollbackTransaction();

                return -3;
            }
            $username = $newname;
        }

        $status = $this->setUser($username, $password, $createdb, $createuser, $expiry);

        if (0 !== $status) {
            $this->rollbackTransaction();

            return -2;
        }

        return $this->endTransaction();
    }

    /**
     * Renames a user.
     *
     * @param string $username The username of the user to rename
     * @param string $newname  The new name of the user
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function renameUser($username, $newname)
    {
        $this->fieldClean($username);
        $this->fieldClean($newname);

        $sql = "ALTER USER \"{$username}\" RENAME TO \"{$newname}\"";

        return $this->execute($sql);
    }

    // Tablespace functions

    /**
     * Adjusts a user's info.
     *
     * @param string $username   The username of the user to modify
     * @param string $password   A new password for the user
     * @param bool   $createdb   boolean Whether or not the user can create databases
     * @param bool   $createuser boolean Whether or not the user can create other users
     * @param string $expiry     string Format 'YYYY-MM-DD HH:MM:SS'.  '' means never expire.
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function setUser($username, $password, $createdb, $createuser, $expiry)
    {
        $enc = $this->_encryptPassword($username, $password);
        $this->fieldClean($username);
        $this->clean($enc);
        $this->clean($expiry);

        $sql = "ALTER USER \"{$username}\"";

        if ('' !== $password) {
            $sql .= " WITH ENCRYPTED PASSWORD '{$enc}'";
        }

        $sql .= $createdb ? ' CREATEDB' : ' NOCREATEDB';
        $sql .= $createuser ? ' CREATEUSER' : ' NOCREATEUSER';

        if ('' !== $expiry) {
            $sql .= " VALID UNTIL '{$expiry}'";
        } else {
            $sql .= " VALID UNTIL 'infinity'";
        }

        return $this->execute($sql);
    }

    /**
     * Removes a user.
     *
     * @param string $username The username of the user to drop
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function dropUser($username)
    {
        $this->fieldClean($username);

        $sql = "DROP USER \"{$username}\"";

        return $this->execute($sql);
    }

    /**
     * Changes a role's password.
     *
     * @param string $rolename The role name
     * @param string $password The new password
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function changePassword($rolename, $password)
    {
        $enc = $this->_encryptPassword($rolename, $password);
        $this->fieldClean($rolename);
        $this->clean($enc);

        $sql = "ALTER ROLE \"{$rolename}\" WITH ENCRYPTED PASSWORD '{$enc}'";

        return $this->execute($sql);
    }

    /**
     * Adds a group member.
     *
     * @param string $groname The name of the group
     * @param string $user    The name of the user to add to the group
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function addGroupMember($groname, $user)
    {
        $this->fieldClean($groname);
        $this->fieldClean($user);

        $sql = "ALTER GROUP \"{$groname}\" ADD USER \"{$user}\"";

        return $this->execute($sql);
    }

    /**
     * Returns all role names which the role belongs to.
     *
     * @param string $rolename The role name
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function getMemberOf($rolename)
    {
        $this->clean($rolename);

        $sql = "
			SELECT rolname FROM pg_catalog.pg_roles R, pg_auth_members M
			WHERE R.oid=M.roleid
				AND member IN (
					SELECT oid FROM pg_catalog.pg_roles
					WHERE rolname='{$rolename}')
			ORDER BY rolname";

        return $this->selectSet($sql);
    }

    // Administration functions

    /**
     * Returns all role names that are members of a role.
     *
     * @param string $rolename The role name
     * @param string $admin    (optional) Find only admin members
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function getMembers($rolename, $admin = 'f')
    {
        $this->clean($rolename);

        $sql = "
			SELECT rolname FROM pg_catalog.pg_roles R, pg_auth_members M
			WHERE R.oid=M.member AND admin_option='{$admin}'
				AND roleid IN (SELECT oid FROM pg_catalog.pg_roles
					WHERE rolname='{$rolename}')
			ORDER BY rolname";

        return $this->selectSet($sql);
    }

    /**
     * Removes a group member.
     *
     * @param string $groname The name of the group
     * @param string $user    The name of the user to remove from the group
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function dropGroupMember($groname, $user)
    {
        $this->fieldClean($groname);
        $this->fieldClean($user);

        $sql = "ALTER GROUP \"{$groname}\" DROP USER \"{$user}\"";

        return $this->execute($sql);
    }

    /**
     * Return users in a specific group.
     *
     * @param string $groname The name of the group
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function getGroup($groname)
    {
        $this->clean($groname);

        $sql = "
			SELECT s.usename FROM pg_catalog.pg_user s, pg_catalog.pg_group g
			WHERE g.groname='{$groname}' AND s.usesysid = ANY (g.grolist)
			ORDER BY s.usename";

        return $this->selectSet($sql);
    }

    /**
     * Returns all groups in the database cluser.
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function getGroups()
    {
        $sql = 'SELECT groname FROM pg_group ORDER BY groname';

        return $this->selectSet($sql);
    }

    /**
     * Creates a new group.
     *
     * @param string $groname The name of the group
     * @param array  $users   An array of users to add to the group
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function createGroup($groname, $users)
    {
        $this->fieldClean($groname);

        $sql = "CREATE GROUP \"{$groname}\"";

        if (\is_array($users) && 0 < \count($users)) {
            $this->fieldArrayClean($users);
            $sql .= ' WITH USER "' . \implode('", "', $users) . '"';
        }

        return $this->execute($sql);
    }

    /**
     * Removes a group.
     *
     * @param string $groname The name of the group to drop
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function dropGroup($groname)
    {
        $this->fieldClean($groname);

        $sql = "DROP GROUP \"{$groname}\"";

        return $this->execute($sql);
    }

    abstract public function fieldClean(&$str);

    abstract public function beginTransaction();

    abstract public function rollbackTransaction();

    abstract public function endTransaction();

    abstract public function execute($sql);

    abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null);

    abstract public function selectSet($sql);

    abstract public function clean(&$str);

    abstract public function hasGrantOption();

    abstract public function getFunction($function_oid);

    abstract public function fieldArrayClean(&$arr);

    private function _dealWithOldParentRoles($original_parent_roles, $new_roles_to_add, $rolename)
    {
        $old = \explode(',', $original_parent_roles);

        // Grant the roles of the old role owners to the new owner
        foreach ($new_roles_to_add as $m) {
            if (!\in_array($m, $old, true)) {
                $status = $this->grantRole($m, $rolename);

                if (0 !== $status) {
                    return -1;
                }
            }
        }

        // Revoke the new role to the old members if they don't have the requested role name

        foreach ($old as $o) {
            if (!\in_array($o, $new_roles_to_add, true)) {
                $status = $this->revokeRole($o, $rolename, 0, 'CASCADE');

                if (0 !== $status) {
                    return -1;
                }
            }
        }

        return 0;
    }

    private function _dealWithOriginalMembers($original_members, $new_members_of_role, $rolename)
    {
        //members
        $old = \explode(',', $original_members);

        foreach ($new_members_of_role as $m) {
            if (!\in_array($m, $old, true)) {
                $status = $this->grantRole($rolename, $m);

                if (0 !== $status) {
                    return -1;
                }
            }
        }

        if ($original_members) {
            foreach ($old as $o) {
                if (!\in_array($o, $new_members_of_role, true)) {
                    $status = $this->revokeRole($rolename, $o, 0, 'CASCADE');

                    if (0 !== $status) {
                        return -1;
                    }
                }
            }
        }

        return 0;
    }

    private function _dealWithOriginalAdmins($original_admins, $new_admins_of_role, $rolename)
    {
        $old = \explode(',', $original_admins);

        foreach ($new_admins_of_role as $m) {
            if (!\in_array($m, $old, true)) {
                $status = $this->grantRole($rolename, $m, 1);

                if (0 !== $status) {
                    return -1;
                }
            }
        }

        foreach ($old as $o) {
            if (!\in_array($o, $new_admins_of_role, true)) {
                $status = $this->revokeRole($rolename, $o, 1, 'CASCADE');

                if (0 !== $status) {
                    return -1;
                }
            }
        }

        return 0;
    }

    private function _alterRole($rolename, $password, $connlimit, $expiry, $superuser, $createdb, $createrole, $inherits, $login)
    {
        $enc = $this->_encryptPassword($rolename, $password);
        $this->clean($enc);
        $this->clean($connlimit);
        $this->clean($expiry);

        $sql = "ALTER ROLE \"{$rolename}\"";

        if ('' !== $password) {
            $sql .= " WITH ENCRYPTED PASSWORD '{$enc}'";
        }

        $sql .= $superuser ? ' SUPERUSER' : ' NOSUPERUSER';
        $sql .= $createdb ? ' CREATEDB' : ' NOCREATEDB';
        $sql .= $createrole ? ' CREATEROLE' : ' NOCREATEROLE';
        $sql .= $inherits ? ' INHERIT' : ' NOINHERIT';
        $sql .= $login ? ' LOGIN' : ' NOLOGIN';

        if ('' !== $connlimit) {
            $sql .= " CONNECTION LIMIT {$connlimit}";
        } else {
            $sql .= ' CONNECTION LIMIT -1';
        }

        if ('' !== $expiry) {
            $sql .= " VALID UNTIL '{$expiry}'";
        } else {
            $sql .= " VALID UNTIL 'infinity'";
        }

        return $this->execute($sql);
    }
}