MediaWiki  master
UserFactory.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\User;
24 
26 use IDBAccessObject;
27 use InvalidArgumentException;
29 use stdClass;
30 use User;
32 
42 
49  private $loadBalancer;
50 
52  private $userNameUtils;
53 
58  public function __construct(
61  ) {
62  $this->loadBalancer = $loadBalancer;
63  $this->userNameUtils = $userNameUtils;
64  }
65 
84  public function newFromName(
85  string $name,
86  string $validate = self::RIGOR_VALID
87  ) : ?User {
88  // RIGOR_* constants are the same here and in the UserNameUtils class
89  $canonicalName = $this->userNameUtils->getCanonical( $name, $validate );
90  if ( $canonicalName === false ) {
91  return null;
92  }
93 
94  $user = new User();
95  $user->mName = $canonicalName;
96  $user->mFrom = 'name';
97  $user->setItemLoaded( 'name' );
98  return $user;
99  }
100 
109  public function newAnonymous( $ip = null ) : User {
110  if ( $ip ) {
111  $validIp = $this->userNameUtils->isIP( $ip );
112  if ( $validIp ) {
113  $user = $this->newFromName( $ip, self::RIGOR_NONE );
114  } else {
115  throw new InvalidArgumentException( 'Invalid IP address' );
116  }
117  } else {
118  $user = new User();
119  }
120  return $user;
121  }
122 
131  public function newFromId( int $id ) : User {
132  $user = new User();
133  $user->mId = $id;
134  $user->mFrom = 'id';
135  $user->setItemLoaded( 'id' );
136  return $user;
137  }
138 
147  public function newFromActorId( int $actorId ) : User {
148  $user = new User();
149  $user->mActorId = $actorId;
150  $user->mFrom = 'actor';
151  $user->setItemLoaded( 'actor' );
152  return $user;
153  }
154 
163  public function newFromUserIdentity( UserIdentity $userIdentity ) : User {
164  if ( $userIdentity instanceof User ) {
165  return $userIdentity;
166  }
167 
168  return $this->newFromAnyId(
169  $userIdentity->getId() === 0 ? null : $userIdentity->getId(),
170  $userIdentity->getName() === '' ? null : $userIdentity->getName(),
171  $userIdentity->getActorId() === 0 ? null : $userIdentity->getActorId()
172  );
173  }
174 
190  public function newFromAnyId(
191  ?int $userId,
192  ?string $userName,
193  ?int $actorId,
194  $dbDomain = false
195  ) : User {
196  // Stop-gap solution for the problem described in T222212.
197  // Force the User ID and Actor ID to zero for users loaded from the database
198  // of another wiki, to prevent subtle data corruption and confusing failure modes.
199  if ( $dbDomain !== false ) {
200  $userId = 0;
201  $actorId = 0;
202  }
203 
204  $user = new User;
205  $user->mFrom = 'defaults';
206 
207  if ( $actorId !== null ) {
208  $user->mActorId = $actorId;
209  if ( $actorId !== 0 ) {
210  $user->mFrom = 'actor';
211  }
212  $user->setItemLoaded( 'actor' );
213  }
214 
215  if ( $userName !== null && $userName !== '' ) {
216  $user->mName = $userName;
217  $user->mFrom = 'name';
218  $user->setItemLoaded( 'name' );
219  }
220 
221  if ( $userId !== null ) {
222  $user->mId = $userId;
223  if ( $userId !== 0 ) {
224  $user->mFrom = 'id';
225  }
226  $user->setItemLoaded( 'id' );
227  }
228 
229  if ( $user->mFrom === 'defaults' ) {
230  throw new InvalidArgumentException(
231  'Cannot create a user with no name, no ID, and no actor ID'
232  );
233  }
234 
235  return $user;
236  }
237 
250  public function newFromConfirmationCode(
251  string $confirmationCode,
252  int $flags = self::READ_NORMAL
253  ) {
254  list( $index, $options ) = DBAccessObjectUtils::getDBOptions( $flags );
255 
256  $db = $this->loadBalancer->getConnectionRef( $index );
257 
258  $id = $db->selectField(
259  'user',
260  'user_id',
261  [
262  'user_email_token' => md5( $confirmationCode ),
263  'user_email_token_expires > ' . $db->addQuotes( $db->timestamp() ),
264  ],
265  __METHOD__,
266  $options
267  );
268 
269  if ( !$id ) {
270  return null;
271  }
272 
273  return $this->newFromId( (int)$id );
274  }
275 
285  public function newFromRow( $row, $data = null ) {
286  return User::newFromRow( $row, $data );
287  }
288 
294  public function newFromAuthority( Authority $authority ): User {
295  if ( $authority instanceof User ) {
296  return $authority;
297  }
298  return $this->newFromUserIdentity( $authority->getActor() );
299  }
300 
301 }
MediaWiki\User\UserFactory\newFromUserIdentity
newFromUserIdentity(UserIdentity $userIdentity)
Factory method for creation fom a given UserIdentity, replacing User::newFromIdentity.
Definition: UserFactory.php:163
MediaWiki\User\UserFactory\newFromId
newFromId(int $id)
Factory method for creation from a given user ID, replacing User::newFromId.
Definition: UserFactory.php:131
MediaWiki\User\UserFactory\newFromRow
newFromRow( $row, $data=null)
Definition: UserFactory.php:285
MediaWiki\User\UserFactory\newFromAuthority
newFromAuthority(Authority $authority)
Definition: UserFactory.php:294
MediaWiki\User\UserFactory\newFromConfirmationCode
newFromConfirmationCode(string $confirmationCode, int $flags=self::READ_NORMAL)
Factory method to fetch the user for a given email confirmation code, replacing User::newFromConfirma...
Definition: UserFactory.php:250
DBAccessObjectUtils\getDBOptions
static getDBOptions( $bitfield)
Get an appropriate DB index, options, and fallback DB index for a query.
Definition: DBAccessObjectUtils.php:52
IDBAccessObject
Interface for database access objects.
Definition: IDBAccessObject.php:57
User\newFromRow
static newFromRow( $row, $data=null)
Create a new user object from a user row.
Definition: User.php:710
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
MediaWiki\User\UserIdentity\getActorId
getActorId()
User\setItemLoaded
setItemLoaded( $item)
Set that an item has been loaded.
Definition: User.php:1227
User\isIP
static isIP( $name)
Does the string match an anonymous IP address?
Definition: User.php:968
MediaWiki\User\UserIdentity\getName
getName()
MediaWiki\User\UserFactory\newFromActorId
newFromActorId(int $actorId)
Factory method for creation from a given actor ID, replacing User::newFromActorId.
Definition: UserFactory.php:147
MediaWiki\User\UserFactory\__construct
__construct(ILoadBalancer $loadBalancer, UserNameUtils $userNameUtils)
Definition: UserFactory.php:58
DBAccessObjectUtils
Helper class for DAO classes.
Definition: DBAccessObjectUtils.php:29
MediaWiki\Permissions\Authority\getActor
getActor()
Returns the actor associated with this authority.
MediaWiki\User\UserFactory\$userNameUtils
UserNameUtils $userNameUtils
Definition: UserFactory.php:52
MediaWiki\Permissions\Authority
@unstable
Definition: Authority.php:30
MediaWiki\User
Definition: DefaultOptionsLookup.php:21
MediaWiki\User\UserRigorOptions
Shared interface for rigor levels when dealing with User methods.
Definition: UserRigorOptions.php:31
MediaWiki\User\UserIdentity\getId
getId()
MediaWiki\User\UserNameUtils
UserNameUtils service.
Definition: UserNameUtils.php:42
MediaWiki\User\UserFactory\newFromAnyId
newFromAnyId(?int $userId, ?string $userName, ?int $actorId, $dbDomain=false)
Factory method for creation from an ID, name, and/or actor ID, replacing User::newFromAnyId.
Definition: UserFactory.php:190
MediaWiki\User\UserFactory\newFromName
newFromName(string $name, string $validate=self::RIGOR_VALID)
Factory method for creating users by name, replacing static User::newFromName.
Definition: UserFactory.php:84
MediaWiki\User\UserFactory\$loadBalancer
ILoadBalancer $loadBalancer
RIGOR_* constants are inherited from UserRigorOptions READ_* constants are inherited from IDBAccessOb...
Definition: UserFactory.php:49
MediaWiki\User\UserFactory\newAnonymous
newAnonymous( $ip=null)
Returns a new anonymous User based on ip.
Definition: UserFactory.php:109
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:63
MediaWiki\User\UserFactory
Creates User objects.
Definition: UserFactory.php:41
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81