| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Api; |
| 4 |
|
| 5 |
use MediaWiki\Auth\AuthManager; |
| 6 |
use MediaWiki\ParamValidator\TypeDef\UserDef; |
| 7 |
use MediaWiki\User\UserFactory; |
| 8 |
use MediaWiki\User\UserRigorOptions; |
| 9 |
use Wikimedia\ParamValidator\ParamValidator; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class ApiValidatePassword extends ApiBase { |
| 15 |
|
| 16 |
private AuthManager $authManager; |
| 17 |
private UserFactory $userFactory; |
| 18 |
|
| 19 |
public function __construct( |
| 20 |
ApiMain $mainModule, |
| 21 |
string $moduleName, |
| 22 |
AuthManager $authManager, |
| 23 |
UserFactory $userFactory |
| 24 |
) { |
| 25 |
parent::__construct( $mainModule, $moduleName ); |
| 26 |
$this->authManager = $authManager; |
| 27 |
$this->userFactory = $userFactory; |
| 28 |
} |
| 29 |
|
| 30 |
public function execute() { |
| 31 |
$params = $this->extractRequestParams(); |
| 32 |
|
| 33 |
$this->requirePostedParameters( [ 'password' ] ); |
| 34 |
|
| 35 |
if ( $params['user'] !== null ) { |
| 36 |
$user = $this->userFactory->newFromName( |
| 37 |
$params['user'], |
| 38 |
UserRigorOptions::RIGOR_CREATABLE |
| 39 |
); |
| 40 |
if ( !$user ) { |
| 41 |
$encParamName = $this->encodeParamName( 'user' ); |
| 42 |
$this->dieWithError( |
| 43 |
[ 'apierror-baduser', $encParamName, wfEscapeWikiText( $params['user'] ) ], |
| 44 |
"baduser_{$encParamName}" |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
if ( $user->isRegistered() || $this->authManager->userExists( $user->getName() ) ) { |
| 49 |
$this->dieWithError( 'userexists' ); |
| 50 |
} |
| 51 |
|
| 52 |
$user->setEmail( (string)$params['email'] ); |
| 53 |
$user->setRealName( (string)$params['realname'] ); |
| 54 |
} else { |
| 55 |
$user = $this->getUser(); |
| 56 |
} |
| 57 |
|
| 58 |
$r = []; |
| 59 |
$validity = $user->checkPasswordValidity( $params['password'] ); |
| 60 |
$r['validity'] = $validity->isGood() ? 'Good' : ( $validity->isOK() ? 'Change' : 'Invalid' ); |
| 61 |
$messages = array_merge( |
| 62 |
$this->getErrorFormatter()->arrayFromStatus( $validity, 'error' ), |
| 63 |
$this->getErrorFormatter()->arrayFromStatus( $validity, 'warning' ) |
| 64 |
); |
| 65 |
if ( $messages ) { |
| 66 |
$r['validitymessages'] = $messages; |
| 67 |
} |
| 68 |
|
| 69 |
$this->getHookRunner()->onApiValidatePassword( $this, $r ); |
| 70 |
|
| 71 |
$this->getResult()->addValue( null, $this->getModuleName(), $r ); |
| 72 |
} |
| 73 |
|
| 74 |
public function mustBePosted() { |
| 75 |
return true; |
| 76 |
} |
| 77 |
|
| 78 |
public function getAllowedParams() { |
| 79 |
return [ |
| 80 |
'password' => [ |
| 81 |
ParamValidator::PARAM_TYPE => 'password', |
| 82 |
ParamValidator::PARAM_REQUIRED => true |
| 83 |
], |
| 84 |
'user' => [ |
| 85 |
ParamValidator::PARAM_TYPE => 'user', |
| 86 |
UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'id' ], |
| 87 |
], |
| 88 |
'email' => null, |
| 89 |
'realname' => null, |
| 90 |
]; |
| 91 |
} |
| 92 |
|
| 93 |
protected function getExamplesMessages() { |
| 94 |
return [ |
| 95 |
'action=validatepassword&password=foobar' |
| 96 |
=> 'apihelp-validatepassword-example-1', |
| 97 |
'action=validatepassword&password=querty&user=Example' |
| 98 |
=> 'apihelp-validatepassword-example-2', |
| 99 |
]; |
| 100 |
} |
| 101 |
|
| 102 |
public function getHelpUrls() { |
| 103 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Validatepassword'; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
class_alias( ApiValidatePassword::class, 'ApiValidatePassword' ); |