ApiResetPassword.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. use MediaWiki\MediaWikiServices;
  23. /**
  24. * Reset password, with AuthManager
  25. *
  26. * @ingroup API
  27. */
  28. class ApiResetPassword extends ApiBase {
  29. private $hasAnyRoutes = null;
  30. /**
  31. * Determine whether any reset routes are available.
  32. * @return bool
  33. */
  34. private function hasAnyRoutes() {
  35. if ( $this->hasAnyRoutes === null ) {
  36. $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
  37. $this->hasAnyRoutes = !empty( $resetRoutes['username'] ) || !empty( $resetRoutes['email'] );
  38. }
  39. return $this->hasAnyRoutes;
  40. }
  41. protected function getExtendedDescription() {
  42. if ( !$this->hasAnyRoutes() ) {
  43. return 'apihelp-resetpassword-extended-description-noroutes';
  44. }
  45. return parent::getExtendedDescription();
  46. }
  47. public function execute() {
  48. if ( !$this->hasAnyRoutes() ) {
  49. $this->dieWithError( 'apihelp-resetpassword-description-noroutes', 'moduledisabled' );
  50. }
  51. $params = $this->extractRequestParams() + [
  52. // Make sure the keys exist even if getAllowedParams didn't define them
  53. 'user' => null,
  54. 'email' => null,
  55. ];
  56. $this->requireOnlyOneParameter( $params, 'user', 'email' );
  57. $passwordReset = MediaWikiServices::getInstance()->getPasswordReset();
  58. $status = $passwordReset->isAllowed( $this->getUser() );
  59. if ( !$status->isOK() ) {
  60. $this->dieStatus( Status::wrap( $status ) );
  61. }
  62. $status = $passwordReset->execute(
  63. $this->getUser(), $params['user'], $params['email']
  64. );
  65. if ( !$status->isOK() ) {
  66. $status->value = null;
  67. $this->dieStatus( Status::wrap( $status ) );
  68. }
  69. $result = $this->getResult();
  70. $result->addValue( [ 'resetpassword' ], 'status', 'success' );
  71. }
  72. public function isWriteMode() {
  73. return $this->hasAnyRoutes();
  74. }
  75. public function needsToken() {
  76. if ( !$this->hasAnyRoutes() ) {
  77. return false;
  78. }
  79. return 'csrf';
  80. }
  81. public function getAllowedParams() {
  82. if ( !$this->hasAnyRoutes() ) {
  83. return [];
  84. }
  85. $ret = [
  86. 'user' => [
  87. ApiBase::PARAM_TYPE => 'user',
  88. ],
  89. 'email' => [
  90. ApiBase::PARAM_TYPE => 'string',
  91. ],
  92. ];
  93. $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
  94. if ( empty( $resetRoutes['username'] ) ) {
  95. unset( $ret['user'] );
  96. }
  97. if ( empty( $resetRoutes['email'] ) ) {
  98. unset( $ret['email'] );
  99. }
  100. return $ret;
  101. }
  102. protected function getExamplesMessages() {
  103. $ret = [];
  104. $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
  105. if ( !empty( $resetRoutes['username'] ) ) {
  106. $ret['action=resetpassword&user=Example&token=123ABC'] = 'apihelp-resetpassword-example-user';
  107. }
  108. if ( !empty( $resetRoutes['email'] ) ) {
  109. $ret['action=resetpassword&user=user@example.com&token=123ABC'] =
  110. 'apihelp-resetpassword-example-email';
  111. }
  112. return $ret;
  113. }
  114. public function getHelpUrls() {
  115. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data';
  116. }
  117. }