session: Add debug message for the used store class
[lhc/web/wiklou.git] / includes / session / UserInfo.php
1 <?php
2 /**
3 * MediaWiki session user info
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Session
22 */
23
24 namespace MediaWiki\Session;
25
26 use User;
27
28 /**
29 * Object holding data about a session's user
30 *
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
36 * confusing.
37 *
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.
42 *
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
46 * UserInfo at all.
47 *
48 * @ingroup Session
49 * @since 1.27
50 */
51 final class UserInfo {
52 /** @var bool */
53 private $verified = false;
54
55 /** @var User|null */
56 private $user = null;
57
58 private function __construct( User $user = null, $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
68 /**
69 * Create an instance for an anonymous (i.e. not logged in) user
70 *
71 * Logged-out users are always "verified".
72 *
73 * @return UserInfo
74 */
75 public static function newAnonymous() {
76 return new self( null, true );
77 }
78
79 /**
80 * Create an instance for a logged-in user by ID
81 * @param int $id User ID
82 * @param bool $verified True if the user is verified
83 * @return UserInfo
84 */
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
97 /**
98 * Create an instance for a logged-in user by name
99 * @param string $name User name (need not exist locally)
100 * @param bool $verified True if the user is verified
101 * @return UserInfo
102 */
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
111 /**
112 * Create an instance from an existing User object
113 * @param User $user (need not exist locally)
114 * @param bool $verified True if the user is verified
115 * @return UserInfo
116 */
117 public static function newFromUser( User $user, $verified = false ) {
118 return new self( $user, $verified );
119 }
120
121 /**
122 * Return whether this is an anonymous user
123 * @return bool
124 */
125 public function isAnon() {
126 return $this->user === null;
127 }
128
129 /**
130 * Return whether this represents a verified user
131 * @return bool
132 */
133 public function isVerified() {
134 return $this->verified;
135 }
136
137 /**
138 * Return the user ID
139 * @note Do not use this to test for anonymous users!
140 * @return int
141 */
142 public function getId() {
143 return $this->user === null ? 0 : $this->user->getId();
144 }
145
146 /**
147 * Return the user name
148 * @return string|null
149 */
150 public function getName() {
151 return $this->user === null ? null : $this->user->getName();
152 }
153
154 /**
155 * Return the user token
156 * @return string
157 */
158 public function getToken() {
159 return $this->user === null || $this->user->getId() === 0 ? '' : $this->user->getToken( false );
160 }
161
162 /**
163 * Return a User object
164 * @return User
165 */
166 public function getUser() {
167 return $this->user === null ? new User : $this->user;
168 }
169
170 /**
171 * Return a verified version of this object
172 * @return UserInfo
173 */
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 }