MediaWiki master
SessionInfo.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Session;
25
39 public const MIN_PRIORITY = 1;
40
42 public const MAX_PRIORITY = 100;
43
45 private $provider;
46
48 private $id;
49
51 private $priority;
52
54 private $userInfo = null;
55
57 private $persisted = false;
58
60 private $remembered = false;
61
63 private $forceHTTPS = false;
64
66 private $idIsSafe = false;
67
69 private $forceUse = false;
70
72 private $providerMetadata = null;
73
102 public function __construct( $priority, array $data ) {
103 if ( $priority < self::MIN_PRIORITY || $priority > self::MAX_PRIORITY ) {
104 throw new \InvalidArgumentException( 'Invalid priority' );
105 }
106
107 if ( isset( $data['copyFrom'] ) ) {
108 $from = $data['copyFrom'];
109 if ( !$from instanceof SessionInfo ) {
110 throw new \InvalidArgumentException( 'Invalid copyFrom' );
111 }
112 $data += [
113 'provider' => $from->provider,
114 'id' => $from->id,
115 'userInfo' => $from->userInfo,
116 'persisted' => $from->persisted,
117 'remembered' => $from->remembered,
118 'forceHTTPS' => $from->forceHTTPS,
119 'metadata' => $from->providerMetadata,
120 'idIsSafe' => $from->idIsSafe,
121 'forceUse' => $from->forceUse,
122 // @codeCoverageIgnoreStart
123 ];
124 // @codeCoverageIgnoreEnd
125 } else {
126 $data += [
127 'provider' => null,
128 'id' => null,
129 'userInfo' => null,
130 'persisted' => false,
131 'remembered' => true,
132 'forceHTTPS' => false,
133 'metadata' => null,
134 'idIsSafe' => false,
135 'forceUse' => false,
136 // @codeCoverageIgnoreStart
137 ];
138 // @codeCoverageIgnoreEnd
139 }
140
141 if ( $data['id'] !== null && !SessionManager::validateSessionId( $data['id'] ) ) {
142 throw new \InvalidArgumentException( 'Invalid session ID' );
143 }
144
145 if ( $data['userInfo'] !== null && !$data['userInfo'] instanceof UserInfo ) {
146 throw new \InvalidArgumentException( 'Invalid userInfo' );
147 }
148
149 if ( !$data['provider'] && $data['id'] === null ) {
150 throw new \InvalidArgumentException(
151 'Must supply an ID when no provider is given'
152 );
153 }
154
155 if ( $data['metadata'] !== null && !is_array( $data['metadata'] ) ) {
156 throw new \InvalidArgumentException( 'Invalid metadata' );
157 }
158
159 $this->provider = $data['provider'];
160 if ( $data['id'] !== null ) {
161 $this->id = $data['id'];
162 $this->idIsSafe = $data['idIsSafe'];
163 $this->forceUse = $data['forceUse'] && $this->provider;
164 } else {
165 $this->id = $this->provider->getManager()->generateSessionId();
166 $this->idIsSafe = true;
167 $this->forceUse = false;
168 }
169 $this->priority = (int)$priority;
170 $this->userInfo = $data['userInfo'];
171 $this->persisted = (bool)$data['persisted'];
172 if ( $data['provider'] !== null ) {
173 if ( $this->userInfo !== null && !$this->userInfo->isAnon() && $this->userInfo->isVerified() ) {
174 $this->remembered = (bool)$data['remembered'];
175 }
176 $this->providerMetadata = $data['metadata'];
177 }
178 $this->forceHTTPS = (bool)$data['forceHTTPS'];
179 }
180
185 final public function getProvider() {
186 return $this->provider;
187 }
188
193 final public function getId() {
194 return $this->id;
195 }
196
209 final public function isIdSafe() {
210 return $this->idIsSafe;
211 }
212
224 final public function forceUse() {
225 return $this->forceUse;
226 }
227
232 final public function getPriority() {
233 return $this->priority;
234 }
235
240 final public function getUserInfo() {
241 return $this->userInfo;
242 }
243
248 final public function wasPersisted() {
249 return $this->persisted;
250 }
251
256 final public function getProviderMetadata() {
257 return $this->providerMetadata;
258 }
259
275 final public function wasRemembered() {
276 return $this->remembered;
277 }
278
285 final public function forceHTTPS() {
286 return $this->forceHTTPS;
287 }
288
289 public function __toString() {
290 return '[' . $this->getPriority() . ']' .
291 ( $this->getProvider() ?: 'null' ) .
292 ( $this->userInfo ?: '<null>' ) . $this->getId();
293 }
294
301 public static function compare( $a, $b ) {
302 return $a->getPriority() <=> $b->getPriority();
303 }
304
305}
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:53