Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiValidatePassword
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 6
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
72
 mustBePosted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Api;
4
5use MediaWiki\Auth\AuthManager;
6use MediaWiki\ParamValidator\TypeDef\UserDef;
7use MediaWiki\User\UserFactory;
8use MediaWiki\User\UserRigorOptions;
9use Wikimedia\ParamValidator\ParamValidator;
10
11/**
12 * @ingroup API
13 */
14class 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->logFeatureUsage( 'action=validatepassword' );
34
35        $this->requirePostedParameters( [ 'password' ] );
36
37        if ( $params['user'] !== null ) {
38            $user = $this->userFactory->newFromName(
39                $params['user'],
40                UserRigorOptions::RIGOR_CREATABLE
41            );
42            if ( !$user ) {
43                $encParamName = $this->encodeParamName( 'user' );
44                $this->dieWithError(
45                    [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $params['user'] ) ],
46                    "baduser_{$encParamName}"
47                );
48            }
49
50            if ( $user->isRegistered() || $this->authManager->userExists( $user->getName() ) ) {
51                $this->dieWithError( 'userexists' );
52            }
53
54            $user->setEmail( (string)$params['email'] );
55            $user->setRealName( (string)$params['realname'] );
56        } else {
57            $user = $this->getUser();
58        }
59
60        $r = [];
61        $validity = $user->checkPasswordValidity( $params['password'] );
62        $r['validity'] = $validity->isGood() ? 'Good' : ( $validity->isOK() ? 'Change' : 'Invalid' );
63        $messages = array_merge(
64            $this->getErrorFormatter()->arrayFromStatus( $validity, 'error' ),
65            $this->getErrorFormatter()->arrayFromStatus( $validity, 'warning' )
66        );
67        if ( $messages ) {
68            $r['validitymessages'] = $messages;
69        }
70
71        $this->getHookRunner()->onApiValidatePassword( $this, $r );
72
73        $this->getResult()->addValue( null, $this->getModuleName(), $r );
74    }
75
76    /** @inheritDoc */
77    public function mustBePosted() {
78        return true;
79    }
80
81    /** @inheritDoc */
82    public function getAllowedParams() {
83        return [
84            'password' => [
85                ParamValidator::PARAM_TYPE => 'password',
86                ParamValidator::PARAM_REQUIRED => true
87            ],
88            'user' => [
89                ParamValidator::PARAM_TYPE => 'user',
90                UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'id' ],
91            ],
92            'email' => null,
93            'realname' => null,
94        ];
95    }
96
97    /** @inheritDoc */
98    protected function getExamplesMessages() {
99        return [
100            'action=validatepassword&password=foobar'
101                => 'apihelp-validatepassword-example-1',
102            'action=validatepassword&password=querty&user=Example'
103                => 'apihelp-validatepassword-example-2',
104        ];
105    }
106
107    /** @inheritDoc */
108    public function getHelpUrls() {
109        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Validatepassword';
110    }
111}
112
113/** @deprecated class alias since 1.43 */
114class_alias( ApiValidatePassword::class, 'ApiValidatePassword' );