MediaWiki REL1_40
ApiResetPassword.php
Go to the documentation of this file.
1<?php
26
33
35 private $passwordReset;
36
42 public function __construct(
43 ApiMain $main,
44 $action,
45 PasswordReset $passwordReset
46 ) {
47 parent::__construct( $main, $action );
48
49 $this->passwordReset = $passwordReset;
50 }
51
53 private $hasAnyRoutes = null;
54
59 private function hasAnyRoutes() {
60 if ( $this->hasAnyRoutes === null ) {
61 $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
62 $this->hasAnyRoutes = !empty( $resetRoutes['username'] ) || !empty( $resetRoutes['email'] );
63 }
64 return $this->hasAnyRoutes;
65 }
66
68 protected function getExtendedDescription() {
69 if ( !$this->hasAnyRoutes() ) {
70 return 'apihelp-resetpassword-extended-description-noroutes';
71 }
72 return parent::getExtendedDescription();
73 }
74
76 public function execute() {
77 if ( !$this->hasAnyRoutes() ) {
78 $this->dieWithError( 'apihelp-resetpassword-description-noroutes', 'moduledisabled' );
79 }
80
81 $params = $this->extractRequestParams() + [
82 // Make sure the keys exist even if getAllowedParams didn't define them
83 'user' => null,
84 'email' => null,
85 ];
86
87 $status = $this->passwordReset->isAllowed( $this->getUser() );
88 if ( !$status->isOK() ) {
89 $this->dieStatus( Status::wrap( $status ) );
90 }
91
92 $status = $this->passwordReset->execute(
93 $this->getUser(), $params['user'], $params['email']
94 );
95 if ( !$status->isOK() ) {
96 $status->value = null;
97 $this->dieStatus( Status::wrap( $status ) );
98 }
99
100 $result = $this->getResult();
101 $result->addValue( [ 'resetpassword' ], 'status', 'success' );
102 }
103
104 public function isWriteMode() {
105 return $this->hasAnyRoutes();
106 }
107
108 public function needsToken() {
109 if ( !$this->hasAnyRoutes() ) {
110 return false;
111 }
112 return 'csrf';
113 }
114
116 public function getAllowedParams() {
117 if ( !$this->hasAnyRoutes() ) {
118 return [];
119 }
120
121 $ret = [
122 'user' => [
123 ParamValidator::PARAM_TYPE => 'user',
124 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name' ],
125 ],
126 'email' => [
127 ParamValidator::PARAM_TYPE => 'string',
128 ],
129 ];
130
131 $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
132 if ( empty( $resetRoutes['username'] ) ) {
133 unset( $ret['user'] );
134 }
135 if ( empty( $resetRoutes['email'] ) ) {
136 unset( $ret['email'] );
137 }
138
139 return $ret;
140 }
141
143 protected function getExamplesMessages() {
144 $ret = [];
145 $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
146
147 if ( !empty( $resetRoutes['username'] ) ) {
148 $ret['action=resetpassword&user=Example&token=123ABC'] = 'apihelp-resetpassword-example-user';
149 }
150 if ( !empty( $resetRoutes['email'] ) ) {
151 $ret['action=resetpassword&user=user@example.com&token=123ABC'] =
152 'apihelp-resetpassword-example-email';
153 }
154
155 return $ret;
156 }
157
159 public function getHelpUrls() {
160 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data';
161 }
162}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:59
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1460
getResult()
Get the result object.
Definition ApiBase.php:637
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:773
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1521
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:58
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.This is additional text to display at the top of the help secti...
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
isWriteMode()
Indicates whether this module requires write mode.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.Concrete implementatio...
__construct(ApiMain $main, $action, PasswordReset $passwordReset)
needsToken()
Returns the token type this module requires in order to execute.
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
A class containing constants representing the names of configuration variables.
Type definition for user types.
Definition UserDef.php:27
Helper class for the password reset functionality shared by the web UI and the API.
Service for formatting and validating API parameters.