MediaWiki  master
UserInfo.php
Go to the documentation of this file.
1 <?php
24 namespace MediaWiki\Session;
25 
26 use User;
27 
51 final class UserInfo {
53  private $verified = false;
54 
56  private $user = null;
57 
58  private function __construct( ?User $user, $verified ) {
59  if ( $user && $user->isAnon() && !User::isUsableName( $user->getName() ) ) {
60  $this->verified = true;
61  $this->user = null;
62  } else {
63  $this->verified = $verified;
64  $this->user = $user;
65  }
66  }
67 
75  public static function newAnonymous() {
76  return new self( null, true );
77  }
78 
85  public static function newFromId( $id, $verified = false ) {
86  $user = User::newFromId( $id );
87 
88  // Ensure the ID actually exists
89  $user->load();
90  if ( $user->isAnon() ) {
91  throw new \InvalidArgumentException( 'Invalid ID' );
92  }
93 
94  return new self( $user, $verified );
95  }
96 
103  public static function newFromName( $name, $verified = false ) {
104  $user = User::newFromName( $name, 'usable' );
105  if ( !$user ) {
106  throw new \InvalidArgumentException( 'Invalid user name' );
107  }
108  return new self( $user, $verified );
109  }
110 
117  public static function newFromUser( User $user, $verified = false ) {
118  return new self( $user, $verified );
119  }
120 
125  public function isAnon() {
126  return $this->user === null;
127  }
128 
133  public function isVerified() {
134  return $this->verified;
135  }
136 
142  public function getId() {
143  return $this->user === null ? 0 : $this->user->getId();
144  }
145 
150  public function getName() {
151  return $this->user === null ? null : $this->user->getName();
152  }
153 
158  public function getToken() {
159  return $this->user === null || $this->user->getId() === 0 ? '' : $this->user->getToken( false );
160  }
161 
166  public function getUser() {
167  return $this->user === null ? new User : $this->user;
168  }
169 
174  public function verified() {
175  return $this->verified ? $this : new self( $this->user, true );
176  }
177 
178  public function __toString() {
179  if ( $this->user === null ) {
180  return '<anon>';
181  }
182  return '<' .
183  ( $this->verified ? '+' : '-' ) . ':' .
184  $this->getId() . ':' . $this->getName() .
185  '>';
186  }
187 
188 }
MediaWiki\Session\UserInfo\newAnonymous
static newAnonymous()
Create an instance for an anonymous (i.e.
Definition: UserInfo.php:75
User\load
load( $flags=self::READ_NORMAL)
Load the user table data for this object from the source given by mFrom.
Definition: User.php:325
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:561
User\isAnon
isAnon()
Get whether the user is anonymous.
Definition: User.php:3589
MediaWiki\Session\UserInfo
Object holding data about a session's user.
Definition: UserInfo.php:51
true
return true
Definition: router.php:92
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:537
MediaWiki\Session\UserInfo\isVerified
isVerified()
Return whether this represents a verified user.
Definition: UserInfo.php:133
MediaWiki\Session\UserInfo\newFromUser
static newFromUser(User $user, $verified=false)
Create an instance from an existing User object.
Definition: UserInfo.php:117
MediaWiki\Session\UserInfo\$user
User null $user
Definition: UserInfo.php:56
MediaWiki\Session\UserInfo\getName
getName()
Return the user name.
Definition: UserInfo.php:150
MediaWiki\Session\UserInfo\__toString
__toString()
Definition: UserInfo.php:178
MediaWiki\Session
Definition: BotPasswordSessionProvider.php:24
MediaWiki\Session\UserInfo\getUser
getUser()
Return a User object.
Definition: UserInfo.php:166
MediaWiki\Session\UserInfo\newFromName
static newFromName( $name, $verified=false)
Create an instance for a logged-in user by name.
Definition: UserInfo.php:103
MediaWiki\Session\UserInfo\getId
getId()
Return the user ID.
Definition: UserInfo.php:142
MediaWiki\Session\UserInfo\getToken
getToken()
Return the user token.
Definition: UserInfo.php:158
MediaWiki\Session\UserInfo\newFromId
static newFromId( $id, $verified=false)
Create an instance for a logged-in user by ID.
Definition: UserInfo.php:85
MediaWiki\Session\UserInfo\$verified
bool $verified
Definition: UserInfo.php:53
User\isUsableName
static isUsableName( $name)
Usernames which fail to pass this function will be blocked from user login and new account registrati...
Definition: User.php:1023
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:52
MediaWiki\Session\UserInfo\__construct
__construct(?User $user, $verified)
Definition: UserInfo.php:58
MediaWiki\Session\UserInfo\verified
verified()
Return a verified version of this object.
Definition: UserInfo.php:174
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2284
MediaWiki\Session\UserInfo\isAnon
isAnon()
Return whether this is an anonymous user.
Definition: UserInfo.php:125