MediaWiki  REL1_31
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 
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 }
MediaWiki\Session\SessionInfo\forceHTTPS
forceHTTPS()
Whether this session should only be used over HTTPS.
Definition: SessionInfo.php:271
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
array
the array() calling protocol came about after MediaWiki 1.4rc1.
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:287
MediaWiki\Session\SessionInfo\getPriority
getPriority()
Return the priority.
Definition: SessionInfo.php:218
MediaWiki\Session\SessionInfo\getId
getId()
Return the session ID.
Definition: SessionInfo.php:179
MediaWiki\Session\SessionInfo\forceUse
forceUse()
Force use of this SessionInfo if validation fails.
Definition: SessionInfo.php:210
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:37
MediaWiki\Session\SessionManager\validateSessionId
static validateSessionId( $id)
Validate a session ID.
Definition: SessionManager.php:371
MediaWiki\Session\SessionInfo\__toString
__toString()
Definition: SessionInfo.php:275
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:171
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:234
MediaWiki\Session\SessionInfo\__construct
__construct( $priority, array $data)
Definition: SessionInfo.php:88
MediaWiki\Session\SessionInfo\getProviderMetadata
getProviderMetadata()
Return provider metadata.
Definition: SessionInfo.php:242
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:226
MediaWiki\Session\SessionInfo\wasRemembered
wasRemembered()
Return whether the user was remembered.
Definition: SessionInfo.php:261
MediaWiki\Session\SessionInfo\isIdSafe
isIdSafe()
Indicate whether the ID is "safe".
Definition: SessionInfo.php:195