cc2fc80970d972404dadf8f63b960cbfbbdad1a4
[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 use MediaWiki\MediaWikiServices;
27
28 /**
29 * Implements Special:CreateAccount
30 *
31 * @ingroup SpecialPage
32 */
33 class SpecialCreateAccount extends LoginSignupSpecialPage {
34 protected static $allowedActions = [
35 AuthManager::ACTION_CREATE,
36 AuthManager::ACTION_CREATE_CONTINUE
37 ];
38
39 protected static $messages = [
40 'authform-newtoken' => 'nocookiesfornew',
41 'authform-notoken' => 'sessionfailure',
42 'authform-wrongtoken' => 'sessionfailure',
43 ];
44
45 public function __construct() {
46 parent::__construct( 'CreateAccount' );
47 }
48
49 public function doesWrites() {
50 return true;
51 }
52
53 public function isRestricted() {
54 return !MediaWikiServices::getInstance()
55 ->getPermissionManager()
56 ->groupHasPermission( '*', 'createaccount' );
57 }
58
59 public function userCanExecute( User $user ) {
60 return $user->isAllowed( 'createaccount' );
61 }
62
63 public function checkPermissions() {
64 parent::checkPermissions();
65
66 $user = $this->getUser();
67 $status = AuthManager::singleton()->checkAccountCreatePermissions( $user );
68 if ( !$status->isGood() ) {
69 // Track block with a cookie if it doesn't exist already
70 if ( $user->isBlockedFromCreateAccount() ) {
71 MediaWikiServices::getInstance()->getBlockManager()->trackBlockWithCookie( $user );
72 }
73 throw new ErrorPageError( 'createacct-error', $status->getMessage() );
74 }
75 }
76
77 protected function getLoginSecurityLevel() {
78 return false;
79 }
80
81 protected function getDefaultAction( $subPage ) {
82 return AuthManager::ACTION_CREATE;
83 }
84
85 public function getDescription() {
86 return $this->msg( 'createaccount' )->text();
87 }
88
89 protected function isSignup() {
90 return true;
91 }
92
93 /**
94 * Run any hooks registered for logins, then display a message welcoming
95 * the user.
96 * @param bool $direct True if the action was successful just now; false if that happened
97 * pre-redirection (so this handler was called already)
98 * @param StatusValue|null $extraMessages
99 */
100 protected function successfulAction( $direct = false, $extraMessages = null ) {
101 $session = $this->getRequest()->getSession();
102 $user = $this->targetUser ?: $this->getUser();
103
104 if ( $direct ) {
105 # Only save preferences if the user is not creating an account for someone else.
106 if ( !$this->proxyAccountCreation ) {
107 Hooks::run( 'AddNewAccount', [ $user, false ] );
108
109 // If the user does not have a session cookie at this point, they probably need to
110 // do something to their browser.
111 if ( !$this->hasSessionCookie() ) {
112 $this->mainLoginForm( [ /*?*/ ], $session->getProvider()->whyNoSession() );
113 // TODO something more specific? This used to use nocookiesnew
114 // FIXME should redirect to login page instead?
115 return;
116 }
117 } else {
118 $byEmail = false; // FIXME no way to set this
119
120 Hooks::run( 'AddNewAccount', [ $user, $byEmail ] );
121
122 $out = $this->getOutput();
123 $out->setPageTitle( $this->msg( $byEmail ? 'accmailtitle' : 'accountcreated' ) );
124 if ( $byEmail ) {
125 $out->addWikiMsg( 'accmailtext', $user->getName(), $user->getEmail() );
126 } else {
127 $out->addWikiMsg( 'accountcreatedtext', $user->getName() );
128 }
129
130 $rt = Title::newFromText( $this->mReturnTo );
131 $out->addReturnTo(
132 ( $rt && !$rt->isExternal() ) ? $rt : $this->getPageTitle(),
133 wfCgiToArray( $this->mReturnToQuery )
134 );
135 return;
136 }
137 }
138
139 $this->clearToken();
140
141 # Run any hooks; display injected HTML
142 $injected_html = '';
143 $welcome_creation_msg = 'welcomecreation-msg';
144 Hooks::run( 'UserLoginComplete', [ &$user, &$injected_html, $direct ] );
145
146 /**
147 * Let any extensions change what message is shown.
148 * @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforeWelcomeCreation
149 * @since 1.18
150 */
151 Hooks::run( 'BeforeWelcomeCreation', [ &$welcome_creation_msg, &$injected_html ] );
152
153 $this->showSuccessPage( 'signup', $this->msg( 'welcomeuser', $this->getUser()->getName() ),
154 $welcome_creation_msg, $injected_html, $extraMessages );
155 }
156
157 protected function getToken() {
158 return $this->getRequest()->getSession()->getToken( '', 'createaccount' );
159 }
160
161 protected function clearToken() {
162 return $this->getRequest()->getSession()->resetToken( 'createaccount' );
163 }
164
165 protected function getTokenName() {
166 return 'wpCreateaccountToken';
167 }
168
169 protected function getGroupName() {
170 return 'login';
171 }
172
173 protected function logAuthResult( $success, $status = null ) {
174 LoggerFactory::getInstance( 'authevents' )->info( 'Account creation attempt', [
175 'event' => 'accountcreation',
176 'successful' => $success,
177 'status' => $status,
178 ] );
179 }
180 }