MediaWiki master
ApiValidatePassword.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Api;
4
10
15
16 public function __construct(
17 ApiMain $mainModule,
18 string $moduleName,
19 private readonly AuthManager $authManager,
20 private readonly UserFactory $userFactory,
21 ) {
22 parent::__construct( $mainModule, $moduleName );
23 }
24
25 public function execute() {
26 $params = $this->extractRequestParams();
27
28 $this->logFeatureUsage( 'action=validatepassword' );
29
30 $this->requirePostedParameters( [ 'password' ] );
31
32 if ( $params['user'] !== null ) {
33 $user = $this->userFactory->newFromName(
34 $params['user'],
35 UserRigorOptions::RIGOR_CREATABLE
36 );
37 if ( !$user ) {
38 $encParamName = $this->encodeParamName( 'user' );
39 $this->dieWithError(
40 [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $params['user'] ) ],
41 "baduser_{$encParamName}"
42 );
43 }
44
45 if ( $user->isRegistered() || $this->authManager->userExists( $user->getName() ) ) {
46 $this->dieWithError( 'userexists' );
47 }
48
49 $user->setEmail( (string)$params['email'] );
50 $user->setRealName( (string)$params['realname'] );
51 } else {
52 $user = $this->getUser();
53 }
54
55 $r = [];
56 $validity = $user->checkPasswordValidity( $params['password'] );
57 $r['validity'] = $validity->isGood() ? 'Good' : ( $validity->isOK() ? 'Change' : 'Invalid' );
58 $messages = array_merge(
59 $this->getErrorFormatter()->arrayFromStatus( $validity, 'error' ),
60 $this->getErrorFormatter()->arrayFromStatus( $validity, 'warning' )
61 );
62 if ( $messages ) {
63 $r['validitymessages'] = $messages;
64 }
65
66 $this->getHookRunner()->onApiValidatePassword( $this, $r );
67
68 $this->getResult()->addValue( null, $this->getModuleName(), $r );
69 }
70
72 public function mustBePosted() {
73 return true;
74 }
75
77 public function getAllowedParams() {
78 return [
79 'password' => [
80 ParamValidator::PARAM_TYPE => 'password',
81 ParamValidator::PARAM_REQUIRED => true
82 ],
83 'user' => [
84 ParamValidator::PARAM_TYPE => 'user',
85 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'id' ],
86 ],
87 'email' => null,
88 'realname' => null,
89 ];
90 }
91
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
103 public function getHelpUrls() {
104 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Validatepassword';
105 }
106}
107
109class_alias( ApiValidatePassword::class, 'ApiValidatePassword' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:60
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1522
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:1101
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:557
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:781
getResult()
Get the result object.
Definition ApiBase.php:696
logFeatureUsage( $feature)
Write logging information for API features to a debug log, for usage analysis.
Definition ApiBase.php:1770
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:815
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:66
__construct(ApiMain $mainModule, string $moduleName, private readonly AuthManager $authManager, private readonly UserFactory $userFactory,)
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
mustBePosted()
Indicates whether this module must be called with a POST request.Implementations of this method must ...
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
AuthManager is the authentication system in MediaWiki and serves entry point for authentication.
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.