1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Auth
*/
namespace MediaWiki\Auth;
use Message;
use RawMessage;
/**
* This is an authentication request that just implements a simple button.
* @stable to extend
* @ingroup Auth
* @since 1.27
*/
class ButtonAuthenticationRequest extends AuthenticationRequest {
/** @var string */
protected $name;
/** @var Message */
protected $label;
/** @var Message */
protected $help;
/**
* @stable to call
* @param string $name Button name
* @param Message $label Button label
* @param Message $help Button help
* @param bool $required The button is required for authentication to proceed.
*/
public function __construct( $name, Message $label, Message $help, $required = false ) {
$this->name = $name;
$this->label = $label;
$this->help = $help;
$this->required = $required ? self::REQUIRED : self::OPTIONAL;
}
/**
* @inheritDoc
* @stable to override
*/
public function getUniqueId() {
return parent::getUniqueId() . ':' . $this->name;
}
/**
* @inheritDoc
* @stable to override
*/
public function getFieldInfo() {
return [
$this->name => [
'type' => 'button',
'label' => $this->label,
'help' => $this->help,
]
];
}
/**
* Fetch a ButtonAuthenticationRequest or subclass by name
* @param AuthenticationRequest[] $reqs Requests to search
* @param string $name Name to look for
* @return ButtonAuthenticationRequest|null Returns null if there is not
* exactly one matching request.
*/
public static function getRequestByName( array $reqs, $name ) {
$requests = array_filter( $reqs, static function ( $req ) use ( $name ) {
return $req instanceof ButtonAuthenticationRequest && $req->name === $name;
} );
// @phan-suppress-next-line PhanTypeMismatchReturnSuperType False positive
return count( $requests ) === 1 ? reset( $requests ) : null;
}
/**
* @codeCoverageIgnore
* @stable to override
* @param array $data
* @return AuthenticationRequest|static
*/
public static function __set_state( $data ) {
if ( !isset( $data['label'] ) ) {
$data['label'] = new RawMessage( '$1', $data['name'] );
} elseif ( is_string( $data['label'] ) ) {
$data['label'] = new Message( $data['label'] );
} elseif ( is_array( $data['label'] ) && $data['label'] ) {
// @phan-suppress-next-line PhanParamTooFewUnpack Should infer non-emptiness from above
$data['label'] = Message::newFromKey( ...$data['label'] );
}
if ( !isset( $data['help'] ) ) {
$data['help'] = new RawMessage( '$1', $data['name'] );
} elseif ( is_string( $data['help'] ) ) {
$data['help'] = new Message( $data['help'] );
} elseif ( is_array( $data['help'] ) && $data['help'] ) {
// @phan-suppress-next-line PhanParamTooFewUnpack Should infer non-emptiness from above
$data['help'] = Message::newFromKey( ...$data['help'] );
}
$ret = new static( $data['name'], $data['label'], $data['help'] );
foreach ( $data as $k => $v ) {
$ret->$k = $v;
}
return $ret;
}
}
|