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