Debian Codegraph @

includes/auth/RememberMeAuthenticationRequest.php

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