3 * Representation of an user on a other locally-hosted wiki.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\MediaWikiServices
;
24 use MediaWiki\User\UserGroupManager
;
25 use MediaWiki\User\UserIdentityValue
;
26 use Wikimedia\Rdbms\IDatabase
;
29 * Cut-down copy of User interface for local-interwiki-database
30 * user rights manipulation.
32 class UserRightsProxy
{
43 /** @var UserGroupManager */
44 private $userGroupManager;
49 * @param IDatabase $db Db connection
50 * @param string $dbDomain Database name
51 * @param string $name User name
52 * @param int $id User ID
54 private function __construct( $db, $dbDomain, $name, $id ) {
56 $this->dbDomain
= $dbDomain;
58 $this->id
= intval( $id );
59 $this->newOptions
= [];
60 $this->userGroupManager
= MediaWikiServices
::getInstance()
61 ->getUserGroupManagerFactory()
62 ->getUserGroupManager( $dbDomain );
66 * Confirm the selected database name is a valid local interwiki database name.
68 * @param string $dbDomain Database name
71 public static function validDatabase( $dbDomain ) {
72 global $wgLocalDatabases;
73 return in_array( $dbDomain, $wgLocalDatabases );
77 * Same as User::whoIs()
79 * @param string $dbDomain Database name
80 * @param int $id User ID
81 * @param bool $ignoreInvalidDB If true, don't check if $dbDomain is in $wgLocalDatabases
82 * @return string User name or false if the user doesn't exist
84 public static function whoIs( $dbDomain, $id, $ignoreInvalidDB = false ) {
85 $user = self
::newFromId( $dbDomain, $id, $ignoreInvalidDB );
94 * Factory function; get a remote user entry by ID number.
96 * @param string $dbDomain Database name
97 * @param int $id User ID
98 * @param bool $ignoreInvalidDB If true, don't check if $dbDomain is in $wgLocalDatabases
99 * @return UserRightsProxy|null If doesn't exist
101 public static function newFromId( $dbDomain, $id, $ignoreInvalidDB = false ) {
102 return self
::newFromLookup( $dbDomain, 'user_id', intval( $id ), $ignoreInvalidDB );
106 * Factory function; get a remote user entry by name.
108 * @param string $dbDomain Database name
109 * @param string $name User name
110 * @param bool $ignoreInvalidDB If true, don't check if $dbDomain is in $wgLocalDatabases
111 * @return UserRightsProxy|null If doesn't exist
113 public static function newFromName( $dbDomain, $name, $ignoreInvalidDB = false ) {
114 return self
::newFromLookup( $dbDomain, 'user_name', $name, $ignoreInvalidDB );
118 * @param string $dbDomain
119 * @param string $field
120 * @param string $value
121 * @param bool $ignoreInvalidDB
122 * @return null|UserRightsProxy
124 private static function newFromLookup( $dbDomain, $field, $value, $ignoreInvalidDB = false ) {
125 global $wgSharedDB, $wgSharedTables;
126 // If the user table is shared, perform the user query on it,
127 // but don't pass it to the UserRightsProxy,
128 // as user rights are normally not shared.
129 if ( $wgSharedDB && in_array( 'user', $wgSharedTables ) ) {
130 $userdb = self
::getDB( $wgSharedDB, $ignoreInvalidDB );
132 $userdb = self
::getDB( $dbDomain, $ignoreInvalidDB );
135 $db = self
::getDB( $dbDomain, $ignoreInvalidDB );
137 if ( $db && $userdb ) {
138 $row = $userdb->selectRow( 'user',
139 [ 'user_id', 'user_name' ],
140 [ $field => $value ],
143 if ( $row !== false ) {
144 return new UserRightsProxy(
145 $db, $dbDomain, $row->user_name
, intval( $row->user_id
) );
152 * Open a database connection to work on for the requested user.
153 * This may be a new connection to another database for remote users.
155 * @param string $dbDomain
156 * @param bool $ignoreInvalidDB If true, don't check if $dbDomain is in $wgLocalDatabases
157 * @return IDatabase|null If invalid selection
159 public static function getDB( $dbDomain, $ignoreInvalidDB = false ) {
160 if ( $ignoreInvalidDB || self
::validDatabase( $dbDomain ) ) {
161 if ( WikiMap
::isCurrentWikiId( $dbDomain ) ) {
162 // Hmm... this shouldn't happen though. :)
163 return wfGetDB( DB_MASTER
);
165 return wfGetDB( DB_MASTER
, [], $dbDomain );
174 public function getId() {
181 public function isAnon() {
182 return $this->getId() == 0;
186 * Same as User::getName()
190 public function getName() {
191 return $this->name
. '@' . $this->dbDomain
;
195 * Same as User::getUserPage()
199 public function getUserPage() {
200 return Title
::makeTitle( NS_USER
, $this->getName() );
204 * Replaces User::getUserGroups()
207 public function getGroups() {
208 return array_keys( self
::getGroupMemberships() );
212 * Replaces User::getGroupMemberships()
214 * @return UserGroupMembership[]
217 public function getGroupMemberships() {
218 // TODO: We are creating an artificial UserIdentity to pass on to the user group manager.
219 // After all the relevant UserGroupMemberships methods are ported into UserGroupManager,
220 // the usages of this class will be changed into usages of the UserGroupManager,
221 // thus the need of this class and the need of this artificial UserIdentityValue will parish.
222 $user = new UserIdentityValue( $this->getId(), $this->getName(), 0 );
223 return $this->userGroupManager
->getUserGroupMemberships( $user, IDBAccessObject
::READ_LATEST
);
227 * Replaces User::addGroup()
229 * @param string $group
230 * @param string|null $expiry
233 public function addGroup( $group, $expiry = null ) {
234 return $this->userGroupManager
->addUserToGroup(
235 // TODO: Artificial UserIdentity just for passing the id and name.
236 // see comment in getGroupMemberships.
237 new UserIdentityValue( $this->getId(), $this->getName(), 0 ),
244 * Replaces User::removeGroup()
246 * @param string $group
249 public function removeGroup( $group ) {
250 return $this->userGroupManager
->removeUserFromGroup(
251 // TODO: Artificial UserIdentity just for passing the id and name.
252 // see comment in getGroupMemberships.
253 new UserIdentityValue( $this->getId(), $this->getName(), 0 ),
259 * Replaces User::setOption()
260 * @param string $option
261 * @param mixed $value
263 public function setOption( $option, $value ) {
264 $this->newOptions
[$option] = $value;
267 public function saveSettings() {
269 foreach ( $this->newOptions
as $option => $value ) {
271 'up_user' => $this->id
,
272 'up_property' => $option,
273 'up_value' => $value,
278 [ [ 'up_user', 'up_property' ] ],
282 $this->invalidateCache();
286 * Replaces User::touchUser()
288 public function invalidateCache() {
291 [ 'user_touched' => $this->db
->timestamp() ],
292 [ 'user_id' => $this->id
],
296 $domainId = $this->db
->getDomainID();
298 $this->db
->onTransactionPreCommitOrIdle(
299 function () use ( $domainId, $userId ) {
300 User
::purge( $domainId, $userId );