Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 56 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| ApiResetPassword | |
0.00% |
0 / 55 |
|
0.00% |
0 / 9 |
462 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| hasAnyRoutes | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| getExtendedDescription | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| execute | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
20 | |||
| isWriteMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| needsToken | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getAllowedParams | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
20 | |||
| getExamplesMessages | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright © 2016 Wikimedia Foundation and contributors |
| 4 | * |
| 5 | * @license GPL-2.0-or-later |
| 6 | * @file |
| 7 | */ |
| 8 | |
| 9 | namespace MediaWiki\Api; |
| 10 | |
| 11 | use MediaWiki\MainConfigNames; |
| 12 | use MediaWiki\ParamValidator\TypeDef\UserDef; |
| 13 | use MediaWiki\Status\Status; |
| 14 | use MediaWiki\User\PasswordReset; |
| 15 | use Wikimedia\ParamValidator\ParamValidator; |
| 16 | |
| 17 | /** |
| 18 | * Reset password, with AuthManager |
| 19 | * |
| 20 | * @ingroup API |
| 21 | */ |
| 22 | class ApiResetPassword extends ApiBase { |
| 23 | |
| 24 | public function __construct( |
| 25 | ApiMain $main, |
| 26 | string $action, |
| 27 | private readonly PasswordReset $passwordReset, |
| 28 | ) { |
| 29 | parent::__construct( $main, $action ); |
| 30 | } |
| 31 | |
| 32 | /** @var bool */ |
| 33 | private $hasAnyRoutes = null; |
| 34 | |
| 35 | /** |
| 36 | * Determine whether any reset routes are available. |
| 37 | * @return bool |
| 38 | */ |
| 39 | private function hasAnyRoutes() { |
| 40 | if ( $this->hasAnyRoutes === null ) { |
| 41 | $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes ); |
| 42 | $this->hasAnyRoutes = !empty( $resetRoutes['username'] ) || !empty( $resetRoutes['email'] ); |
| 43 | } |
| 44 | return $this->hasAnyRoutes; |
| 45 | } |
| 46 | |
| 47 | /** @inheritDoc */ |
| 48 | protected function getExtendedDescription() { |
| 49 | if ( !$this->hasAnyRoutes() ) { |
| 50 | return 'apihelp-resetpassword-extended-description-noroutes'; |
| 51 | } |
| 52 | return parent::getExtendedDescription(); |
| 53 | } |
| 54 | |
| 55 | /** @inheritDoc */ |
| 56 | public function execute() { |
| 57 | if ( !$this->hasAnyRoutes() ) { |
| 58 | $this->dieWithError( 'apihelp-resetpassword-description-noroutes', 'moduledisabled' ); |
| 59 | } |
| 60 | |
| 61 | $params = $this->extractRequestParams() + [ |
| 62 | // Make sure the keys exist even if getAllowedParams didn't define them |
| 63 | 'user' => null, |
| 64 | 'email' => null, |
| 65 | ]; |
| 66 | |
| 67 | $status = $this->passwordReset->isAllowed( $this->getUser() ); |
| 68 | if ( !$status->isOK() ) { |
| 69 | $this->dieStatus( Status::wrap( $status ) ); |
| 70 | } |
| 71 | |
| 72 | $status = $this->passwordReset->execute( |
| 73 | $this->getUser(), $params['user'], $params['email'] |
| 74 | ); |
| 75 | if ( !$status->isOK() ) { |
| 76 | $status->value = null; |
| 77 | $this->dieStatus( Status::wrap( $status ) ); |
| 78 | } |
| 79 | |
| 80 | $result = $this->getResult(); |
| 81 | $result->addValue( [ 'resetpassword' ], 'status', 'success' ); |
| 82 | } |
| 83 | |
| 84 | /** @inheritDoc */ |
| 85 | public function isWriteMode() { |
| 86 | return $this->hasAnyRoutes(); |
| 87 | } |
| 88 | |
| 89 | /** @inheritDoc */ |
| 90 | public function needsToken() { |
| 91 | if ( !$this->hasAnyRoutes() ) { |
| 92 | return false; |
| 93 | } |
| 94 | return 'csrf'; |
| 95 | } |
| 96 | |
| 97 | /** @inheritDoc */ |
| 98 | public function getAllowedParams() { |
| 99 | if ( !$this->hasAnyRoutes() ) { |
| 100 | return []; |
| 101 | } |
| 102 | |
| 103 | $ret = [ |
| 104 | 'user' => [ |
| 105 | ParamValidator::PARAM_TYPE => 'user', |
| 106 | UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name' ], |
| 107 | ], |
| 108 | 'email' => [ |
| 109 | ParamValidator::PARAM_TYPE => 'string', |
| 110 | ], |
| 111 | ]; |
| 112 | |
| 113 | $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes ); |
| 114 | if ( empty( $resetRoutes['username'] ) ) { |
| 115 | unset( $ret['user'] ); |
| 116 | } |
| 117 | if ( empty( $resetRoutes['email'] ) ) { |
| 118 | unset( $ret['email'] ); |
| 119 | } |
| 120 | |
| 121 | return $ret; |
| 122 | } |
| 123 | |
| 124 | /** @inheritDoc */ |
| 125 | protected function getExamplesMessages() { |
| 126 | $ret = []; |
| 127 | $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes ); |
| 128 | |
| 129 | if ( !empty( $resetRoutes['username'] ) ) { |
| 130 | $ret['action=resetpassword&user=Example&token=123ABC'] = 'apihelp-resetpassword-example-user'; |
| 131 | } |
| 132 | if ( !empty( $resetRoutes['email'] ) ) { |
| 133 | $ret['action=resetpassword&user=user@example.com&token=123ABC'] = |
| 134 | 'apihelp-resetpassword-example-email'; |
| 135 | } |
| 136 | |
| 137 | return $ret; |
| 138 | } |
| 139 | |
| 140 | /** @inheritDoc */ |
| 141 | public function getHelpUrls() { |
| 142 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data'; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** @deprecated class alias since 1.43 */ |
| 147 | class_alias( ApiResetPassword::class, 'ApiResetPassword' ); |