MediaWiki  master
ApiResetPassword.php
Go to the documentation of this file.
1 <?php
28 
34 class ApiResetPassword extends ApiBase {
35 
36  private PasswordReset $passwordReset;
37 
43  public function __construct(
44  ApiMain $main,
45  $action,
46  PasswordReset $passwordReset
47  ) {
48  parent::__construct( $main, $action );
49 
50  $this->passwordReset = $passwordReset;
51  }
52 
54  private $hasAnyRoutes = null;
55 
60  private function hasAnyRoutes() {
61  if ( $this->hasAnyRoutes === null ) {
62  $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
63  $this->hasAnyRoutes = !empty( $resetRoutes['username'] ) || !empty( $resetRoutes['email'] );
64  }
65  return $this->hasAnyRoutes;
66  }
67 
68  protected function getExtendedDescription() {
69  if ( !$this->hasAnyRoutes() ) {
70  return 'apihelp-resetpassword-extended-description-noroutes';
71  }
72  return parent::getExtendedDescription();
73  }
74 
75  public function execute() {
76  if ( !$this->hasAnyRoutes() ) {
77  $this->dieWithError( 'apihelp-resetpassword-description-noroutes', 'moduledisabled' );
78  }
79 
80  $params = $this->extractRequestParams() + [
81  // Make sure the keys exist even if getAllowedParams didn't define them
82  'user' => null,
83  'email' => null,
84  ];
85 
86  $this->requireOnlyOneParameter( $params, 'user', 'email' );
87 
88  $status = $this->passwordReset->isAllowed( $this->getUser() );
89  if ( !$status->isOK() ) {
90  $this->dieStatus( Status::wrap( $status ) );
91  }
92 
93  $status = $this->passwordReset->execute(
94  $this->getUser(), $params['user'], $params['email']
95  );
96  if ( !$status->isOK() ) {
97  $status->value = null;
98  $this->dieStatus( Status::wrap( $status ) );
99  }
100 
101  $result = $this->getResult();
102  $result->addValue( [ 'resetpassword' ], 'status', 'success' );
103  }
104 
105  public function isWriteMode() {
106  return $this->hasAnyRoutes();
107  }
108 
109  public function needsToken() {
110  if ( !$this->hasAnyRoutes() ) {
111  return false;
112  }
113  return 'csrf';
114  }
115 
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 
142  protected function getExamplesMessages() {
143  $ret = [];
144  $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
145 
146  if ( !empty( $resetRoutes['username'] ) ) {
147  $ret['action=resetpassword&user=Example&token=123ABC'] = 'apihelp-resetpassword-example-user';
148  }
149  if ( !empty( $resetRoutes['email'] ) ) {
150  $ret['action=resetpassword&user=user@example.com&token=123ABC'] =
151  'apihelp-resetpassword-example-email';
152  }
153 
154  return $ret;
155  }
156 
157  public function getHelpUrls() {
158  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data';
159  }
160 }
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:63
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition: ApiBase.php:1516
requireOnlyOneParameter( $params,... $required)
Die if 0 or more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:947
getResult()
Get the result object.
Definition: ApiBase.php:668
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:808
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1571
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:64
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.
__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.
A class containing constants representing the names of configuration variables.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:58
Helper class for the password reset functionality shared by the web UI and the API.
Service for formatting and validating API parameters.