MediaWiki  1.28.1
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 }
const MIN_PRIORITY
Minimum allowed priority.
Definition: SessionInfo.php:36
the array() calling protocol came about after MediaWiki 1.4rc1.
getUserInfo()
Return the user.
Object holding data about a session's user.
Definition: UserInfo.php:51
getPriority()
Return the priority.
getId()
Return the session ID.
forceUse()
Force use of this SessionInfo if validation fails.
getManager()
Get the session manager.
getProvider()
Return the provider.
getProviderMetadata()
Return provider metadata.
wasPersisted()
Return whether the session is persisted.
static compare($a, $b)
Compare two SessionInfo objects by priority.
SessionProvider null $provider
Definition: SessionInfo.php:42
const MAX_PRIORITY
Maximum allowed priority.
Definition: SessionInfo.php:39
$from
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
__construct($priority, array $data)
Definition: SessionInfo.php:87
static validateSessionId($id)
Validate a session ID.
forceHTTPS()
Whether this session should only be used over HTTPS.
Value object returned by SessionProvider.
Definition: SessionInfo.php:34
wasRemembered()
Return whether the user was remembered.
isIdSafe()
Indicate whether the ID is "safe".