Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiResetPassword
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 9
462
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 hasAnyRoutes
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 getExtendedDescription
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 execute
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
 isWriteMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 needsToken
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Copyright © 2016 Wikimedia Foundation and contributors
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23use MediaWiki\MainConfigNames;
24use MediaWiki\ParamValidator\TypeDef\UserDef;
25use MediaWiki\Status\Status;
26use MediaWiki\User\PasswordReset;
27use Wikimedia\ParamValidator\ParamValidator;
28
29/**
30 * Reset password, with AuthManager
31 *
32 * @ingroup API
33 */
34class ApiResetPassword extends ApiBase {
35
36    private PasswordReset $passwordReset;
37
38    /**
39     * @param ApiMain $main
40     * @param string $action
41     * @param PasswordReset $passwordReset
42     */
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
53    /** @var bool */
54    private $hasAnyRoutes = null;
55
56    /**
57     * Determine whether any reset routes are available.
58     * @return bool
59     */
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    /** @inheritDoc */
69    protected function getExtendedDescription() {
70        if ( !$this->hasAnyRoutes() ) {
71            return 'apihelp-resetpassword-extended-description-noroutes';
72        }
73        return parent::getExtendedDescription();
74    }
75
76    /** @inheritDoc */
77    public function execute() {
78        if ( !$this->hasAnyRoutes() ) {
79            $this->dieWithError( 'apihelp-resetpassword-description-noroutes', 'moduledisabled' );
80        }
81
82        $params = $this->extractRequestParams() + [
83            // Make sure the keys exist even if getAllowedParams didn't define them
84            'user' => null,
85            'email' => null,
86        ];
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    /** @inheritDoc */
117    public function getAllowedParams() {
118        if ( !$this->hasAnyRoutes() ) {
119            return [];
120        }
121
122        $ret = [
123            'user' => [
124                ParamValidator::PARAM_TYPE => 'user',
125                UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name' ],
126            ],
127            'email' => [
128                ParamValidator::PARAM_TYPE => 'string',
129            ],
130        ];
131
132        $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
133        if ( empty( $resetRoutes['username'] ) ) {
134            unset( $ret['user'] );
135        }
136        if ( empty( $resetRoutes['email'] ) ) {
137            unset( $ret['email'] );
138        }
139
140        return $ret;
141    }
142
143    /** @inheritDoc */
144    protected function getExamplesMessages() {
145        $ret = [];
146        $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
147
148        if ( !empty( $resetRoutes['username'] ) ) {
149            $ret['action=resetpassword&user=Example&token=123ABC'] = 'apihelp-resetpassword-example-user';
150        }
151        if ( !empty( $resetRoutes['email'] ) ) {
152            $ret['action=resetpassword&user=user@example.com&token=123ABC'] =
153                'apihelp-resetpassword-example-email';
154        }
155
156        return $ret;
157    }
158
159    /** @inheritDoc */
160    public function getHelpUrls() {
161        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data';
162    }
163}