Merge "Add .pipeline/ with dev image variant"
[lhc/web/wiklou.git] / includes / api / ApiValidatePassword.php
1 <?php
2
3 use MediaWiki\Auth\AuthManager;
4
5 /**
6 * @ingroup API
7 */
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',
60 ApiBase::PARAM_REQUIRED => true
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 }