MediaWiki master
SpecialUserLogin.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Specials;
8
17use StatusValue;
18
27 protected static $allowedActions = [
28 AuthManager::ACTION_LOGIN,
29 AuthManager::ACTION_LOGIN_CONTINUE
30 ];
31
33 protected static $messages = [
34 'authform-newtoken' => 'nocookiesforlogin',
35 'authform-notoken' => 'sessionfailure',
36 'authform-wrongtoken' => 'sessionfailure',
37 ];
38
39 private UserIdentityUtils $identityUtils;
40
41 public function __construct( AuthManager $authManager, UserIdentityUtils $identityUtils ) {
42 parent::__construct( 'Userlogin' );
43 $this->setAuthManager( $authManager );
44 $this->identityUtils = $identityUtils;
45 }
46
48 public function doesWrites() {
49 return true;
50 }
51
53 public function isListed() {
54 return $this->getAuthManager()->canAuthenticateNow();
55 }
56
58 protected function getLoginSecurityLevel() {
59 return false;
60 }
61
63 protected function getDefaultAction( $subPage ) {
64 return AuthManager::ACTION_LOGIN;
65 }
66
68 public function getDescription() {
69 return $this->msg( 'login' );
70 }
71
72 public function setHeaders() {
73 // override the page title if we are doing a forced reauthentication
74 parent::setHeaders();
75 if ( $this->securityLevel && $this->getUser()->isRegistered() ) {
76 $this->getOutput()->setPageTitleMsg( $this->msg( 'login-security' ) );
77 }
78 }
79
81 protected function isSignup() {
82 return false;
83 }
84
86 protected function beforeExecute( $subPage ) {
87 if ( $subPage === 'signup' || $this->getRequest()->getText( 'type' ) === 'signup' ) {
88 // B/C for old account creation URLs
89 $title = SpecialPage::getTitleFor( 'CreateAccount' );
90 $query = array_diff_key( $this->getRequest()->getQueryValues(),
91 array_fill_keys( [ 'type', 'title' ], true ) );
92 $url = $title->getFullURL( $query, false, PROTO_CURRENT );
93 $this->getOutput()->redirect( $url );
94 return false;
95 }
96 return parent::beforeExecute( $subPage );
97 }
98
110 protected function successfulAction( $direct = false, $extraMessages = null ) {
111 $secureLogin = $this->getConfig()->get( MainConfigNames::SecureLogin );
112
113 $user = $this->targetUser ?: $this->getUser();
114 $session = $this->getRequest()->getSession();
115
116 $injected_html = '';
117 if ( $direct ) {
118 $user->touch();
119 $user->debouncedDBTouch();
120
121 $this->clearToken();
122
123 if ( $user->requiresHTTPS() ) {
124 $this->mStickHTTPS = true;
125 }
126 $session->setForceHTTPS( $secureLogin && $this->mStickHTTPS );
127
128 # Run any hooks; display injected HTML if any, else redirect
129 $this->getHookRunner()->onUserLoginComplete(
130 $user, $injected_html, $direct );
131 }
132
133 if ( $injected_html !== '' || $extraMessages ) {
134 $this->showSuccessPage( 'success', $this->msg( 'loginsuccesstitle' ),
135 'loginsuccess', $injected_html, $extraMessages );
136 } else {
137 $helper = new LoginHelper( $this->getContext() );
138 $helper->showReturnToPage( 'successredirect', $this->mReturnTo, $this->mReturnToQuery,
139 $this->mStickHTTPS, $this->mReturnToAnchor );
140 }
141 }
142
144 protected function getToken() {
145 return $this->getRequest()->getSession()->getToken( '', 'login' );
146 }
147
148 protected function clearToken() {
149 $this->getRequest()->getSession()->resetToken( 'login' );
150 }
151
153 protected function getTokenName() {
154 return 'wpLoginToken';
155 }
156
158 protected function getGroupName() {
159 return 'login';
160 }
161
163 protected function logAuthResult( $success, UserIdentity $performer, $status = null ) {
164 LoggerFactory::getInstance( 'authevents' )->info( 'Login attempt', [
165 'event' => 'login',
166 'successful' => $success,
167 'accountType' => $this->identityUtils->getShortUserTypeInternal( $performer ),
168 'status' => strval( $status ),
169 ] );
170 }
171}
172
177class_alias( SpecialUserLogin::class, 'SpecialUserLogin' );
const PROTO_CURRENT
Definition Defines.php:222
Helper functions for the login form that need to be shared with other special pages (such as CentralA...
AuthManager is the authentication system in MediaWiki and serves entry point for authentication.
Create PSR-3 logger objects.
A class containing constants representing the names of configuration variables.
const SecureLogin
Name constant for the SecureLogin setting, for use with Config::get()
getRequest()
Get the WebRequest being used for this instance.WebRequest 1.18
Holds shared logic for login and account creation pages.
showSuccessPage( $type, $title, $msgname, $injected_html, $extraMessages)
Show the success page.
Parent class for all special pages.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
getUser()
Shortcut to get the User executing this instance.
setAuthManager(AuthManager $authManager)
Set the injected AuthManager from the special page constructor.
getConfig()
Shortcut to get main config object.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
Implements Special:UserLogin.
getToken()
Returns the CSRF token.to override Token
beforeExecute( $subPage)
Gets called before execute.Return false to prevent calling execute() (since 1.27+)....
isListed()
Whether this special page is listed in Special:SpecialPages.to override 1.3 (r3583) bool
successfulAction( $direct=false, $extraMessages=null)
Run any hooks registered for logins, then HTTP redirect to $this->mReturnTo (or Main Page if that's u...
getLoginSecurityLevel()
to override bool|string
doesWrites()
Indicates whether POST requests to this special page require write access to the wiki....
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
logAuthResult( $success, UserIdentity $performer, $status=null)
Logs to the authmanager-stats channel.
getDescription()
Returns the name that goes in the <h1> in the special page itself, and also the name that will be l...
__construct(AuthManager $authManager, UserIdentityUtils $identityUtils)
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
getDefaultAction( $subPage)
Get the default action for this special page if none is given via URL/POST data.Subclasses should ove...
Convenience functions for interpreting UserIdentity objects using additional services or config.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Interface for objects representing user identity.