Merge "Convert Special:DeletedContributions to use OOUI."
[lhc/web/wiklou.git] / includes / auth / CheckBlocksSecondaryAuthenticationProvider.php
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 Config;
25 use StatusValue;
26
27 /**
28 * Check if the user is blocked, and prevent authentication if so.
29 *
30 * @ingroup Auth
31 * @since 1.27
32 */
33 class CheckBlocksSecondaryAuthenticationProvider extends AbstractSecondaryAuthenticationProvider {
34
35 /** @var bool */
36 protected $blockDisablesLogin = null;
37
38 /**
39 * @param array $params
40 * - blockDisablesLogin: (bool) Whether blocked accounts can log in,
41 * defaults to $wgBlockDisablesLogin
42 */
43 public function __construct( $params = [] ) {
44 if ( isset( $params['blockDisablesLogin'] ) ) {
45 $this->blockDisablesLogin = (bool)$params['blockDisablesLogin'];
46 }
47 }
48
49 public function setConfig( Config $config ) {
50 parent::setConfig( $config );
51
52 if ( $this->blockDisablesLogin === null ) {
53 $this->blockDisablesLogin = $this->config->get( 'BlockDisablesLogin' );
54 }
55 }
56
57 public function getAuthenticationRequests( $action, array $options ) {
58 return [];
59 }
60
61 public function beginSecondaryAuthentication( $user, array $reqs ) {
62 if ( !$this->blockDisablesLogin ) {
63 return AuthenticationResponse::newAbstain();
64 } elseif ( $user->isBlocked() ) {
65 return AuthenticationResponse::newFail(
66 new \Message( 'login-userblocked', [ $user->getName() ] )
67 );
68 } else {
69 return AuthenticationResponse::newPass();
70 }
71 }
72
73 public function beginSecondaryAccountCreation( $user, $creator, array $reqs ) {
74 return AuthenticationResponse::newAbstain();
75 }
76
77 public function testUserForCreation( $user, $autocreate, array $options = [] ) {
78 $block = $user->isBlockedFromCreateAccount();
79 if ( $block ) {
80 $errorParams = [
81 $block->getTarget(),
82 $block->mReason ?: \Message::newFromKey( 'blockednoreason' )->text(),
83 $block->getByName()
84 ];
85
86 if ( $block->getType() === \Block::TYPE_RANGE ) {
87 $errorMessage = 'cantcreateaccount-range-text';
88 $errorParams[] = $this->manager->getRequest()->getIP();
89 } else {
90 $errorMessage = 'cantcreateaccount-text';
91 }
92
93 return StatusValue::newFatal(
94 new \Message( $errorMessage, $errorParams )
95 );
96 } else {
97 return StatusValue::newGood();
98 }
99 }
100
101 }