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