2016-03-28 08:26:48 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Body of LoginNotify extension
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
*/
|
|
|
|
|
2016-07-06 22:06:27 +00:00
|
|
|
use MediaWiki\Auth\AuthenticationResponse;
|
|
|
|
|
2016-03-28 08:26:48 +00:00
|
|
|
class LoginNotifyHooks {
|
|
|
|
|
|
|
|
const OPTIONS_FAKE_TRUTH = 2;
|
|
|
|
const OPTIONS_FAKE_FALSE = 'fake-false';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add LoginNotify events to Echo
|
|
|
|
*
|
2017-03-23 03:28:30 +00:00
|
|
|
* @param string[] &$notifications Array of Echo notifications
|
|
|
|
* @param string[] &$notificationCategories Array of Echo notification categories
|
|
|
|
* @param string[] &$icons Array of icon details
|
2016-03-28 08:26:48 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-04-26 16:24:19 +00:00
|
|
|
public static function onBeforeCreateEchoEvent(
|
|
|
|
&$notifications,
|
|
|
|
&$notificationCategories,
|
|
|
|
&$icons
|
|
|
|
) {
|
2016-03-28 08:26:48 +00:00
|
|
|
global $wgLoginNotifyEnableOnSuccess;
|
|
|
|
|
2017-03-17 20:26:31 +00:00
|
|
|
$icons['LoginNotify-user-avatar'] = [
|
|
|
|
'path' => 'LoginNotify/UserAvatar.svg'
|
2016-03-28 08:26:48 +00:00
|
|
|
];
|
|
|
|
|
2016-04-26 16:24:19 +00:00
|
|
|
$notificationCategories['login-fail'] = [
|
2016-03-28 08:26:48 +00:00
|
|
|
'priority' => 7,
|
|
|
|
'tooltip' => 'echo-pref-tooltip-login-fail',
|
2016-04-26 16:24:19 +00:00
|
|
|
];
|
2016-03-28 08:26:48 +00:00
|
|
|
|
|
|
|
$loginBase = [
|
|
|
|
EchoAttributeManager::ATTR_LOCATORS => [
|
|
|
|
'EchoUserLocator::locateEventAgent'
|
|
|
|
],
|
|
|
|
'category' => 'login-fail',
|
|
|
|
'group' => 'negative',
|
|
|
|
'presentation-model' => 'LoginNotifyPresentationModel',
|
|
|
|
// fixme, what does this actually do?
|
|
|
|
'title-message' => 'loginnotify-login-fail',
|
|
|
|
'title-params' => [],
|
|
|
|
'email-subject-message' => 'notification-loginnotify-login-fail-email-subject',
|
2016-04-26 16:24:19 +00:00
|
|
|
// FIXME Should count be a parameter
|
2016-03-28 08:26:48 +00:00
|
|
|
'email-subject-params' => [ 'agent', 'count' ],
|
|
|
|
'email-body-batch-params' => [ 'agent', 'count' ],
|
|
|
|
// FIXME is it ok not to set batch email messages, since
|
|
|
|
// we have immediate flag?
|
2017-03-17 20:26:31 +00:00
|
|
|
'icon' => 'LoginNotify-user-avatar',
|
2016-03-28 08:26:48 +00:00
|
|
|
'immediate' => true,
|
|
|
|
];
|
|
|
|
$notifications['login-fail-new'] = [
|
2017-04-07 21:48:57 +00:00
|
|
|
'email-body-batch-message' => 'notification-loginnotify-login-fail-new-emailbatch',
|
|
|
|
'bundle' => [
|
|
|
|
'web' => true,
|
|
|
|
'expandable' => false
|
|
|
|
]
|
2016-03-28 08:26:48 +00:00
|
|
|
] + $loginBase;
|
|
|
|
$notifications['login-fail-known'] = [
|
2017-04-07 21:48:57 +00:00
|
|
|
'email-body-batch-message' => 'notification-loginnotify-login-fail-known-emailbatch',
|
|
|
|
'bundle' => [
|
|
|
|
'web' => true,
|
|
|
|
'expandable' => false
|
|
|
|
]
|
2016-03-28 08:26:48 +00:00
|
|
|
] + $loginBase;
|
|
|
|
if ( $wgLoginNotifyEnableOnSuccess ) {
|
2016-04-26 16:24:19 +00:00
|
|
|
$notificationCategories['login-success'] = [
|
2016-03-28 08:26:48 +00:00
|
|
|
'priority' => 7,
|
|
|
|
'tooltip' => 'echo-pref-tooltip-login-success',
|
2016-04-26 16:24:19 +00:00
|
|
|
];
|
2016-03-28 08:26:48 +00:00
|
|
|
$notifications['login-success'] = [
|
|
|
|
'category' => 'login-success',
|
|
|
|
'email-subject-message' => 'notification-loginnotify-login-success-email-subject',
|
|
|
|
'email-body-batch-message' => 'notification-loginnotify-login-success-emailbatch',
|
|
|
|
'email-body-batch-params' => [ 'agent' ],
|
|
|
|
// FIXME title-message. What is its purpose??
|
|
|
|
] + $loginBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-07 21:48:57 +00:00
|
|
|
public static function onEchoGetBundleRules( $event, &$bundleString ) {
|
|
|
|
switch ( $event->getType() ) {
|
|
|
|
case 'login-fail-known':
|
|
|
|
case 'login-fail-new':
|
|
|
|
$bundleString = 'login-fail';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-28 08:26:48 +00:00
|
|
|
/**
|
2016-07-06 22:06:27 +00:00
|
|
|
* Old hook for pre 1.27 or wikis with auth manager disabled.
|
2016-03-28 08:26:48 +00:00
|
|
|
*
|
|
|
|
* @todo Doesn't catcha captcha or throttle failures
|
2017-03-23 03:28:30 +00:00
|
|
|
* @param User $user User in question.
|
|
|
|
* @param string $pass The password (parameter not used).
|
|
|
|
* @param integer $retval A LoginForm constant (e.g. LoginForm::SUCCESS).
|
2016-03-28 08:26:48 +00:00
|
|
|
*/
|
|
|
|
public static function onLoginAuthenticateAudit( User $user, $pass, $retval ) {
|
|
|
|
if ( $retval === LoginForm::WRONG_PASS ) {
|
2016-07-06 22:06:27 +00:00
|
|
|
self::doFailedLogin( $user );
|
2016-03-28 08:26:48 +00:00
|
|
|
} elseif ( $retval === LoginForm::SUCCESS ) {
|
2016-07-06 22:06:27 +00:00
|
|
|
self::doSuccessfulLogin( $user );
|
2016-03-28 08:26:48 +00:00
|
|
|
}
|
2016-07-06 22:06:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook for login auditing post 1.27
|
|
|
|
*
|
2017-03-23 03:28:30 +00:00
|
|
|
* @param AuthenticationResponse $ret Is login successful?
|
|
|
|
* @param User|null $user User object on successful auth
|
|
|
|
* @param string $username Username for failed attempts.
|
2016-07-06 22:06:27 +00:00
|
|
|
*/
|
|
|
|
public static function onAuthManagerLoginAuthenticateAudit(
|
|
|
|
AuthenticationResponse $ret, $user, $username
|
|
|
|
) {
|
|
|
|
if ( $user ) {
|
|
|
|
$userObj = $user;
|
|
|
|
} else {
|
|
|
|
$userObj = User::newFromName( $username, 'usable' );
|
|
|
|
}
|
|
|
|
if ( !$userObj ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $ret->status === AuthenticationResponse::PASS ) {
|
|
|
|
self::doSuccessfulLogin( $userObj );
|
|
|
|
} elseif ( $ret->status === AuthenticationResponse::FAIL ) {
|
|
|
|
self::doFailedLogin( $userObj );
|
|
|
|
}
|
|
|
|
// Other statuses include Abstain, Redirect, or UI. We ignore such
|
|
|
|
// statuses.
|
|
|
|
}
|
|
|
|
|
2017-03-23 03:28:30 +00:00
|
|
|
/**
|
|
|
|
* Handle a successful login (clear the attempt counter, send a notice, and record the
|
|
|
|
* current IP address as known).
|
|
|
|
*
|
|
|
|
* @param User $user The user who logged in.
|
|
|
|
*/
|
2016-07-06 22:06:27 +00:00
|
|
|
public static function doSuccessfulLogin( User $user ) {
|
|
|
|
$loginNotify = new LoginNotify();
|
|
|
|
$loginNotify->clearCounters( $user );
|
|
|
|
$loginNotify->sendSuccessNotice( $user );
|
|
|
|
$loginNotify->setCurrentAddressAsKnown( $user );
|
|
|
|
}
|
|
|
|
|
2017-03-23 03:28:30 +00:00
|
|
|
/**
|
|
|
|
* Handle a failed login (record the failure).
|
|
|
|
*
|
|
|
|
* @param User $user The user that failed to log in.
|
|
|
|
*/
|
2016-07-06 22:06:27 +00:00
|
|
|
public static function doFailedLogin( User $user ) {
|
|
|
|
$loginNotify = new LoginNotify();
|
|
|
|
$loginNotify->recordFailure( $user );
|
2016-03-28 08:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-23 06:37:08 +00:00
|
|
|
* Deprecated since v1.27
|
|
|
|
*
|
2016-03-28 08:26:48 +00:00
|
|
|
* Set a cookie saying this is a known computer when creating an account.
|
|
|
|
*
|
2017-03-23 03:28:30 +00:00
|
|
|
* @todo This still sets cookies if user creates an account while logged in as someone else.
|
|
|
|
* @param User $user The user that has been created.
|
2016-03-28 08:26:48 +00:00
|
|
|
* @param boolean $byMail Account created by email
|
|
|
|
*/
|
|
|
|
public static function onAddNewAccount( User $user, $byMail ) {
|
|
|
|
if ( !$byMail ) {
|
|
|
|
$loginNotify = new LoginNotify();
|
|
|
|
$loginNotify->setCurrentAddressAsKnown( $user );
|
2017-01-23 06:37:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook for new account creation since v1.27
|
|
|
|
*
|
|
|
|
* Called immediately after a local user has been created and saved to the database
|
|
|
|
*
|
|
|
|
* @todo This still sets cookies if user creates account well logged in as someone else.
|
|
|
|
* @param User $user User created
|
|
|
|
* @param boolean $autocreated Whether this was an auto-created account
|
|
|
|
*/
|
|
|
|
public static function onLocalUserCreated( $user, $autocreated ) {
|
|
|
|
if ( !$autocreated ) {
|
|
|
|
$loginNotify = new LoginNotify();
|
|
|
|
$loginNotify->setCurrentAddressAsKnown( $user );
|
2016-03-28 08:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook for loading options.
|
|
|
|
*
|
|
|
|
* This is a bit hacky. Used to be able to set a different
|
|
|
|
* default for admins then other users
|
|
|
|
*
|
2017-03-23 03:28:30 +00:00
|
|
|
* @param User $user The user in question.
|
|
|
|
* @param mixed[] &$options The options.
|
2016-03-28 08:26:48 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function onUserLoadOptions( User $user, array &$options ) {
|
|
|
|
global $wgLoginNotifyEnableForPriv;
|
|
|
|
if ( !is_array( $wgLoginNotifyEnableForPriv ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-23 15:43:50 +00:00
|
|
|
if ( !self::isUserOptionOverriden( $user ) ) {
|
2016-03-28 08:26:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$defaultOpts = User::getDefaultOptions();
|
|
|
|
$optionsToCheck = self::getOverridenOptions();
|
|
|
|
|
2016-04-26 16:24:19 +00:00
|
|
|
foreach ( $optionsToCheck as $opt ) {
|
2016-03-28 08:26:48 +00:00
|
|
|
if ( $options[$opt] === self::OPTIONS_FAKE_FALSE ) {
|
|
|
|
$options[$opt] = '0';
|
|
|
|
}
|
|
|
|
if ( $defaultOpts[$opt] !== false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( $options[$opt] === false ) {
|
|
|
|
$options[$opt] = self::OPTIONS_FAKE_TRUTH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook for saving options.
|
|
|
|
*
|
|
|
|
* This is a bit hacky. Used to be able to set a different
|
|
|
|
* default for admins then other users. Since admins are higher value
|
|
|
|
* targets, it may make sense to have notices enabled by default for
|
|
|
|
* them, but disabled for normal users.
|
|
|
|
*
|
|
|
|
* @todo This is a bit icky. Need to decide if we really want to do this.
|
|
|
|
* @todo If someone explicitly enables, gets admin rights, gets de-admined,
|
2016-09-12 11:37:31 +00:00
|
|
|
* this will then disable the preference, which is definitely non-ideal.
|
2017-03-23 03:28:30 +00:00
|
|
|
* @param User $user The user that is being saved.
|
|
|
|
* @param mixed[] &$options The options.
|
2016-03-28 08:26:48 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-02-05 10:55:55 +00:00
|
|
|
public static function onUserSaveOptions( User $user, array &$options ) {
|
2016-03-28 08:26:48 +00:00
|
|
|
$optionsToCheck = self::getOverridenOptions();
|
|
|
|
$defaultOpts = User::getDefaultOptions();
|
2017-01-23 15:43:50 +00:00
|
|
|
if ( !self::isUserOptionOverriden( $user ) ) {
|
2016-03-28 08:26:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-04-26 16:24:19 +00:00
|
|
|
foreach ( $optionsToCheck as $opt ) {
|
2016-03-28 08:26:48 +00:00
|
|
|
if ( $defaultOpts[$opt] !== false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $options[$opt] === self::OPTIONS_FAKE_TRUTH ) {
|
|
|
|
$options[$opt] = false;
|
|
|
|
}
|
|
|
|
if ( $options[$opt] !== self::OPTIONS_FAKE_TRUTH
|
|
|
|
&& $options[$opt]
|
|
|
|
) {
|
|
|
|
// Its checked on the form. Keep at default
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !$options[$opt] ) {
|
|
|
|
// Somehow this means it got unchecked on form
|
|
|
|
$options[$opt] = self::OPTIONS_FAKE_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper for onUser(Load|Save)Options
|
|
|
|
*
|
2016-09-12 11:37:31 +00:00
|
|
|
* @return array Which option keys to check
|
2016-03-28 08:26:48 +00:00
|
|
|
*/
|
|
|
|
private static function getOverridenOptions() {
|
|
|
|
// For login-success, it makes most sense to email
|
|
|
|
// people about it, but auto-subscribing people to email
|
|
|
|
// is a bit icky as nobody likes to be spammed.
|
|
|
|
return [
|
|
|
|
'echo-subscriptions-web-login-fail',
|
|
|
|
'echo-subscriptions-web-login-success'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function isUserOptionOverriden( User $user ) {
|
|
|
|
global $wgLoginNotifyEnableForPriv;
|
|
|
|
// Note: isAllowedAny calls into session for per-session restrictions,
|
|
|
|
// which we do not want to take into account, and more importantly
|
|
|
|
// causes an infinite loop.
|
|
|
|
$rights = User::getGroupPermissions( $user->getEffectiveGroups() );
|
|
|
|
if ( !array_intersect( $rights, $wgLoginNotifyEnableForPriv ) ) {
|
|
|
|
// Not a user we care about.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|