EditPage: Style anonymous editor warning as a warningbox for consistency
[mediawiki.git] / includes / session / SessionInfo.php
blob1b5a834c9460ddd8bb35cc1627e5683449946cc2
1 <?php
2 /**
3 * MediaWiki session 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 /**
27 * Value object returned by SessionProvider
29 * This holds the data necessary to construct a Session.
31 * @ingroup Session
32 * @since 1.27
34 class SessionInfo {
35 /** Minimum allowed priority */
36 const MIN_PRIORITY = 1;
38 /** Maximum allowed priority */
39 const MAX_PRIORITY = 100;
41 /** @var SessionProvider|null */
42 private $provider;
44 /** @var string */
45 private $id;
47 /** @var int */
48 private $priority;
50 /** @var UserInfo|null */
51 private $userInfo = null;
53 private $persisted = false;
54 private $remembered = false;
55 private $forceHTTPS = false;
56 private $idIsSafe = false;
58 /** @var array|null */
59 private $providerMetadata = null;
61 /**
62 * @param int $priority Session priority
63 * @param array $data
64 * - provider: (SessionProvider|null) If not given, the provider will be
65 * determined from the saved session data.
66 * - id: (string|null) Session ID
67 * - userInfo: (UserInfo|null) User known from the request. If
68 * $provider->canChangeUser() is false, a verified user
69 * must be provided.
70 * - persisted: (bool) Whether this session was persisted
71 * - remembered: (bool) Whether the verified user was remembered.
72 * Defaults to true.
73 * - forceHTTPS: (bool) Whether to force HTTPS for this session
74 * - metadata: (array) Provider metadata, to be returned by
75 * Session::getProviderMetadata().
76 * - idIsSafe: (bool) Set true if the 'id' did not come from the user.
77 * Generally you'll use this from SessionProvider::newEmptySession(),
78 * and not from any other method.
79 * - copyFrom: (SessionInfo) SessionInfo to copy other data items from.
81 public function __construct( $priority, array $data ) {
82 if ( $priority < self::MIN_PRIORITY || $priority > self::MAX_PRIORITY ) {
83 throw new \InvalidArgumentException( 'Invalid priority' );
86 if ( isset( $data['copyFrom'] ) ) {
87 $from = $data['copyFrom'];
88 if ( !$from instanceof SessionInfo ) {
89 throw new \InvalidArgumentException( 'Invalid copyFrom' );
91 $data += [
92 'provider' => $from->provider,
93 'id' => $from->id,
94 'userInfo' => $from->userInfo,
95 'persisted' => $from->persisted,
96 'remembered' => $from->remembered,
97 'forceHTTPS' => $from->forceHTTPS,
98 'metadata' => $from->providerMetadata,
99 'idIsSafe' => $from->idIsSafe,
100 // @codeCoverageIgnoreStart
102 // @codeCoverageIgnoreEnd
103 } else {
104 $data += [
105 'provider' => null,
106 'id' => null,
107 'userInfo' => null,
108 'persisted' => false,
109 'remembered' => true,
110 'forceHTTPS' => false,
111 'metadata' => null,
112 'idIsSafe' => false,
113 // @codeCoverageIgnoreStart
115 // @codeCoverageIgnoreEnd
118 if ( $data['id'] !== null && !SessionManager::validateSessionId( $data['id'] ) ) {
119 throw new \InvalidArgumentException( 'Invalid session ID' );
122 if ( $data['userInfo'] !== null && !$data['userInfo'] instanceof UserInfo ) {
123 throw new \InvalidArgumentException( 'Invalid userInfo' );
126 if ( !$data['provider'] && $data['id'] === null ) {
127 throw new \InvalidArgumentException(
128 'Must supply an ID when no provider is given'
132 if ( $data['metadata'] !== null && !is_array( $data['metadata'] ) ) {
133 throw new \InvalidArgumentException( 'Invalid metadata' );
136 $this->provider = $data['provider'];
137 if ( $data['id'] !== null ) {
138 $this->id = $data['id'];
139 $this->idIsSafe = $data['idIsSafe'];
140 } else {
141 $this->id = $this->provider->getManager()->generateSessionId();
142 $this->idIsSafe = true;
144 $this->priority = (int)$priority;
145 $this->userInfo = $data['userInfo'];
146 $this->persisted = (bool)$data['persisted'];
147 if ( $data['provider'] !== null ) {
148 if ( $this->userInfo !== null && !$this->userInfo->isAnon() && $this->userInfo->isVerified() ) {
149 $this->remembered = (bool)$data['remembered'];
151 $this->providerMetadata = $data['metadata'];
153 $this->forceHTTPS = (bool)$data['forceHTTPS'];
157 * Return the provider
158 * @return SessionProvider|null
160 final public function getProvider() {
161 return $this->provider;
165 * Return the session ID
166 * @return string
168 final public function getId() {
169 return $this->id;
173 * Indicate whether the ID is "safe"
175 * The ID is safe in the following cases:
176 * - The ID was randomly generated by the constructor.
177 * - The ID was found in the backend data store.
178 * - $this->getProvider()->persistsSessionId() is false.
179 * - The constructor was explicitly told it's safe using the 'idIsSafe'
180 * parameter.
182 * @return bool
184 final public function isIdSafe() {
185 return $this->idIsSafe;
189 * Return the priority
190 * @return int
192 final public function getPriority() {
193 return $this->priority;
197 * Return the user
198 * @return UserInfo|null
200 final public function getUserInfo() {
201 return $this->userInfo;
205 * Return whether the session is persisted
206 * @return bool
208 final public function wasPersisted() {
209 return $this->persisted;
213 * Return provider metadata
214 * @return array|null
216 final public function getProviderMetadata() {
217 return $this->providerMetadata;
221 * Return whether the user was remembered
223 * For providers that can persist the user separately from the session,
224 * the human using it may not actually *want* that to be done. For example,
225 * a cookie-based provider can set cookies that are longer-lived than the
226 * backend session data, but on a public terminal the human likely doesn't
227 * want those cookies set.
229 * This is false unless a non-anonymous verified user was passed to
230 * the SessionInfo constructor by the provider, and the provider didn't
231 * pass false for the 'remembered' data item.
233 * @return bool
235 final public function wasRemembered() {
236 return $this->remembered;
240 * Whether this session should only be used over HTTPS
241 * @return bool
243 final public function forceHTTPS() {
244 return $this->forceHTTPS;
247 public function __toString() {
248 return '[' . $this->getPriority() . ']' .
249 ( $this->getProvider() ?: 'null' ) .
250 ( $this->userInfo ?: '<null>' ) . $this->getId();
254 * Compare two SessionInfo objects by priority
255 * @param SessionInfo $a
256 * @param SessionInfo $b
257 * @return int Negative if $a < $b, positive if $a > $b, zero if equal
259 public static function compare( $a, $b ) {
260 return $a->getPriority() - $b->getPriority();