MediaWiki  master
ApiValidatePassword.php
Go to the documentation of this file.
1 <?php
2 
6 
11 
13  private $authManager;
14 
16  private $userFactory;
17 
24  public function __construct(
25  ApiMain $mainModule,
26  string $moduleName,
29  ) {
30  parent::__construct( $mainModule, $moduleName );
31  $this->authManager = $authManager;
32  $this->userFactory = $userFactory;
33  }
34 
35  public function execute() {
36  $params = $this->extractRequestParams();
37 
38  $this->requirePostedParameters( [ 'password' ] );
39 
40  if ( $params['user'] !== null ) {
41  $user = $this->userFactory->newFromName(
42  $params['user'],
43  UserFactory::RIGOR_CREATABLE
44  );
45  if ( !$user ) {
46  $encParamName = $this->encodeParamName( 'user' );
47  $this->dieWithError(
48  [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $params['user'] ) ],
49  "baduser_{$encParamName}"
50  );
51  }
52 
53  if ( $user->isRegistered() || $this->authManager->userExists( $user->getName() ) ) {
54  $this->dieWithError( 'userexists' );
55  }
56 
57  $user->setEmail( (string)$params['email'] );
58  $user->setRealName( (string)$params['realname'] );
59  } else {
60  $user = $this->getUser();
61  }
62 
63  $r = [];
64  $validity = $user->checkPasswordValidity( $params['password'] );
65  $r['validity'] = $validity->isGood() ? 'Good' : ( $validity->isOK() ? 'Change' : 'Invalid' );
66  $messages = array_merge(
67  $this->getErrorFormatter()->arrayFromStatus( $validity, 'error' ),
68  $this->getErrorFormatter()->arrayFromStatus( $validity, 'warning' )
69  );
70  if ( $messages ) {
71  $r['validitymessages'] = $messages;
72  }
73 
74  $this->getHookRunner()->onApiValidatePassword( $this, $r );
75 
76  $this->getResult()->addValue( null, $this->getModuleName(), $r );
77  }
78 
79  public function mustBePosted() {
80  return true;
81  }
82 
83  public function getAllowedParams() {
84  return [
85  'password' => [
86  ApiBase::PARAM_TYPE => 'password',
88  ],
89  'user' => [
90  ApiBase::PARAM_TYPE => 'user',
91  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'id' ],
92  ],
93  'email' => null,
94  'realname' => null,
95  ];
96  }
97 
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 
107  public function getHelpUrls() {
108  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Validatepassword';
109  }
110 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:49
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
Definition: ApiBase.php:105
ApiValidatePassword\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiValidatePassword.php:79
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1436
true
return true
Definition: router.php:90
ApiValidatePassword\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiValidatePassword.php:107
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:81
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:628
ApiValidatePassword\$userFactory
UserFactory $userFactory
Definition: ApiValidatePassword.php:16
ContextSource\getUser
getUser()
Definition: ContextSource.php:136
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:55
Wikimedia\ParamValidator\ParamValidator::TypeDef\UserDef
Type definition for user types.
Definition: UserDef.php:26
ApiValidatePassword
Definition: ApiValidatePassword.php:10
ApiValidatePassword\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiValidatePassword.php:83
ApiValidatePassword\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiValidatePassword.php:98
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:764
ApiValidatePassword\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiValidatePassword.php:35
ApiBase\encodeParamName
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition: ApiBase.php:742
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1456
MediaWiki\Auth\AuthManager
This serves as the entry point to the authentication system.
Definition: AuthManager.php:102
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:497
ApiValidatePassword\__construct
__construct(ApiMain $mainModule, string $moduleName, AuthManager $authManager, UserFactory $userFactory)
Definition: ApiValidatePassword.php:24
ApiBase\getHookRunner
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition: ApiBase.php:710
ApiValidatePassword\$authManager
AuthManager $authManager
Definition: ApiValidatePassword.php:13
MediaWiki\User\UserFactory
Creates User objects.
Definition: UserFactory.php:41
ApiBase\getErrorFormatter
getErrorFormatter()
Definition: ApiBase.php:639
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:988