3 namespace MediaWiki\Api
;
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
;
14 class ApiValidatePassword
extends ApiBase
{
16 private AuthManager
$authManager;
17 private UserFactory
$userFactory;
19 public function __construct(
22 AuthManager
$authManager,
23 UserFactory
$userFactory
25 parent
::__construct( $mainModule, $moduleName );
26 $this->authManager
= $authManager;
27 $this->userFactory
= $userFactory;
30 public function execute() {
31 $params = $this->extractRequestParams();
33 $this->requirePostedParameters( [ 'password' ] );
35 if ( $params['user'] !== null ) {
36 $user = $this->userFactory
->newFromName(
38 UserRigorOptions
::RIGOR_CREATABLE
41 $encParamName = $this->encodeParamName( 'user' );
43 [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $params['user'] ) ],
44 "baduser_{$encParamName}"
48 if ( $user->isRegistered() ||
$this->authManager
->userExists( $user->getName() ) ) {
49 $this->dieWithError( 'userexists' );
52 $user->setEmail( (string)$params['email'] );
53 $user->setRealName( (string)$params['realname'] );
55 $user = $this->getUser();
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' )
66 $r['validitymessages'] = $messages;
69 $this->getHookRunner()->onApiValidatePassword( $this, $r );
71 $this->getResult()->addValue( null, $this->getModuleName(), $r );
74 public function mustBePosted() {
78 public function getAllowedParams() {
81 ParamValidator
::PARAM_TYPE
=> 'password',
82 ParamValidator
::PARAM_REQUIRED
=> true
85 ParamValidator
::PARAM_TYPE
=> 'user',
86 UserDef
::PARAM_ALLOWED_USER_TYPES
=> [ 'name', 'id' ],
93 protected function getExamplesMessages() {
95 'action=validatepassword&password=foobar'
96 => 'apihelp-validatepassword-example-1',
97 'action=validatepassword&password=querty&user=Example'
98 => 'apihelp-validatepassword-example-2',
102 public function getHelpUrls() {
103 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Validatepassword';
107 /** @deprecated class alias since 1.43 */
108 class_alias( ApiValidatePassword
::class, 'ApiValidatePassword' );