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
23 use MediaWiki\Auth\AuthManager
;
24 use MediaWiki\MediaWikiServices
;
27 * Reset password, with AuthManager
31 class ApiResetPassword
extends ApiBase
{
33 private $hasAnyRoutes = null;
36 * Determine whether any reset routes are available.
39 private function hasAnyRoutes() {
40 if ( $this->hasAnyRoutes
=== null ) {
41 $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
42 $this->hasAnyRoutes
= !empty( $resetRoutes['username'] ) ||
!empty( $resetRoutes['email'] );
44 return $this->hasAnyRoutes
;
47 protected function getExtendedDescription() {
48 if ( !$this->hasAnyRoutes() ) {
49 return 'apihelp-resetpassword-extended-description-noroutes';
51 return parent
::getExtendedDescription();
54 public function execute() {
55 if ( !$this->hasAnyRoutes() ) {
56 $this->dieWithError( 'apihelp-resetpassword-description-noroutes', 'moduledisabled' );
59 $params = $this->extractRequestParams() +
[
60 // Make sure the keys exist even if getAllowedParams didn't define them
65 $this->requireOnlyOneParameter( $params, 'user', 'email' );
67 $passwordReset = new PasswordReset(
69 AuthManager
::singleton(),
70 MediaWikiServices
::getInstance()->getPermissionManager()
73 $status = $passwordReset->isAllowed( $this->getUser() );
74 if ( !$status->isOK() ) {
75 $this->dieStatus( Status
::wrap( $status ) );
78 $status = $passwordReset->execute(
79 $this->getUser(), $params['user'], $params['email']
81 if ( !$status->isOK() ) {
82 $status->value
= null;
83 $this->dieStatus( Status
::wrap( $status ) );
86 $result = $this->getResult();
87 $result->addValue( [ 'resetpassword' ], 'status', 'success' );
90 public function isWriteMode() {
91 return $this->hasAnyRoutes();
94 public function needsToken() {
95 if ( !$this->hasAnyRoutes() ) {
101 public function getAllowedParams() {
102 if ( !$this->hasAnyRoutes() ) {
108 ApiBase
::PARAM_TYPE
=> 'user',
111 ApiBase
::PARAM_TYPE
=> 'string',
115 $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
116 if ( empty( $resetRoutes['username'] ) ) {
117 unset( $ret['user'] );
119 if ( empty( $resetRoutes['email'] ) ) {
120 unset( $ret['email'] );
126 protected function getExamplesMessages() {
128 $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
130 if ( !empty( $resetRoutes['username'] ) ) {
131 $ret['action=resetpassword&user=Example&token=123ABC'] = 'apihelp-resetpassword-example-user';
133 if ( !empty( $resetRoutes['email'] ) ) {
134 $ret['action=resetpassword&user=user@example.com&token=123ABC'] =
135 'apihelp-resetpassword-example-email';
141 public function getHelpUrls() {
142 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data';