Debian Codegraph @

includes/api/ApiResetPassword.php

raw symbols SCIP
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
23 namespace MediaWiki\Api;
24
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;
30
31 /**
32 * Reset password, with AuthManager
33 *
34 * @ingroup API
35 */
36 class ApiResetPassword extends ApiBase {
37
38 private PasswordReset $passwordReset;
39
40 public function __construct(
41 ApiMain $main,
42 string $action,
43 PasswordReset $passwordReset
44 ) {
45 parent::__construct( $main, $action );
46
47 $this->passwordReset = $passwordReset;
48 }
49
50 /** @var bool */
51 private $hasAnyRoutes = null;
52
53 /**
54 * Determine whether any reset routes are available.
55 * @return bool
56 */
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'] );
61 }
62 return $this->hasAnyRoutes;
63 }
64
65 /** @inheritDoc */
66 protected function getExtendedDescription() {
67 if ( !$this->hasAnyRoutes() ) {
68 return 'apihelp-resetpassword-extended-description-noroutes';
69 }
70 return parent::getExtendedDescription();
71 }
72
73 /** @inheritDoc */
74 public function execute() {
75 if ( !$this->hasAnyRoutes() ) {
76 $this->dieWithError( 'apihelp-resetpassword-description-noroutes', 'moduledisabled' );
77 }
78
79 $params = $this->extractRequestParams() + [
80 // Make sure the keys exist even if getAllowedParams didn't define them
81 'user' => null,
82 'email' => null,
83 ];
84
85 $status = $this->passwordReset->isAllowed( $this->getUser() );
86 if ( !$status->isOK() ) {
87 $this->dieStatus( Status::wrap( $status ) );
88 }
89
90 $status = $this->passwordReset->execute(
91 $this->getUser(), $params['user'], $params['email']
92 );
93 if ( !$status->isOK() ) {
94 $status->value = null;
95 $this->dieStatus( Status::wrap( $status ) );
96 }
97
98 $result = $this->getResult();
99 $result->addValue( [ 'resetpassword' ], 'status', 'success' );
100 }
101
102 public function isWriteMode() {
103 return $this->hasAnyRoutes();
104 }
105
106 public function needsToken() {
107 if ( !$this->hasAnyRoutes() ) {
108 return false;
109 }
110 return 'csrf';
111 }
112
113 /** @inheritDoc */
114 public function getAllowedParams() {
115 if ( !$this->hasAnyRoutes() ) {
116 return [];
117 }
118
119 $ret = [
120 'user' => [
121 ParamValidator::PARAM_TYPE => 'user',
122 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name' ],
123 ],
124 'email' => [
125 ParamValidator::PARAM_TYPE => 'string',
126 ],
127 ];
128
129 $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
130 if ( empty( $resetRoutes['username'] ) ) {
131 unset( $ret['user'] );
132 }
133 if ( empty( $resetRoutes['email'] ) ) {
134 unset( $ret['email'] );
135 }
136
137 return $ret;
138 }
139
140 /** @inheritDoc */
141 protected function getExamplesMessages() {
142 $ret = [];
143 $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
144
145 if ( !empty( $resetRoutes['username'] ) ) {
146 $ret['action=resetpassword&user=Example&token=123ABC'] = 'apihelp-resetpassword-example-user';
147 }
148 if ( !empty( $resetRoutes['email'] ) ) {
149 $ret['action=resetpassword&[email protected]&token=123ABC'] =
150 'apihelp-resetpassword-example-email';
151 }
152
153 return $ret;
154 }
155
156 /** @inheritDoc */
157 public function getHelpUrls() {
158 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data';
159 }
160 }
161
162 /** @deprecated class alias since 1.43 */
163 class_alias( ApiResetPassword::class, 'ApiResetPassword' );