MediaWiki  master
SessionInfo.php
Go to the documentation of this file.
1 <?php
24 namespace MediaWiki\Session;
25 
34 class SessionInfo {
36  public const MIN_PRIORITY = 1;
37 
39  public 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  $this->id = $this->provider->getManager()->generateSessionId();
161  $this->idIsSafe = true;
162  $this->forceUse = false;
163  }
164  $this->priority = (int)$priority;
165  $this->userInfo = $data['userInfo'];
166  $this->persisted = (bool)$data['persisted'];
167  if ( $data['provider'] !== null ) {
168  if ( $this->userInfo !== null && !$this->userInfo->isAnon() && $this->userInfo->isVerified() ) {
169  $this->remembered = (bool)$data['remembered'];
170  }
171  $this->providerMetadata = $data['metadata'];
172  }
173  $this->forceHTTPS = (bool)$data['forceHTTPS'];
174  }
175 
180  final public function getProvider() {
181  return $this->provider;
182  }
183 
188  final public function getId() {
189  return $this->id;
190  }
191 
204  final public function isIdSafe() {
205  return $this->idIsSafe;
206  }
207 
219  final public function forceUse() {
220  return $this->forceUse;
221  }
222 
227  final public function getPriority() {
228  return $this->priority;
229  }
230 
235  final public function getUserInfo() {
236  return $this->userInfo;
237  }
238 
243  final public function wasPersisted() {
244  return $this->persisted;
245  }
246 
251  final public function getProviderMetadata() {
253  }
254 
270  final public function wasRemembered() {
271  return $this->remembered;
272  }
273 
280  final public function forceHTTPS() {
281  return $this->forceHTTPS;
282  }
283 
284  public function __toString() {
285  return '[' . $this->getPriority() . ']' .
286  ( $this->getProvider() ?: 'null' ) .
287  ( $this->userInfo ?: '<null>' ) . $this->getId();
288  }
289 
296  public static function compare( $a, $b ) {
297  return $a->getPriority() <=> $b->getPriority();
298  }
299 
300 }
MediaWiki\Session\SessionInfo\forceHTTPS
forceHTTPS()
Whether this session should only be used over HTTPS.
Definition: SessionInfo.php:280
MediaWiki\Session\SessionProvider\getManager
getManager()
Get the session manager.
Definition: SessionProvider.php:136
MediaWiki\Session\SessionInfo\$idIsSafe
bool $idIsSafe
Definition: SessionInfo.php:63
MediaWiki\Session\SessionInfo\$priority
int $priority
Definition: SessionInfo.php:48
MediaWiki\Session\SessionInfo\$forceUse
bool $forceUse
Definition: SessionInfo.php:66
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:296
MediaWiki\Session\SessionInfo\getPriority
getPriority()
Return the priority.
Definition: SessionInfo.php:227
MediaWiki\Session\SessionInfo\getId
getId()
Return the session ID.
Definition: SessionInfo.php:188
MediaWiki\Session\SessionInfo\forceUse
forceUse()
Force use of this SessionInfo if validation fails.
Definition: SessionInfo.php:219
MediaWiki\Session\SessionManager\validateSessionId
static validateSessionId( $id)
Validate a session ID.
Definition: SessionManager.php:389
MediaWiki\Session\SessionInfo\__toString
__toString()
Definition: SessionInfo.php:284
MediaWiki\Session\SessionInfo\$providerMetadata
array null $providerMetadata
Definition: SessionInfo.php:69
MediaWiki\Session\SessionProvider
A SessionProvider provides SessionInfo and support for Session.
Definition: SessionProvider.php:80
MediaWiki\Session\SessionInfo\getProvider
getProvider()
Return the provider.
Definition: SessionInfo.php:180
MediaWiki\Session\SessionInfo\$id
string $id
Definition: SessionInfo.php:45
MediaWiki\Session
Definition: BotPasswordSessionProvider.php:24
MediaWiki\Session\SessionInfo\wasPersisted
wasPersisted()
Return whether the session is persisted.
Definition: SessionInfo.php:243
MediaWiki\Session\SessionInfo\__construct
__construct( $priority, array $data)
Definition: SessionInfo.php:97
MediaWiki\Session\SessionInfo\getProviderMetadata
getProviderMetadata()
Return provider metadata.
Definition: SessionInfo.php:251
MediaWiki\Session\SessionInfo\MAX_PRIORITY
const MAX_PRIORITY
Maximum allowed priority.
Definition: SessionInfo.php:39
MediaWiki\Session\SessionInfo\$persisted
bool $persisted
Definition: SessionInfo.php:54
MediaWiki\Session\SessionInfo\$provider
SessionProvider null $provider
Definition: SessionInfo.php:42
MediaWiki\Session\SessionInfo
Value object returned by SessionProvider.
Definition: SessionInfo.php:34
MediaWiki\Session\SessionInfo\$userInfo
UserInfo null $userInfo
Definition: SessionInfo.php:51
MediaWiki\Session\SessionInfo\$remembered
bool $remembered
Definition: SessionInfo.php:57
MediaWiki\Session\SessionInfo\$forceHTTPS
bool $forceHTTPS
Definition: SessionInfo.php:60
MediaWiki\Session\SessionInfo\MIN_PRIORITY
const MIN_PRIORITY
Minimum allowed priority.
Definition: SessionInfo.php:36
MediaWiki\Session\SessionInfo\getUserInfo
getUserInfo()
Return the user.
Definition: SessionInfo.php:235
MediaWiki\Session\SessionInfo\wasRemembered
wasRemembered()
Return whether the user was remembered.
Definition: SessionInfo.php:270
MediaWiki\Session\SessionInfo\isIdSafe
isIdSafe()
Indicate whether the ID is "safe".
Definition: SessionInfo.php:204