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() {
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
77 public function mustBePosted() {
78 return true;
79 }
80
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
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
108 public function getHelpUrls() {
109 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Validatepassword';
110 }
111}
112
114class_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:61
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1507
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:1087
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:543
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:767
getResult()
Get the result object.
Definition ApiBase.php:682
logFeatureUsage( $feature)
Write logging information for API features to a debug log, for usage analysis.
Definition ApiBase.php:1755
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:801
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:823
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:67
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 ...
__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.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.