wiki.techinc.nl/includes/api/ApiValidatePassword.php
Bartosz Dziewoński 365a588238 Use real type hints for services etc. in api/ except ApiQuery*.php
Mostly used find-and-replace:

Find:
/\*[\*\s]+@var (I?[A-Z](\w+)(?:Interface)?)[\s\*]+/\s*(private|protected|public) (\$[a-z]\w+;\n)((?=\s*/\*[\*\s]+@var (I?[A-Z](\w+)(?:Interface)?))\n|)
Replace with:
\3 \1 \4

Followed by some manual review to make sure I'm not changing too much,
omitting some changes that looked too complicated and anything that
caused test failures, and some whitespace fixes.

Change-Id: I6ec7587607df4f1a4f448a096c3e44c4e5270b70
2023-08-29 01:04:26 +00:00

109 lines
2.8 KiB
PHP

<?php
use MediaWiki\Auth\AuthManager;
use MediaWiki\ParamValidator\TypeDef\UserDef;
use MediaWiki\User\UserFactory;
use MediaWiki\User\UserRigorOptions;
use Wikimedia\ParamValidator\ParamValidator;
/**
* @ingroup API
*/
class ApiValidatePassword extends ApiBase {
private AuthManager $authManager;
private UserFactory $userFactory;
/**
* @param ApiMain $mainModule
* @param string $moduleName
* @param AuthManager $authManager
* @param UserFactory $userFactory
*/
public function __construct(
ApiMain $mainModule,
string $moduleName,
AuthManager $authManager,
UserFactory $userFactory
) {
parent::__construct( $mainModule, $moduleName );
$this->authManager = $authManager;
$this->userFactory = $userFactory;
}
public function execute() {
$params = $this->extractRequestParams();
$this->requirePostedParameters( [ 'password' ] );
if ( $params['user'] !== null ) {
$user = $this->userFactory->newFromName(
$params['user'],
UserRigorOptions::RIGOR_CREATABLE
);
if ( !$user ) {
$encParamName = $this->encodeParamName( 'user' );
$this->dieWithError(
[ 'apierror-baduser', $encParamName, wfEscapeWikiText( $params['user'] ) ],
"baduser_{$encParamName}"
);
}
if ( $user->isRegistered() || $this->authManager->userExists( $user->getName() ) ) {
$this->dieWithError( 'userexists' );
}
$user->setEmail( (string)$params['email'] );
$user->setRealName( (string)$params['realname'] );
} else {
$user = $this->getUser();
}
$r = [];
$validity = $user->checkPasswordValidity( $params['password'] );
$r['validity'] = $validity->isGood() ? 'Good' : ( $validity->isOK() ? 'Change' : 'Invalid' );
$messages = array_merge(
$this->getErrorFormatter()->arrayFromStatus( $validity, 'error' ),
$this->getErrorFormatter()->arrayFromStatus( $validity, 'warning' )
);
if ( $messages ) {
$r['validitymessages'] = $messages;
}
$this->getHookRunner()->onApiValidatePassword( $this, $r );
$this->getResult()->addValue( null, $this->getModuleName(), $r );
}
public function mustBePosted() {
return true;
}
public function getAllowedParams() {
return [
'password' => [
ParamValidator::PARAM_TYPE => 'password',
ParamValidator::PARAM_REQUIRED => true
],
'user' => [
ParamValidator::PARAM_TYPE => 'user',
UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'id' ],
],
'email' => null,
'realname' => null,
];
}
protected function getExamplesMessages() {
return [
'action=validatepassword&password=foobar'
=> 'apihelp-validatepassword-example-1',
'action=validatepassword&password=querty&user=Example'
=> 'apihelp-validatepassword-example-2',
];
}
public function getHelpUrls() {
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Validatepassword';
}
}