MediaWiki  1.29.2
SessionInfo.php
Go to the documentation of this file.
1 <?php
24 namespace MediaWiki\Session;
25 
34 class SessionInfo {
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 
87  public function __construct( $priority, array $data ) {
88  if ( $priority < self::MIN_PRIORITY || $priority > self::MAX_PRIORITY ) {
89  throw new \InvalidArgumentException( 'Invalid priority' );
90  }
91 
92  if ( isset( $data['copyFrom'] ) ) {
93  $from = $data['copyFrom'];
94  if ( !$from instanceof SessionInfo ) {
95  throw new \InvalidArgumentException( 'Invalid copyFrom' );
96  }
97  $data += [
98  'provider' => $from->provider,
99  'id' => $from->id,
100  'userInfo' => $from->userInfo,
101  'persisted' => $from->persisted,
102  'remembered' => $from->remembered,
103  'forceHTTPS' => $from->forceHTTPS,
104  'metadata' => $from->providerMetadata,
105  'idIsSafe' => $from->idIsSafe,
106  'forceUse' => $from->forceUse,
107  // @codeCoverageIgnoreStart
108  ];
109  // @codeCoverageIgnoreEnd
110  } else {
111  $data += [
112  'provider' => null,
113  'id' => null,
114  'userInfo' => null,
115  'persisted' => false,
116  'remembered' => true,
117  'forceHTTPS' => false,
118  'metadata' => null,
119  'idIsSafe' => false,
120  'forceUse' => false,
121  // @codeCoverageIgnoreStart
122  ];
123  // @codeCoverageIgnoreEnd
124  }
125 
126  if ( $data['id'] !== null && !SessionManager::validateSessionId( $data['id'] ) ) {
127  throw new \InvalidArgumentException( 'Invalid session ID' );
128  }
129 
130  if ( $data['userInfo'] !== null && !$data['userInfo'] instanceof UserInfo ) {
131  throw new \InvalidArgumentException( 'Invalid userInfo' );
132  }
133 
134  if ( !$data['provider'] && $data['id'] === null ) {
135  throw new \InvalidArgumentException(
136  'Must supply an ID when no provider is given'
137  );
138  }
139 
140  if ( $data['metadata'] !== null && !is_array( $data['metadata'] ) ) {
141  throw new \InvalidArgumentException( 'Invalid metadata' );
142  }
143 
144  $this->provider = $data['provider'];
145  if ( $data['id'] !== null ) {
146  $this->id = $data['id'];
147  $this->idIsSafe = $data['idIsSafe'];
148  $this->forceUse = $data['forceUse'] && $this->provider;
149  } else {
150  $this->id = $this->provider->getManager()->generateSessionId();
151  $this->idIsSafe = true;
152  $this->forceUse = false;
153  }
154  $this->priority = (int)$priority;
155  $this->userInfo = $data['userInfo'];
156  $this->persisted = (bool)$data['persisted'];
157  if ( $data['provider'] !== null ) {
158  if ( $this->userInfo !== null && !$this->userInfo->isAnon() && $this->userInfo->isVerified() ) {
159  $this->remembered = (bool)$data['remembered'];
160  }
161  $this->providerMetadata = $data['metadata'];
162  }
163  $this->forceHTTPS = (bool)$data['forceHTTPS'];
164  }
165 
170  final public function getProvider() {
171  return $this->provider;
172  }
173 
178  final public function getId() {
179  return $this->id;
180  }
181 
194  final public function isIdSafe() {
195  return $this->idIsSafe;
196  }
197 
209  final public function forceUse() {
210  return $this->forceUse;
211  }
212 
217  final public function getPriority() {
218  return $this->priority;
219  }
220 
225  final public function getUserInfo() {
226  return $this->userInfo;
227  }
228 
233  final public function wasPersisted() {
234  return $this->persisted;
235  }
236 
241  final public function getProviderMetadata() {
243  }
244 
260  final public function wasRemembered() {
261  return $this->remembered;
262  }
263 
268  final public function forceHTTPS() {
269  return $this->forceHTTPS;
270  }
271 
272  public function __toString() {
273  return '[' . $this->getPriority() . ']' .
274  ( $this->getProvider() ?: 'null' ) .
275  ( $this->userInfo ?: '<null>' ) . $this->getId();
276  }
277 
284  public static function compare( $a, $b ) {
285  return $a->getPriority() - $b->getPriority();
286  }
287 
288 }
MediaWiki\Session\SessionInfo\forceHTTPS
forceHTTPS()
Whether this session should only be used over HTTPS.
Definition: SessionInfo.php:268
MediaWiki\Session\SessionInfo\$idIsSafe
$idIsSafe
Definition: SessionInfo.php:56
MediaWiki\Session\SessionProvider\getManager
getManager()
Get the session manager.
Definition: SessionProvider.php:128
MediaWiki\Session\SessionInfo\$remembered
$remembered
Definition: SessionInfo.php:54
MediaWiki\Session\SessionInfo\$priority
int $priority
Definition: SessionInfo.php:48
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:284
MediaWiki\Session\SessionInfo\getPriority
getPriority()
Return the priority.
Definition: SessionInfo.php:217
MediaWiki\Session\SessionInfo\getId
getId()
Return the session ID.
Definition: SessionInfo.php:178
MediaWiki\Session\SessionInfo\forceUse
forceUse()
Force use of this SessionInfo if validation fails.
Definition: SessionInfo.php:209
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaWiki\Session\SessionManager\validateSessionId
static validateSessionId( $id)
Validate a session ID.
Definition: SessionManager.php:370
MediaWiki\Session\SessionInfo\__toString
__toString()
Definition: SessionInfo.php:272
MediaWiki\Session\SessionInfo\$providerMetadata
array null $providerMetadata
Definition: SessionInfo.php:60
MediaWiki\Session\SessionProvider
A SessionProvider provides SessionInfo and support for Session.
Definition: SessionProvider.php:78
MediaWiki\Session\SessionInfo\getProvider
getProvider()
Return the provider.
Definition: SessionInfo.php:170
MediaWiki\Session\SessionInfo\$id
string $id
Definition: SessionInfo.php:45
MediaWiki\Session\SessionInfo\$forceUse
$forceUse
Definition: SessionInfo.php:57
MediaWiki\Session
Definition: BotPasswordSessionProvider.php:24
MediaWiki\Session\SessionInfo\wasPersisted
wasPersisted()
Return whether the session is persisted.
Definition: SessionInfo.php:233
MediaWiki\Session\SessionInfo\__construct
__construct( $priority, array $data)
Definition: SessionInfo.php:87
MediaWiki\Session\SessionInfo\getProviderMetadata
getProviderMetadata()
Return provider metadata.
Definition: SessionInfo.php:241
MediaWiki\Session\SessionInfo\MAX_PRIORITY
const MAX_PRIORITY
Maximum allowed priority.
Definition: SessionInfo.php:39
MediaWiki\Session\SessionInfo\$persisted
$persisted
Definition: SessionInfo.php:53
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\$forceHTTPS
$forceHTTPS
Definition: SessionInfo.php:55
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:225
array
the array() calling protocol came about after MediaWiki 1.4rc1.
MediaWiki\Session\SessionInfo\wasRemembered
wasRemembered()
Return whether the user was remembered.
Definition: SessionInfo.php:260
MediaWiki\Session\SessionInfo\isIdSafe
isIdSafe()
Indicate whether the ID is "safe".
Definition: SessionInfo.php:194