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