Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
52 / 52 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
CentralAuthIdLookup | |
100.00% |
52 / 52 |
|
100.00% |
5 / 5 |
20 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
lookupCentralIds | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
7 | |||
lookupUserNames | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
7 | |||
isAttached | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
centralIdFromLocalUser | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 |
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 | |
21 | namespace MediaWiki\Extension\CentralAuth\User; |
22 | |
23 | use IDBAccessObject; |
24 | use MediaWiki\Extension\CentralAuth\CentralAuthDatabaseManager; |
25 | use MediaWiki\User\CentralId\CentralIdLookup; |
26 | use MediaWiki\User\UserIdentity; |
27 | use MediaWiki\WikiMap\WikiMap; |
28 | |
29 | /** |
30 | * Look up central IDs using CentralAuth |
31 | */ |
32 | class CentralAuthIdLookup extends CentralIdLookup { |
33 | |
34 | /** @var CentralAuthDatabaseManager */ |
35 | private $databaseManager; |
36 | |
37 | /** |
38 | * @param CentralAuthDatabaseManager $databaseManager |
39 | */ |
40 | public function __construct( CentralAuthDatabaseManager $databaseManager ) { |
41 | $this->databaseManager = $databaseManager; |
42 | } |
43 | |
44 | /** @inheritDoc */ |
45 | public function lookupCentralIds( |
46 | array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = IDBAccessObject::READ_NORMAL |
47 | ): array { |
48 | if ( !$idToName ) { |
49 | return []; |
50 | } |
51 | |
52 | $audience = $this->checkAudience( $audience ); |
53 | $fromPrimaryDb = ( $flags & IDBAccessObject::READ_LATEST ) === IDBAccessObject::READ_LATEST; |
54 | $db = $this->databaseManager->getCentralDB( |
55 | $fromPrimaryDb ? DB_PRIMARY : DB_REPLICA |
56 | ); |
57 | |
58 | $queryInfo = CentralAuthUser::selectQueryInfo(); |
59 | |
60 | $res = $db->newSelectQueryBuilder() |
61 | ->tables( $queryInfo['tables'] ) |
62 | ->fields( $queryInfo['fields'] ) |
63 | ->where( [ 'gu_id' => array_map( 'intval', array_keys( $idToName ) ) ] ) |
64 | ->andWhere( $queryInfo['where'] ) |
65 | ->caller( __METHOD__ ) |
66 | ->options( $queryInfo['options'] ) |
67 | ->joinConds( $queryInfo['joinConds'] ) |
68 | ->fetchResultSet(); |
69 | foreach ( $res as $row ) { |
70 | $centralUser = CentralAuthUser::newFromRow( $row, [], $fromPrimaryDb ); |
71 | if ( $centralUser->getHiddenLevelInt() === CentralAuthUser::HIDDEN_LEVEL_NONE |
72 | || $audience === null || $audience->isAllowed( 'centralauth-suppress' ) |
73 | ) { |
74 | $idToName[$centralUser->getId()] = $centralUser->getName(); |
75 | } else { |
76 | $idToName[$centralUser->getId()] = ''; |
77 | } |
78 | } |
79 | |
80 | return $idToName; |
81 | } |
82 | |
83 | /** @inheritDoc */ |
84 | public function lookupUserNames( |
85 | array $nameToId, $audience = self::AUDIENCE_PUBLIC, $flags = IDBAccessObject::READ_NORMAL |
86 | ): array { |
87 | if ( !$nameToId ) { |
88 | return []; |
89 | } |
90 | |
91 | $audience = $this->checkAudience( $audience ); |
92 | $fromPrimaryDb = ( $flags & IDBAccessObject::READ_LATEST ) === IDBAccessObject::READ_LATEST; |
93 | $db = $this->databaseManager->getCentralDB( |
94 | $fromPrimaryDb ? DB_PRIMARY : DB_REPLICA |
95 | ); |
96 | |
97 | $queryInfo = CentralAuthUser::selectQueryInfo(); |
98 | |
99 | $res = $db->newSelectQueryBuilder() |
100 | ->tables( $queryInfo['tables'] ) |
101 | ->fields( $queryInfo['fields'] ) |
102 | ->where( [ 'gu_name' => array_map( 'strval', array_keys( $nameToId ) ) ] ) |
103 | ->andWhere( $queryInfo['where'] ) |
104 | ->caller( __METHOD__ ) |
105 | ->options( $queryInfo['options'] ) |
106 | ->joinConds( $queryInfo['joinConds'] ) |
107 | ->fetchResultSet(); |
108 | foreach ( $res as $row ) { |
109 | $centralUser = CentralAuthUser::newFromRow( $row, [], $fromPrimaryDb ); |
110 | if ( $centralUser->getHiddenLevelInt() === CentralAuthUser::HIDDEN_LEVEL_NONE |
111 | || $audience === null || $audience->isAllowed( 'centralauth-suppress' ) |
112 | ) { |
113 | $nameToId[$centralUser->getName()] = $centralUser->getId(); |
114 | } |
115 | } |
116 | |
117 | return $nameToId; |
118 | } |
119 | |
120 | /** @inheritDoc */ |
121 | public function isAttached( $user, $wikiId = UserIdentity::LOCAL ): bool { |
122 | $wikiId = $wikiId ?: WikiMap::getCurrentWikiId(); |
123 | $centralUser = CentralAuthUser::getInstance( $user ); |
124 | return $centralUser->getId() != 0 && $centralUser->attachedOn( $wikiId ); |
125 | } |
126 | |
127 | /** @inheritDoc */ |
128 | public function centralIdFromLocalUser( |
129 | $user, $audience = self::AUDIENCE_PUBLIC, $flags = IDBAccessObject::READ_NORMAL |
130 | ): int { |
131 | return $this->isAttached( $user ) ? CentralAuthUser::getInstance( $user )->getId() : 0; |
132 | } |
133 | |
134 | } |