Use AuthManager on special pages
[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 Psr\Log\LogLevel;
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 !User::groupHasPermission( '*', 'createaccount' );
55 }
56
57 public function userCanExecute( User $user ) {
58 return $user->isAllowed( 'createaccount' );
59 }
60
61 public function checkPermissions() {
62 parent::checkPermissions();
63
64 $user = $this->getUser();
65 $status = AuthManager::singleton()->checkAccountCreatePermissions( $user );
66 if ( !$status->isGood() ) {
67 throw new ErrorPageError( 'createacct-error', $status->getMessage() );
68 }
69 }
70
71 protected function getLoginSecurityLevel() {
72 return false;
73 }
74
75 protected function getDefaultAction( $subPage ) {
76 return AuthManager::ACTION_CREATE;
77 }
78
79 public function getDescription() {
80 return $this->msg( 'createaccount' )->text();
81 }
82
83 protected function isSignup() {
84 return true;
85 }
86
87 /**
88 * Run any hooks registered for logins, then display a message welcoming
89 * the user.
90 * @param bool $direct True if the action was successful just now; false if that happened
91 * pre-redirection (so this handler was called already)
92 * @param StatusValue|null $extraMessages
93 */
94 protected function successfulAction( $direct = false, $extraMessages = null ) {
95 $session = $this->getRequest()->getSession();
96 $user = $this->targetUser ?: $this->getUser();
97
98 if ( $direct ) {
99 # Only save preferences if the user is not creating an account for someone else.
100 if ( !$this->proxyAccountCreation ) {
101 Hooks::run( 'AddNewAccount', [ $user, false ] );
102
103 // If the user does not have a session cookie at this point, they probably need to
104 // do something to their browser.
105 if ( !$this->hasSessionCookie() ) {
106 $this->mainLoginForm( [ /*?*/ ], $session->getProvider()->whyNoSession() );
107 // TODO something more specific? This used to use nocookiesnew
108 // FIXME should redirect to login page instead?
109 return;
110 }
111 } else {
112 $byEmail = false; // FIXME no way to set this
113
114 Hooks::run( 'AddNewAccount', [ $user, $byEmail ] );
115
116 $out = $this->getOutput();
117 $out->setPageTitle( $this->msg( $byEmail ? 'accmailtitle' : 'accountcreated' ) );
118 if ( $byEmail ) {
119 $out->addWikiMsg( 'accmailtext', $user->getName(), $user->getEmail() );
120 } else {
121 $out->addWikiMsg( 'accountcreatedtext', $user->getName() );
122 }
123 $out->addReturnTo( $this->getPageTitle() );
124 return;
125 }
126 }
127
128 $this->clearToken();
129
130 # Run any hooks; display injected HTML
131 $injected_html = '';
132 $welcome_creation_msg = 'welcomecreation-msg';
133 Hooks::run( 'UserLoginComplete', [ &$user, &$injected_html ] );
134
135 /**
136 * Let any extensions change what message is shown.
137 * @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforeWelcomeCreation
138 * @since 1.18
139 */
140 Hooks::run( 'BeforeWelcomeCreation', [ &$welcome_creation_msg, &$injected_html ] );
141
142 $this->showSuccessPage( 'signup', $this->msg( 'welcomeuser', $this->getUser()->getName() ),
143 $welcome_creation_msg, $injected_html, $extraMessages );
144 }
145
146 protected function getToken() {
147 return $this->getRequest()->getSession()->getToken( '', 'createaccount' );
148 }
149
150 protected function clearToken() {
151 return $this->getRequest()->getSession()->resetToken( 'createaccount' );
152 }
153
154 protected function getTokenName() {
155 return 'wpCreateaccountToken';
156 }
157
158 protected function getGroupName() {
159 return 'login';
160 }
161
162 protected function logAuthResult( $success, $status = null ) {
163 LoggerFactory::getInstance( 'authmanager-stats' )->info( 'Account creation attempt', [
164 'event' => 'accountcreation',
165 'successful' => $success,
166 'status' => $status,
167 ] );
168 }
169 }