MediaWiki REL1_35
ApiValidatePassword.php
Go to the documentation of this file.
1<?php
2
5
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}
getUser()
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:52
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1437
const PARAM_REQUIRED
Definition ApiBase.php:102
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:750
const PARAM_TYPE
Definition ApiBase.php:78
getErrorFormatter()
Get the error formatter Stable to override.
Definition ApiBase.php:635
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:996
getResult()
Get the result object.
Definition ApiBase.php:620
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:772
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:499
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:717
getExamplesMessages()
Returns usage examples for this module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
mustBePosted()
Indicates whether this module must be called with a POST request Stable to override.
getHelpUrls()
Return links to more detailed help pages about the module.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Type definition for user types.
Definition UserDef.php:23
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:541
return true
Definition router.php:92