MediaWiki master
ApiResetPassword.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
16
23
24 public function __construct(
25 ApiMain $main,
26 string $action,
27 private readonly PasswordReset $passwordReset,
28 ) {
29 parent::__construct( $main, $action );
30 }
31
33 private $hasAnyRoutes = null;
34
39 private function hasAnyRoutes() {
40 if ( $this->hasAnyRoutes === null ) {
41 $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
42 $this->hasAnyRoutes = !empty( $resetRoutes['username'] ) || !empty( $resetRoutes['email'] );
43 }
44 return $this->hasAnyRoutes;
45 }
46
48 protected function getExtendedDescription() {
49 if ( !$this->hasAnyRoutes() ) {
50 return 'apihelp-resetpassword-extended-description-noroutes';
51 }
52 return parent::getExtendedDescription();
53 }
54
56 public function execute() {
57 if ( !$this->hasAnyRoutes() ) {
58 $this->dieWithError( 'apihelp-resetpassword-description-noroutes', 'moduledisabled' );
59 }
60
61 $params = $this->extractRequestParams() + [
62 // Make sure the keys exist even if getAllowedParams didn't define them
63 'user' => null,
64 'email' => null,
65 ];
66
67 $status = $this->passwordReset->isAllowed( $this->getUser() );
68 if ( !$status->isOK() ) {
69 $this->dieStatus( Status::wrap( $status ) );
70 }
71
72 $status = $this->passwordReset->execute(
73 $this->getUser(), $params['user'], $params['email']
74 );
75 if ( !$status->isOK() ) {
76 $status->value = null;
77 $this->dieStatus( Status::wrap( $status ) );
78 }
79
80 $result = $this->getResult();
81 $result->addValue( [ 'resetpassword' ], 'status', 'success' );
82 }
83
85 public function isWriteMode() {
86 return $this->hasAnyRoutes();
87 }
88
90 public function needsToken() {
91 if ( !$this->hasAnyRoutes() ) {
92 return false;
93 }
94 return 'csrf';
95 }
96
98 public function getAllowedParams() {
99 if ( !$this->hasAnyRoutes() ) {
100 return [];
101 }
102
103 $ret = [
104 'user' => [
105 ParamValidator::PARAM_TYPE => 'user',
106 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name' ],
107 ],
108 'email' => [
109 ParamValidator::PARAM_TYPE => 'string',
110 ],
111 ];
112
113 $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
114 if ( empty( $resetRoutes['username'] ) ) {
115 unset( $ret['user'] );
116 }
117 if ( empty( $resetRoutes['email'] ) ) {
118 unset( $ret['email'] );
119 }
120
121 return $ret;
122 }
123
125 protected function getExamplesMessages() {
126 $ret = [];
127 $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
128
129 if ( !empty( $resetRoutes['username'] ) ) {
130 $ret['action=resetpassword&user=Example&token=123ABC'] = 'apihelp-resetpassword-example-user';
131 }
132 if ( !empty( $resetRoutes['email'] ) ) {
133 $ret['action=resetpassword&user=user@example.com&token=123ABC'] =
134 'apihelp-resetpassword-example-email';
135 }
136
137 return $ret;
138 }
139
141 public function getHelpUrls() {
142 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data';
143 }
144}
145
147class_alias( ApiResetPassword::class, 'ApiResetPassword' );
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:60
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1522
getResult()
Get the result object.
Definition ApiBase.php:696
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1573
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:66
Reset password, with AuthManager.
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.API modules must override this method...
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...
__construct(ApiMain $main, string $action, private readonly PasswordReset $passwordReset,)
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.Modules are strongly encouraged to us...
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:44
Password reset helper for functionality shared by the web UI and the API.
Service for formatting and validating API parameters.