]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/session/UserInfo.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.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         private $verified = false;
53
54         /** @var User|null */
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
67         /**
68          * Create an instance for an anonymous (i.e. not logged in) user
69          *
70          * Logged-out users are always "verified".
71          *
72          * @return UserInfo
73          */
74         public static function newAnonymous() {
75                 return new self( null, true );
76         }
77
78         /**
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
82          * @return UserInfo
83          */
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
96         /**
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
100          * @return UserInfo
101          */
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
110         /**
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
114          * @return UserInfo
115          */
116         public static function newFromUser( User $user, $verified = false ) {
117                 return new self( $user, $verified );
118         }
119
120         /**
121          * Return whether this is an anonymous user
122          * @return bool
123          */
124         public function isAnon() {
125                 return $this->user === null;
126         }
127
128         /**
129          * Return whether this represents a verified user
130          * @return bool
131          */
132         public function isVerified() {
133                 return $this->verified;
134         }
135
136         /**
137          * Return the user ID
138          * @note Do not use this to test for anonymous users!
139          * @return int
140          */
141         public function getId() {
142                 return $this->user === null ? 0 : $this->user->getId();
143         }
144
145         /**
146          * Return the user name
147          * @return string|null
148          */
149         public function getName() {
150                 return $this->user === null ? null : $this->user->getName();
151         }
152
153         /**
154          * Return the user token
155          * @return string
156          */
157         public function getToken() {
158                 return $this->user === null || $this->user->getId() === 0 ? '' : $this->user->getToken( false );
159         }
160
161         /**
162          * Return a User object
163          * @return User
164          */
165         public function getUser() {
166                 return $this->user === null ? new User : $this->user;
167         }
168
169         /**
170          * Return a verified version of this object
171          * @return UserInfo
172          */
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 }