Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
92.00% |
23 / 25 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
UserNotLoggedIn | |
92.00% |
23 / 25 |
|
50.00% |
1 / 2 |
9.04 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
report | |
88.89% |
16 / 18 |
|
0.00% |
0 / 1 |
5.03 |
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 | |
21 | use MediaWiki\Context\RequestContext; |
22 | use MediaWiki\SpecialPage\SpecialPage; |
23 | |
24 | /** |
25 | * Redirect a user to the login page or account creation page |
26 | * |
27 | * This is essentially an ErrorPageError exception which by default uses the |
28 | * 'exception-nologin' as a title and 'exception-nologin-text' for the message. |
29 | * |
30 | * When the user is a temporary account, the redirect will point to the Special:CreateAccount page unless |
31 | * specifically set not to. In all other cases, the redirect is to the Special:UserLogin page. |
32 | * |
33 | * The message key for the reason will be modified to include '-for-temp-user' when the user is logged |
34 | * in to a temporary account and this message key exists (i.e. defined and not empty). |
35 | * |
36 | * @note In order for this exception to redirect, the error message passed to the |
37 | * constructor has to be explicitly added to LoginHelper::validErrorMessages or with |
38 | * the LoginFormValidErrorMessages hook. Otherwise, the user will just be shown the message |
39 | * rather than redirected. |
40 | * |
41 | * @par Example: |
42 | * @code |
43 | * if ( $user->isAnon() ) { |
44 | * throw new UserNotLoggedIn(); |
45 | * } |
46 | * @endcode |
47 | * |
48 | * Note the parameter order differs from ErrorPageError, this allows you to |
49 | * simply specify a reason without overriding the default title. |
50 | * |
51 | * @par Example: |
52 | * @code |
53 | * if ( $user->isAnon() ) { |
54 | * throw new UserNotLoggedIn( 'action-require-loggedin' ); |
55 | * } |
56 | * @endcode |
57 | * |
58 | * You can use {@link SpecialPage::requireLogin} and {@link SpecialPage::requireNamedUser} to throw this |
59 | * exception when the user is an anon user or not named respectively. |
60 | * |
61 | * @newable |
62 | * @see T39627 |
63 | * @since 1.20 |
64 | * @ingroup Exception |
65 | */ |
66 | class UserNotLoggedIn extends ErrorPageError { |
67 | |
68 | private bool $alwaysRedirectToLoginPage; |
69 | |
70 | /** |
71 | * @stable to call |
72 | * |
73 | * @note The value of the $reasonMsg parameter must be set with the LoginFormValidErrorMessages |
74 | * hook if you want the user to be automatically redirected to the login form. |
75 | * |
76 | * @param string $reasonMsg A message key containing the reason for the error. '-for-temp-user' will be |
77 | * appended to the end of the message key if the user is a temporary account and the redirect is |
78 | * to the Special:CreateAccount page. The modification is skipped if the message key does not |
79 | * exist. |
80 | * Optional, default: 'exception-nologin-text' |
81 | * @param string $titleMsg A message key to set the page title. |
82 | * Optional, default: 'exception-nologin' |
83 | * @param array $params Parameters to wfMessage() for $reasonMsg and $tempUserReasonMsg |
84 | * Optional, default: [] |
85 | * @param bool $alwaysRedirectToLoginPage Whether we should always redirect to the login page, even if the |
86 | * user is a temporary account. If false (the default), the redirect will be to Special:CreateAccount |
87 | * when the user is logged in to a temporary account. |
88 | */ |
89 | public function __construct( |
90 | $reasonMsg = 'exception-nologin-text', |
91 | $titleMsg = 'exception-nologin', |
92 | $params = [], |
93 | bool $alwaysRedirectToLoginPage = false |
94 | ) { |
95 | $context = RequestContext::getMain(); |
96 | // Replace the reason message for one that describes creating account when the user is a temporary account |
97 | // when such a custom message exists (T358586). |
98 | if ( $context->getUser()->isTemp() && !$alwaysRedirectToLoginPage ) { |
99 | // For grep to find usages: exception-nologin-text-for-temp-user |
100 | $tempUserReasonMsg = $reasonMsg . '-for-temp-user'; |
101 | if ( $context->msg( $tempUserReasonMsg )->exists() ) { |
102 | $reasonMsg = $tempUserReasonMsg; |
103 | } |
104 | } |
105 | parent::__construct( $titleMsg, $reasonMsg, $params ); |
106 | $this->alwaysRedirectToLoginPage = $alwaysRedirectToLoginPage; |
107 | } |
108 | |
109 | /** |
110 | * Redirect to Special:Userlogin or Special:CreateAccount if the specified message is compatible. Otherwise, |
111 | * show an error page as usual. |
112 | * @param int $action |
113 | */ |
114 | public function report( $action = self::SEND_OUTPUT ) { |
115 | // If an unsupported message is used, don't try redirecting to Special:Userlogin, |
116 | // since the message may not be compatible. |
117 | if ( !in_array( $this->msg, LoginHelper::getValidErrorMessages() ) ) { |
118 | parent::report( $action ); |
119 | return; |
120 | } |
121 | |
122 | $context = RequestContext::getMain(); |
123 | |
124 | // Message is valid. Redirect to Special:Userlogin, unless the user is a temporary account in which case |
125 | // redirect to Special:CreateAccount (T358586). |
126 | $specialPageName = 'Userlogin'; |
127 | if ( $context->getUser()->isTemp() && !$this->alwaysRedirectToLoginPage ) { |
128 | $specialPageName = 'CreateAccount'; |
129 | } |
130 | |
131 | $output = $context->getOutput(); |
132 | $query = $context->getRequest()->getQueryValues(); |
133 | // Title will be overridden by returnto |
134 | unset( $query['title'] ); |
135 | // Redirect to Special:Userlogin |
136 | $output->redirect( SpecialPage::getTitleFor( $specialPageName )->getFullURL( [ |
137 | // Return to this page when the user logs in |
138 | 'returnto' => $context->getTitle()->getFullText(), |
139 | 'returntoquery' => wfArrayToCgi( $query ), |
140 | 'warning' => $this->msg, |
141 | // Forward the 'display' parameter if provided |
142 | 'display' => $query['display'] ?? null, |
143 | ] ) ); |
144 | |
145 | if ( $action === self::SEND_OUTPUT ) { |
146 | $output->output(); |
147 | } |
148 | } |
149 | } |