MediaWiki  1.27.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 
86  public function __construct( $priority, array $data ) {
87  if ( $priority < self::MIN_PRIORITY || $priority > self::MAX_PRIORITY ) {
88  throw new \InvalidArgumentException( 'Invalid priority' );
89  }
90 
91  if ( isset( $data['copyFrom'] ) ) {
92  $from = $data['copyFrom'];
93  if ( !$from instanceof SessionInfo ) {
94  throw new \InvalidArgumentException( 'Invalid copyFrom' );
95  }
96  $data += [
97  'provider' => $from->provider,
98  'id' => $from->id,
99  'userInfo' => $from->userInfo,
100  'persisted' => $from->persisted,
101  'remembered' => $from->remembered,
102  'forceHTTPS' => $from->forceHTTPS,
103  'metadata' => $from->providerMetadata,
104  'idIsSafe' => $from->idIsSafe,
105  'forceUse' => $from->forceUse,
106  // @codeCoverageIgnoreStart
107  ];
108  // @codeCoverageIgnoreEnd
109  } else {
110  $data += [
111  'provider' => null,
112  'id' => null,
113  'userInfo' => null,
114  'persisted' => false,
115  'remembered' => true,
116  'forceHTTPS' => false,
117  'metadata' => null,
118  'idIsSafe' => false,
119  'forceUse' => false,
120  // @codeCoverageIgnoreStart
121  ];
122  // @codeCoverageIgnoreEnd
123  }
124 
125  if ( $data['id'] !== null && !SessionManager::validateSessionId( $data['id'] ) ) {
126  throw new \InvalidArgumentException( 'Invalid session ID' );
127  }
128 
129  if ( $data['userInfo'] !== null && !$data['userInfo'] instanceof UserInfo ) {
130  throw new \InvalidArgumentException( 'Invalid userInfo' );
131  }
132 
133  if ( !$data['provider'] && $data['id'] === null ) {
134  throw new \InvalidArgumentException(
135  'Must supply an ID when no provider is given'
136  );
137  }
138 
139  if ( $data['metadata'] !== null && !is_array( $data['metadata'] ) ) {
140  throw new \InvalidArgumentException( 'Invalid metadata' );
141  }
142 
143  $this->provider = $data['provider'];
144  if ( $data['id'] !== null ) {
145  $this->id = $data['id'];
146  $this->idIsSafe = $data['idIsSafe'];
147  $this->forceUse = $data['forceUse'] && $this->provider;
148  } else {
149  $this->id = $this->provider->getManager()->generateSessionId();
150  $this->idIsSafe = true;
151  $this->forceUse = false;
152  }
153  $this->priority = (int)$priority;
154  $this->userInfo = $data['userInfo'];
155  $this->persisted = (bool)$data['persisted'];
156  if ( $data['provider'] !== null ) {
157  if ( $this->userInfo !== null && !$this->userInfo->isAnon() && $this->userInfo->isVerified() ) {
158  $this->remembered = (bool)$data['remembered'];
159  }
160  $this->providerMetadata = $data['metadata'];
161  }
162  $this->forceHTTPS = (bool)$data['forceHTTPS'];
163  }
164 
169  final public function getProvider() {
170  return $this->provider;
171  }
172 
177  final public function getId() {
178  return $this->id;
179  }
180 
193  final public function isIdSafe() {
194  return $this->idIsSafe;
195  }
196 
207  final public function forceUse() {
208  return $this->forceUse;
209  }
210 
215  final public function getPriority() {
216  return $this->priority;
217  }
218 
223  final public function getUserInfo() {
224  return $this->userInfo;
225  }
226 
231  final public function wasPersisted() {
232  return $this->persisted;
233  }
234 
239  final public function getProviderMetadata() {
241  }
242 
258  final public function wasRemembered() {
259  return $this->remembered;
260  }
261 
266  final public function forceHTTPS() {
267  return $this->forceHTTPS;
268  }
269 
270  public function __toString() {
271  return '[' . $this->getPriority() . ']' .
272  ( $this->getProvider() ?: 'null' ) .
273  ( $this->userInfo ?: '<null>' ) . $this->getId();
274  }
275 
282  public static function compare( $a, $b ) {
283  return $a->getPriority() - $b->getPriority();
284  }
285 
286 }
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:86
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".