Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
GlobalGroupLookup
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
4 / 4
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDefinedGroups
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 getRightsForGroup
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 getGroupsWithPermission
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Extension\CentralAuth\GlobalGroup;
22
23use MediaWiki\Extension\CentralAuth\CentralAuthDatabaseManager;
24use Wikimedia\Rdbms\IDBAccessObject;
25
26/**
27 * @since 1.37
28 * @author Taavi "Majavah" Väänänen <hi@taavi.wtf>
29 */
30class GlobalGroupLookup {
31
32    private CentralAuthDatabaseManager $dbManager;
33
34    public function __construct( CentralAuthDatabaseManager $dbManager ) {
35        $this->dbManager = $dbManager;
36    }
37
38    /**
39     * Returns all defined global groups.
40     * @param int $flags One of the IDBAccessObject::READ_* constants
41     * @return string[]
42     */
43    public function getDefinedGroups( int $flags = IDBAccessObject::READ_NORMAL ): array {
44        $dbr = $this->dbManager->getCentralDBFromRecency( $flags );
45        return $dbr->newSelectQueryBuilder()
46            ->select( 'ggp_group' )
47            ->distinct()
48            ->from( 'global_group_permissions' )
49            ->recency( $flags )
50            ->caller( __METHOD__ )
51            ->fetchFieldValues();
52    }
53
54    /**
55     * Returns all rights assigned to a specified global group.
56     * @param string $group
57     * @param int $flags One of the IDBAccessObject::READ_* constants
58     * @return string[]
59     */
60    public function getRightsForGroup( string $group, int $flags = IDBAccessObject::READ_NORMAL ): array {
61        $dbr = $this->dbManager->getCentralDBFromRecency( $flags );
62        return $dbr->newSelectQueryBuilder()
63            ->select( 'ggp_permission' )
64            ->from( 'global_group_permissions' )
65            ->where( [ 'ggp_group' => $group ] )
66            ->recency( $flags )
67            ->caller( __METHOD__ )
68            ->fetchFieldValues();
69    }
70
71    /**
72     * Returns all global groups with a specified permission.
73     *
74     * @since 1.44
75     * @param string $permission
76     * @param int $flags {@link IDBAccessObject} flags
77     * @return string[] internal global group names with the given permission
78     */
79    public function getGroupsWithPermission( string $permission, int $flags = IDBAccessObject::READ_NORMAL ): array {
80        $dbr = $this->dbManager->getCentralDBFromRecency( $flags );
81        return $dbr->newSelectQueryBuilder()
82            ->select( 'ggp_group' )
83            ->from( 'global_group_permissions' )
84            ->where( [ 'ggp_permission' => $permission ] )
85            ->recency( $flags )
86            ->caller( __METHOD__ )
87            ->fetchFieldValues();
88    }
89}