Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 52 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
ApiValidatePassword | |
0.00% |
0 / 52 |
|
0.00% |
0 / 6 |
182 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
72 | |||
mustBePosted | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getAllowedParams | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | use MediaWiki\Auth\AuthManager; |
4 | use MediaWiki\ParamValidator\TypeDef\UserDef; |
5 | use MediaWiki\User\UserFactory; |
6 | use MediaWiki\User\UserRigorOptions; |
7 | use Wikimedia\ParamValidator\ParamValidator; |
8 | |
9 | /** |
10 | * @ingroup API |
11 | */ |
12 | class ApiValidatePassword extends ApiBase { |
13 | |
14 | private AuthManager $authManager; |
15 | private UserFactory $userFactory; |
16 | |
17 | /** |
18 | * @param ApiMain $mainModule |
19 | * @param string $moduleName |
20 | * @param AuthManager $authManager |
21 | * @param UserFactory $userFactory |
22 | */ |
23 | public function __construct( |
24 | ApiMain $mainModule, |
25 | string $moduleName, |
26 | AuthManager $authManager, |
27 | UserFactory $userFactory |
28 | ) { |
29 | parent::__construct( $mainModule, $moduleName ); |
30 | $this->authManager = $authManager; |
31 | $this->userFactory = $userFactory; |
32 | } |
33 | |
34 | public function execute() { |
35 | $params = $this->extractRequestParams(); |
36 | |
37 | $this->requirePostedParameters( [ 'password' ] ); |
38 | |
39 | if ( $params['user'] !== null ) { |
40 | $user = $this->userFactory->newFromName( |
41 | $params['user'], |
42 | UserRigorOptions::RIGOR_CREATABLE |
43 | ); |
44 | if ( !$user ) { |
45 | $encParamName = $this->encodeParamName( 'user' ); |
46 | $this->dieWithError( |
47 | [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $params['user'] ) ], |
48 | "baduser_{$encParamName}" |
49 | ); |
50 | } |
51 | |
52 | if ( $user->isRegistered() || $this->authManager->userExists( $user->getName() ) ) { |
53 | $this->dieWithError( 'userexists' ); |
54 | } |
55 | |
56 | $user->setEmail( (string)$params['email'] ); |
57 | $user->setRealName( (string)$params['realname'] ); |
58 | } else { |
59 | $user = $this->getUser(); |
60 | } |
61 | |
62 | $r = []; |
63 | $validity = $user->checkPasswordValidity( $params['password'] ); |
64 | $r['validity'] = $validity->isGood() ? 'Good' : ( $validity->isOK() ? 'Change' : 'Invalid' ); |
65 | $messages = array_merge( |
66 | $this->getErrorFormatter()->arrayFromStatus( $validity, 'error' ), |
67 | $this->getErrorFormatter()->arrayFromStatus( $validity, 'warning' ) |
68 | ); |
69 | if ( $messages ) { |
70 | $r['validitymessages'] = $messages; |
71 | } |
72 | |
73 | $this->getHookRunner()->onApiValidatePassword( $this, $r ); |
74 | |
75 | $this->getResult()->addValue( null, $this->getModuleName(), $r ); |
76 | } |
77 | |
78 | public function mustBePosted() { |
79 | return true; |
80 | } |
81 | |
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 | protected function getExamplesMessages() { |
98 | return [ |
99 | 'action=validatepassword&password=foobar' |
100 | => 'apihelp-validatepassword-example-1', |
101 | 'action=validatepassword&password=querty&user=Example' |
102 | => 'apihelp-validatepassword-example-2', |
103 | ]; |
104 | } |
105 | |
106 | public function getHelpUrls() { |
107 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Validatepassword'; |
108 | } |
109 | } |