MediaWiki REL1_35
ApiResetPassword.php
Go to the documentation of this file.
1<?php
25
32
33 private $hasAnyRoutes = null;
34
39 private function hasAnyRoutes() {
40 if ( $this->hasAnyRoutes === null ) {
41 $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
42 $this->hasAnyRoutes = !empty( $resetRoutes['username'] ) || !empty( $resetRoutes['email'] );
43 }
45 }
46
47 protected function getExtendedDescription() {
48 if ( !$this->hasAnyRoutes() ) {
49 return 'apihelp-resetpassword-extended-description-noroutes';
50 }
51 return parent::getExtendedDescription();
52 }
53
54 public function execute() {
55 if ( !$this->hasAnyRoutes() ) {
56 $this->dieWithError( 'apihelp-resetpassword-description-noroutes', 'moduledisabled' );
57 }
58
59 $params = $this->extractRequestParams() + [
60 // Make sure the keys exist even if getAllowedParams didn't define them
61 'user' => null,
62 'email' => null,
63 ];
64
65 $this->requireOnlyOneParameter( $params, 'user', 'email' );
66
67 $passwordReset = MediaWikiServices::getInstance()->getPasswordReset();
68
69 $status = $passwordReset->isAllowed( $this->getUser() );
70 if ( !$status->isOK() ) {
71 $this->dieStatus( Status::wrap( $status ) );
72 }
73
74 $status = $passwordReset->execute(
75 $this->getUser(), $params['user'], $params['email']
76 );
77 if ( !$status->isOK() ) {
78 $status->value = null;
79 $this->dieStatus( Status::wrap( $status ) );
80 }
81
82 $result = $this->getResult();
83 $result->addValue( [ 'resetpassword' ], 'status', 'success' );
84 }
85
86 public function isWriteMode() {
87 return $this->hasAnyRoutes();
88 }
89
90 public function needsToken() {
91 if ( !$this->hasAnyRoutes() ) {
92 return false;
93 }
94 return 'csrf';
95 }
96
97 public function getAllowedParams() {
98 if ( !$this->hasAnyRoutes() ) {
99 return [];
100 }
101
102 $ret = [
103 'user' => [
104 ApiBase::PARAM_TYPE => 'user',
105 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name' ],
106 ],
107 'email' => [
108 ApiBase::PARAM_TYPE => 'string',
109 ],
110 ];
111
112 $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
113 if ( empty( $resetRoutes['username'] ) ) {
114 unset( $ret['user'] );
115 }
116 if ( empty( $resetRoutes['email'] ) ) {
117 unset( $ret['email'] );
118 }
119
120 return $ret;
121 }
122
123 protected function getExamplesMessages() {
124 $ret = [];
125 $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
126
127 if ( !empty( $resetRoutes['username'] ) ) {
128 $ret['action=resetpassword&user=Example&token=123ABC'] = 'apihelp-resetpassword-example-user';
129 }
130 if ( !empty( $resetRoutes['email'] ) ) {
131 $ret['action=resetpassword&user=user@example.com&token=123ABC'] =
132 'apihelp-resetpassword-example-email';
133 }
134
135 return $ret;
136 }
137
138 public function getHelpUrls() {
139 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data';
140 }
141}
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_TYPE
Definition ApiBase.php:78
requireOnlyOneParameter( $params,... $required)
Die if none or more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:909
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
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1495
Reset password, with AuthManager.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getExtendedDescription()
Return the extended help text message.
getExamplesMessages()
Returns usage examples for this module.
isWriteMode()
Indicates whether this module requires write mode.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
needsToken()
Returns the token type this module requires in order to execute.
hasAnyRoutes()
Determine whether any reset routes are available.
getHelpUrls()
Return links to more detailed help pages about the module.
getUser()
Stable to override.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Type definition for user types.
Definition UserDef.php:23