MediaWiki REL1_31
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
53 private $persisted = false;
54 private $remembered = false;
55 private $forceHTTPS = false;
56 private $idIsSafe = false;
57 private $forceUse = false;
58
60 private $providerMetadata = null;
61
88 public function __construct( $priority, array $data ) {
89 if ( $priority < self::MIN_PRIORITY || $priority > self::MAX_PRIORITY ) {
90 throw new \InvalidArgumentException( 'Invalid priority' );
91 }
92
93 if ( isset( $data['copyFrom'] ) ) {
94 $from = $data['copyFrom'];
95 if ( !$from instanceof SessionInfo ) {
96 throw new \InvalidArgumentException( 'Invalid copyFrom' );
97 }
98 $data += [
99 'provider' => $from->provider,
100 'id' => $from->id,
101 'userInfo' => $from->userInfo,
102 'persisted' => $from->persisted,
103 'remembered' => $from->remembered,
104 'forceHTTPS' => $from->forceHTTPS,
105 'metadata' => $from->providerMetadata,
106 'idIsSafe' => $from->idIsSafe,
107 'forceUse' => $from->forceUse,
108 // @codeCoverageIgnoreStart
109 ];
110 // @codeCoverageIgnoreEnd
111 } else {
112 $data += [
113 'provider' => null,
114 'id' => null,
115 'userInfo' => null,
116 'persisted' => false,
117 'remembered' => true,
118 'forceHTTPS' => false,
119 'metadata' => null,
120 'idIsSafe' => false,
121 'forceUse' => false,
122 // @codeCoverageIgnoreStart
123 ];
124 // @codeCoverageIgnoreEnd
125 }
126
127 if ( $data['id'] !== null && !SessionManager::validateSessionId( $data['id'] ) ) {
128 throw new \InvalidArgumentException( 'Invalid session ID' );
129 }
130
131 if ( $data['userInfo'] !== null && !$data['userInfo'] instanceof UserInfo ) {
132 throw new \InvalidArgumentException( 'Invalid userInfo' );
133 }
134
135 if ( !$data['provider'] && $data['id'] === null ) {
136 throw new \InvalidArgumentException(
137 'Must supply an ID when no provider is given'
138 );
139 }
140
141 if ( $data['metadata'] !== null && !is_array( $data['metadata'] ) ) {
142 throw new \InvalidArgumentException( 'Invalid metadata' );
143 }
144
145 $this->provider = $data['provider'];
146 if ( $data['id'] !== null ) {
147 $this->id = $data['id'];
148 $this->idIsSafe = $data['idIsSafe'];
149 $this->forceUse = $data['forceUse'] && $this->provider;
150 } else {
151 $this->id = $this->provider->getManager()->generateSessionId();
152 $this->idIsSafe = true;
153 $this->forceUse = false;
154 }
155 $this->priority = (int)$priority;
156 $this->userInfo = $data['userInfo'];
157 $this->persisted = (bool)$data['persisted'];
158 if ( $data['provider'] !== null ) {
159 if ( $this->userInfo !== null && !$this->userInfo->isAnon() && $this->userInfo->isVerified() ) {
160 $this->remembered = (bool)$data['remembered'];
161 }
162 $this->providerMetadata = $data['metadata'];
163 }
164 $this->forceHTTPS = (bool)$data['forceHTTPS'];
165 }
166
171 final public function getProvider() {
172 return $this->provider;
173 }
174
179 final public function getId() {
180 return $this->id;
181 }
182
195 final public function isIdSafe() {
196 return $this->idIsSafe;
197 }
198
210 final public function forceUse() {
211 return $this->forceUse;
212 }
213
218 final public function getPriority() {
219 return $this->priority;
220 }
221
226 final public function getUserInfo() {
227 return $this->userInfo;
228 }
229
234 final public function wasPersisted() {
235 return $this->persisted;
236 }
237
242 final public function getProviderMetadata() {
244 }
245
261 final public function wasRemembered() {
262 return $this->remembered;
263 }
264
271 final public function forceHTTPS() {
272 return $this->forceHTTPS;
273 }
274
275 public function __toString() {
276 return '[' . $this->getPriority() . ']' .
277 ( $this->getProvider() ?: 'null' ) .
278 ( $this->userInfo ?: '<null>' ) . $this->getId();
279 }
280
287 public static function compare( $a, $b ) {
288 return $a->getPriority() - $b->getPriority();
289 }
290
291}
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