Merge "Add checkDependencies.php"
[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 MediaWiki\Block\DatabaseBlock;
26 use StatusValue;
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 // @TODO Partial blocks should not prevent the user from logging in.
64 // see: https://phabricator.wikimedia.org/T208895
65 if ( !$this->blockDisablesLogin ) {
66 return AuthenticationResponse::newAbstain();
67 } elseif ( $user->getBlock() ) {
68 return AuthenticationResponse::newFail(
69 new \Message( 'login-userblocked', [ $user->getName() ] )
70 );
71 } else {
72 return AuthenticationResponse::newPass();
73 }
74 }
75
76 public function beginSecondaryAccountCreation( $user, $creator, array $reqs ) {
77 return AuthenticationResponse::newAbstain();
78 }
79
80 public function testUserForCreation( $user, $autocreate, array $options = [] ) {
81 $block = $user->isBlockedFromCreateAccount();
82 if ( $block ) {
83 if ( $block->getReason() ) {
84 $reason = $block->getReason();
85 } else {
86 $msg = \Message::newFromKey( 'blockednoreason' );
87 if ( !\RequestContext::getMain()->getUser()->isSafeToLoad() ) {
88 $msg->inContentLanguage();
89 }
90 $reason = $msg->text();
91 }
92
93 $errorParams = [
94 $block->getTarget(),
95 $reason,
96 $block->getByName()
97 ];
98
99 if ( $block->getType() === DatabaseBlock::TYPE_RANGE ) {
100 $errorMessage = 'cantcreateaccount-range-text';
101 $errorParams[] = $this->manager->getRequest()->getIP();
102 } else {
103 $errorMessage = 'cantcreateaccount-text';
104 }
105
106 return StatusValue::newFatal(
107 new \Message( $errorMessage, $errorParams )
108 );
109 } else {
110 return StatusValue::newGood();
111 }
112 }
113
114 }