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
24 namespace MediaWiki\Session
;
29 * Object holding data about a session's user
31 * In general, this class exists for two purposes:
32 * - User doesn't distinguish between "anonymous user" and "non-anonymous user
33 * that doesn't exist locally", while we do need to.
34 * - We also need the "verified" property described below; tracking it via
35 * another data item to SessionInfo's constructor makes things much more
38 * A UserInfo may be "verified". This indicates that the creator knows that the
39 * request really comes from that user, whether that's by validating OAuth
40 * credentials, SSL client certificates, or by having both the user ID and
41 * token available from cookies.
43 * An "unverified" UserInfo should be used when it's not possible to
44 * authenticate the user, e.g. the user ID cookie is set but the user Token
45 * cookie isn't. If the Token is available but doesn't match, don't return a
51 final class UserInfo
{
52 private $verified = false;
57 private function __construct( User
$user = null, $verified ) {
58 if ( $user && $user->isAnon() && !User
::isUsableName( $user->getName() ) ) {
59 $this->verified
= true;
62 $this->verified
= $verified;
68 * Create an instance for an anonymous (i.e. not logged in) user
70 * Logged-out users are always "verified".
74 public static function newAnonymous() {
75 return new self( null, true );
79 * Create an instance for a logged-in user by ID
80 * @param int $id User ID
81 * @param bool $verified True if the user is verified
84 public static function newFromId( $id, $verified = false ) {
85 $user = User
::newFromId( $id );
87 // Ensure the ID actually exists
89 if ( $user->isAnon() ) {
90 throw new \
InvalidArgumentException( 'Invalid ID' );
93 return new self( $user, $verified );
97 * Create an instance for a logged-in user by name
98 * @param string $name User name (need not exist locally)
99 * @param bool $verified True if the user is verified
102 public static function newFromName( $name, $verified = false ) {
103 $user = User
::newFromName( $name, 'usable' );
105 throw new \
InvalidArgumentException( 'Invalid user name' );
107 return new self( $user, $verified );
111 * Create an instance from an existing User object
112 * @param User $user (need not exist locally)
113 * @param bool $verified True if the user is verified
116 public static function newFromUser( User
$user, $verified = false ) {
117 return new self( $user, $verified );
121 * Return whether this is an anonymous user
124 public function isAnon() {
125 return $this->user
=== null;
129 * Return whether this represents a verified user
132 public function isVerified() {
133 return $this->verified
;
138 * @note Do not use this to test for anonymous users!
141 public function getId() {
142 return $this->user
=== null ?
0 : $this->user
->getId();
146 * Return the user name
147 * @return string|null
149 public function getName() {
150 return $this->user
=== null ?
null : $this->user
->getName();
154 * Return the user token
157 public function getToken() {
158 return $this->user
=== null ||
$this->user
->getId() === 0 ?
'' : $this->user
->getToken( false );
162 * Return a User object
165 public function getUser() {
166 return $this->user
=== null ?
new User
: $this->user
;
170 * Return a verified version of this object
173 public function verified() {
174 return $this->verified ?
$this : new self( $this->user
, true );
177 public function __toString() {
178 if ( $this->user
=== null ) {
182 ( $this->verified ?
'+' : '-' ) . ':' .
183 $this->getId() . ':' . $this->getName() .