MediaWiki master
SessionInfo.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Session;
25
26use InvalidArgumentException;
27
41 public const MIN_PRIORITY = 1;
42
44 public const MAX_PRIORITY = 100;
45
47 private $provider;
48
50 private $id;
51
53 private $priority;
54
56 private $userInfo = null;
57
59 private $persisted = false;
60
62 private $remembered = false;
63
65 private $forceHTTPS = false;
66
68 private $idIsSafe = false;
69
71 private $forceUse = false;
72
74 private $providerMetadata = null;
75
104 public function __construct( $priority, array $data ) {
105 if ( $priority < self::MIN_PRIORITY || $priority > self::MAX_PRIORITY ) {
106 throw new InvalidArgumentException( 'Invalid priority' );
107 }
108
109 if ( isset( $data['copyFrom'] ) ) {
110 $from = $data['copyFrom'];
111 if ( !$from instanceof SessionInfo ) {
112 throw new InvalidArgumentException( 'Invalid copyFrom' );
113 }
114 $data += [
115 'provider' => $from->provider,
116 'id' => $from->id,
117 'userInfo' => $from->userInfo,
118 'persisted' => $from->persisted,
119 'remembered' => $from->remembered,
120 'forceHTTPS' => $from->forceHTTPS,
121 'metadata' => $from->providerMetadata,
122 'idIsSafe' => $from->idIsSafe,
123 'forceUse' => $from->forceUse,
124 // @codeCoverageIgnoreStart
125 ];
126 // @codeCoverageIgnoreEnd
127 } else {
128 $data += [
129 'provider' => null,
130 'id' => null,
131 'userInfo' => null,
132 'persisted' => false,
133 'remembered' => true,
134 'forceHTTPS' => false,
135 'metadata' => null,
136 'idIsSafe' => false,
137 'forceUse' => false,
138 // @codeCoverageIgnoreStart
139 ];
140 // @codeCoverageIgnoreEnd
141 }
142
143 if ( $data['id'] !== null && !SessionManager::validateSessionId( $data['id'] ) ) {
144 throw new InvalidArgumentException( 'Invalid session ID' );
145 }
146
147 if ( $data['userInfo'] !== null && !$data['userInfo'] instanceof UserInfo ) {
148 throw new InvalidArgumentException( 'Invalid userInfo' );
149 }
150
151 if ( !$data['provider'] && $data['id'] === null ) {
152 throw new InvalidArgumentException(
153 'Must supply an ID when no provider is given'
154 );
155 }
156
157 if ( $data['metadata'] !== null && !is_array( $data['metadata'] ) ) {
158 throw new InvalidArgumentException( 'Invalid metadata' );
159 }
160
161 $this->provider = $data['provider'];
162 if ( $data['id'] !== null ) {
163 $this->id = $data['id'];
164 $this->idIsSafe = $data['idIsSafe'];
165 $this->forceUse = $data['forceUse'] && $this->provider;
166 } else {
167 $this->id = $this->provider->getManager()->generateSessionId();
168 $this->idIsSafe = true;
169 $this->forceUse = false;
170 }
171 $this->priority = (int)$priority;
172 $this->userInfo = $data['userInfo'];
173 $this->persisted = (bool)$data['persisted'];
174 if ( $data['provider'] !== null ) {
175 if ( $this->userInfo !== null && !$this->userInfo->isAnon() && $this->userInfo->isVerified() ) {
176 $this->remembered = (bool)$data['remembered'];
177 }
178 $this->providerMetadata = $data['metadata'];
179 }
180 $this->forceHTTPS = (bool)$data['forceHTTPS'];
181 }
182
187 final public function getProvider() {
188 return $this->provider;
189 }
190
195 final public function getId() {
196 return $this->id;
197 }
198
211 final public function isIdSafe() {
212 return $this->idIsSafe;
213 }
214
226 final public function forceUse() {
227 return $this->forceUse;
228 }
229
234 final public function getPriority() {
235 return $this->priority;
236 }
237
242 final public function getUserInfo() {
243 return $this->userInfo;
244 }
245
250 final public function wasPersisted() {
251 return $this->persisted;
252 }
253
258 final public function getProviderMetadata() {
259 return $this->providerMetadata;
260 }
261
277 final public function wasRemembered() {
278 return $this->remembered;
279 }
280
287 final public function forceHTTPS() {
288 return $this->forceHTTPS;
289 }
290
291 public function __toString() {
292 return '[' . $this->getPriority() . ']' .
293 ( $this->getProvider() ?: 'null' ) .
294 ( $this->userInfo ?: '<null>' ) . $this->getId();
295 }
296
303 public static function compare( $a, $b ) {
304 return $a->getPriority() <=> $b->getPriority();
305 }
306
307}
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:54