MediaWiki  master
UserInfo.php
Go to the documentation of this file.
1 <?php
24 namespace MediaWiki\Session;
25 
28 use User;
29 
53 final class UserInfo {
55  private $verified = false;
56 
58  private $user = null;
59 
60  private function __construct( ?User $user, $verified ) {
61  $userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils();
62  if ( $user && $user->isAnon() && !$userNameUtils->isUsable( $user->getName() ) ) {
63  $this->verified = true;
64  $this->user = null;
65  } else {
66  $this->verified = $verified;
67  $this->user = $user;
68  }
69  }
70 
78  public static function newAnonymous() {
79  return new self( null, true );
80  }
81 
88  public static function newFromId( $id, $verified = false ) {
89  $user = MediaWikiServices::getInstance()->getUserFactory()->newFromId( (int)$id );
90 
91  // Ensure the ID actually exists
92  $user->load();
93  if ( $user->isAnon() ) {
94  throw new \InvalidArgumentException( 'Invalid ID' );
95  }
96 
97  return new self( $user, $verified );
98  }
99 
106  public static function newFromName( $name, $verified = false ) {
107  $user = MediaWikiServices::getInstance()->getUserFactory()->newFromName(
108  (string)$name,
109  UserFactory::RIGOR_USABLE
110  );
111  if ( !$user ) {
112  throw new \InvalidArgumentException( 'Invalid user name' );
113  }
114  return new self( $user, $verified );
115  }
116 
123  public static function newFromUser( User $user, $verified = false ) {
124  return new self( $user, $verified );
125  }
126 
131  public function isAnon() {
132  return $this->user === null;
133  }
134 
139  public function isVerified() {
140  return $this->verified;
141  }
142 
148  public function getId() {
149  return $this->user === null ? 0 : $this->user->getId();
150  }
151 
156  public function getName() {
157  return $this->user === null ? null : $this->user->getName();
158  }
159 
164  public function getToken() {
165  return $this->user === null || $this->user->getId() === 0 ? '' : $this->user->getToken( false );
166  }
167 
172  public function getUser() {
173  return $this->user ?? new User;
174  }
175 
180  public function verified() {
181  return $this->verified ? $this : new self( $this->user, true );
182  }
183 
184  public function __toString() {
185  if ( $this->user === null ) {
186  return '<anon>';
187  }
188  return '<' .
189  ( $this->verified ? '+' : '-' ) . ':' .
190  $this->getId() . ':' . $this->getName() .
191  '>';
192  }
193 
194 }
MediaWiki\Session\UserInfo\newAnonymous
static newAnonymous()
Create an instance for an anonymous (i.e.
Definition: UserInfo.php:78
User\load
load( $flags=self::READ_NORMAL)
Load the user table data for this object from the source given by mFrom.
Definition: User.php:379
User\isAnon
isAnon()
Get whether the user is anonymous.
Definition: User.php:2996
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:200
MediaWiki\Session\UserInfo
Object holding data about a session's user.
Definition: UserInfo.php:53
true
return true
Definition: router.php:90
MediaWiki\Session\UserInfo\isVerified
isVerified()
Return whether this represents a verified user.
Definition: UserInfo.php:139
MediaWiki\Session\UserInfo\newFromUser
static newFromUser(User $user, $verified=false)
Create an instance from an existing User object.
Definition: UserInfo.php:123
MediaWiki\Session\UserInfo\$user
User null $user
Definition: UserInfo.php:58
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:261
MediaWiki\Session\UserInfo\getName
getName()
Return the user name.
Definition: UserInfo.php:156
MediaWiki\Session\UserInfo\__toString
__toString()
Definition: UserInfo.php:184
MediaWiki\Session
Definition: BotPasswordSessionProvider.php:24
MediaWiki\Session\UserInfo\getUser
getUser()
Return a User object.
Definition: UserInfo.php:172
MediaWiki\Session\UserInfo\newFromName
static newFromName( $name, $verified=false)
Create an instance for a logged-in user by name.
Definition: UserInfo.php:106
MediaWiki\Session\UserInfo\getId
getId()
Return the user ID.
Definition: UserInfo.php:148
MediaWiki\Session\UserInfo\getToken
getToken()
Return the user token.
Definition: UserInfo.php:164
MediaWiki\Session\UserInfo\newFromId
static newFromId( $id, $verified=false)
Create an instance for a logged-in user by ID.
Definition: UserInfo.php:88
MediaWiki\Session\UserInfo\$verified
bool $verified
Definition: UserInfo.php:55
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:69
MediaWiki\Session\UserInfo\__construct
__construct(?User $user, $verified)
Definition: UserInfo.php:60
MediaWiki\Session\UserInfo\verified
verified()
Return a verified version of this object.
Definition: UserInfo.php:180
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2122
MediaWiki\User\UserFactory
Creates User objects.
Definition: UserFactory.php:41
MediaWiki\Session\UserInfo\isAnon
isAnon()
Return whether this is an anonymous user.
Definition: UserInfo.php:131