| 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 | */ |
| 20 | |
| 21 | namespace MediaWiki\Permissions; |
| 22 | |
| 23 | use MediaWiki\User\UserIdentity; |
| 24 | |
| 25 | /** |
| 26 | * Represents the subject that rate limits are applied to. |
| 27 | * |
| 28 | * @unstable |
| 29 | * @since 1.39 |
| 30 | */ |
| 31 | class RateLimitSubject { |
| 32 | |
| 33 | /** |
| 34 | * @var UserIdentity |
| 35 | */ |
| 36 | private $user; |
| 37 | |
| 38 | /** |
| 39 | * @var string|null |
| 40 | */ |
| 41 | private $ip; |
| 42 | |
| 43 | /** |
| 44 | * @var array |
| 45 | */ |
| 46 | private $flags; |
| 47 | |
| 48 | /** @var string Flag indicating the user is exempt from rate limits */ |
| 49 | public const EXEMPT = 'exempt'; |
| 50 | |
| 51 | /** @var string Flag indicating the user is a newbie */ |
| 52 | public const NEWBIE = 'newbie'; |
| 53 | |
| 54 | /** |
| 55 | * @internal |
| 56 | * |
| 57 | * @param UserIdentity $user |
| 58 | * @param string|null $ip |
| 59 | * @param array<string,bool> $flags |
| 60 | */ |
| 61 | public function __construct( UserIdentity $user, ?string $ip, array $flags ) { |
| 62 | $this->user = $user; |
| 63 | $this->ip = $ip; |
| 64 | $this->flags = $flags; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @return UserIdentity |
| 69 | */ |
| 70 | public function getUser(): UserIdentity { |
| 71 | return $this->user; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @return string|null |
| 76 | */ |
| 77 | public function getIP(): ?string { |
| 78 | return $this->ip; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Checks whether the given flag applies. |
| 83 | * |
| 84 | * @param string $flag |
| 85 | * |
| 86 | * @return bool |
| 87 | */ |
| 88 | public function is( string $flag ) { |
| 89 | return !empty( $this->flags[$flag] ); |
| 90 | } |
| 91 | |
| 92 | } |