From: Brad Jorsch Date: Fri, 2 Dec 2016 18:50:01 +0000 (-0500) Subject: Add password validation to Special:ChangeCredentials X-Git-Tag: 1.31.0-rc.0~3776^2 X-Git-Url: http://git.cyclocoop.org/%24dirpuce/puce%24spip_lang_rtl.gif?a=commitdiff_plain;h=d7e06fe0aaab53d356f40a7639cd0465d2180629;p=lhc%2Fweb%2Fwiklou.git Add password validation to Special:ChangeCredentials Change-Id: I70a99f4e742a2ba9ade0348001924fc5a50428d4 --- diff --git a/includes/specials/SpecialChangeCredentials.php b/includes/specials/SpecialChangeCredentials.php index b81ca3d50f..47f8d2f436 100644 --- a/includes/specials/SpecialChangeCredentials.php +++ b/includes/specials/SpecialChangeCredentials.php @@ -126,7 +126,27 @@ class SpecialChangeCredentials extends AuthManagerSpecialPage { if ( !static::$loadUserData ) { return []; } else { - return parent::getAuthFormDescriptor( $requests, $action ); + $descriptor = parent::getAuthFormDescriptor( $requests, $action ); + + $any = false; + foreach ( $descriptor as &$field ) { + if ( $field['type'] === 'password' && $field['name'] !== 'retype' ) { + $any = true; + if ( isset( $field['cssclass'] ) ) { + $field['cssclass'] .= ' mw-changecredentials-validate-password'; + } else { + $field['cssclass'] = 'mw-changecredentials-validate-password'; + } + } + } + + if ( $any ) { + $this->getOutput()->addModules( [ + 'mediawiki.special.changecredentials.js' + ] ); + } + + return $descriptor; } } diff --git a/resources/Resources.php b/resources/Resources.php index 814a3af36e..64dcf79150 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -1905,6 +1905,14 @@ return [ 'mediawiki.htmlform', ], ], + 'mediawiki.special.changecredentials.js' => [ + 'scripts' => 'resources/src/mediawiki.special/mediawiki.special.changecredentials.js', + 'dependencies' => [ + 'mediawiki.api', + 'mediawiki.htmlform.ooui' + ], + 'targets' => [ 'desktop', 'mobile' ], + ], 'mediawiki.special.changeslist' => [ 'styles' => 'resources/src/mediawiki.special/mediawiki.special.changeslist.css', 'targets' => [ 'desktop', 'mobile' ], diff --git a/resources/src/mediawiki.special/mediawiki.special.changecredentials.js b/resources/src/mediawiki.special/mediawiki.special.changecredentials.js new file mode 100644 index 0000000000..959287991d --- /dev/null +++ b/resources/src/mediawiki.special/mediawiki.special.changecredentials.js @@ -0,0 +1,55 @@ +/*! + * JavaScript for change credentials form. + */ +( function ( mw, $, OO ) { + mw.hook( 'htmlform.enhance' ).add( function ( $root ) { + var api = new mw.Api(); + + $root.find( '.mw-changecredentials-validate-password.oo-ui-fieldLayout' ).each( function () { + var currentApiPromise, + self = OO.ui.FieldLayout.static.infuse( $( this ) ); + + self.getField().setValidation( function ( password ) { + var d; + + if ( currentApiPromise ) { + currentApiPromise.abort(); + currentApiPromise = undefined; + } + + password = $.trim( password ); + + if ( password === '' ) { + self.setErrors( [] ); + return true; + } + + d = $.Deferred(); + currentApiPromise = api.post( { + action: 'validatepassword', + password: password, + formatversion: 2, + errorformat: 'html', + errorsuselocal: true, + uselang: mw.config.get( 'wgUserLanguage' ) + } ).done( function ( resp ) { + var pwinfo = resp.validatepassword, + good = pwinfo.validity === 'Good', + errors = []; + + currentApiPromise = undefined; + + if ( !good ) { + pwinfo.validitymessages.map( function ( m ) { + errors.push( new OO.ui.HtmlSnippet( m.html ) ); + } ); + } + self.setErrors( errors ); + d.resolve( good ); + } ).fail( d.reject ); + + return d.promise( { abort: currentApiPromise.abort } ); + } ); + } ); + } ); +}( mediaWiki, jQuery, OO ) );