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.
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.
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
22 namespace MediaWiki\Auth
;
24 use MediaWiki\Language\RawMessage
;
25 use MediaWiki\Message\Message
;
28 * This is an authentication request that just implements a simple button. Can either be
29 * subclassed, or used directly. When used directly, with $required = false in the constructor,
30 * the ButtonAuthenticationRequest will only be included in the set of requests when the button
31 * was clicked; with $required = true, it will always be included and $button->{$name} can be
32 * used to check whether it has been clicked.
38 #[\AllowDynamicProperties]
39 class ButtonAuthenticationRequest
extends AuthenticationRequest
{
43 protected Message
$label;
44 protected Message
$help;
48 * @param string $name Button name (e.g. HTML "name" attribute)
49 * @param Message $label Button label
50 * @param Message $help Button help
51 * @param bool $required The button is required for authentication to proceed.
53 public function __construct( $name, Message
$label, Message
$help, $required = false ) {
55 $this->label
= $label;
57 $this->required
= $required ? self
::REQUIRED
: self
::OPTIONAL
;
64 public function getUniqueId() {
65 return parent
::getUniqueId() . ':' . $this->name
;
72 public function getFieldInfo() {
76 'label' => $this->label
,
77 'help' => $this->help
,
83 * Fetch a ButtonAuthenticationRequest or subclass by name
84 * @param AuthenticationRequest[] $reqs Requests to search
85 * @param string $name Name to look for
86 * @return ButtonAuthenticationRequest|null Returns null if there is not
87 * exactly one matching request.
89 public static function getRequestByName( array $reqs, $name ) {
90 $requests = array_filter( $reqs, static function ( $req ) use ( $name ) {
91 return $req instanceof ButtonAuthenticationRequest
&& $req->name
=== $name;
93 // @phan-suppress-next-line PhanTypeMismatchReturnSuperType False positive
94 return count( $requests ) === 1 ?
reset( $requests ) : null;
101 * @return AuthenticationRequest|static
103 public static function __set_state( $data ) {
104 if ( !isset( $data['label'] ) ) {
105 $data['label'] = new RawMessage( '$1', $data['name'] );
106 } elseif ( is_string( $data['label'] ) ) {
107 $data['label'] = new Message( $data['label'] );
108 } elseif ( is_array( $data['label'] ) && $data['label'] ) {
109 // @phan-suppress-next-line PhanParamTooFewUnpack Should infer non-emptiness from above
110 $data['label'] = Message
::newFromKey( ...$data['label'] );
112 if ( !isset( $data['help'] ) ) {
113 $data['help'] = new RawMessage( '$1', $data['name'] );
114 } elseif ( is_string( $data['help'] ) ) {
115 $data['help'] = new Message( $data['help'] );
116 } elseif ( is_array( $data['help'] ) && $data['help'] ) {
117 // @phan-suppress-next-line PhanParamTooFewUnpack Should infer non-emptiness from above
118 $data['help'] = Message
::newFromKey( ...$data['help'] );
120 $ret = new static( $data['name'], $data['label'], $data['help'] );
121 foreach ( $data as $k => $v ) {