Add password validation to Special:ChangeCredentials
authorBrad Jorsch <bjorsch@wikimedia.org>
Fri, 2 Dec 2016 18:50:01 +0000 (13:50 -0500)
committerAnomie <bjorsch@wikimedia.org>
Thu, 16 Mar 2017 15:42:24 +0000 (15:42 +0000)
Change-Id: I70a99f4e742a2ba9ade0348001924fc5a50428d4

includes/specials/SpecialChangeCredentials.php
resources/Resources.php
resources/src/mediawiki.special/mediawiki.special.changecredentials.js [new file with mode: 0644]

index b81ca3d..47f8d2f 100644 (file)
@@ -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;
                }
        }
 
index 814a3af..64dcf79 100644 (file)
@@ -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 (file)
index 0000000..9592879
--- /dev/null
@@ -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 ) );