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