| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\ParamValidator\TypeDef; |
| 4 |
|
| 5 |
use MediaWiki\Title\MalformedTitleException; |
| 6 |
use MediaWiki\Title\TitleParser; |
| 7 |
use MediaWiki\User\ExternalUserNames; |
| 8 |
use MediaWiki\User\UserIdentity; |
| 9 |
use MediaWiki\User\UserIdentityLookup; |
| 10 |
use MediaWiki\User\UserIdentityValue; |
| 11 |
use MediaWiki\User\UserNameUtils; |
| 12 |
use MediaWiki\User\UserRigorOptions; |
| 13 |
use Wikimedia\IPUtils; |
| 14 |
use Wikimedia\Message\MessageValue; |
| 15 |
use Wikimedia\ParamValidator\Callbacks; |
| 16 |
use Wikimedia\ParamValidator\ParamValidator; |
| 17 |
use Wikimedia\ParamValidator\TypeDef; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
class UserDef extends TypeDef { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public const PARAM_ALLOWED_USER_TYPES = 'param-allowed-user-types'; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public const PARAM_RETURN_OBJECT = 'param-return-object'; |
| 58 |
|
| 59 |
|
| 60 |
private $userIdentityLookup; |
| 61 |
|
| 62 |
|
| 63 |
private $titleParser; |
| 64 |
|
| 65 |
|
| 66 |
private $userNameUtils; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public function __construct( |
| 75 |
Callbacks $callbacks, |
| 76 |
UserIdentityLookup $userIdentityLookup, |
| 77 |
TitleParser $titleParser, |
| 78 |
UserNameUtils $userNameUtils |
| 79 |
) { |
| 80 |
parent::__construct( $callbacks ); |
| 81 |
$this->userIdentityLookup = $userIdentityLookup; |
| 82 |
$this->titleParser = $titleParser; |
| 83 |
$this->userNameUtils = $userNameUtils; |
| 84 |
} |
| 85 |
|
| 86 |
public function validate( $name, $value, array $settings, array $options ) { |
| 87 |
$this->failIfNotString( $name, $value, $settings, $options ); |
| 88 |
|
| 89 |
[ $type, $user ] = $this->processUser( $value ); |
| 90 |
|
| 91 |
if ( !$user || !in_array( $type, $settings[self::PARAM_ALLOWED_USER_TYPES], true ) ) { |
| 92 |
|
| 93 |
$this->failure( 'baduser', $name, $value, $settings, $options ); |
| 94 |
} |
| 95 |
|
| 96 |
return empty( $settings[self::PARAM_RETURN_OBJECT] ) ? $user->getName() : $user; |
| 97 |
} |
| 98 |
|
| 99 |
public function normalizeSettings( array $settings ) { |
| 100 |
if ( isset( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) { |
| 101 |
$settings[self::PARAM_ALLOWED_USER_TYPES] = array_values( array_intersect( |
| 102 |
[ 'name', 'ip', 'temp', 'cidr', 'interwiki', 'id' ], |
| 103 |
$settings[self::PARAM_ALLOWED_USER_TYPES] |
| 104 |
) ); |
| 105 |
} |
| 106 |
if ( empty( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) { |
| 107 |
$settings[self::PARAM_ALLOWED_USER_TYPES] = [ 'name', 'ip', 'temp', 'cidr', 'interwiki' ]; |
| 108 |
} |
| 109 |
|
| 110 |
return parent::normalizeSettings( $settings ); |
| 111 |
} |
| 112 |
|
| 113 |
public function checkSettings( string $name, $settings, array $options, array $ret ): array { |
| 114 |
$ret = parent::checkSettings( $name, $settings, $options, $ret ); |
| 115 |
|
| 116 |
$ret['allowedKeys'] = array_merge( $ret['allowedKeys'], [ |
| 117 |
self::PARAM_ALLOWED_USER_TYPES, self::PARAM_RETURN_OBJECT, |
| 118 |
] ); |
| 119 |
|
| 120 |
if ( !is_bool( $settings[self::PARAM_RETURN_OBJECT] ?? false ) ) { |
| 121 |
$ret['issues'][self::PARAM_RETURN_OBJECT] = 'PARAM_RETURN_OBJECT must be boolean, got ' |
| 122 |
. gettype( $settings[self::PARAM_RETURN_OBJECT] ); |
| 123 |
} |
| 124 |
|
| 125 |
$hasId = false; |
| 126 |
if ( isset( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) { |
| 127 |
if ( !is_array( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) { |
| 128 |
$ret['issues'][self::PARAM_ALLOWED_USER_TYPES] = 'PARAM_ALLOWED_USER_TYPES must be an array, ' |
| 129 |
. 'got ' . gettype( $settings[self::PARAM_ALLOWED_USER_TYPES] ); |
| 130 |
} elseif ( $settings[self::PARAM_ALLOWED_USER_TYPES] === [] ) { |
| 131 |
$ret['issues'][self::PARAM_ALLOWED_USER_TYPES] = 'PARAM_ALLOWED_USER_TYPES cannot be empty'; |
| 132 |
} else { |
| 133 |
$bad = array_diff( |
| 134 |
$settings[self::PARAM_ALLOWED_USER_TYPES], |
| 135 |
[ 'name', 'ip', 'temp', 'cidr', 'interwiki', 'id' ] |
| 136 |
); |
| 137 |
if ( $bad ) { |
| 138 |
$ret['issues'][self::PARAM_ALLOWED_USER_TYPES] = |
| 139 |
'PARAM_ALLOWED_USER_TYPES contains invalid values: ' . implode( ', ', $bad ); |
| 140 |
} |
| 141 |
|
| 142 |
$hasId = in_array( 'id', $settings[self::PARAM_ALLOWED_USER_TYPES], true ); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
if ( !empty( $settings[ParamValidator::PARAM_ISMULTI] ) && |
| 147 |
( $hasId || !empty( $settings[self::PARAM_RETURN_OBJECT] ) ) && |
| 148 |
( |
| 149 |
( $settings[ParamValidator::PARAM_ISMULTI_LIMIT1] ?? 100 ) > 10 || |
| 150 |
( $settings[ParamValidator::PARAM_ISMULTI_LIMIT2] ?? 100 ) > 10 |
| 151 |
) |
| 152 |
) { |
| 153 |
$ret['issues'][] = 'Multi-valued user-type parameters with PARAM_RETURN_OBJECT or allowing IDs ' |
| 154 |
. 'should set low values (<= 10) for PARAM_ISMULTI_LIMIT1 and PARAM_ISMULTI_LIMIT2.' |
| 155 |
. ' (Note that "<= 10" is arbitrary. If something hits this, we can investigate a real limit ' |
| 156 |
. 'once we have a real use case to look at.)'; |
| 157 |
} |
| 158 |
|
| 159 |
return $ret; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
private function processUser( string $value ): array { |
| 169 |
|
| 170 |
if ( preg_match( '/^#(\d+)$/D', $value, $m ) ) { |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
$userId = (int)$m[1]; |
| 178 |
if ( $userId !== 0 ) { |
| 179 |
|
| 180 |
$userIdentity = $this->userIdentityLookup->getUserIdentityByUserId( $userId ); |
| 181 |
if ( $userIdentity ) { |
| 182 |
return [ 'id', $userIdentity ]; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
return [ |
| 187 |
'id', |
| 188 |
new UserIdentityValue( 0, "Unknown user" ) |
| 189 |
]; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
if ( ExternalUserNames::isExternal( $value ) ) { |
| 194 |
$name = $this->userNameUtils->getCanonical( $value, UserRigorOptions::RIGOR_NONE ); |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
$user = $name !== false ? new UserIdentityValue( 0, $value, UserIdentityValue::LOCAL ) : null; |
| 202 |
return [ 'interwiki', $user ]; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
if ( $this->userNameUtils->isTemp( $value ) ) { |
| 207 |
$userIdentity = $this->userIdentityLookup->getUserIdentityByName( $value ); |
| 208 |
return [ 'temp', $userIdentity ]; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
$canonicalName = $this->userNameUtils->getCanonical( $value, UserRigorOptions::RIGOR_VALID ); |
| 218 |
if ( $canonicalName !== false ) { |
| 219 |
$userIdentity = $this->userIdentityLookup->getUserIdentityByName( $canonicalName ); |
| 220 |
if ( $userIdentity ) { |
| 221 |
return [ 'name', $userIdentity ]; |
| 222 |
} |
| 223 |
|
| 224 |
return [ |
| 225 |
'name', |
| 226 |
new UserIdentityValue( 0, $canonicalName ) |
| 227 |
]; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
if ( strpos( $value, '#' ) !== false ) { |
| 233 |
return [ '', null ]; |
| 234 |
} |
| 235 |
|
| 236 |
try { |
| 237 |
$t = $this->titleParser->parseTitle( $value ); |
| 238 |
} catch ( MalformedTitleException $_ ) { |
| 239 |
$t = null; |
| 240 |
} |
| 241 |
if ( !$t || $t->getNamespace() !== NS_USER || $t->isExternal() ) { |
| 242 |
try { |
| 243 |
$t = $this->titleParser->parseTitle( "User:$value" ); |
| 244 |
} catch ( MalformedTitleException $_ ) { |
| 245 |
$t = null; |
| 246 |
} |
| 247 |
} |
| 248 |
if ( !$t || $t->getNamespace() !== NS_USER || $t->isExternal() ) { |
| 249 |
|
| 250 |
return [ '', null ]; |
| 251 |
} |
| 252 |
$value = $t->getText(); |
| 253 |
|
| 254 |
|
| 255 |
$b = IPUtils::RE_IP_BYTE; |
| 256 |
if ( IPUtils::isValid( $value ) || |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
preg_match( "/^$b\.$b\.$b\.xxx$/D", $value ) |
| 262 |
) { |
| 263 |
$name = IPUtils::sanitizeIP( $value ); |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
$name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_NONE ); |
| 270 |
return [ 'ip', UserIdentityValue::newAnonymous( $name ) ]; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
if ( IPUtils::isValidRange( $value ) ) { |
| 275 |
$name = IPUtils::sanitizeIP( $value ); |
| 276 |
|
| 277 |
$name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_NONE ); |
| 278 |
return [ 'cidr', UserIdentityValue::newAnonymous( $name ) ]; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
return [ '', null ]; |
| 283 |
} |
| 284 |
|
| 285 |
public function getParamInfo( $name, array $settings, array $options ) { |
| 286 |
$info = parent::getParamInfo( $name, $settings, $options ); |
| 287 |
|
| 288 |
$info['subtypes'] = $settings[self::PARAM_ALLOWED_USER_TYPES]; |
| 289 |
|
| 290 |
return $info; |
| 291 |
} |
| 292 |
|
| 293 |
public function getHelpInfo( $name, array $settings, array $options ) { |
| 294 |
$info = parent::getParamInfo( $name, $settings, $options ); |
| 295 |
|
| 296 |
$isMulti = !empty( $settings[ParamValidator::PARAM_ISMULTI] ); |
| 297 |
|
| 298 |
$subtypes = []; |
| 299 |
foreach ( $settings[self::PARAM_ALLOWED_USER_TYPES] as $st ) { |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
$subtypes[] = MessageValue::new( "paramvalidator-help-type-user-subtype-$st" ); |
| 305 |
} |
| 306 |
$info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-user' ) |
| 307 |
->params( $isMulti ? 2 : 1 ) |
| 308 |
->textListParams( $subtypes ) |
| 309 |
->numParams( count( $subtypes ) ); |
| 310 |
|
| 311 |
return $info; |
| 312 |
} |
| 313 |
|
| 314 |
} |