30 use Wikimedia\IPUtils;
63 private $userOptionsLookup;
79 parent::__construct( $params );
81 if ( isset( $params[
'emailEnabled'] ) ) {
82 $this->emailEnabled = (bool)$params[
'emailEnabled'];
84 if ( isset( $params[
'newPasswordExpiry'] ) ) {
85 $this->newPasswordExpiry = (int)$params[
'newPasswordExpiry'];
87 if ( isset( $params[
'passwordReminderResendTime'] ) ) {
88 $this->passwordReminderResendTime = $params[
'passwordReminderResendTime'];
90 if ( isset( $params[
'allowRequiringEmailForResets'] ) ) {
91 $this->allowRequiringEmail = $params[
'allowRequiringEmailForResets'];
93 $this->dbProvider = $dbProvider;
94 $this->userOptionsLookup = $userOptionsLookup;
100 $this->passwordReminderResendTime ??=
102 $this->allowRequiringEmail ??=
109 'msg' =>
wfMessage(
'resetpass-temp-emailed' ),
123 if ( isset( $options[
'username'] ) && $this->emailEnabled ) {
142 if ( !$req || $req->username ===
null || $req->password ===
null ) {
146 $username = $this->userNameUtils->getCanonical(
147 $req->username, UserRigorOptions::RIGOR_USABLE );
148 if ( $username ===
false ) {
152 $row = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
153 ->select( [
'user_id',
'user_newpassword',
'user_newpass_time' ] )
155 ->where( [
'user_name' => $username ] )
156 ->caller( __METHOD__ )->fetchRow();
162 if ( !$status->isOK() ) {
166 $pwhash = $this->
getPassword( $row->user_newpassword );
167 if ( !$pwhash->verify( $req->password ) ||
168 !$this->isTimestampValid( $row->user_newpass_time )
176 $this->logger->info(
"{user} successfully logged in using temp password",
179 'requestIP' => $this->manager->getRequest()->getIP()
189 $username = $this->userNameUtils->getCanonical( $username, UserRigorOptions::RIGOR_USABLE );
190 if ( $username ===
false ) {
194 $row = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
195 ->select( [
'user_newpassword',
'user_newpass_time' ] )
197 ->where( [
'user_name' => $username ] )
198 ->caller( __METHOD__ )->fetchRow();
200 !( $this->
getPassword( $row->user_newpassword ) instanceof \InvalidPassword ) &&
205 $username = $this->userNameUtils->getCanonical( $username, UserRigorOptions::RIGOR_USABLE );
206 if ( $username ===
false ) {
212 return (
bool)$db->newSelectQueryBuilder()
213 ->select( [
'user_id' ] )
215 ->where( [
'user_name' => $username ] )
216 ->options( $options )
217 ->caller( __METHOD__ )->fetchField();
223 if ( get_class( $req ) !== TemporaryPasswordAuthenticationRequest::class ) {
225 return \StatusValue::newGood(
'ignored' );
229 return \StatusValue::newGood();
232 $username = $this->userNameUtils->getCanonical(
233 $req->username, UserRigorOptions::RIGOR_USABLE );
234 if ( $username ===
false ) {
235 return \StatusValue::newGood(
'ignored' );
238 $row = $this->dbProvider->getPrimaryDatabase()->newSelectQueryBuilder()
239 ->select( [
'user_id',
'user_newpass_time' ] )
241 ->where( [
'user_name' => $username ] )
242 ->caller( __METHOD__ )->fetchRow();
244 return \StatusValue::newGood(
'ignored' );
248 if ( $req->password !==
null ) {
251 if ( $req->mailpassword ) {
252 if ( !$this->emailEnabled ) {
253 return \StatusValue::newFatal(
'passwordreset-emaildisabled' );
262 $this->passwordReminderResendTime
263 && $row->user_newpass_time
264 && time() < (
int)
wfTimestamp( TS_UNIX, $row->user_newpass_time )
265 + $this->passwordReminderResendTime * 3600
269 return \StatusValue::newFatal(
'throttled-mailpassword',
270 round( $this->passwordReminderResendTime, 3 ) );
273 if ( !$req->caller ) {
274 return \StatusValue::newFatal(
'passwordreset-nocaller' );
276 if ( !IPUtils::isValid( $req->caller ) ) {
279 return \StatusValue::newFatal(
'passwordreset-nosuchcaller', $req->caller );
288 $username = $req->username !==
null ?
289 $this->userNameUtils->getCanonical( $req->username, UserRigorOptions::RIGOR_USABLE ) :
false;
290 if ( $username ===
false ) {
294 $dbw = $this->dbProvider->getPrimaryDatabase();
298 get_class( $req ) === TemporaryPasswordAuthenticationRequest::class
301 $newpassTime = $dbw->timestamp();
302 $sendMail = $req->mailpassword;
309 $dbw->newUpdateQueryBuilder()
312 'user_newpassword' => $pwhash->toString(),
313 'user_newpass_time' => $newpassTime,
315 ->where( [
'user_name' => $username ] )
316 ->caller( __METHOD__ )->execute();
320 $dbw->onTransactionCommitOrIdle(
321 function () use ( $req ) {
337 $reqs, TemporaryPasswordAuthenticationRequest::class
342 if ( $req->mailpassword ) {
343 if ( !$this->emailEnabled ) {
345 } elseif ( !$user->getEmail() ) {
360 $reqs, TemporaryPasswordAuthenticationRequest::class
362 if ( $req && $req->username !==
null && $req->password !==
null ) {
364 if ( $req->username !== $user->getName() ) {
366 $req->username = $user->getName();
369 if ( $req->mailpassword ) {
371 $this->manager->setAuthenticationSessionData(
'no-email',
true );
375 $ret->createRequest = $req;
383 $req = $res->createRequest;
384 $mailpassword = $req->mailpassword;
385 $req->mailpassword =
false;
390 if ( $mailpassword ) {
392 $this->dbProvider->getPrimaryDatabase()->onTransactionCommitOrIdle(
393 function () use ( $user, $creator, $req ) {
400 return $mailpassword ?
'byemail' :
null;
410 if ( $time !==
null ) {
412 if ( time() >= $expiry ) {
430 return \MediaWiki\Status\Status::newFatal(
'badipaddress' );
434 $this->
getHookRunner()->onUser__mailPasswordInternal( $creatingUser, $ip, $user );
437 $userLanguage = $this->userOptionsLookup->getOption( $user,
'language' );
438 $subjectMessage =
wfMessage(
'createaccount-title' )->inLanguage( $userLanguage );
439 $bodyMessage =
wfMessage(
'createaccount-text', $ip, $user->
getName(), $password,
440 '<' . $mainPageUrl .
'>', round( $this->newPasswordExpiry / 86400 ) )
441 ->inLanguage( $userLanguage );
443 $status = $user->
sendMail( $subjectMessage->text(), $bodyMessage->text() );
447 if ( !$status->isGood() ) {
448 $this->logger->warning(
'Could not send account creation email: ' .
449 $status->getWikiText(
false,
false,
'en' ) );
463 return \MediaWiki\Status\Status::newFatal(
'noname' );
465 $userLanguage = $this->userOptionsLookup->getOption( $user,
'language' );
466 $callerIsAnon = IPUtils::isValid( $req->caller );
467 $callerName = $callerIsAnon ? $req->caller :
User::newFromName( $req->caller )->getName();
468 $passwordMessage =
wfMessage(
'passwordreset-emailelement', $user->getName(),
469 $req->password )->inLanguage( $userLanguage );
470 $emailMessage =
wfMessage( $callerIsAnon ?
'passwordreset-emailtext-ip'
471 :
'passwordreset-emailtext-user' )->inLanguage( $userLanguage );
472 $body = $emailMessage->params( $callerName, $passwordMessage->text(), 1,
474 round( $this->newPasswordExpiry / 86400 ) )->text();
476 if ( $this->allowRequiringEmail && !$this->userOptionsLookup
477 ->getBoolOption( $user,
'requireemail' )
482 $body .=
wfMessage(
'passwordreset-emailtext-require-email' )
483 ->inLanguage( $userLanguage )
488 $emailTitle =
wfMessage(
'passwordreset-emailtitle' )->inLanguage( $userLanguage );
489 return $user->sendMail( $emailTitle->text(), $body );
wfTimestampOrNull( $outputtype=TS_UNIX, $ts=null)
Return a formatted timestamp, or null if input is null.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
static getDBOptions( $bitfield)
Get an appropriate DB index, options, and fallback DB index for a query.
static getDBFromIndex(IConnectionProvider $dbProvider, int $index)
Takes $index from ::getDBOptions() and return proper Database object.
A class containing constants representing the names of configuration variables.
const PasswordReminderResendTime
Name constant for the PasswordReminderResendTime setting, for use with Config::get()
const EnableEmail
Name constant for the EnableEmail setting, for use with Config::get()
const NewPasswordExpiry
Name constant for the NewPasswordExpiry setting, for use with Config::get()
const AllowRequiringEmailForResets
Name constant for the AllowRequiringEmailForResets setting, for use with Config::get()
Parent class for all special pages.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
static newFatal( $message,... $parameters)
Factory function for fatal errors.
static newGood( $value=null)
Factory function for good results.