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