29use Psr\Log\LoggerAwareInterface;
30use Psr\Log\LoggerAwareTrait;
31use Psr\Log\LoggerInterface;
64 'AllowRequiringEmailForResets',
66 'PasswordResetRoutes',
83 LoggerInterface $logger =
null
86 $this->authManager = $authManager;
87 $this->permissionManager = $permissionManager;
90 wfDeprecated(
'Not passing LoadBalancer to ' . __METHOD__,
'1.34' );
91 $loadBalancer = MediaWikiServices::getInstance()->getDBLoadBalancer();
96 wfDeprecated(
'Not passing LoggerInterface to ' . __METHOD__,
'1.34' );
97 $logger = LoggerFactory::getInstance(
'authentication' );
99 $this->logger = $logger;
111 $status = $this->permissionCache->get( $user->
getName() );
113 $resetRoutes = $this->config->get(
'PasswordResetRoutes' );
114 $status = StatusValue::newGood();
116 if ( !is_array( $resetRoutes ) || !in_array(
true, $resetRoutes,
true ) ) {
118 $status = StatusValue::newFatal(
'passwordreset-disabled' );
120 ( $providerStatus = $this->authManager->allowsAuthenticationDataChange(
122 && !$providerStatus->isGood()
125 $status = StatusValue::newFatal(
'resetpass_forbidden-reason',
126 $providerStatus->getMessage() );
127 } elseif ( !$this->config->get(
'EnableEmail' ) ) {
129 $status = StatusValue::newFatal(
'passwordreset-emaildisabled' );
130 } elseif ( !$this->permissionManager->userHasRight( $user,
'editmyprivateinfo' ) ) {
132 $status = StatusValue::newFatal(
'badaccess' );
137 $status = StatusValue::newFatal(
'blocked-mailpassword' );
140 $this->permissionCache->set( $user->
getName(), $status );
162 User $performingUser, $username =
null, $email =
null
164 if ( !$this->
isAllowed( $performingUser )->isGood() ) {
165 throw new LogicException(
'User ' . $performingUser->
getName()
166 .
' is not allowed to reset passwords' );
171 if ( $performingUser->
pingLimiter(
'mailpassword' ) ) {
172 return StatusValue::newGood();
175 $username = $username ??
'';
176 $email = $email ??
'';
178 $resetRoutes = $this->config->get(
'PasswordResetRoutes' )
179 + [
'username' =>
false,
'email' => false ];
180 if ( $resetRoutes[
'username'] && $username ) {
181 $method =
'username';
183 } elseif ( $resetRoutes[
'email'] && $email ) {
184 if ( !Sanitizer::validateEmail( $email ) ) {
186 return StatusValue::newGood();
193 if ( $this->config->get(
'AllowRequiringEmailForResets' ) ) {
194 foreach ( $users as $index => $user ) {
195 if ( $user->getBoolOption(
'requireemail' ) ) {
196 unset( $users[$index] );
202 return StatusValue::newFatal(
'passwordreset-nodata' );
208 'Username' => $username,
210 'Email' => $method ===
'email' ? $email :
null,
216 $users = array_values( $users );
218 if ( !Hooks::run(
'SpecialPasswordResetOnSubmit', [ &$users, $data, &$error ] ) ) {
224 $firstUser = reset( $users ) ??
null;
226 $requireEmail = $this->config->get(
'AllowRequiringEmailForResets' )
227 && $method ===
'username'
229 && $firstUser->getBoolOption(
'requireemail' );
230 if ( $requireEmail && ( $email ===
'' || !Sanitizer::validateEmail( $email ) ) ) {
232 return StatusValue::newGood();
236 if ( $method ===
'email' ) {
238 return StatusValue::newGood();
240 return StatusValue::newFatal(
'noname' );
245 if ( $username && !User::getCanonicalName( $username ) ) {
246 return StatusValue::newFatal(
'noname' );
252 if ( !$firstUser instanceof
User || !$firstUser->
getId() ) {
253 return StatusValue::newGood();
259 if ( !$firstUser->getEmail() ) {
260 return StatusValue::newGood();
264 if ( $requireEmail && $firstUser->getEmail() !== $email ) {
265 return StatusValue::newGood();
272 return StatusValue::newFatal(
'badipaddress' );
275 Hooks::run(
'User::mailPasswordInternal', [ &$performingUser, &$ip, &$firstUser ] );
277 $result = StatusValue::newGood();
279 foreach ( $users as $user ) {
280 $req = TemporaryPasswordAuthenticationRequest::newRandom();
281 $req->username = $user->getName();
282 $req->mailpassword =
true;
283 $req->caller = $performingUser->
getName();
285 $status = $this->authManager->allowsAuthenticationDataChange( $req,
true );
289 if ( $status->isGood() && $status->getValue() ===
'throttled-mailpassword' ) {
290 return StatusValue::newGood();
293 if ( $status->isGood() && $status->getValue() !==
'ignored' ) {
295 } elseif ( $result->isGood() ) {
298 if ( $status->getValue() ===
'ignored' ) {
299 $status = StatusValue::newFatal(
'passwordreset-ignored' );
301 $result->merge( $status );
306 'requestingIp' => $ip,
307 'requestingUser' => $performingUser->
getName(),
308 'targetUsername' => $username,
309 'targetEmail' => $email,
312 if ( !$result->isGood() ) {
314 "{requestingUser} attempted password reset of {actualUser} but failed",
315 $logContext + [
'errors' => $result->getErrors() ]
320 DeferredUpdates::addUpdate(
322 DeferredUpdates::POSTSEND
325 return StatusValue::newGood();
340 return $block->appliesToPasswordReset();
349 $userQuery = User::getQueryInfo();
350 $res = $this->loadBalancer->getConnectionRef(
DB_REPLICA )->select(
351 $userQuery[
'tables'],
352 $userQuery[
'fields'],
353 [
'user_email' => $email ],
361 throw new MWException(
'Unknown database error in ' . __METHOD__ );
365 foreach (
$res as $row ) {
366 $users[] = User::newFromRow( $row );
379 return User::newFromName( $username );
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Handles a simple LRU key/value map with a maximum number of entries.
static newFromSpecifier( $value)
Transform a MessageSpecifier or a primitive value used interchangeably with specifiers (a message key...
Helper class for the password reset functionality shared by the web UI and the API.
isBlocked(User $user)
Check whether the user is blocked.
isAllowed(User $user)
Check if a given user has permission to use this functionality.
__construct( $config, AuthManager $authManager, PermissionManager $permissionManager, ILoadBalancer $loadBalancer=null, LoggerInterface $logger=null)
This class is managed by MediaWikiServices, don't instantiate directly.
execute(User $performingUser, $username=null, $email=null)
Do a password reset.
ILoadBalancer $loadBalancer
ServiceOptions Config $config
const CONSTRUCTOR_OPTIONS
PermissionManager $permissionManager
MapCacheLRU $permissionCache
In-process cache for isAllowed lookups, by username.
lookupUser( $username)
User object creation helper for testability.
Sends emails to all accounts associated with that email to reset the password.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
getRequest()
Get the WebRequest object to use with this object.
getName()
Get the user name, or the IP of an anonymous user.
getId()
Get the user's ID.
getGlobalBlock( $ip='')
Check if user is blocked on all wikis.
pingLimiter( $action='edit', $incrBy=1)
Primitive rate limits: enforce maximum actions per time period to put a brake on flooding.
getBlock( $fromReplica=true)
Get the block affecting the user, or null if the user is not blocked.
Interface for configuration instances.