wiki.techinc.nl/includes/api/ApiValidatePassword.php
Brad Jorsch c2b1525908 API: Use ParamValidator library
This brings significant modularization to the Action API's parameter
validation, and allows the Action API and MW REST API to share
validation code.

Note there are several changes in this patch that may affect other code;
see the entries in RELEASE-NOTES-1.35 for details.

Bug: T142080
Bug: T232672
Bug: T21195
Bug: T34675
Bug: T154774
Change-Id: I1462edc1701278760fa695308007006868b249fc
Depends-On: I10011be060fe6d27c7527312ad41218786b3f40d
2020-02-04 13:36:14 -05:00

84 lines
2.1 KiB
PHP

<?php
use MediaWiki\Auth\AuthManager;
use MediaWiki\ParamValidator\TypeDef\UserDef;
/**
* @ingroup API
*/
class ApiValidatePassword extends ApiBase {
public function execute() {
$params = $this->extractRequestParams();
// For sanity
$this->requirePostedParameters( [ 'password' ] );
if ( $params['user'] !== null ) {
$user = User::newFromName( $params['user'], 'creatable' );
if ( !$user ) {
$encParamName = $this->encodeParamName( 'user' );
$this->dieWithError(
[ 'apierror-baduser', $encParamName, wfEscapeWikiText( $params['user'] ) ],
"baduser_{$encParamName}"
);
}
if ( !$user->isAnon() || AuthManager::singleton()->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;
}
Hooks::run( 'ApiValidatePassword', [ $this, &$r ] );
$this->getResult()->addValue( null, $this->getModuleName(), $r );
}
public function mustBePosted() {
return true;
}
public function getAllowedParams() {
return [
'password' => [
ApiBase::PARAM_TYPE => 'password',
ApiBase::PARAM_REQUIRED => true
],
'user' => [
ApiBase::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';
}
}