Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
52.38% covered (warning)
52.38%
33 / 63
53.85% covered (warning)
53.85%
7 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialCreateAccount
53.23% covered (warning)
53.23%
33 / 62
53.85% covered (warning)
53.85%
7 / 13
77.13
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 doesWrites
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 checkPermissions
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
0.00%
0 / 1
4.00
 getLoginSecurityLevel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDefaultAction
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDescription
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isSignup
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 successfulAction
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
72
 getToken
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 clearToken
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTokenName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 logAuthResult
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\Specials;
8
9use MediaWiki\Auth\AuthManager;
10use MediaWiki\Context\RequestContext;
11use MediaWiki\Exception\ErrorPageError;
12use MediaWiki\Language\FormatterFactory;
13use MediaWiki\Logger\LoggerFactory;
14use MediaWiki\SpecialPage\LoginSignupSpecialPage;
15use MediaWiki\Title\Title;
16use MediaWiki\User\UserIdentity;
17use MediaWiki\User\UserIdentityUtils;
18use StatusValue;
19
20/**
21 * Implements Special:CreateAccount
22 *
23 * @ingroup SpecialPage
24 * @ingroup Auth
25 */
26class SpecialCreateAccount extends LoginSignupSpecialPage {
27    /** @inheritDoc */
28    protected static $allowedActions = [
29        AuthManager::ACTION_CREATE,
30        AuthManager::ACTION_CREATE_CONTINUE
31    ];
32
33    /** @inheritDoc */
34    protected static $messages = [
35        'authform-newtoken' => 'nocookiesfornew',
36        'authform-notoken' => 'sessionfailure',
37        'authform-wrongtoken' => 'sessionfailure',
38    ];
39
40    private FormatterFactory $formatterFactory;
41
42    private UserIdentityUtils $identityUtils;
43
44    public function __construct(
45        AuthManager $authManager,
46        FormatterFactory $formatterFactory,
47        UserIdentityUtils $identityUtils
48    ) {
49        parent::__construct( 'CreateAccount', 'createaccount' );
50
51        $this->setAuthManager( $authManager );
52        $this->formatterFactory = $formatterFactory;
53        $this->identityUtils = $identityUtils;
54    }
55
56    /** @inheritDoc */
57    public function doesWrites() {
58        return true;
59    }
60
61    public function checkPermissions() {
62        parent::checkPermissions();
63
64        $performer = $this->getAuthority();
65        $authManager = $this->getAuthManager();
66
67        $status = $this->mPosted ?
68            $authManager->authorizeCreateAccount( $performer ) :
69            $authManager->probablyCanCreateAccount( $performer );
70
71        if ( !$status->isGood() ) {
72            $formatter = $this->formatterFactory->getStatusFormatter( $this->getContext() );
73            $messages = [];
74            foreach ( $status->getMessages() as $message ) {
75                $messages[] = $message->getKey();
76            }
77            $this->logAuthResult(
78                false, $performer->getUser(),
79                implode( '|', $messages )
80            );
81            throw new ErrorPageError(
82                'createacct-error',
83                $formatter->getMessage( $status )
84            );
85        }
86    }
87
88    /** @inheritDoc */
89    protected function getLoginSecurityLevel() {
90        return false;
91    }
92
93    /** @inheritDoc */
94    protected function getDefaultAction( $subPage ) {
95        return AuthManager::ACTION_CREATE;
96    }
97
98    /** @inheritDoc */
99    public function getDescription() {
100        return $this->msg( 'createaccount' );
101    }
102
103    /** @inheritDoc */
104    protected function isSignup() {
105        return true;
106    }
107
108    /**
109     * Run any hooks registered for logins, then display a message welcoming
110     * the user.
111     * @param bool $direct True if the action was successful just now; false if that happened
112     *    pre-redirection (so this handler was called already)
113     * @param StatusValue|null $extraMessages
114     */
115    protected function successfulAction( $direct = false, $extraMessages = null ) {
116        $session = $this->getRequest()->getSession();
117        $user = $this->targetUser ?: $this->getUser();
118
119        $injected_html = '';
120        if ( $direct ) {
121            # Only save preferences if the user is not creating an account for someone else.
122            if ( $this->proxyAccountCreation ) {
123                $byEmail = false; // FIXME no way to set this
124
125                $out = $this->getOutput();
126                // @phan-suppress-next-line PhanImpossibleCondition
127                $out->setPageTitleMsg( $this->msg( $byEmail ? 'accmailtitle' : 'accountcreated' ) );
128                // @phan-suppress-next-line PhanImpossibleCondition
129                if ( $byEmail ) {
130                    $out->addWikiMsg( 'accmailtext', $user->getName(), $user->getEmail() );
131                } else {
132                    $out->addWikiMsg( 'accountcreatedtext', $user->getName() );
133                }
134
135                $rt = Title::newFromText( $this->mReturnTo );
136                $out->addReturnTo(
137                    ( $rt && !$rt->isExternal() ) ? $rt : $this->getPageTitle(),
138                    wfCgiToArray( $this->mReturnToQuery )
139                );
140                return;
141            }
142            $this->getHookRunner()->onUserLoginComplete( $user, $injected_html, $direct );
143        }
144
145        $this->clearToken();
146
147        # Run any hooks; display injected HTML
148        $welcome_creation_msg = 'welcomecreation-msg';
149        /**
150         * Let any extensions change what message is shown.
151         * @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforeWelcomeCreation
152         * @since 1.18
153         */
154        $this->getHookRunner()->onBeforeWelcomeCreation( $welcome_creation_msg, $injected_html );
155
156        $this->showSuccessPage( 'signup',
157            // T308471: ensure username is plaintext (aka escaped)
158            $this->msg( 'welcomeuser' )->plaintextParams( $this->getUser()->getName() ),
159            $welcome_creation_msg, $injected_html, $extraMessages );
160    }
161
162    /** @inheritDoc */
163    protected function getToken() {
164        return $this->getRequest()->getSession()->getToken( '', 'createaccount' );
165    }
166
167    protected function clearToken() {
168        $this->getRequest()->getSession()->resetToken( 'createaccount' );
169    }
170
171    /** @inheritDoc */
172    protected function getTokenName() {
173        return 'wpCreateaccountToken';
174    }
175
176    /** @inheritDoc */
177    protected function getGroupName() {
178        return 'users';
179    }
180
181    /** @inheritDoc */
182    protected function logAuthResult( $success, UserIdentity $performer, $status = null ) {
183        LoggerFactory::getInstance( 'authevents' )->info( 'Account creation attempt', [
184            'event' => 'accountcreation',
185            'successful' => $success,
186            'accountType' => $this->identityUtils->getShortUserTypeInternal( $performer ),
187            'status' => strval( $status )
188        ] + RequestContext::getMain()->getRequest()->getSecurityLogContext( $performer ) );
189    }
190}
191
192/** @deprecated class alias since 1.41 */
193class_alias( SpecialCreateAccount::class, 'SpecialCreateAccount' );