8 * Let users recover their password.
11 class SpecialResetpass
extends SpecialPage
{
13 private $mSelfChange = true; // Usually, but sometimes not :)
15 public function __construct() {
16 parent
::__construct( 'Resetpass' );
20 * Main execution point
22 function execute( $par ) {
23 global $wgUser, $wgAuth, $wgOut, $wgRequest;
25 $this->mUserName
= $wgRequest->getVal( 'wpName', $par );
26 $this->mOldpass
= $wgRequest->getVal( 'wpPassword' );
27 $this->mNewpass
= $wgRequest->getVal( 'wpNewPassword' );
28 $this->mRetype
= $wgRequest->getVal( 'wpRetype' );
31 $this->outputHeader();
33 if( !$wgAuth->allowPasswordChange() ) {
34 $this->error( wfMsg( 'resetpass_forbidden' ) );
38 // Default to our own username when not given one
39 if ( !$this->mUserName
) {
40 $this->mUserName
= $wgUser->getName();
43 // Are we changing our own?
44 if ( $wgUser->getName() != $this->mUserName
) {
45 $this->mSelfChange
= false; // We're changing someone else
48 if( !$wgRequest->wasPosted() && !$wgUser->isLoggedIn() ) {
49 $this->error( wfMsg( 'resetpass-no-info' ) );
53 if ( !$this->mSelfChange
&& !$wgUser->isAllowed( 'reset-passwords' ) ) {
54 $this->error( wfMsg( 'resetpass-no-others' ) );
58 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal('token') ) ) {
60 $this->attemptReset( $this->mNewpass
, $this->mRetype
);
61 $wgOut->addWikiMsg( 'resetpass_success' );
62 // Only attempt this login session if we're changing our own password
63 if( $this->mSelfChange
&& !$wgUser->isLoggedIn() ) {
65 'action' => 'submitlogin',
66 'wpName' => $this->mUserName
,
67 'wpPassword' => $this->mNewpass
,
68 'returnto' => $wgRequest->getVal( 'returnto' ),
70 if( $wgRequest->getCheck( 'wpRemember' ) ) {
71 $data['wpRemember'] = 1;
73 $login = new LoginForm( new FauxRequest( $data, true ) );
76 $titleObj = Title
::newFromText( $wgRequest->getVal( 'returnto' ) );
77 if ( !$titleObj instanceof Title
) {
78 $titleObj = Title
::newMainPage();
80 $wgOut->redirect( $titleObj->getFullURL() );
81 } catch( PasswordError
$e ) {
82 $this->error( $e->getMessage() );
88 function error( $msg ) {
90 $wgOut->addHTML( Xml
::element('p', array( 'class' => 'error' ), $msg ) );
94 global $wgOut, $wgUser, $wgRequest;
96 $wgOut->disallowUserJs();
98 $self = SpecialPage
::getTitleFor( 'Resetpass' );
101 if ( !$wgUser->isLoggedIn() ) {
102 $rememberMe = '<tr>' .
104 '<td class="mw-input">' .
105 Xml
::checkLabel( wfMsg( 'remembermypassword' ),
106 'wpRemember', 'wpRemember',
107 $wgRequest->getCheck( 'wpRemember' ) ) .
110 $submitMsg = 'resetpass_submit';
111 $oldpassMsg = 'resetpass-temp-password';
113 $oldpassMsg = 'oldpassword';
114 $submitMsg = 'resetpass-submit-loggedin';
117 Xml
::fieldset( wfMsg( 'resetpass_header' ) ) .
118 Xml
::openElement( 'form',
121 'action' => $self->getLocalUrl(),
122 'id' => 'mw-resetpass-form' ) ) .
123 Xml
::hidden( 'token', $wgUser->editToken() ) .
124 Xml
::hidden( 'returnto', $wgRequest->getVal( 'returnto' ) ) .
125 wfMsgExt( 'resetpass_text', array( 'parse' ) ) .
126 Xml
::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) .
127 $this->pretty( array(
128 array( 'wpName', 'username', 'text', $this->mUserName
, $wgUser->isAllowed( 'reset-passwords' ) ),
129 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass
, $this->mSelfChange
),
130 array( 'wpNewPassword', 'newpassword', 'password', '', true ),
131 array( 'wpRetype', 'retypenew', 'password', '', true ),
136 '<td class="mw-input">' .
137 Xml
::submitButton( wfMsg( $submitMsg ) ) .
140 Xml
::closeElement( 'table' ) .
141 Xml
::closeElement( 'form' ) .
142 Xml
::closeElement( 'fieldset' )
146 function pretty( $fields ) {
148 foreach( $fields as $list ) {
149 list( $name, $label, $type, $value, $enabled ) = $list;
150 $params = array( 'id' => $name, 'type' => $type );
152 $params['disabled'] = 'disabled';
153 $field = Xml
::input( $name, 20, $value, $params );
155 $out .= '<td class="mw-label">';
156 $out .= Xml
::label( wfMsg( $label ), $name );
158 $out .= '<td class="mw-input">';
167 * @throws PasswordError when cannot set the new password because requirements not met.
169 protected function attemptReset( $newpass, $retype ) {
170 $user = User
::newFromName( $this->mUserName
);
171 if( !$user ||
$user->isAnon() ) {
172 throw new PasswordError( 'no such user' );
175 if( $newpass !== $retype ) {
176 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
177 throw new PasswordError( wfMsg( 'badretype' ) );
180 if ( $this->mSelfChange
) {
181 if( !$user->checkTemporaryPassword($this->mOldpass
) && !$user->checkPassword($this->mOldpass
) ) {
182 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
183 throw new PasswordError( wfMsg( 'resetpass-wrong-oldpass' ) );
188 $user->setPassword( $this->mNewpass
);
189 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
190 $this->mNewpass
= $this->mOldpass
= $this->mRetypePass
= '';
191 } catch( PasswordError
$e ) {
192 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
193 throw new PasswordError( $e->getMessage() );
198 $user->saveSettings();