MediaWiki master
ApiValidatePassword.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Api;
4
10
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() {
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
108class_alias( ApiValidatePassword::class, 'ApiValidatePassword' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
array $params
The job parameters.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:76
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1565
requirePostedParameters( $params, $prefix='prefix')
Die if any of the specified parameters were found in the query part of the URL rather than the HTTP p...
Definition ApiBase.php:1112
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:571
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:795
getResult()
Get the result object.
Definition ApiBase.php:710
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:829
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:78
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getHelpUrls()
Return links to more detailed help pages about the module.
mustBePosted()
Indicates whether this module must be called with a POST request.
__construct(ApiMain $mainModule, string $moduleName, AuthManager $authManager, UserFactory $userFactory)
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getExamplesMessages()
Returns usage examples for this module.
This serves as the entry point to the authentication system.
Type definition for user types.
Definition UserDef.php:27
Create User objects.
Service for formatting and validating API parameters.
Shared interface for rigor levels when dealing with User methods.