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 }
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below...
Definition: ApiBase.php:94
getErrorFormatter()
Get the error formatter.
Definition: ApiBase.php:654
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking, formatting, etc.
getResult()
Get the result object.
Definition: ApiBase.php:640
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:118
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2005
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user...
Definition: ApiBase.php:761
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:520
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition: ApiBase.php:739
This abstract class implements many basic API functions, and is the base of all API classes...
Definition: ApiBase.php:42
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
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:515
return true
Definition: router.php:92
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200