Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / session / UserInfo.php
bloba55725625624fcb60ab648bd19f509c68c74e297
1 <?php
2 /**
3 * MediaWiki session user info
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Session
24 namespace MediaWiki\Session;
26 use InvalidArgumentException;
27 use MediaWiki\MediaWikiServices;
28 use MediaWiki\User\User;
29 use MediaWiki\User\UserRigorOptions;
30 use Stringable;
32 /**
33 * Object holding data about a session's user
35 * In general, this class exists for two purposes:
36 * - User doesn't distinguish between "anonymous user" and "non-anonymous user
37 * that doesn't exist locally", while we do need to.
38 * - We also need the "verified" property described below; tracking it via
39 * another data item to SessionInfo's constructor makes things much more
40 * confusing.
42 * A UserInfo may be "verified". This indicates that the creator knows that the
43 * request really comes from that user, whether that's by validating OAuth
44 * credentials, SSL client certificates, or by having both the user ID and
45 * token available from cookies.
47 * An "unverified" UserInfo should be used when it's not possible to
48 * authenticate the user, e.g. the user ID cookie is set but the user Token
49 * cookie isn't. If the Token is available but doesn't match, don't return a
50 * UserInfo at all.
52 * @ingroup Session
53 * @since 1.27
55 final class UserInfo implements Stringable {
56 /** @var bool */
57 private $verified = false;
59 /** @var User|null */
60 private $user = null;
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;
73 /**
74 * Create an instance for an anonymous (i.e. not logged in) user
76 * Logged-out users are always "verified".
78 * @return UserInfo
80 public static function newAnonymous() {
81 return new self( null, true );
84 /**
85 * Create an instance for a logged-in user by ID
86 * @param int $id User ID
87 * @param bool $verified True if the user is verified
88 * @return UserInfo
90 public static function newFromId( $id, $verified = false ) {
91 $user = MediaWikiServices::getInstance()->getUserFactory()->newFromId( (int)$id );
93 // Ensure the ID actually exists
94 $user->load();
95 if ( $user->isAnon() ) {
96 throw new InvalidArgumentException( 'Invalid ID' );
99 return new self( $user, $verified );
103 * Create an instance for a logged-in user by name
104 * @param string $name User name (need not exist locally)
105 * @param bool $verified True if the user is verified
106 * @return UserInfo
108 public static function newFromName( $name, $verified = false ) {
109 $user = MediaWikiServices::getInstance()->getUserFactory()->newFromName(
110 (string)$name,
111 UserRigorOptions::RIGOR_USABLE
113 if ( !$user ) {
114 throw new InvalidArgumentException( 'Invalid user name' );
116 return new self( $user, $verified );
120 * Create an instance from an existing User object
121 * @param User $user (need not exist locally)
122 * @param bool $verified True if the user is verified
123 * @return UserInfo
125 public static function newFromUser( User $user, $verified = false ) {
126 return new self( $user, $verified );
130 * Return whether this is an anonymous user
131 * @return bool
133 public function isAnon() {
134 return $this->user === null;
138 * Return whether this represents a verified user
139 * @return bool
141 public function isVerified() {
142 return $this->verified;
146 * Return the user ID
147 * @note Do not use this to test for anonymous users!
148 * @return int
150 public function getId() {
151 return $this->user === null ? 0 : $this->user->getId();
155 * Return the user name
156 * @return string|null
158 public function getName() {
159 return $this->user === null ? null : $this->user->getName();
163 * Return the user token
164 * @return string
166 public function getToken() {
167 return $this->user === null || $this->user->getId() === 0 ? '' : $this->user->getToken( false );
171 * Return a User object
172 * @return User
174 public function getUser() {
175 return $this->user ?? MediaWikiServices::getInstance()->getUserFactory()->newAnonymous();
179 * Return a verified version of this object
180 * @return UserInfo
182 public function verified() {
183 return $this->verified ? $this : new self( $this->user, true );
186 public function __toString() {
187 if ( $this->user === null ) {
188 return '<anon>';
190 return '<' .
191 ( $this->verified ? '+' : '-' ) . ':' .
192 $this->getId() . ':' . $this->getName() .
193 '>';