use Psr\Log\LoggerInterface;
use BagOStuff;
+use CachedBagOStuff;
use Config;
use FauxRequest;
use Language;
/** @var Config */
private $config;
- /** @var BagOStuff|null */
+ /** @var CachedBagOStuff|null */
private $store;
/** @var SessionProvider[] */
'$options[\'store\'] must be an instance of BagOStuff'
);
}
- $this->store = $options['store'];
+ $store = $options['store'];
} else {
- $this->store = \ObjectCache::getInstance( $this->config->get( 'SessionCacheType' ) );
- $this->store->setLogger( $this->logger );
+ $store = \ObjectCache::getInstance( $this->config->get( 'SessionCacheType' ) );
+ $store->setLogger( $this->logger );
}
+ $this->store = $store instanceof CachedBagOStuff ? $store : new CachedBagOStuff( $store );
register_shutdown_function( array( $this, 'shutdown' ) );
}
try {
$session = $this->getEmptySessionInternal( $request, $id );
} catch ( \Exception $ex ) {
- $this->logger->error( __METHOD__ . ': failed to create empty session: ' .
- $ex->getMessage() );
+ $this->logger->error( 'Failed to create empty session: {exception}',
+ array(
+ 'method' => __METHOD__,
+ 'exception' => $ex,
+ ) );
$session = null;
}
}
// Checks passed, create the user...
$from = isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : 'CLI';
- $logger->info( __METHOD__ . ": creating new user ($userName) - from: $from" );
+ $logger->info( __METHOD__ . ': creating new user ({username}) - from: {url}',
+ array(
+ 'username' => $userName,
+ 'url' => $from,
+ ) );
try {
// Insert the user into the local DB master
$status = $user->addToDatabase();
if ( !$status->isOK() ) {
// @codeCoverageIgnoreStart
- $logger->error( __METHOD__ . ': failed with message ' . $status->getWikiText() );
+ $logger->error( __METHOD__ . ': failed with message ' . $status->getWikiText(),
+ array(
+ 'username' => $userName,
+ ) );
$user->setId( 0 );
$user->loadFromId();
return false;
}
} catch ( \Exception $ex ) {
// @codeCoverageIgnoreStart
- $logger->error( __METHOD__ . ': failed with exception ' . $ex->getMessage() );
+ $logger->error( __METHOD__ . ': failed with exception {exception}', array(
+ 'exception' => $ex,
+ 'username' => $userName,
+ ) );
// Do not keep throwing errors for a while
$cache->set( $backoffKey, 1, 600 );
// Bubble up error; which should normally trigger DB rollbacks
// @codeCoverageIgnoreEnd
}
- # Notify hooks (e.g. Newuserlog)
- \Hooks::run( 'AuthPluginAutoCreate', array( $user ) );
- \Hooks::run( 'LocalUserCreated', array( $user, true ) );
-
- # Notify AuthPlugin too
+ # Notify AuthPlugin
$tmpUser = $user;
$wgAuth->initUser( $tmpUser, true );
if ( $tmpUser !== $user ) {
get_class( $wgAuth ) . '::initUser() replaced the user object' );
}
+ # Notify hooks (e.g. Newuserlog)
+ \Hooks::run( 'AuthPluginAutoCreate', array( $user ) );
+ \Hooks::run( 'LocalUserCreated', array( $user, true ) );
+
$user->saveSettings();
# Update user count
public function preventSessionsForUser( $username ) {
$this->preventUsers[$username] = true;
- // Reset the user's token to kill existing sessions
- $user = User::newFromName( $username );
- if ( $user && $user->getToken( false ) ) {
- $user->setToken( true );
- $user->saveSettings();
- }
-
// Instruct the session providers to kill any other sessions too.
foreach ( $this->getProviders() as $provider ) {
$provider->preventSessionsForUser( $username );
if ( $blob !== false ) {
// Sanity check: blob must be an array, if it's saved at all
if ( !is_array( $blob ) ) {
- $this->logger->warning( "Session $info: Bad data" );
+ $this->logger->warning( 'Session "{session}": Bad data', array(
+ 'session' => $info,
+ ) );
$this->store->delete( $key );
return false;
}
if ( !isset( $blob['data'] ) || !is_array( $blob['data'] ) ||
!isset( $blob['metadata'] ) || !is_array( $blob['metadata'] )
) {
- $this->logger->warning( "Session $info: Bad data structure" );
+ $this->logger->warning( 'Session "{session}": Bad data structure', array(
+ 'session' => $info,
+ ) );
$this->store->delete( $key );
return false;
}
!array_key_exists( 'userToken', $metadata ) ||
!array_key_exists( 'provider', $metadata )
) {
- $this->logger->warning( "Session $info: Bad metadata" );
+ $this->logger->warning( 'Session "{session}": Bad metadata', array(
+ 'session' => $info,
+ ) );
$this->store->delete( $key );
return false;
}
if ( $provider === null ) {
$newParams['provider'] = $provider = $this->getProvider( $metadata['provider'] );
if ( !$provider ) {
- $this->logger->warning( "Session $info: Unknown provider, " . $metadata['provider'] );
+ $this->logger->warning(
+ 'Session "{session}": Unknown provider ' . $metadata['provider'],
+ array(
+ 'session' => $info,
+ )
+ );
$this->store->delete( $key );
return false;
}
} elseif ( $metadata['provider'] !== (string)$provider ) {
- $this->logger->warning( "Session $info: Wrong provider, " .
- $metadata['provider'] . ' !== ' . $provider );
+ $this->logger->warning( 'Session "{session}": Wrong provider ' .
+ $metadata['provider'] . ' !== ' . $provider,
+ array(
+ 'session' => $info,
+ ) );
return false;
}
$newParams['metadata'] = $newProviderMetadata;
}
} catch ( \UnexpectedValueException $ex ) {
- $this->logger->warning( "Session $info: Metadata merge failed: " . $ex->getMessage() );
+ $this->logger->warning(
+ 'Session "{session}": Metadata merge failed: {exception}',
+ array(
+ 'session' => $info,
+ 'exception' => $ex,
+ ) );
return false;
}
}
$userInfo = UserInfo::newAnonymous();
}
} catch ( \InvalidArgumentException $ex ) {
- $this->logger->error( "Session $info: " . $ex->getMessage() );
+ $this->logger->error( 'Session "{session}": {exception}', array(
+ 'session' => $info,
+ 'exception' => $ex,
+ ) );
return false;
}
$newParams['userInfo'] = $userInfo;
// is no saved ID and the names match.
if ( $metadata['userId'] ) {
if ( $metadata['userId'] !== $userInfo->getId() ) {
- $this->logger->warning( "Session $info: User ID mismatch, " .
- $metadata['userId'] . ' !== ' . $userInfo->getId() );
+ $this->logger->warning(
+ 'Session "{session}": User ID mismatch, {uid_a} !== {uid_b}',
+ array(
+ 'session' => $info,
+ 'uid_a' => $metadata['userId'],
+ 'uid_b' => $userInfo->getId(),
+ ) );
return false;
}
if ( $metadata['userName'] !== null &&
$userInfo->getName() !== $metadata['userName']
) {
- $this->logger->warning( "Session $info: User ID matched but name didn't (rename?), " .
- $metadata['userName'] . ' !== ' . $userInfo->getName() );
+ $this->logger->warning(
+ 'Session "{session}": User ID matched but name didn\'t (rename?), {uname_a} !== {uname_b}',
+ array(
+ 'session' => $info,
+ 'uname_a' => $metadata['userName'],
+ 'uname_b' => $userInfo->getName(),
+ ) );
return false;
}
} elseif ( $metadata['userName'] !== null ) { // Shouldn't happen, but just in case
if ( $metadata['userName'] !== $userInfo->getName() ) {
- $this->logger->warning( "Session $info: User name mismatch, " .
- $metadata['userName'] . ' !== ' . $userInfo->getName() );
+ $this->logger->warning(
+ 'Session "{session}": User name mismatch, {uname_a} !== {uname_b}',
+ array(
+ 'session' => $info,
+ 'uname_a' => $metadata['userName'],
+ 'uname_b' => $userInfo->getName(),
+ ) );
return false;
}
} elseif ( !$userInfo->isAnon() ) {
// Metadata specifies an anonymous user, but the passed-in
// user isn't anonymous.
$this->logger->warning(
- "Session $info: Metadata has an anonymous user, " .
- 'but a non-anon user was provided'
- );
+ 'Session "{session}": Metadata has an anonymous user, but a non-anon user was provided',
+ array(
+ 'session' => $info,
+ ) );
return false;
}
}
if ( $metadata['userToken'] !== null &&
$userInfo->getToken() !== $metadata['userToken']
) {
- $this->logger->warning( "Session $info: User token mismatch" );
+ $this->logger->warning( 'Session "{session}": User token mismatch', array(
+ 'session' => $info,
+ ) );
return false;
}
if ( !$userInfo->isVerified() ) {
} else {
// No metadata, so we can't load the provider if one wasn't given.
if ( $info->getProvider() === null ) {
- $this->logger->warning( "Session $info: Null provider and no metadata" );
+ $this->logger->warning(
+ 'Session "{session}": Null provider and no metadata',
+ array(
+ 'session' => $info,
+ ) );
return false;
}
$newParams['userInfo'] = UserInfo::newAnonymous();
} else {
$this->logger->info(
- "Session $info: No user provided and provider cannot set user"
- );
+ 'Session "{session}": No user provided and provider cannot set user',
+ array(
+ 'session' => $info,
+ ) );
return false;
}
} elseif ( !$info->getUserInfo()->isVerified() ) {
$this->logger->warning(
- "Session $info: Unverified user provided and no metadata to auth it"
- );
+ 'Session "{session}": Unverified user provided and no metadata to auth it',
+ array(
+ 'session' => $info,
+ ) );
return false;
}
'SessionCheckInfo',
array( &$reason, $info, $request, $metadata, $data )
) ) {
- $this->logger->warning( "Session $info: $reason" );
+ $this->logger->warning( 'Session "{session}": ' . $reason, array(
+ 'session' => $info,
+ ) );
return false;
}