3 * A class to check request restrictions expressed as a JSON object
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 use MediaWiki\Json\FormatJson
;
22 use MediaWiki\Linker\LinkTarget
;
23 use MediaWiki\Request\WebRequest
;
24 use MediaWiki\Status\Status
;
25 use MediaWiki\Title\Title
;
27 use Wikimedia\IPUtils
;
30 * A class to check request restrictions expressed as a JSON object
32 class MWRestrictions
implements Stringable
{
35 private $ipAddresses = [ '0.0.0.0/0', '::/0' ];
40 public StatusValue
$validity;
43 * @param array|null $restrictions
44 * @throws InvalidArgumentException
46 protected function __construct( ?
array $restrictions = null ) {
47 $this->validity
= StatusValue
::newGood();
48 if ( $restrictions !== null ) {
49 $this->loadFromArray( $restrictions );
54 * @return MWRestrictions
56 public static function newDefault() {
61 * @param array $restrictions
62 * @return MWRestrictions
63 * @throws InvalidArgumentException
65 public static function newFromArray( array $restrictions ) {
66 return new self( $restrictions );
70 * @param string $json JSON representation of the restrictions
71 * @return MWRestrictions
72 * @throws InvalidArgumentException
74 public static function newFromJson( $json ) {
75 $restrictions = FormatJson
::decode( $json, true );
76 if ( !is_array( $restrictions ) ) {
77 throw new InvalidArgumentException( 'Invalid restrictions JSON' );
79 return new self( $restrictions );
82 private function loadFromArray( array $restrictions ) {
83 static $neededKeys = [ 'IPAddresses' ];
85 $keys = array_keys( $restrictions );
86 $missingKeys = array_diff( $neededKeys, $keys );
88 throw new InvalidArgumentException(
89 'Array is missing required keys: ' . implode( ', ', $missingKeys )
93 if ( !is_array( $restrictions['IPAddresses'] ) ) {
94 throw new InvalidArgumentException( 'IPAddresses is not an array' );
96 foreach ( $restrictions['IPAddresses'] as $ip ) {
97 if ( !IPUtils
::isIPAddress( $ip ) ) {
98 $this->validity
->fatal( 'restrictionsfield-badip', $ip );
101 $this->ipAddresses
= $restrictions['IPAddresses'];
103 if ( isset( $restrictions['Pages'] ) ) {
104 if ( !is_array( $restrictions['Pages'] ) ) {
105 throw new InvalidArgumentException( 'Pages is not an array of page names' );
107 foreach ( $restrictions['Pages'] as $page ) {
108 if ( !is_string( $page ) ) {
109 throw new InvalidArgumentException( "Pages contains non-string value: $page" );
112 $this->pages
= $restrictions['Pages'];
117 * Return the restrictions as an array
120 public function toArray() {
121 $arr = [ 'IPAddresses' => $this->ipAddresses
];
122 if ( count( $this->pages
) ) {
123 $arr['Pages'] = $this->pages
;
129 * Return the restrictions as a JSON string
130 * @param bool|string $pretty Pretty-print the JSON output, see FormatJson::encode
133 public function toJson( $pretty = false ) {
134 return FormatJson
::encode( $this->toArray(), $pretty, FormatJson
::ALL_OK
);
137 public function __toString() {
138 return $this->toJson();
142 * Test against the passed WebRequest
143 * @param WebRequest $request
146 public function check( WebRequest
$request ) {
148 'ip' => $this->checkIP( $request->getIP() ),
150 $status = Status
::newGood();
151 $status->setResult( $ok === array_filter( $ok ), $ok );
156 * Test whether an action on the target is allowed by the restrictions
159 * @param LinkTarget $target
160 * @return StatusValue
162 public function userCan( LinkTarget
$target ) {
163 if ( !$this->checkPage( $target ) ) {
164 return StatusValue
::newFatal( 'session-page-restricted' );
166 return StatusValue
::newGood();
170 * Test if an IP address is allowed by the restrictions
174 public function checkIP( $ip ) {
175 $set = new IPSet( $this->ipAddresses
);
176 return $set->match( $ip );
180 * Test if an action on a title is allowed by the restrictions
182 * @param LinkTarget $target
185 private function checkPage( LinkTarget
$target ) {
186 if ( count( $this->pages
) === 0 ) {
189 $pagesNormalized = array_map( static function ( $titleText ) {
190 $title = Title
::newFromText( $titleText );
191 return $title ?
$title->getPrefixedText() : '';
193 return in_array( Title
::newFromLinkTarget( $target )->getPrefixedText(), $pagesNormalized, true );