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