Merge "Add option for showing valid IPs in UsersMultiselectWidget menu"

This commit is contained in:
jenkins-bot 2019-11-22 16:35:01 +00:00 committed by Gerrit Code Review
commit c31f1919cc
3 changed files with 128 additions and 29 deletions

View file

@ -83,6 +83,18 @@ class HTMLUsersMultiselectField extends HTMLUserTextField {
$params['tagLimit'] = $this->mParams['max'];
}
if ( isset( $this->mParams['ipallowed'] ) ) {
$params['ipAllowed'] = $this->mParams['ipallowed'];
}
if ( isset( $this->mParams['iprange'] ) ) {
$params['ipRangeAllowed'] = $this->mParams['iprange'];
}
if ( isset( $this->mParams['iprangelimits'] ) ) {
$params['ipRangeLimits'] = $this->mParams['iprangelimits'];
}
if ( !is_null( $value ) ) {
// $value is a string, but the widget expects an array
$params['default'] = $value === '' ? [] : explode( "\n", $value );

View file

@ -9,12 +9,35 @@ namespace MediaWiki\Widget;
* @license MIT
*/
class UsersMultiselectWidget extends TagMultiselectWidget {
/** @var bool */
protected $ipAllowed;
/** @var bool */
protected $ipRangeAllowed;
/** @var array */
protected $ipRangeLimits;
/**
* @param array $config Configuration options
* - bool $config['ipAllowed'] Accept valid IP addresses
* - bool $config['ipRangeAllowed'] Accept valid IP ranges
* - array $config['ipRangeLimits'] Maximum allowed IP range sizes
*/
public function __construct( array $config = [] ) {
parent::__construct( $config );
if ( isset( $config['ipAllowed'] ) ) {
$this->ipAllowed = $config['ipAllowed'];
}
if ( isset( $config['ipRangeAllowed'] ) ) {
$this->ipRangeAllowed = $config['ipRangeAllowed'];
}
if ( isset( $config['ipRangeLimits'] ) ) {
$this->ipRangeLimits = $config['ipRangeLimits'];
}
}
protected function getJavaScriptClassName() {
@ -22,6 +45,18 @@ class UsersMultiselectWidget extends TagMultiselectWidget {
}
public function getConfig( &$config ) {
if ( $this->ipAllowed !== null ) {
$config['ipAllowed'] = $this->ipAllowed;
}
if ( $this->ipRangeAllowed !== null ) {
$config['ipRangeAllowed'] = $this->ipRangeAllowed;
}
if ( $this->ipRangeLimits !== null ) {
$config['ipRangeLimits'] = $this->ipRangeLimits;
}
return parent::getConfig( $config );
}

View file

@ -13,6 +13,9 @@
* If used inside HTML form the results will be sent as the list of
* newline-separated usernames.
*
* This can be configured to accept IP addresses and/or ranges as well as
* usernames.
*
* @class
* @extends OO.ui.MenuTagMultiselectWidget
*
@ -21,11 +24,23 @@
* @cfg {mw.Api} [api] Instance of mw.Api (or subclass thereof) to use for queries
* @cfg {number} [limit=10] Number of results to show in autocomplete menu
* @cfg {string} [name] Name of input to submit results (when used in HTML forms)
* @cfg {boolean} [ipAllowed=false] Show IP addresses in autocomplete menu
* If false, single IP addresses are not allowed, even if IP ranges are allowed.
* @cfg {boolean} [ipRangeAllowed=false] Show IP ranges in autocomplete menu
* @cfg {Object} [ipRangeLimits] Maximum allowed IP ranges (defaults match HTMLUserTextField.php)
* @cfg {number} [ipRangeLimits.IPv4 = 16] Maximum allowed IPv4 range
* @cfg {number} [ipRangeLimits.IPv6 = 32] Maximum allowed IPv6 range
*/
mw.widgets.UsersMultiselectWidget = function MwWidgetsUsersMultiselectWidget( config ) {
// Config initialization
config = $.extend( {
limit: 10
limit: 10,
ipAllowed: false,
ipRangeAllowed: false,
ipRangeLimits: {
IPv4: 16,
IPv6: 32
}
}, config, {
// Because of using autocomplete (constantly changing menu), we need to
// allow adding usernames, which do not present in the menu.
@ -40,6 +55,9 @@
// Properties
this.limit = config.limit;
this.ipAllowed = config.ipAllowed;
this.ipRangeAllowed = config.ipRangeAllowed;
this.ipRangeLimits = config.ipRangeLimits;
if ( 'name' in config ) {
// Use this instead of <input type="hidden">, because hidden inputs do not have separate
@ -89,7 +107,9 @@
* @private
*/
mw.widgets.UsersMultiselectWidget.prototype.updateMenuItems = function () {
var inputValue = this.input.getValue();
var isValidIp,
isValidRange,
inputValue = this.input.getValue();
if ( inputValue === this.inputValue ) {
// Do not restart api query if nothing has changed in the input
@ -103,42 +123,74 @@
if ( inputValue.length > 0 ) {
this.pushPending();
this.api.get( {
action: 'query',
list: 'allusers',
// Prefix of list=allusers is case sensitive. Normalise first
// character to uppercase so that "fo" may yield "Foo".
auprefix: inputValue[ 0 ].toUpperCase() + inputValue.slice( 1 ),
aulimit: this.limit
} ).done( function ( response ) {
var suggestions = response.query.allusers,
selected = this.getSelectedUsernames();
if ( this.ipAllowed || this.ipRangeAllowed ) {
isValidIp = mw.util.isIPAddress( inputValue, false );
isValidRange = !isValidIp &&
mw.util.isIPAddress( inputValue, true ) &&
this.validateIpRange( inputValue );
}
// Remove usernames, which are already selected from suggestions
suggestions = suggestions.map( function ( user ) {
if ( selected.indexOf( user.name ) === -1 ) {
return new OO.ui.MenuOptionWidget( {
data: user.name,
label: user.name
} );
}
} ).filter( function ( item ) {
return item !== undefined;
} );
// Remove all items from menu add fill it with new
if ( this.ipAllowed && isValidIp || this.ipRangeAllowed && isValidRange ) {
this.menu.clearItems();
this.menu.addItems( suggestions );
// Make the menu visible; it might not be if it was previously empty
this.menu.addItems( [
new OO.ui.MenuOptionWidget( {
data: inputValue,
label: inputValue
} )
] );
this.menu.toggle( true );
this.popPending();
}.bind( this ) ).fail( this.popPending.bind( this ) );
} else {
this.api.get( {
action: 'query',
list: 'allusers',
// Prefix of list=allusers is case sensitive. Normalise first
// character to uppercase so that "fo" may yield "Foo".
auprefix: inputValue[ 0 ].toUpperCase() + inputValue.slice( 1 ),
aulimit: this.limit
} ).done( function ( response ) {
var suggestions = response.query.allusers,
selected = this.getSelectedUsernames();
// Remove usernames, which are already selected from suggestions
suggestions = suggestions.map( function ( user ) {
if ( selected.indexOf( user.name ) === -1 ) {
return new OO.ui.MenuOptionWidget( {
data: user.name,
label: user.name
} );
}
} ).filter( function ( item ) {
return item !== undefined;
} );
// Remove all items from menu add fill it with new
this.menu.clearItems();
this.menu.addItems( suggestions );
// Make the menu visible; it might not be if it was previously empty
this.menu.toggle( true );
this.popPending();
}.bind( this ) ).fail( this.popPending.bind( this ) );
}
} else {
this.menu.clearItems();
}
};
/**
* @private
* @param {string} ipRange Valid IPv4 or IPv6 range
* @return {boolean} The IP range is within the size limit
*/
mw.widgets.UsersMultiselectWidget.prototype.validateIpRange = function ( ipRange ) {
ipRange = ipRange.split( '/' );
return mw.util.isIPv4Address( ipRange[ 0 ] ) && +ipRange[ 1 ] >= this.ipRangeLimits.IPv4 ||
mw.util.isIPv6Address( ipRange[ 0 ] ) && +ipRange[ 1 ] >= this.ipRangeLimits.IPv6;
};
mw.widgets.UsersMultiselectWidget.prototype.onInputChange = function () {
mw.widgets.UsersMultiselectWidget.parent.prototype.onInputChange.apply( this, arguments );