Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
31 / 31 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| RememberMeAuthenticationRequest | |
100.00% |
31 / 31 |
|
100.00% |
2 / 2 |
9 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
6 | |||
| getFieldInfo | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\Auth; |
| 8 | |
| 9 | use MediaWiki\Context\RequestContext; |
| 10 | use MediaWiki\Session\SessionProvider; |
| 11 | use UnexpectedValueException; |
| 12 | |
| 13 | /** |
| 14 | * This is an authentication request added by AuthManager to show a "remember me" checkbox. |
| 15 | * |
| 16 | * When checked, it will take more time for the authenticated session to expire. |
| 17 | * |
| 18 | * @stable to extend |
| 19 | * @ingroup Auth |
| 20 | * @since 1.27 |
| 21 | */ |
| 22 | class RememberMeAuthenticationRequest extends AuthenticationRequest { |
| 23 | |
| 24 | /** |
| 25 | * Indicates that the user may be able to choose whether to be remembered or not. The |
| 26 | * choice field is skippable if no other fields are on the login form. This is the original |
| 27 | * pre-1.36 behavior. |
| 28 | */ |
| 29 | public const CHOOSE_REMEMBER = 'choose'; |
| 30 | |
| 31 | /** |
| 32 | * Indicates that the user will be able to choose whether to be remembered or not. The |
| 33 | * choice field is not skippable, even if there are no other fields on the login form. |
| 34 | */ |
| 35 | public const FORCE_CHOOSE_REMEMBER = 'force-choose'; |
| 36 | |
| 37 | /** |
| 38 | * Indicates that the user will always be remembered. |
| 39 | */ |
| 40 | public const ALWAYS_REMEMBER = 'always'; |
| 41 | |
| 42 | /** |
| 43 | * Indicates that the user will never be remembered. |
| 44 | */ |
| 45 | public const NEVER_REMEMBER = 'never'; |
| 46 | |
| 47 | /** |
| 48 | * Allowed configuration flags |
| 49 | */ |
| 50 | public const ALLOWED_FLAGS = [ |
| 51 | self::CHOOSE_REMEMBER, |
| 52 | self::FORCE_CHOOSE_REMEMBER, |
| 53 | self::ALWAYS_REMEMBER, |
| 54 | self::NEVER_REMEMBER, |
| 55 | ]; |
| 56 | |
| 57 | /** |
| 58 | * Whether this field must be filled in on the form. Since the field is a checkbox, which can |
| 59 | * by definition be left blank, it is always optional. |
| 60 | * @var bool |
| 61 | */ |
| 62 | public $required = self::OPTIONAL; |
| 63 | |
| 64 | /** |
| 65 | * Whether display of this field can be skipped, accepting the default value, if there are no |
| 66 | * other fields on the form. |
| 67 | * @var bool |
| 68 | * @since 1.36 |
| 69 | */ |
| 70 | public $skippable = true; |
| 71 | |
| 72 | /** @var bool */ |
| 73 | private $checkbox = false; |
| 74 | |
| 75 | /** |
| 76 | * @var int|null How long the user will be remembered, in seconds. |
| 77 | * Null means setting the $rememberMe will have no effect |
| 78 | */ |
| 79 | protected $expiration = null; |
| 80 | |
| 81 | /** @var bool */ |
| 82 | public $rememberMe = false; |
| 83 | |
| 84 | /** |
| 85 | * @stable to call |
| 86 | * @param string $flag |
| 87 | */ |
| 88 | public function __construct( string $flag = self::CHOOSE_REMEMBER ) { |
| 89 | /** @var SessionProvider $provider */ |
| 90 | $provider = RequestContext::getMain()->getRequest()->getSession()->getProvider(); |
| 91 | '@phan-var SessionProvider $provider'; |
| 92 | |
| 93 | switch ( $flag ) { |
| 94 | case self::CHOOSE_REMEMBER: |
| 95 | $this->skippable = true; |
| 96 | $this->checkbox = true; |
| 97 | $this->expiration = $provider->getRememberUserDuration(); |
| 98 | break; |
| 99 | case self::FORCE_CHOOSE_REMEMBER: |
| 100 | $this->skippable = false; |
| 101 | $this->checkbox = true; |
| 102 | $this->expiration = $provider->getRememberUserDuration(); |
| 103 | break; |
| 104 | case self::ALWAYS_REMEMBER: |
| 105 | $this->skippable = true; |
| 106 | $this->checkbox = false; |
| 107 | $this->expiration = $provider->getRememberUserDuration(); |
| 108 | break; |
| 109 | case self::NEVER_REMEMBER: |
| 110 | $this->skippable = true; |
| 111 | $this->checkbox = false; |
| 112 | $this->expiration = null; |
| 113 | break; |
| 114 | default: |
| 115 | throw new UnexpectedValueException( '$flag must be one of the values: \'' . |
| 116 | implode( "', '", self::ALLOWED_FLAGS ) . '\'' ); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @inheritDoc |
| 122 | * @stable to override |
| 123 | */ |
| 124 | public function getFieldInfo() { |
| 125 | if ( !$this->expiration || !$this->checkbox ) { |
| 126 | return []; |
| 127 | } |
| 128 | |
| 129 | $expirationDays = ceil( $this->expiration / ( 3600 * 24 ) ); |
| 130 | return [ |
| 131 | 'rememberMe' => [ |
| 132 | 'type' => 'checkbox', |
| 133 | 'label' => wfMessage( 'userlogin-remembermypassword' )->numParams( $expirationDays ), |
| 134 | 'help' => wfMessage( 'authmanager-userlogin-remembermypassword-help' ), |
| 135 | 'optional' => true, |
| 136 | 'skippable' => $this->skippable, |
| 137 | ] |
| 138 | ]; |
| 139 | } |
| 140 | } |