MediaWiki  master
ApiValidatePassword.php
Go to the documentation of this file.
1 <?php
2 
4 
8 class ApiValidatePassword extends ApiBase {
9 
10  public function execute() {
11  $params = $this->extractRequestParams();
12 
13  // For sanity
14  $this->requirePostedParameters( [ 'password' ] );
15 
16  if ( $params['user'] !== null ) {
17  $user = User::newFromName( $params['user'], 'creatable' );
18  if ( !$user ) {
19  $encParamName = $this->encodeParamName( 'user' );
20  $this->dieWithError(
21  [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $params['user'] ) ],
22  "baduser_{$encParamName}"
23  );
24  }
25 
26  if ( !$user->isAnon() || AuthManager::singleton()->userExists( $user->getName() ) ) {
27  $this->dieWithError( 'userexists' );
28  }
29 
30  $user->setEmail( (string)$params['email'] );
31  $user->setRealName( (string)$params['realname'] );
32  } else {
33  $user = $this->getUser();
34  }
35 
36  $r = [];
37  $validity = $user->checkPasswordValidity( $params['password'] );
38  $r['validity'] = $validity->isGood() ? 'Good' : ( $validity->isOK() ? 'Change' : 'Invalid' );
39  $messages = array_merge(
40  $this->getErrorFormatter()->arrayFromStatus( $validity, 'error' ),
41  $this->getErrorFormatter()->arrayFromStatus( $validity, 'warning' )
42  );
43  if ( $messages ) {
44  $r['validitymessages'] = $messages;
45  }
46 
47  Hooks::run( 'ApiValidatePassword', [ $this, &$r ] );
48 
49  $this->getResult()->addValue( null, $this->getModuleName(), $r );
50  }
51 
52  public function mustBePosted() {
53  return true;
54  }
55 
56  public function getAllowedParams() {
57  return [
58  'password' => [
59  ApiBase::PARAM_TYPE => 'password',
61  ],
62  'user' => [
63  ApiBase::PARAM_TYPE => 'user',
64  ],
65  'email' => null,
66  'realname' => null,
67  ];
68  }
69 
70  protected function getExamplesMessages() {
71  return [
72  'action=validatepassword&password=foobar'
73  => 'apihelp-validatepassword-example-1',
74  'action=validatepassword&password=querty&user=Example'
75  => 'apihelp-validatepassword-example-2',
76  ];
77  }
78 
79  public function getHelpUrls() {
80  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Validatepassword';
81  }
82 }
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:118
ApiValidatePassword\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiValidatePassword.php:52
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2006
true
return true
Definition: router.php:92
ApiValidatePassword\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiValidatePassword.php:79
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:94
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:640
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:536
ContextSource\getUser
getUser()
Definition: ContextSource.php:120
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:42
ApiValidatePassword
Definition: ApiValidatePassword.php:8
ApiValidatePassword\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiValidatePassword.php:56
ApiValidatePassword\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiValidatePassword.php:70
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:761
ApiValidatePassword\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiValidatePassword.php:10
ApiBase\encodeParamName
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition: ApiBase.php:739
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1550
MediaWiki\Auth\AuthManager
This serves as the entry point to the authentication system.
Definition: AuthManager.php:85
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:520
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
ApiBase\getErrorFormatter
getErrorFormatter()
Get the error formatter.
Definition: ApiBase.php:654
ApiBase\requirePostedParameters
requirePostedParameters( $params, $prefix='prefix')
Die if any of the specified parameters were found in the query part of the URL rather than the post b...
Definition: ApiBase.php:980