3 use MediaWiki\Auth\AuthManager
;
8 class ApiValidatePassword
extends ApiBase
{
10 public function execute() {
11 $params = $this->extractRequestParams();
14 $this->requirePostedParameters( [ 'password' ] );
16 if ( $params['user'] !== null ) {
17 $user = User
::newFromName( $params['user'], 'creatable' );
19 $encParamName = $this->encodeParamName( 'user' );
21 [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $params['user'] ) ],
22 "baduser_{$encParamName}"
26 if ( !$user->isAnon() || AuthManager
::singleton()->userExists( $user->getName() ) ) {
27 $this->dieWithError( 'userexists' );
30 $user->setEmail( (string)$params['email'] );
31 $user->setRealName( (string)$params['realname'] );
33 $user = $this->getUser();
36 $validity = $user->checkPasswordValidity( $params['password'] );
37 $r['validity'] = $validity->isGood() ?
'Good' : ( $validity->isOK() ?
'Change' : 'Invalid' );
38 $messages = array_merge(
39 $this->getErrorFormatter()->arrayFromStatus( $validity, 'error' ),
40 $this->getErrorFormatter()->arrayFromStatus( $validity, 'warning' )
43 $r['validitymessages'] = $messages;
46 Hooks
::run( 'ApiValidatePassword', [ $this, &$r ] );
48 $this->getResult()->addValue( null, $this->getModuleName(), $r );
51 public function mustBePosted() {
55 public function getAllowedParams() {
58 ApiBase
::PARAM_TYPE
=> 'password',
59 ApiBase
::PARAM_REQUIRED
=> true
62 ApiBase
::PARAM_TYPE
=> 'user',
69 protected function getExamplesMessages() {
71 'action=validatepassword&password=foobar'
72 => 'apihelp-validatepassword-example-1',
73 'action=validatepassword&password=querty&user=Example'
74 => 'apihelp-validatepassword-example-2',
78 public function getHelpUrls() {
79 return 'https://www.mediawiki.org/wiki/API:Validatepassword';