Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 63
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiAMCreateAccount
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 10
306
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getFinalDescription
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
72
 isReadMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 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 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 dynamicParameterDocumentation
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 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
23namespace MediaWiki\Api;
24
25use MediaWiki\Auth\AuthenticationResponse;
26use MediaWiki\Auth\AuthManager;
27use MediaWiki\Utils\UrlUtils;
28
29/**
30 * Create an account with AuthManager
31 *
32 * @ingroup API
33 */
34class ApiAMCreateAccount extends ApiBase {
35
36    private AuthManager $authManager;
37    private UrlUtils $urlUtils;
38
39    public function __construct(
40        ApiMain $main,
41        string $action,
42        AuthManager $authManager,
43        UrlUtils $urlUtils
44    ) {
45        parent::__construct( $main, $action, 'create' );
46        $this->authManager = $authManager;
47        $this->urlUtils = $urlUtils;
48    }
49
50    public function getFinalDescription() {
51        // A bit of a hack to append 'api-help-authmanager-general-usage'
52        $msgs = parent::getFinalDescription();
53        $msgs[] = $this->msg( 'api-help-authmanager-general-usage',
54            $this->getModulePrefix(),
55            $this->getModuleName(),
56            $this->getModulePath(),
57            AuthManager::ACTION_CREATE,
58            $this->needsToken(),
59        );
60        return $msgs;
61    }
62
63    public function execute() {
64        $params = $this->extractRequestParams();
65        $performer = $this->getUser();
66
67        $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
68
69        if ( $params['returnurl'] !== null ) {
70            $bits = $this->urlUtils->parse( $params['returnurl'] );
71            if ( !$bits || $bits['scheme'] === '' ) {
72                $encParamName = $this->encodeParamName( 'returnurl' );
73                $this->dieWithError(
74                    [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
75                    "badurl_{$encParamName}"
76                );
77            }
78        }
79
80        $helper = new ApiAuthManagerHelper( $this, $this->authManager );
81
82        // Make sure it's possible to create accounts
83        if ( !$this->authManager->canCreateAccounts() ) {
84            $res = AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_CREATE ) );
85            $this->getResult()->addValue( null, 'createaccount',
86                $helper->formatAuthenticationResponse( $res ) );
87            $helper->logAuthenticationResult( 'accountcreation', $performer, $res );
88            return;
89        }
90
91        // Perform the create step
92        if ( $params['continue'] ) {
93            $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_CREATE_CONTINUE );
94            $res = $this->authManager->continueAccountCreation( $reqs );
95        } else {
96            $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_CREATE );
97            if ( $params['preservestate'] ) {
98                $req = $helper->getPreservedRequest();
99                if ( $req ) {
100                    $reqs[] = $req;
101                }
102            }
103            $res = $this->authManager->beginAccountCreation(
104                $this->getAuthority(),
105                $reqs,
106                $params['returnurl']
107            );
108        }
109
110        $this->getResult()->addValue( null, 'createaccount',
111            $helper->formatAuthenticationResponse( $res ) );
112        $helper->logAuthenticationResult( 'accountcreation', $performer, $res );
113    }
114
115    public function isReadMode() {
116        return false;
117    }
118
119    public function isWriteMode() {
120        return true;
121    }
122
123    public function needsToken() {
124        return 'createaccount';
125    }
126
127    public function getAllowedParams() {
128        $ret = ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_CREATE,
129            'requests', 'messageformat', 'mergerequestfields', 'preservestate', 'returnurl', 'continue'
130        );
131        $ret['preservestate'][ApiBase::PARAM_HELP_MSG_APPEND][] =
132            'apihelp-createaccount-param-preservestate';
133        return $ret;
134    }
135
136    public function dynamicParameterDocumentation() {
137        return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_CREATE ];
138    }
139
140    protected function getExamplesMessages() {
141        return [
142            'action=createaccount&username=Example&password=ExamplePassword&retype=ExamplePassword'
143                . '&createreturnurl=http://example.org/&createtoken=123ABC'
144                => 'apihelp-createaccount-example-create',
145        ];
146    }
147
148    public function getHelpUrls() {
149        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Account_creation';
150    }
151}
152
153/** @deprecated class alias since 1.43 */
154class_alias( ApiAMCreateAccount::class, 'ApiAMCreateAccount' );