MediaWiki REL1_34
SessionInfo.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Session;
25
36 const MIN_PRIORITY = 1;
37
39 const MAX_PRIORITY = 100;
40
42 private $provider;
43
45 private $id;
46
48 private $priority;
49
51 private $userInfo = null;
52
54 private $persisted = false;
55
57 private $remembered = false;
58
60 private $forceHTTPS = false;
61
63 private $idIsSafe = false;
64
66 private $forceUse = false;
67
69 private $providerMetadata = null;
70
97 public function __construct( $priority, array $data ) {
98 if ( $priority < self::MIN_PRIORITY || $priority > self::MAX_PRIORITY ) {
99 throw new \InvalidArgumentException( 'Invalid priority' );
100 }
101
102 if ( isset( $data['copyFrom'] ) ) {
103 $from = $data['copyFrom'];
104 if ( !$from instanceof SessionInfo ) {
105 throw new \InvalidArgumentException( 'Invalid copyFrom' );
106 }
107 $data += [
108 'provider' => $from->provider,
109 'id' => $from->id,
110 'userInfo' => $from->userInfo,
111 'persisted' => $from->persisted,
112 'remembered' => $from->remembered,
113 'forceHTTPS' => $from->forceHTTPS,
114 'metadata' => $from->providerMetadata,
115 'idIsSafe' => $from->idIsSafe,
116 'forceUse' => $from->forceUse,
117 // @codeCoverageIgnoreStart
118 ];
119 // @codeCoverageIgnoreEnd
120 } else {
121 $data += [
122 'provider' => null,
123 'id' => null,
124 'userInfo' => null,
125 'persisted' => false,
126 'remembered' => true,
127 'forceHTTPS' => false,
128 'metadata' => null,
129 'idIsSafe' => false,
130 'forceUse' => false,
131 // @codeCoverageIgnoreStart
132 ];
133 // @codeCoverageIgnoreEnd
134 }
135
136 if ( $data['id'] !== null && !SessionManager::validateSessionId( $data['id'] ) ) {
137 throw new \InvalidArgumentException( 'Invalid session ID' );
138 }
139
140 if ( $data['userInfo'] !== null && !$data['userInfo'] instanceof UserInfo ) {
141 throw new \InvalidArgumentException( 'Invalid userInfo' );
142 }
143
144 if ( !$data['provider'] && $data['id'] === null ) {
145 throw new \InvalidArgumentException(
146 'Must supply an ID when no provider is given'
147 );
148 }
149
150 if ( $data['metadata'] !== null && !is_array( $data['metadata'] ) ) {
151 throw new \InvalidArgumentException( 'Invalid metadata' );
152 }
153
154 $this->provider = $data['provider'];
155 if ( $data['id'] !== null ) {
156 $this->id = $data['id'];
157 $this->idIsSafe = $data['idIsSafe'];
158 $this->forceUse = $data['forceUse'] && $this->provider;
159 } else {
160 // @phan-suppress-next-line PhanUndeclaredMethod
161 $this->id = $this->provider->getManager()->generateSessionId();
162 $this->idIsSafe = true;
163 $this->forceUse = false;
164 }
165 $this->priority = (int)$priority;
166 $this->userInfo = $data['userInfo'];
167 $this->persisted = (bool)$data['persisted'];
168 if ( $data['provider'] !== null ) {
169 if ( $this->userInfo !== null && !$this->userInfo->isAnon() && $this->userInfo->isVerified() ) {
170 $this->remembered = (bool)$data['remembered'];
171 }
172 $this->providerMetadata = $data['metadata'];
173 }
174 $this->forceHTTPS = (bool)$data['forceHTTPS'];
175 }
176
181 final public function getProvider() {
182 return $this->provider;
183 }
184
189 final public function getId() {
190 return $this->id;
191 }
192
205 final public function isIdSafe() {
206 return $this->idIsSafe;
207 }
208
220 final public function forceUse() {
221 return $this->forceUse;
222 }
223
228 final public function getPriority() {
229 return $this->priority;
230 }
231
236 final public function getUserInfo() {
237 return $this->userInfo;
238 }
239
244 final public function wasPersisted() {
245 return $this->persisted;
246 }
247
252 final public function getProviderMetadata() {
254 }
255
271 final public function wasRemembered() {
272 return $this->remembered;
273 }
274
281 final public function forceHTTPS() {
282 return $this->forceHTTPS;
283 }
284
285 public function __toString() {
286 return '[' . $this->getPriority() . ']' .
287 ( $this->getProvider() ?: 'null' ) .
288 ( $this->userInfo ?: '<null>' ) . $this->getId();
289 }
290
297 public static function compare( $a, $b ) {
298 return $a->getPriority() <=> $b->getPriority();
299 }
300
301}
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.
SessionProvider null $provider
static validateSessionId( $id)
Validate a session ID.
A SessionProvider provides SessionInfo and support for Session.
getManager()
Get the session manager.
Object holding data about a session's user.
Definition UserInfo.php:51