MediaWiki master
SessionInfo.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Session;
25
26use InvalidArgumentException;
27use Stringable;
28
40class SessionInfo implements Stringable {
42 public const MIN_PRIORITY = 1;
43
45 public const MAX_PRIORITY = 100;
46
48 private $provider;
49
51 private $id;
52
54 private $priority;
55
57 private $userInfo = null;
58
60 private $persisted = false;
61
63 private $remembered = false;
64
66 private $forceHTTPS = false;
67
69 private $idIsSafe = false;
70
72 private $forceUse = false;
73
75 private $providerMetadata = null;
76
105 public function __construct( $priority, array $data ) {
106 if ( $priority < self::MIN_PRIORITY || $priority > self::MAX_PRIORITY ) {
107 throw new InvalidArgumentException( 'Invalid priority' );
108 }
109
110 if ( isset( $data['copyFrom'] ) ) {
111 $from = $data['copyFrom'];
112 if ( !$from instanceof SessionInfo ) {
113 throw new InvalidArgumentException( 'Invalid copyFrom' );
114 }
115 $data += [
116 'provider' => $from->provider,
117 'id' => $from->id,
118 'userInfo' => $from->userInfo,
119 'persisted' => $from->persisted,
120 'remembered' => $from->remembered,
121 'forceHTTPS' => $from->forceHTTPS,
122 'metadata' => $from->providerMetadata,
123 'idIsSafe' => $from->idIsSafe,
124 'forceUse' => $from->forceUse,
125 // @codeCoverageIgnoreStart
126 ];
127 // @codeCoverageIgnoreEnd
128 } else {
129 $data += [
130 'provider' => null,
131 'id' => null,
132 'userInfo' => null,
133 'persisted' => false,
134 'remembered' => true,
135 'forceHTTPS' => false,
136 'metadata' => null,
137 'idIsSafe' => false,
138 'forceUse' => false,
139 // @codeCoverageIgnoreStart
140 ];
141 // @codeCoverageIgnoreEnd
142 }
143
144 if ( $data['id'] !== null && !SessionManager::validateSessionId( $data['id'] ) ) {
145 throw new InvalidArgumentException( 'Invalid session ID' );
146 }
147
148 if ( $data['userInfo'] !== null && !$data['userInfo'] instanceof UserInfo ) {
149 throw new InvalidArgumentException( 'Invalid userInfo' );
150 }
151
152 if ( !$data['provider'] && $data['id'] === null ) {
153 throw new InvalidArgumentException(
154 'Must supply an ID when no provider is given'
155 );
156 }
157
158 if ( $data['metadata'] !== null && !is_array( $data['metadata'] ) ) {
159 throw new InvalidArgumentException( 'Invalid metadata' );
160 }
161
162 $this->provider = $data['provider'];
163 if ( $data['id'] !== null ) {
164 $this->id = $data['id'];
165 $this->idIsSafe = $data['idIsSafe'];
166 $this->forceUse = $data['forceUse'] && $this->provider;
167 } else {
168 $this->id = $this->provider->getManager()->generateSessionId();
169 $this->idIsSafe = true;
170 $this->forceUse = false;
171 }
172 $this->priority = (int)$priority;
173 $this->userInfo = $data['userInfo'];
174 $this->persisted = (bool)$data['persisted'];
175 if ( $data['provider'] !== null ) {
176 if ( $this->userInfo !== null && !$this->userInfo->isAnon() && $this->userInfo->isVerified() ) {
177 $this->remembered = (bool)$data['remembered'];
178 }
179 $this->providerMetadata = $data['metadata'];
180 }
181 $this->forceHTTPS = (bool)$data['forceHTTPS'];
182 }
183
188 final public function getProvider() {
189 return $this->provider;
190 }
191
196 final public function getId() {
197 return $this->id;
198 }
199
212 final public function isIdSafe() {
213 return $this->idIsSafe;
214 }
215
227 final public function forceUse() {
228 return $this->forceUse;
229 }
230
235 final public function getPriority() {
236 return $this->priority;
237 }
238
243 final public function getUserInfo() {
244 return $this->userInfo;
245 }
246
251 final public function wasPersisted() {
252 return $this->persisted;
253 }
254
259 final public function getProviderMetadata() {
260 return $this->providerMetadata;
261 }
262
278 final public function wasRemembered() {
279 return $this->remembered;
280 }
281
288 final public function forceHTTPS() {
289 return $this->forceHTTPS;
290 }
291
292 public function __toString() {
293 return '[' . $this->getPriority() . ']' .
294 ( $this->getProvider() ?: 'null' ) .
295 ( $this->userInfo ?: '<null>' ) . $this->getId();
296 }
297
304 public static function compare( $a, $b ) {
305 return $a->getPriority() <=> $b->getPriority();
306 }
307
308}
Value object returned by SessionProvider.
forceUse()
Force use of this SessionInfo if validation fails.
getProviderMetadata()
Return provider metadata.
getId()
Return the session ID.
getProvider()
Return the provider.
isIdSafe()
Indicate whether the ID is "safe".
getUserInfo()
Return the user.
wasPersisted()
Return whether the session is persisted.
const MIN_PRIORITY
Minimum allowed priority.
const MAX_PRIORITY
Maximum allowed priority.
getPriority()
Return the priority.
__construct( $priority, array $data)
wasRemembered()
Return whether the user was remembered.
forceHTTPS()
Whether this session should only be used over HTTPS.
static compare( $a, $b)
Compare two SessionInfo objects by priority.
static validateSessionId( $id)
Validate a session ID.
A SessionProvider provides SessionInfo and support for Session.
Object holding data about a session's user.
Definition UserInfo.php:55