MediaWiki  master
SpecialPasswordReset.php
Go to the documentation of this file.
1 <?php
33  private $email;
34 
38  private $firstUser;
39 
43  private $result;
44 
48  private $method;
49 
50  public function __construct() {
51  parent::__construct( 'PasswordReset', 'editmyprivateinfo' );
52  }
53 
54  public function doesWrites() {
55  return true;
56  }
57 
58  public function userCanExecute( User $user ) {
59  return $this->canChangePassword( $user ) === true && parent::userCanExecute( $user );
60  }
61 
62  public function checkExecutePermissions( User $user ) {
63  $error = $this->canChangePassword( $user );
64  if ( is_string( $error ) ) {
65  throw new ErrorPageError( 'internalerror', $error );
66  } elseif ( !$error ) {
67  throw new ErrorPageError( 'internalerror', 'resetpass_forbidden' );
68  }
69 
70  parent::checkExecutePermissions( $user );
71  }
72 
73  protected function getFormFields() {
75  $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
76  $a = [];
77  if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
78  $a['Username'] = [
79  'type' => 'text',
80  'label-message' => 'passwordreset-username',
81  ];
82 
83  if ( $this->getUser()->isLoggedIn() ) {
84  $a['Username']['default'] = $this->getUser()->getName();
85  }
86  }
87 
88  if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
89  $a['Email'] = [
90  'type' => 'email',
91  'label-message' => 'passwordreset-email',
92  ];
93  }
94 
95  if ( isset( $resetRoutes['domain'] ) && $resetRoutes['domain'] ) {
96  $domains = $wgAuth->domainList();
97  $a['Domain'] = [
98  'type' => 'select',
99  'options' => $domains,
100  'label-message' => 'passwordreset-domain',
101  ];
102  }
103 
104  if ( $this->getUser()->isAllowed( 'passwordreset' ) ) {
105  $a['Capture'] = [
106  'type' => 'check',
107  'label-message' => 'passwordreset-capture',
108  'help-message' => 'passwordreset-capture-help',
109  ];
110  }
111 
112  return $a;
113  }
114 
115  protected function getDisplayFormat() {
116  return 'ooui';
117  }
118 
119  public function alterForm( HTMLForm $form ) {
120  $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
121 
122  $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
123 
124  $i = 0;
125  if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
126  $i++;
127  }
128  if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
129  $i++;
130  }
131  if ( isset( $resetRoutes['domain'] ) && $resetRoutes['domain'] ) {
132  $i++;
133  }
134 
135  $message = ( $i > 1 ) ? 'passwordreset-text-many' : 'passwordreset-text-one';
136 
137  $form->setHeaderText( $this->msg( $message, $i )->parseAsBlock() );
138  $form->setSubmitTextMsg( 'mailmypassword' );
139  }
140 
150  public function onSubmit( array $data ) {
152 
153  if ( isset( $data['Domain'] ) ) {
154  if ( $wgAuth->validDomain( $data['Domain'] ) ) {
155  $wgAuth->setDomain( $data['Domain'] );
156  } else {
157  $wgAuth->setDomain( 'invaliddomain' );
158  }
159  }
160 
161  if ( isset( $data['Capture'] ) && !$this->getUser()->isAllowed( 'passwordreset' ) ) {
162  // The user knows they don't have the passwordreset permission,
163  // but they tried to spoof the form. That's naughty
164  throw new PermissionsError( 'passwordreset' );
165  }
166 
172  if ( isset( $data['Username'] ) && $data['Username'] !== '' ) {
173  $method = 'username';
174  $users = [ User::newFromName( $data['Username'] ) ];
175  } elseif ( isset( $data['Email'] )
176  && $data['Email'] !== ''
177  && Sanitizer::validateEmail( $data['Email'] )
178  ) {
179  $method = 'email';
180  $res = wfGetDB( DB_SLAVE )->select(
181  'user',
183  [ 'user_email' => $data['Email'] ],
184  __METHOD__
185  );
186 
187  if ( $res ) {
188  $users = [];
189 
190  foreach ( $res as $row ) {
191  $users[] = User::newFromRow( $row );
192  }
193  } else {
194  // Some sort of database error, probably unreachable
195  throw new MWException( 'Unknown database error in ' . __METHOD__ );
196  }
197  } else {
198  // The user didn't supply any data
199  return false;
200  }
201 
202  // Check for hooks (captcha etc), and allow them to modify the users list
203  $error = [];
204  if ( !Hooks::run( 'SpecialPasswordResetOnSubmit', [ &$users, $data, &$error ] ) ) {
205  return [ $error ];
206  }
207 
208  $this->method = $method;
209 
210  if ( count( $users ) == 0 ) {
211  if ( $method == 'email' ) {
212  // Don't reveal whether or not an email address is in use
213  return true;
214  } else {
215  return [ 'noname' ];
216  }
217  }
218 
219  $firstUser = $users[0];
220 
221  if ( !$firstUser instanceof User || !$firstUser->getId() ) {
222  // Don't parse username as wikitext (bug 65501)
223  return [ [ 'nosuchuser', wfEscapeWikiText( $data['Username'] ) ] ];
224  }
225 
226  // Check against the rate limiter
227  if ( $this->getUser()->pingLimiter( 'mailpassword' ) ) {
228  throw new ThrottledError;
229  }
230 
231  // Check against password throttle
232  foreach ( $users as $user ) {
233  if ( $user->isPasswordReminderThrottled() ) {
234 
235  # Round the time in hours to 3 d.p., in case someone is specifying
236  # minutes or seconds.
237  return [ [
238  'throttled-mailpassword',
239  round( $this->getConfig()->get( 'PasswordReminderResendTime' ), 3 )
240  ] ];
241  }
242  }
243 
244  // All the users will have the same email address
245  if ( $firstUser->getEmail() == '' ) {
246  // This won't be reachable from the email route, so safe to expose the username
247  return [ [ 'noemail', wfEscapeWikiText( $firstUser->getName() ) ] ];
248  }
249 
250  // We need to have a valid IP address for the hook, but per bug 18347, we should
251  // send the user's name if they're logged in.
252  $ip = $this->getRequest()->getIP();
253  if ( !$ip ) {
254  return [ 'badipaddress' ];
255  }
256  $caller = $this->getUser();
257  Hooks::run( 'User::mailPasswordInternal', [ &$caller, &$ip, &$firstUser ] );
258  $username = $caller->getName();
259  $msg = IP::isValid( $username )
260  ? 'passwordreset-emailtext-ip'
261  : 'passwordreset-emailtext-user';
262 
263  // Send in the user's language; which should hopefully be the same
264  $userLanguage = $firstUser->getOption( 'language' );
265 
266  $passwords = [];
267  foreach ( $users as $user ) {
268  $password = PasswordFactory::generateRandomPasswordString( $wgMinimalPasswordLength );
269  $user->setNewpassword( $password );
270  $user->saveSettings();
271  $passwords[] = $this->msg( 'passwordreset-emailelement', $user->getName(), $password )
272  ->inLanguage( $userLanguage )->text(); // We'll escape the whole thing later
273  }
274  $passwordBlock = implode( "\n\n", $passwords );
275 
276  $this->email = $this->msg( $msg )->inLanguage( $userLanguage );
277  $this->email->params(
278  $username,
279  $passwordBlock,
280  count( $passwords ),
281  '<' . Title::newMainPage()->getCanonicalURL() . '>',
282  round( $this->getConfig()->get( 'NewPasswordExpiry' ) / 86400 )
283  );
284 
285  $title = $this->msg( 'passwordreset-emailtitle' )->inLanguage( $userLanguage );
286 
287  $this->result = $firstUser->sendMail( $title->text(), $this->email->text() );
288 
289  if ( isset( $data['Capture'] ) && $data['Capture'] ) {
290  // Save the user, will be used if an error occurs when sending the email
291  $this->firstUser = $firstUser;
292  } else {
293  // Blank the email if the user is not supposed to see it
294  $this->email = null;
295  }
296 
297  if ( $this->result->isGood() ) {
298  return true;
299  } elseif ( isset( $data['Capture'] ) && $data['Capture'] ) {
300  // The email didn't send, but maybe they knew that and that's why they captured it
301  return true;
302  } else {
303  // @todo FIXME: The email wasn't sent, but we have already set
304  // the password throttle timestamp, so they won't be able to try
305  // again until it expires... :(
306  return [ [ 'mailerror', $this->result->getMessage() ] ];
307  }
308  }
309 
310  public function onSuccess() {
311  if ( $this->getUser()->isAllowed( 'passwordreset' ) && $this->email != null ) {
312  // @todo Logging
313 
314  if ( $this->result->isGood() ) {
315  $this->getOutput()->addWikiMsg( 'passwordreset-emailsent-capture' );
316  } else {
317  $this->getOutput()->addWikiMsg( 'passwordreset-emailerror-capture',
318  $this->result->getMessage(), $this->firstUser->getName() );
319  }
320 
321  $this->getOutput()->addHTML( Html::rawElement( 'pre', [], $this->email->escaped() ) );
322  }
323 
324  if ( $this->method === 'email' ) {
325  $this->getOutput()->addWikiMsg( 'passwordreset-emailsentemail' );
326  } else {
327  $this->getOutput()->addWikiMsg( 'passwordreset-emailsentusername' );
328  }
329 
330  $this->getOutput()->returnToMain();
331  }
332 
333  protected function canChangePassword( User $user ) {
334  global $wgAuth;
335  $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
336 
337  // Maybe password resets are disabled, or there are no allowable routes
338  if ( !is_array( $resetRoutes ) ||
339  !in_array( true, array_values( $resetRoutes ) )
340  ) {
341  return 'passwordreset-disabled';
342  }
343 
344  // Maybe the external auth plugin won't allow local password changes
345  if ( !$wgAuth->allowPasswordChange() ) {
346  return 'resetpass_forbidden';
347  }
348 
349  // Maybe email features have been disabled
350  if ( !$this->getConfig()->get( 'EnableEmail' ) ) {
351  return 'passwordreset-emaildisabled';
352  }
353 
354  // Maybe the user is blocked (check this here rather than relying on the parent
355  // method as we have a more specific error message to use here
356  if ( $user->isBlocked() ) {
357  return 'blocked-mailpassword';
358  }
359 
360  return true;
361  }
362 
367  function isListed() {
368  if ( $this->canChangePassword( $this->getUser() ) === true ) {
369  return parent::isListed();
370  }
371 
372  return false;
373  }
374 
375  protected function getGroupName() {
376  return 'users';
377  }
378 }
static newFromName($name, $validate= 'valid')
Static factory method for creation from username.
Definition: User.php:522
getEmail()
Get the user's e-mail address.
Definition: User.php:2796
static newFromRow($row, $data=null)
Create a new user object from a user row.
Definition: User.php:609
wfGetDB($db, $groups=[], $wiki=false)
Get a Database object.
the array() calling protocol came about after MediaWiki 1.4rc1.
static newMainPage()
Create a new Title for the Main Page.
Definition: Title.php:548
static rawElement($element, $attribs=[], $contents= '')
Returns an HTML element in a string.
Definition: Html.php:210
static generateRandomPasswordString($minLength=10)
Generate a random string suitable for a password.
msg()
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
$wgAuth $wgAuth
Authentication plugin.
Special page for requesting a password reset email.
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
Special page which uses an HTMLForm to handle processing.
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2139
static selectFields()
Return the list of user fields that should be selected to create a new user object.
Definition: User.php:5546
addHiddenFields(array $fields)
Add an array of hidden fields to the output.
Definition: HTMLForm.php:890
sendMail($subject, $body, $from=null, $replyto=null)
Send an e-mail to this user's account.
Definition: User.php:4542
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
Definition: User.php:47
wfEscapeWikiText($text)
Escapes the given text so that it may be output using addWikiText() without any linking, formatting, etc.
An error page which can definitely be safely rendered using the OutputPage.
static isValid($ip)
Validate an IP address.
Definition: IP.php:113
$res
Definition: database.txt:21
MediaWiki exception.
Definition: MWException.php:26
setSubmitTextMsg($msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1303
Object handling generic submission, CSRF protection, layout and other logic for UI forms...
Definition: HTMLForm.php:128
const DB_SLAVE
Definition: Defines.php:46
Allows to change the fields on the form that will be generated are created Can be used to omit specific feeds from being outputted You must not use this hook to add use OutputPage::addFeedLink() instead.&$feedLinks conditions will AND in the final query as a Content object as a Content object $title
Definition: hooks.txt:312
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
This directory hold several benchmarking scripts used as a proof of speed or to track PHP performances over time To get somehow accurate result
Definition: README:4
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition: hooks.txt:242
isBlocked($bFromSlave=true)
Check if user is blocked.
Definition: User.php:1965
getOption($oname, $defaultOverride=null, $ignoreHidden=false)
Get the user's current setting for a given option.
Definition: User.php:2915
setHeaderText($msg, $section=null)
Set header text, inside the form.
Definition: HTMLForm.php:758
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
this hook is for auditing only or null if authentication failed before getting that far $username
Definition: hooks.txt:776
$wgMinimalPasswordLength
Specifies the minimal length of a user password.
getId()
Get the user's ID.
Definition: User.php:2114
getUser()
Shortcut to get the User executing this instance.
isListed()
Hide the password reset page if resets are disabled.
getConfig()
Shortcut to get main config object.
Show an error when a user tries to do something they do not have the necessary permissions for...
static validateEmail($addr)
Does a string look like an e-mail address?
Definition: Sanitizer.php:1941
getRequest()
Get the WebRequest being used for this instance.
Show an error when the user hits a rate limit.
onSubmit(array $data)
Process the form.