MediaWiki REL1_31
UserInfo.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Session;
25
26use User;
27
51final class UserInfo {
52 private $verified = false;
53
55 private $user = null;
56
57 private function __construct( User $user = null, $verified ) {
58 if ( $user && $user->isAnon() && !User::isUsableName( $user->getName() ) ) {
59 $this->verified = true;
60 $this->user = null;
61 } else {
62 $this->verified = $verified;
63 $this->user = $user;
64 }
65 }
66
74 public static function newAnonymous() {
75 return new self( null, true );
76 }
77
84 public static function newFromId( $id, $verified = false ) {
85 $user = User::newFromId( $id );
86
87 // Ensure the ID actually exists
88 $user->load();
89 if ( $user->isAnon() ) {
90 throw new \InvalidArgumentException( 'Invalid ID' );
91 }
92
93 return new self( $user, $verified );
94 }
95
102 public static function newFromName( $name, $verified = false ) {
103 $user = User::newFromName( $name, 'usable' );
104 if ( !$user ) {
105 throw new \InvalidArgumentException( 'Invalid user name' );
106 }
107 return new self( $user, $verified );
108 }
109
116 public static function newFromUser( User $user, $verified = false ) {
117 return new self( $user, $verified );
118 }
119
124 public function isAnon() {
125 return $this->user === null;
126 }
127
132 public function isVerified() {
133 return $this->verified;
134 }
135
141 public function getId() {
142 return $this->user === null ? 0 : $this->user->getId();
143 }
144
149 public function getName() {
150 return $this->user === null ? null : $this->user->getName();
151 }
152
157 public function getToken() {
158 return $this->user === null || $this->user->getId() === 0 ? '' : $this->user->getToken( false );
159 }
160
165 public function getUser() {
166 return $this->user === null ? new User : $this->user;
167 }
168
173 public function verified() {
174 return $this->verified ? $this : new self( $this->user, true );
175 }
176
177 public function __toString() {
178 if ( $this->user === null ) {
179 return '<anon>';
180 }
181 return '<' .
182 ( $this->verified ? '+' : '-' ) . ':' .
183 $this->getId() . ':' . $this->getName() .
184 '>';
185 }
186
187}
Object holding data about a session's user.
Definition UserInfo.php:51
getId()
Return the user ID.
Definition UserInfo.php:141
static newFromName( $name, $verified=false)
Create an instance for a logged-in user by name.
Definition UserInfo.php:102
isVerified()
Return whether this represents a verified user.
Definition UserInfo.php:132
__construct(User $user=null, $verified)
Definition UserInfo.php:57
static newAnonymous()
Create an instance for an anonymous (i.e.
Definition UserInfo.php:74
isAnon()
Return whether this is an anonymous user.
Definition UserInfo.php:124
getUser()
Return a User object.
Definition UserInfo.php:165
getName()
Return the user name.
Definition UserInfo.php:149
static newFromUser(User $user, $verified=false)
Create an instance from an existing User object.
Definition UserInfo.php:116
static newFromId( $id, $verified=false)
Create an instance for a logged-in user by ID.
Definition UserInfo.php:84
verified()
Return a verified version of this object.
Definition UserInfo.php:173
getToken()
Return the user token.
Definition UserInfo.php:157
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:53
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:591
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition User.php:614
static isUsableName( $name)
Usernames which fail to pass this function will be blocked from user login and new account registrati...
Definition User.php:1018
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2006