MediaWiki master
ApiResetPassword.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Api;
24
30
37
38 private PasswordReset $passwordReset;
39
40 public function __construct(
41 ApiMain $main,
42 string $action,
43 PasswordReset $passwordReset
44 ) {
45 parent::__construct( $main, $action );
46
47 $this->passwordReset = $passwordReset;
48 }
49
51 private $hasAnyRoutes = null;
52
57 private function hasAnyRoutes() {
58 if ( $this->hasAnyRoutes === null ) {
59 $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
60 $this->hasAnyRoutes = !empty( $resetRoutes['username'] ) || !empty( $resetRoutes['email'] );
61 }
62 return $this->hasAnyRoutes;
63 }
64
66 protected function getExtendedDescription() {
67 if ( !$this->hasAnyRoutes() ) {
68 return 'apihelp-resetpassword-extended-description-noroutes';
69 }
70 return parent::getExtendedDescription();
71 }
72
74 public function execute() {
75 if ( !$this->hasAnyRoutes() ) {
76 $this->dieWithError( 'apihelp-resetpassword-description-noroutes', 'moduledisabled' );
77 }
78
79 $params = $this->extractRequestParams() + [
80 // Make sure the keys exist even if getAllowedParams didn't define them
81 'user' => null,
82 'email' => null,
83 ];
84
85 $status = $this->passwordReset->isAllowed( $this->getUser() );
86 if ( !$status->isOK() ) {
87 $this->dieStatus( Status::wrap( $status ) );
88 }
89
90 $status = $this->passwordReset->execute(
91 $this->getUser(), $params['user'], $params['email']
92 );
93 if ( !$status->isOK() ) {
94 $status->value = null;
95 $this->dieStatus( Status::wrap( $status ) );
96 }
97
98 $result = $this->getResult();
99 $result->addValue( [ 'resetpassword' ], 'status', 'success' );
100 }
101
102 public function isWriteMode() {
103 return $this->hasAnyRoutes();
104 }
105
106 public function needsToken() {
107 if ( !$this->hasAnyRoutes() ) {
108 return false;
109 }
110 return 'csrf';
111 }
112
114 public function getAllowedParams() {
115 if ( !$this->hasAnyRoutes() ) {
116 return [];
117 }
118
119 $ret = [
120 'user' => [
121 ParamValidator::PARAM_TYPE => 'user',
122 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name' ],
123 ],
124 'email' => [
125 ParamValidator::PARAM_TYPE => 'string',
126 ],
127 ];
128
129 $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
130 if ( empty( $resetRoutes['username'] ) ) {
131 unset( $ret['user'] );
132 }
133 if ( empty( $resetRoutes['email'] ) ) {
134 unset( $ret['email'] );
135 }
136
137 return $ret;
138 }
139
141 protected function getExamplesMessages() {
142 $ret = [];
143 $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
144
145 if ( !empty( $resetRoutes['username'] ) ) {
146 $ret['action=resetpassword&user=Example&token=123ABC'] = 'apihelp-resetpassword-example-user';
147 }
148 if ( !empty( $resetRoutes['email'] ) ) {
149 $ret['action=resetpassword&user=user@example.com&token=123ABC'] =
150 'apihelp-resetpassword-example-email';
151 }
152
153 return $ret;
154 }
155
157 public function getHelpUrls() {
158 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data';
159 }
160}
161
163class_alias( ApiResetPassword::class, 'ApiResetPassword' );
array $params
The job parameters.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:76
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1565
getResult()
Get the result object.
Definition ApiBase.php:710
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1620
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:78
Reset password, with AuthManager.
__construct(ApiMain $main, string $action, PasswordReset $passwordReset)
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 access to the wiki.
getExtendedDescription()
Return the extended help text message.This is additional text to display at the top of the help secti...
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
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.Concrete implementatio...
needsToken()
Returns the token type this module requires in order to execute.
A class containing constants representing the names of configuration variables.
const PasswordResetRoutes
Name constant for the PasswordResetRoutes setting, for use with Config::get()
Type definition for user types.
Definition UserDef.php:27
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Password reset helper for functionality shared by the web UI and the API.
Service for formatting and validating API parameters.