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