MediaWiki  master
SessionInfo.php
Go to the documentation of this file.
1 <?php
24 namespace MediaWiki\Session;
25 
37 class SessionInfo {
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() {
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 }
MediaWiki\Session\SessionInfo\forceHTTPS
forceHTTPS()
Whether this session should only be used over HTTPS.
Definition: SessionInfo.php:285
MediaWiki\Session\SessionProvider\getManager
getManager()
Get the session manager.
Definition: SessionProvider.php:138
MediaWiki\Session\SessionInfo\$idIsSafe
bool $idIsSafe
Definition: SessionInfo.php:66
MediaWiki\Session\SessionInfo\$priority
int $priority
Definition: SessionInfo.php:51
MediaWiki\Session\SessionInfo\$forceUse
bool $forceUse
Definition: SessionInfo.php:69
MediaWiki\Session\UserInfo
Object holding data about a session's user.
Definition: UserInfo.php:51
MediaWiki\Session\SessionInfo\compare
static compare( $a, $b)
Compare two SessionInfo objects by priority.
Definition: SessionInfo.php:301
MediaWiki\Session\SessionInfo\getPriority
getPriority()
Return the priority.
Definition: SessionInfo.php:232
MediaWiki\Session\SessionInfo\getId
getId()
Return the session ID.
Definition: SessionInfo.php:193
MediaWiki\Session\SessionInfo\forceUse
forceUse()
Force use of this SessionInfo if validation fails.
Definition: SessionInfo.php:224
MediaWiki\Session\SessionManager\validateSessionId
static validateSessionId( $id)
Validate a session ID.
Definition: SessionManager.php:389
MediaWiki\Session\SessionInfo\__toString
__toString()
Definition: SessionInfo.php:289
MediaWiki\Session\SessionInfo\$providerMetadata
array null $providerMetadata
Definition: SessionInfo.php:72
MediaWiki\Session\SessionProvider
A SessionProvider provides SessionInfo and support for Session.
Definition: SessionProvider.php:81
MediaWiki\Session\SessionInfo\getProvider
getProvider()
Return the provider.
Definition: SessionInfo.php:185
MediaWiki\Session\SessionInfo\$id
string $id
Definition: SessionInfo.php:48
MediaWiki\Session
Definition: BotPasswordSessionProvider.php:24
MediaWiki\Session\SessionInfo\wasPersisted
wasPersisted()
Return whether the session is persisted.
Definition: SessionInfo.php:248
MediaWiki\Session\SessionInfo\__construct
__construct( $priority, array $data)
Stable to call.
Definition: SessionInfo.php:102
MediaWiki\Session\SessionInfo\getProviderMetadata
getProviderMetadata()
Return provider metadata.
Definition: SessionInfo.php:256
MediaWiki\Session\SessionInfo\MAX_PRIORITY
const MAX_PRIORITY
Maximum allowed priority.
Definition: SessionInfo.php:42
MediaWiki\Session\SessionInfo\$persisted
bool $persisted
Definition: SessionInfo.php:57
MediaWiki\Session\SessionInfo\$provider
SessionProvider null $provider
Definition: SessionInfo.php:45
MediaWiki\Session\SessionInfo
Value object returned by SessionProvider.
Definition: SessionInfo.php:37
MediaWiki\Session\SessionInfo\$userInfo
UserInfo null $userInfo
Definition: SessionInfo.php:54
MediaWiki\Session\SessionInfo\$remembered
bool $remembered
Definition: SessionInfo.php:60
MediaWiki\Session\SessionInfo\$forceHTTPS
bool $forceHTTPS
Definition: SessionInfo.php:63
MediaWiki\Session\SessionInfo\MIN_PRIORITY
const MIN_PRIORITY
Minimum allowed priority.
Definition: SessionInfo.php:39
MediaWiki\Session\SessionInfo\getUserInfo
getUserInfo()
Return the user.
Definition: SessionInfo.php:240
MediaWiki\Session\SessionInfo\wasRemembered
wasRemembered()
Return whether the user was remembered.
Definition: SessionInfo.php:275
MediaWiki\Session\SessionInfo\isIdSafe
isIdSafe()
Indicate whether the ID is "safe".
Definition: SessionInfo.php:209