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