Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiResetPassword.php
blob96cd3e76a1ba3e0361c6cef1de54b900f0171d47
1 <?php
2 /**
3 * Copyright © 2016 Wikimedia Foundation and contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 namespace MediaWiki\Api;
25 use MediaWiki\MainConfigNames;
26 use MediaWiki\ParamValidator\TypeDef\UserDef;
27 use MediaWiki\Status\Status;
28 use MediaWiki\User\PasswordReset;
29 use Wikimedia\ParamValidator\ParamValidator;
31 /**
32 * Reset password, with AuthManager
34 * @ingroup API
36 class ApiResetPassword extends ApiBase {
38 private PasswordReset $passwordReset;
40 public function __construct(
41 ApiMain $main,
42 string $action,
43 PasswordReset $passwordReset
44 ) {
45 parent::__construct( $main, $action );
47 $this->passwordReset = $passwordReset;
50 /** @var bool */
51 private $hasAnyRoutes = null;
53 /**
54 * Determine whether any reset routes are available.
55 * @return bool
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'] );
62 return $this->hasAnyRoutes;
65 /** @inheritDoc */
66 protected function getExtendedDescription() {
67 if ( !$this->hasAnyRoutes() ) {
68 return 'apihelp-resetpassword-extended-description-noroutes';
70 return parent::getExtendedDescription();
73 /** @inheritDoc */
74 public function execute() {
75 if ( !$this->hasAnyRoutes() ) {
76 $this->dieWithError( 'apihelp-resetpassword-description-noroutes', 'moduledisabled' );
79 $params = $this->extractRequestParams() + [
80 // Make sure the keys exist even if getAllowedParams didn't define them
81 'user' => null,
82 'email' => null,
85 $status = $this->passwordReset->isAllowed( $this->getUser() );
86 if ( !$status->isOK() ) {
87 $this->dieStatus( Status::wrap( $status ) );
90 $status = $this->passwordReset->execute(
91 $this->getUser(), $params['user'], $params['email']
93 if ( !$status->isOK() ) {
94 $status->value = null;
95 $this->dieStatus( Status::wrap( $status ) );
98 $result = $this->getResult();
99 $result->addValue( [ 'resetpassword' ], 'status', 'success' );
102 public function isWriteMode() {
103 return $this->hasAnyRoutes();
106 public function needsToken() {
107 if ( !$this->hasAnyRoutes() ) {
108 return false;
110 return 'csrf';
113 /** @inheritDoc */
114 public function getAllowedParams() {
115 if ( !$this->hasAnyRoutes() ) {
116 return [];
119 $ret = [
120 'user' => [
121 ParamValidator::PARAM_TYPE => 'user',
122 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name' ],
124 'email' => [
125 ParamValidator::PARAM_TYPE => 'string',
129 $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
130 if ( empty( $resetRoutes['username'] ) ) {
131 unset( $ret['user'] );
133 if ( empty( $resetRoutes['email'] ) ) {
134 unset( $ret['email'] );
137 return $ret;
140 /** @inheritDoc */
141 protected function getExamplesMessages() {
142 $ret = [];
143 $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
145 if ( !empty( $resetRoutes['username'] ) ) {
146 $ret['action=resetpassword&user=Example&token=123ABC'] = 'apihelp-resetpassword-example-user';
148 if ( !empty( $resetRoutes['email'] ) ) {
149 $ret['action=resetpassword&user=user@example.com&token=123ABC'] =
150 'apihelp-resetpassword-example-email';
153 return $ret;
156 /** @inheritDoc */
157 public function getHelpUrls() {
158 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data';
162 /** @deprecated class alias since 1.43 */
163 class_alias( ApiResetPassword::class, 'ApiResetPassword' );