MediaWiki  master
ApiValidatePassword.php
Go to the documentation of this file.
1 <?php
2 
5 
9 class ApiValidatePassword extends ApiBase {
10 
11  public function execute() {
12  $params = $this->extractRequestParams();
13 
14  // For sanity
15  $this->requirePostedParameters( [ 'password' ] );
16 
17  if ( $params['user'] !== null ) {
18  $user = User::newFromName( $params['user'], 'creatable' );
19  if ( !$user ) {
20  $encParamName = $this->encodeParamName( 'user' );
21  $this->dieWithError(
22  [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $params['user'] ) ],
23  "baduser_{$encParamName}"
24  );
25  }
26 
27  if ( !$user->isAnon() || AuthManager::singleton()->userExists( $user->getName() ) ) {
28  $this->dieWithError( 'userexists' );
29  }
30 
31  $user->setEmail( (string)$params['email'] );
32  $user->setRealName( (string)$params['realname'] );
33  } else {
34  $user = $this->getUser();
35  }
36 
37  $r = [];
38  $validity = $user->checkPasswordValidity( $params['password'] );
39  $r['validity'] = $validity->isGood() ? 'Good' : ( $validity->isOK() ? 'Change' : 'Invalid' );
40  $messages = array_merge(
41  $this->getErrorFormatter()->arrayFromStatus( $validity, 'error' ),
42  $this->getErrorFormatter()->arrayFromStatus( $validity, 'warning' )
43  );
44  if ( $messages ) {
45  $r['validitymessages'] = $messages;
46  }
47 
48  $this->getHookRunner()->onApiValidatePassword( $this, $r );
49 
50  $this->getResult()->addValue( null, $this->getModuleName(), $r );
51  }
52 
53  public function mustBePosted() {
54  return true;
55  }
56 
57  public function getAllowedParams() {
58  return [
59  'password' => [
60  ApiBase::PARAM_TYPE => 'password',
62  ],
63  'user' => [
64  ApiBase::PARAM_TYPE => 'user',
65  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'id' ],
66  ],
67  'email' => null,
68  'realname' => null,
69  ];
70  }
71 
72  protected function getExamplesMessages() {
73  return [
74  'action=validatepassword&password=foobar'
75  => 'apihelp-validatepassword-example-1',
76  'action=validatepassword&password=querty&user=Example'
77  => 'apihelp-validatepassword-example-2',
78  ];
79  }
80 
81  public function getHelpUrls() {
82  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Validatepassword';
83  }
84 }
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:74
ApiValidatePassword\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiValidatePassword.php:53
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1415
true
return true
Definition: router.php:90
ApiValidatePassword\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiValidatePassword.php:81
ApiBase\PARAM_TYPE
const PARAM_TYPE
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:68
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:546
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:534
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:50
Wikimedia\ParamValidator\ParamValidator::TypeDef\UserDef
Type definition for user types.
Definition: UserDef.php:23
ApiValidatePassword
Definition: ApiValidatePassword.php:9
ApiValidatePassword\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiValidatePassword.php:57
ApiValidatePassword\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiValidatePassword.php:72
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:695
ApiValidatePassword\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiValidatePassword.php:11
ApiBase\encodeParamName
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition: ApiBase.php:673
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1485
MediaWiki\Auth\AuthManager
This serves as the entry point to the authentication system.
Definition: AuthManager.php:88
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:426
ApiBase\getHookRunner
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition: ApiBase.php:641
ApiBase\getErrorFormatter
getErrorFormatter()
Get the error formatter.
Definition: ApiBase.php:560
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:919