Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 53
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 / 52
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 / 29
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->requirePostedParameters( [ 'password' ] );
34
35        if ( $params['user'] !== null ) {
36            $user = $this->userFactory->newFromName(
37                $params['user'],
38                UserRigorOptions::RIGOR_CREATABLE
39            );
40            if ( !$user ) {
41                $encParamName = $this->encodeParamName( 'user' );
42                $this->dieWithError(
43                    [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $params['user'] ) ],
44                    "baduser_{$encParamName}"
45                );
46            }
47
48            if ( $user->isRegistered() || $this->authManager->userExists( $user->getName() ) ) {
49                $this->dieWithError( 'userexists' );
50            }
51
52            $user->setEmail( (string)$params['email'] );
53            $user->setRealName( (string)$params['realname'] );
54        } else {
55            $user = $this->getUser();
56        }
57
58        $r = [];
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' )
64        );
65        if ( $messages ) {
66            $r['validitymessages'] = $messages;
67        }
68
69        $this->getHookRunner()->onApiValidatePassword( $this, $r );
70
71        $this->getResult()->addValue( null, $this->getModuleName(), $r );
72    }
73
74    public function mustBePosted() {
75        return true;
76    }
77
78    public function getAllowedParams() {
79        return [
80            'password' => [
81                ParamValidator::PARAM_TYPE => 'password',
82                ParamValidator::PARAM_REQUIRED => true
83            ],
84            'user' => [
85                ParamValidator::PARAM_TYPE => 'user',
86                UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'id' ],
87            ],
88            'email' => null,
89            'realname' => null,
90        ];
91    }
92
93    protected function getExamplesMessages() {
94        return [
95            'action=validatepassword&password=foobar'
96                => 'apihelp-validatepassword-example-1',
97            'action=validatepassword&password=querty&user=Example'
98                => 'apihelp-validatepassword-example-2',
99        ];
100    }
101
102    public function getHelpUrls() {
103        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Validatepassword';
104    }
105}
106
107/** @deprecated class alias since 1.43 */
108class_alias( ApiValidatePassword::class, 'ApiValidatePassword' );