MediaWiki master
UserInfo.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Session;
25
26use InvalidArgumentException;
30use Stringable;
31
55final class UserInfo implements Stringable {
57 private $verified = false;
58
60 private $user = null;
61
62 private function __construct( ?User $user, $verified ) {
63 $userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils();
64 if ( $user && $user->isAnon() && !$userNameUtils->isUsable( $user->getName() ) ) {
65 $this->verified = true;
66 $this->user = null;
67 } else {
68 $this->verified = $verified;
69 $this->user = $user;
70 }
71 }
72
80 public static function newAnonymous() {
81 return new self( null, true );
82 }
83
90 public static function newFromId( $id, $verified = false ) {
91 $user = MediaWikiServices::getInstance()->getUserFactory()->newFromId( (int)$id );
92
93 // Ensure the ID actually exists
94 $user->load();
95 if ( $user->isAnon() ) {
96 throw new InvalidArgumentException( 'Invalid ID' );
97 }
98
99 return new self( $user, $verified );
100 }
101
108 public static function newFromName( $name, $verified = false ) {
109 $user = MediaWikiServices::getInstance()->getUserFactory()->newFromName(
110 (string)$name,
111 UserRigorOptions::RIGOR_USABLE
112 );
113 if ( !$user ) {
114 throw new InvalidArgumentException( 'Invalid user name' );
115 }
116 return new self( $user, $verified );
117 }
118
125 public static function newFromUser( User $user, $verified = false ) {
126 return new self( $user, $verified );
127 }
128
133 public function isAnon() {
134 return $this->user === null;
135 }
136
141 public function isVerified() {
142 return $this->verified;
143 }
144
150 public function getId() {
151 return $this->user === null ? 0 : $this->user->getId();
152 }
153
158 public function getName() {
159 return $this->user === null ? null : $this->user->getName();
160 }
161
166 public function getToken() {
167 return $this->user === null || $this->user->getId() === 0 ? '' : $this->user->getToken( false );
168 }
169
174 public function getUser() {
175 return $this->user ?? MediaWikiServices::getInstance()->getUserFactory()->newAnonymous();
176 }
177
182 public function verified() {
183 return $this->verified ? $this : new self( $this->user, true );
184 }
185
186 public function __toString() {
187 if ( $this->user === null ) {
188 return '<anon>';
189 }
190 return '<' .
191 ( $this->verified ? '+' : '-' ) . ':' .
192 $this->getId() . ':' . $this->getName() .
193 '>';
194 }
195
196}
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Object holding data about a session's user.
Definition UserInfo.php:55
getId()
Return the user ID.
Definition UserInfo.php:150
static newFromName( $name, $verified=false)
Create an instance for a logged-in user by name.
Definition UserInfo.php:108
isVerified()
Return whether this represents a verified user.
Definition UserInfo.php:141
static newAnonymous()
Create an instance for an anonymous (i.e.
Definition UserInfo.php:80
isAnon()
Return whether this is an anonymous user.
Definition UserInfo.php:133
getUser()
Return a User object.
Definition UserInfo.php:174
getName()
Return the user name.
Definition UserInfo.php:158
static newFromUser(User $user, $verified=false)
Create an instance from an existing User object.
Definition UserInfo.php:125
static newFromId( $id, $verified=false)
Create an instance for a logged-in user by ID.
Definition UserInfo.php:90
verified()
Return a verified version of this object.
Definition UserInfo.php:182
getToken()
Return the user token.
Definition UserInfo.php:166
internal since 1.36
Definition User.php:93
load( $flags=IDBAccessObject::READ_NORMAL)
Load the user table data for this object from the source given by mFrom.
Definition User.php:348
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition User.php:614
isAnon()
Get whether the user is anonymous.
Definition User.php:2122
getName()
Get the user name, or the IP of an anonymous user.
Definition User.php:1566
static newFromName( $name, $validate='valid')
Definition User.php:580
Shared interface for rigor levels when dealing with User methods.