UserDef: use UserIdentityValue in a few places

Only documented to return UserIdentity

Change-Id: Id5ab900ac39fe521b8e156cfb2042cc4c2793f90
This commit is contained in:
DannyS712 2021-05-21 23:06:55 +00:00
parent 59ebd480c8
commit 878ea9b6e6
2 changed files with 33 additions and 10 deletions

View file

@ -5,6 +5,7 @@ namespace MediaWiki\ParamValidator\TypeDef;
use ExternalUserNames;
use MediaWiki\User\UserFactory;
use MediaWiki\User\UserIdentity;
use MediaWiki\User\UserIdentityValue;
use MediaWiki\User\UserNameUtils;
use TitleFactory;
use Wikimedia\IPUtils;
@ -168,10 +169,14 @@ class UserDef extends TypeDef {
// An interwiki username?
if ( ExternalUserNames::isExternal( $value ) ) {
$name = $this->userNameUtils->getCanonical( $value, UserNameUtils::RIGOR_NONE );
return [
'interwiki',
is_string( $name ) ? $this->userFactory->newFromAnyId( 0, $value, null ) : null
];
// UserIdentityValue has the username which includes the > separating the external
// wiki database and the actual name, but is created for the *local* wiki, like
// for User objects (local is the default, but we specify it anyway to show
// that its intentional even though the username is for a different wiki)
// NOTE: We deliberately use the raw $value instead of the canonical $name
// to avoid convering the first character of the interwiki prefic to uppercase
$user = is_string( $name ) ? new UserIdentityValue( 0, $value, UserIdentityValue::LOCAL ) : null;
return [ 'interwiki', $user ];
}
// A valid user name?
@ -205,12 +210,22 @@ class UserDef extends TypeDef {
// addresses.
preg_match( "/^$b\.$b\.$b\.xxx$/D", $value )
) {
return [ 'ip', $this->userFactory->newAnonymous( IPUtils::sanitizeIP( $value ) ) ];
$name = IPUtils::sanitizeIP( $value );
// We don't really need to use UserNameUtils::getCanonical() because for anonymous
// users the only validation is that there is no `#` (which is already the case if its
// a valid IP or matches the regex) and the only normalization is making the first
// character uppercase (doesn't matter for numbers) and replacing underscores with
// spaces (doesn't apply to IPs). But, better safe than sorry?
$name = $this->userNameUtils->getCanonical( $name, UserNameUtils::RIGOR_NONE );
return [ 'ip', UserIdentityValue::newAnonymous( $name ) ];
}
// A range?
if ( IPUtils::isValidRange( $value ) ) {
return [ 'cidr', $this->userFactory->newFromAnyId( 0, IPUtils::sanitizeIP( $value ), null ) ];
$name = IPUtils::sanitizeIP( $value );
// Per above, the UserNameUtils call isn't strictly needed, but doesn't hurt
$name = $this->userNameUtils->getCanonical( $name, UserNameUtils::RIGOR_NONE );
return [ 'cidr', UserIdentityValue::newAnonymous( $name ) ];
}
// Fail.

View file

@ -4,6 +4,7 @@ namespace MediaWiki\ParamValidator\TypeDef;
use MediaWiki\Interwiki\ClassicInterwikiLookup;
use MediaWiki\MediaWikiServices;
use MediaWiki\User\UserIdentityValue;
use User;
use Wikimedia\Message\DataMessageValue;
use Wikimedia\ParamValidator\ParamValidator;
@ -114,11 +115,18 @@ class UserDefTest extends TypeDefTestCase {
$ex,
[ UserDef::PARAM_ALLOWED_USER_TYPES => $types ],
];
if ( $type === 'ip' ) {
$obj = $userFactory->newAnonymous( $expect );
} elseif ( $type === 'interwiki' || $type === 'cidr' ) {
$obj = $userFactory->newFromAnyId( 0, $expect, null );
if ( $type === 'ip'
|| $type === 'interwiki'
|| $type === 'cidr'
) {
// For all of these the UserIdentity returned will be a
// UserIdentityValue object since the name and id are both
// known (id is 0 for all)
$obj = UserIdentityValue::newAnonymous( $expect );
} else {
// Creating from name, not a UserIdentityValue (yet) since
// UserDef does not check for the relevant user id itself,
// but relies on the loading in User::getId() instead
$obj = $userFactory->newFromName( $expect );
}