Create a function that checks if a username is a temporary user in JS
Bug: T327888 Change-Id: I45c888251676e7e84fef2eccf0e33b29d46c12e1
This commit is contained in:
parent
4058db4250
commit
9e32ff669e
3 changed files with 55 additions and 0 deletions
|
|
@ -1190,6 +1190,7 @@ return [
|
|||
MainConfigNames::FragmentMode,
|
||||
MainConfigNames::GenerateThumbnailOnParse,
|
||||
MainConfigNames::LoadScript,
|
||||
MainConfigNames::AutoCreateTempUser,
|
||||
] ],
|
||||
[ 'name' => 'portletLinkOptions.json', 'callback' => [ Skin::class, 'getPortletLinkOptions' ] ],
|
||||
],
|
||||
|
|
|
|||
|
|
@ -1009,6 +1009,39 @@ util = {
|
|||
ip = ip.toLowerCase();
|
||||
}
|
||||
return ip;
|
||||
},
|
||||
|
||||
/**
|
||||
* This functionality has been adapted from MediaWiki\User\TempUser\Pattern::isMatch()
|
||||
*
|
||||
* Checks if the pattern matches the given username
|
||||
*
|
||||
* @param {string} username
|
||||
* @return {boolean}
|
||||
*/
|
||||
isTemporaryUser: function ( username ) {
|
||||
var autoCreateUserMatchPattern = config.AutoCreateTempUser.matchPattern;
|
||||
var position = autoCreateUserMatchPattern.indexOf( '$1' );
|
||||
|
||||
if ( !config.AutoCreateTempUser.enabled ) {
|
||||
return false;
|
||||
}
|
||||
// '$1' was not found in autoCreateUserMatchPattern
|
||||
if ( position === -1 ) {
|
||||
return false;
|
||||
}
|
||||
var prefix = autoCreateUserMatchPattern.slice( 0, position );
|
||||
var suffix = autoCreateUserMatchPattern.slice( position + '$1'.length );
|
||||
|
||||
var match = true;
|
||||
if ( prefix !== '' ) {
|
||||
match = ( username.indexOf( prefix ) === 0 );
|
||||
}
|
||||
if ( match && suffix !== '' ) {
|
||||
match = ( username.indexOf( suffix, username.length - suffix.length ) !== -1 ) &&
|
||||
( username.length >= prefix.length + suffix.length );
|
||||
}
|
||||
return match;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -852,4 +852,25 @@
|
|||
} );
|
||||
|
||||
} );
|
||||
|
||||
QUnit.test( 'isTemporaryUser', function ( assert ) {
|
||||
var usernames = [
|
||||
[ '*$1', 'Test', false, true, 'prefix mismatch' ],
|
||||
[ '*$1', '*Some user', true, true, 'prefix match' ],
|
||||
[ '$1*', 'Some user*', true, true, 'suffix only match' ],
|
||||
[ '$1*', 'Some user', false, true, 'suffix only mismatch' ],
|
||||
[ '*$1*', '*Unregistered 123*', true, true, 'prefix and suffix match' ],
|
||||
[ '*$1*', 'Unregistered 123*', false, true, 'prefix and suffix mismatch' ],
|
||||
[ '*$1*', '**', true, true, 'prefix and suffix zero length match' ],
|
||||
[ '*$1*', '*', false, true, 'prefix and suffix overlapping' ],
|
||||
[ '*$1*', '*', false, false, 'Auto create temporary user disabled' ]
|
||||
];
|
||||
usernames.forEach( function ( username ) {
|
||||
mw.util.setOptionsForTest( {
|
||||
AutoCreateTempUser: { enabled: username[ 3 ], matchPattern: username[ 0 ] }
|
||||
} );
|
||||
|
||||
assert.strictEqual( util.isTemporaryUser( username[ 1 ] ), username[ 2 ], username[ 4 ] );
|
||||
} );
|
||||
} );
|
||||
}() );
|
||||
|
|
|
|||
Loading…
Reference in a new issue