ApiAMCreateAccount.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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\Auth\AuthManager;
  23. use MediaWiki\Auth\AuthenticationResponse;
  24. /**
  25. * Create an account with AuthManager
  26. *
  27. * @ingroup API
  28. */
  29. class ApiAMCreateAccount extends ApiBase {
  30. public function __construct( ApiMain $main, $action ) {
  31. parent::__construct( $main, $action, 'create' );
  32. }
  33. public function getFinalDescription() {
  34. // A bit of a hack to append 'api-help-authmanager-general-usage'
  35. $msgs = parent::getFinalDescription();
  36. $msgs[] = ApiBase::makeMessage( 'api-help-authmanager-general-usage', $this->getContext(), [
  37. $this->getModulePrefix(),
  38. $this->getModuleName(),
  39. $this->getModulePath(),
  40. AuthManager::ACTION_CREATE,
  41. self::needsToken(),
  42. ] );
  43. return $msgs;
  44. }
  45. public function execute() {
  46. $params = $this->extractRequestParams();
  47. $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
  48. if ( $params['returnurl'] !== null ) {
  49. $bits = wfParseUrl( $params['returnurl'] );
  50. if ( !$bits || $bits['scheme'] === '' ) {
  51. $encParamName = $this->encodeParamName( 'returnurl' );
  52. $this->dieWithError(
  53. [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
  54. "badurl_{$encParamName}"
  55. );
  56. }
  57. }
  58. $helper = new ApiAuthManagerHelper( $this );
  59. $manager = AuthManager::singleton();
  60. // Make sure it's possible to create accounts
  61. if ( !$manager->canCreateAccounts() ) {
  62. $this->getResult()->addValue( null, 'createaccount', $helper->formatAuthenticationResponse(
  63. AuthenticationResponse::newFail(
  64. $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_CREATE )
  65. )
  66. ) );
  67. $helper->logAuthenticationResult( 'accountcreation',
  68. 'userlogin-cannot-' . AuthManager::ACTION_CREATE );
  69. return;
  70. }
  71. // Perform the create step
  72. if ( $params['continue'] ) {
  73. $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_CREATE_CONTINUE );
  74. $res = $manager->continueAccountCreation( $reqs );
  75. } else {
  76. $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_CREATE );
  77. if ( $params['preservestate'] ) {
  78. $req = $helper->getPreservedRequest();
  79. if ( $req ) {
  80. $reqs[] = $req;
  81. }
  82. }
  83. $res = $manager->beginAccountCreation( $this->getUser(), $reqs, $params['returnurl'] );
  84. }
  85. $this->getResult()->addValue( null, 'createaccount',
  86. $helper->formatAuthenticationResponse( $res ) );
  87. $helper->logAuthenticationResult( 'accountcreation', $res );
  88. }
  89. public function isReadMode() {
  90. return false;
  91. }
  92. public function isWriteMode() {
  93. return true;
  94. }
  95. public function needsToken() {
  96. return 'createaccount';
  97. }
  98. public function getAllowedParams() {
  99. $ret = ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_CREATE,
  100. 'requests', 'messageformat', 'mergerequestfields', 'preservestate', 'returnurl', 'continue'
  101. );
  102. $ret['preservestate'][ApiBase::PARAM_HELP_MSG_APPEND][] =
  103. 'apihelp-createaccount-param-preservestate';
  104. return $ret;
  105. }
  106. public function dynamicParameterDocumentation() {
  107. return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_CREATE ];
  108. }
  109. protected function getExamplesMessages() {
  110. return [
  111. 'action=createaccount&username=Example&password=ExamplePassword&retype=ExamplePassword'
  112. . '&createreturnurl=http://example.org/&createtoken=123ABC'
  113. => 'apihelp-createaccount-example-create',
  114. ];
  115. }
  116. public function getHelpUrls() {
  117. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Account_creation';
  118. }
  119. }