Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
32.20% covered (danger)
32.20%
19 / 59
46.15% covered (danger)
46.15%
6 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialCreateAccount
32.76% covered (danger)
32.76%
19 / 58
46.15% covered (danger)
46.15%
6 / 13
183.83
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
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
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
3.01
 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 / 29
0.00% covered (danger)
0.00%
0 / 1
90
 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
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Specials;
22
23use ErrorPageError;
24use MediaWiki\Auth\AuthManager;
25use MediaWiki\Language\FormatterFactory;
26use MediaWiki\Logger\LoggerFactory;
27use MediaWiki\SpecialPage\LoginSignupSpecialPage;
28use MediaWiki\Title\Title;
29use StatusValue;
30
31/**
32 * Implements Special:CreateAccount
33 *
34 * @ingroup SpecialPage
35 * @ingroup Auth
36 */
37class SpecialCreateAccount extends LoginSignupSpecialPage {
38    protected static $allowedActions = [
39        AuthManager::ACTION_CREATE,
40        AuthManager::ACTION_CREATE_CONTINUE
41    ];
42
43    protected static $messages = [
44        'authform-newtoken' => 'nocookiesfornew',
45        'authform-notoken' => 'sessionfailure',
46        'authform-wrongtoken' => 'sessionfailure',
47    ];
48
49    private FormatterFactory $formatterFactory;
50
51    /**
52     * @param AuthManager $authManager
53     * @param FormatterFactory $formatterFactory
54     */
55    public function __construct( AuthManager $authManager, FormatterFactory $formatterFactory ) {
56        parent::__construct( 'CreateAccount', 'createaccount' );
57
58        $this->setAuthManager( $authManager );
59        $this->formatterFactory = $formatterFactory;
60    }
61
62    public function doesWrites() {
63        return true;
64    }
65
66    public function checkPermissions() {
67        parent::checkPermissions();
68
69        $performer = $this->getAuthority();
70        $authManager = $this->getAuthManager();
71
72        $status = $this->mPosted ?
73            $authManager->authorizeCreateAccount( $performer ) :
74            $authManager->probablyCanCreateAccount( $performer );
75
76        if ( !$status->isGood() ) {
77            $formatter = $this->formatterFactory->getStatusFormatter( $this->getContext() );
78            throw new ErrorPageError(
79                'createacct-error',
80                $formatter->getMessage( $status )
81            );
82        }
83    }
84
85    protected function getLoginSecurityLevel() {
86        return false;
87    }
88
89    protected function getDefaultAction( $subPage ) {
90        return AuthManager::ACTION_CREATE;
91    }
92
93    public function getDescription() {
94        return $this->msg( 'createaccount' );
95    }
96
97    protected function isSignup() {
98        return true;
99    }
100
101    /**
102     * Run any hooks registered for logins, then display a message welcoming
103     * the user.
104     * @param bool $direct True if the action was successful just now; false if that happened
105     *    pre-redirection (so this handler was called already)
106     * @param StatusValue|null $extraMessages
107     */
108    protected function successfulAction( $direct = false, $extraMessages = null ) {
109        $session = $this->getRequest()->getSession();
110        $user = $this->targetUser ?: $this->getUser();
111
112        $injected_html = '';
113        if ( $direct ) {
114            # Only save preferences if the user is not creating an account for someone else.
115            if ( !$this->proxyAccountCreation ) {
116                $this->getHookRunner()->onAddNewAccount( $user, false );
117
118                // If the user does not have a session cookie at this point, they probably need to
119                // do something to their browser.
120                if ( !$this->hasSessionCookie() ) {
121                    $this->mainLoginForm( [ /*?*/ ], $session->getProvider()->whyNoSession() );
122                    // TODO something more specific? This used to use nocookiesnew
123                    // FIXME should redirect to login page instead?
124                    return;
125                }
126            } else {
127                $byEmail = false; // FIXME no way to set this
128
129                $this->getHookRunner()->onAddNewAccount( $user, $byEmail );
130
131                $out = $this->getOutput();
132                // @phan-suppress-next-line PhanImpossibleCondition
133                $out->setPageTitleMsg( $this->msg( $byEmail ? 'accmailtitle' : 'accountcreated' ) );
134                // @phan-suppress-next-line PhanImpossibleCondition
135                if ( $byEmail ) {
136                    $out->addWikiMsg( 'accmailtext', $user->getName(), $user->getEmail() );
137                } else {
138                    $out->addWikiMsg( 'accountcreatedtext', $user->getName() );
139                }
140
141                $rt = Title::newFromText( $this->mReturnTo );
142                $out->addReturnTo(
143                    ( $rt && !$rt->isExternal() ) ? $rt : $this->getPageTitle(),
144                    wfCgiToArray( $this->mReturnToQuery )
145                );
146                return;
147            }
148            $this->getHookRunner()->onUserLoginComplete( $user, $injected_html, $direct );
149        }
150
151        $this->clearToken();
152
153        # Run any hooks; display injected HTML
154        $welcome_creation_msg = 'welcomecreation-msg';
155        /**
156         * Let any extensions change what message is shown.
157         * @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforeWelcomeCreation
158         * @since 1.18
159         */
160        $this->getHookRunner()->onBeforeWelcomeCreation( $welcome_creation_msg, $injected_html );
161
162        $this->showSuccessPage( 'signup',
163            // T308471: ensure username is plaintext (aka escaped)
164            $this->msg( 'welcomeuser' )->plaintextParams( $this->getUser()->getName() ),
165            $welcome_creation_msg, $injected_html, $extraMessages );
166    }
167
168    protected function getToken() {
169        return $this->getRequest()->getSession()->getToken( '', 'createaccount' );
170    }
171
172    protected function clearToken() {
173        $this->getRequest()->getSession()->resetToken( 'createaccount' );
174    }
175
176    protected function getTokenName() {
177        return 'wpCreateaccountToken';
178    }
179
180    protected function getGroupName() {
181        return 'users';
182    }
183
184    protected function logAuthResult( $success, $status = null ) {
185        LoggerFactory::getInstance( 'authevents' )->info( 'Account creation attempt', [
186            'event' => 'accountcreation',
187            'successful' => $success,
188            'status' => strval( $status ),
189        ] );
190    }
191}
192
193/** @deprecated class alias since 1.41 */
194class_alias( SpecialCreateAccount::class, 'SpecialCreateAccount' );