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' );
29 $this->mComment
= $wgRequest->getVal( 'wpComment' );
32 $this->outputHeader();
34 if( !$wgAuth->allowPasswordChange() ) {
35 $this->error( wfMsg( 'resetpass_forbidden' ) );
39 // Default to our own username when not given one
40 if ( !$this->mUserName
) {
41 $this->mUserName
= $wgUser->getName();
44 // Are we changing our own?
45 if ( $wgUser->getName() != $this->mUserName
) {
46 $this->mSelfChange
= false; // We're changing someone else
49 if( !$wgRequest->wasPosted() && !$wgUser->isLoggedIn() ) {
50 $this->error( wfMsg( 'resetpass-no-info' ) );
54 if ( !$this->mSelfChange
&& !$wgUser->isAllowed( 'reset-passwords' ) ) {
55 $this->error( wfMsg( 'resetpass-no-others' ) );
59 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal('token') ) ) {
61 $this->attemptReset( $this->mNewpass
, $this->mRetype
);
62 $wgOut->addWikiMsg( 'resetpass_success' );
63 // Only attempt this login session if we're changing our own password
64 if( $this->mSelfChange
&& !$wgUser->isLoggedIn() ) {
66 'action' => 'submitlogin',
67 'wpName' => $this->mUserName
,
68 'wpPassword' => $this->mNewpass
,
69 'returnto' => $wgRequest->getVal( 'returnto' ),
71 if( $wgRequest->getCheck( 'wpRemember' ) ) {
72 $data['wpRemember'] = 1;
74 $login = new LoginForm( new FauxRequest( $data, true ) );
77 $titleObj = Title
::newFromText( $wgRequest->getVal( 'returnto' ) );
78 if ( !$titleObj instanceof Title
) {
79 $titleObj = Title
::newMainPage();
81 $wgOut->redirect( $titleObj->getFullURL() );
82 } catch( PasswordError
$e ) {
83 $this->error( $e->getMessage() );
89 function error( $msg ) {
91 $wgOut->addHTML( Xml
::element('p', array( 'class' => 'error' ), $msg ) );
95 global $wgOut, $wgUser, $wgRequest;
97 $wgOut->disallowUserJs();
99 if ( $wgUser->isAllowed( 'reset-passwords') ) {
100 $wgOut->addScriptFile( 'changepassword.js' );
103 $self = SpecialPage
::getTitleFor( 'Resetpass' );
106 if ( !$wgUser->isLoggedIn() ) {
107 $rememberMe = '<tr>' .
109 '<td class="mw-input">' .
110 Xml
::checkLabel( wfMsg( 'remembermypassword' ),
111 'wpRemember', 'wpRemember',
112 $wgRequest->getCheck( 'wpRemember' ) ) .
115 $submitMsg = 'resetpass_submit';
116 $oldpassMsg = 'resetpass-temp-password';
118 $oldpassMsg = 'oldpassword';
119 $submitMsg = 'resetpass-submit-loggedin';
121 $s = Xml
::fieldset( wfMsg( 'resetpass_header' ) ) .
122 Xml
::openElement( 'form',
125 'action' => $self->getLocalUrl(),
126 'id' => 'mw-resetpass-form' ) ) .
127 Xml
::hidden( 'token', $wgUser->editToken() ) .
128 Xml
::hidden( 'returnto', $wgRequest->getVal( 'returnto' ) ) .
129 wfMsgExt( 'resetpass_text', array( 'parse' ) ) .
130 Xml
::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) );
131 $formElements = array(
132 array( 'wpName', 'username', 'text', $this->mUserName
, $wgUser->isAllowed( 'reset-passwords' ) ),
133 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass
, $this->mSelfChange
),
134 array( 'wpNewPassword', 'newpassword', 'password', '', true ),
135 array( 'wpRetype', 'retypenew', 'password', '', true ) );
136 if ( $wgUser->isAllowed( 'reset-passwords' ) && $this->mSelfChange
)
137 $formElements[] = array( 'wpComment', 'resetpass-comment', 'text', $this->mComment
, true );
138 $s .= $this->pretty( $formElements ) .
142 '<td class="mw-input">' .
143 Xml
::submitButton( wfMsg( $submitMsg ) ) .
146 Xml
::closeElement( 'table' ) .
147 Xml
::closeElement( 'form' ) .
148 Xml
::closeElement( 'fieldset' );
149 $wgOut->addHtml( $s );
152 function pretty( $fields ) {
154 foreach( $fields as $list ) {
155 list( $name, $label, $type, $value, $enabled ) = $list;
156 $params = array( 'id' => $name, 'type' => $type );
158 $params['disabled'] = 'disabled';
159 $field = Xml
::input( $name, 20, $value, $params );
161 $out .= '<td class="mw-label">';
162 $out .= Xml
::label( wfMsg( $label ), $name );
164 $out .= '<td class="mw-input">';
173 * @throws PasswordError when cannot set the new password because requirements not met.
175 protected function attemptReset( $newpass, $retype ) {
176 $user = User
::newFromName( $this->mUserName
);
177 if( !$user ||
$user->isAnon() ) {
178 throw new PasswordError( 'no such user' );
181 if( $newpass !== $retype ) {
182 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
183 throw new PasswordError( wfMsg( 'badretype' ) );
186 if ( $this->mSelfChange
) {
187 if( !$user->checkTemporaryPassword($this->mOldpass
) && !$user->checkPassword($this->mOldpass
) ) {
188 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
189 throw new PasswordError( wfMsg( 'resetpass-wrong-oldpass' ) );
194 $user->setPassword( $this->mNewpass
);
195 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
196 $this->mNewpass
= $this->mOldpass
= $this->mRetypePass
= '';
197 } catch( PasswordError
$e ) {
198 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
199 throw new PasswordError( $e->getMessage() );
203 if ( !$this->mSelfChange
) {
204 $log = new LogPage( 'password' );
205 $log->addEntry( 'reset', $user->getUserPage(), $this->mComment
);
209 $user->saveSettings();