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() ||
28  MediaWikiServices::getInstance()->getAuthManager()->userExists( $user->getName() )
29  ) {
30  $this->dieWithError( 'userexists' );
31  }
32 
33  $user->setEmail( (string)$params['email'] );
34  $user->setRealName( (string)$params['realname'] );
35  } else {
36  $user = $this->getUser();
37  }
38 
39  $r = [];
40  $validity = $user->checkPasswordValidity( $params['password'] );
41  $r['validity'] = $validity->isGood() ? 'Good' : ( $validity->isOK() ? 'Change' : 'Invalid' );
42  $messages = array_merge(
43  $this->getErrorFormatter()->arrayFromStatus( $validity, 'error' ),
44  $this->getErrorFormatter()->arrayFromStatus( $validity, 'warning' )
45  );
46  if ( $messages ) {
47  $r['validitymessages'] = $messages;
48  }
49 
50  $this->getHookRunner()->onApiValidatePassword( $this, $r );
51 
52  $this->getResult()->addValue( null, $this->getModuleName(), $r );
53  }
54 
55  public function mustBePosted() {
56  return true;
57  }
58 
59  public function getAllowedParams() {
60  return [
61  'password' => [
62  ApiBase::PARAM_TYPE => 'password',
64  ],
65  'user' => [
66  ApiBase::PARAM_TYPE => 'user',
67  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'id' ],
68  ],
69  'email' => null,
70  'realname' => null,
71  ];
72  }
73 
74  protected function getExamplesMessages() {
75  return [
76  'action=validatepassword&password=foobar'
77  => 'apihelp-validatepassword-example-1',
78  'action=validatepassword&password=querty&user=Example'
79  => 'apihelp-validatepassword-example-2',
80  ];
81  }
82 
83  public function getHelpUrls() {
84  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Validatepassword';
85  }
86 }
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:77
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
ApiValidatePassword\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request Stable to override.
Definition: ApiValidatePassword.php:55
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1382
true
return true
Definition: router.php:90
ApiValidatePassword\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiValidatePassword.php:83
ApiBase\PARAM_TYPE
const PARAM_TYPE
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:71
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:565
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:538
ContextSource\getUser
getUser()
Stable to override.
Definition: ContextSource.php:131
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:52
Wikimedia\ParamValidator\ParamValidator::TypeDef\UserDef
Type definition for user types.
Definition: UserDef.php:25
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:59
ApiValidatePassword\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiValidatePassword.php:74
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:717
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:695
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1487
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:444
ApiBase\getHookRunner
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition: ApiBase.php:662
ApiBase\getErrorFormatter
getErrorFormatter()
Get the error formatter Stable to override.
Definition: ApiBase.php:580
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:941