MediaWiki  master
CheckBlocksSecondaryAuthenticationProvider.php
Go to the documentation of this file.
1 <?php
22 namespace MediaWiki\Auth;
23 
27 use StatusValue;
28 
39 
41  protected $blockDisablesLogin = null;
42 
48  public function __construct( $params = [] ) {
49  if ( isset( $params['blockDisablesLogin'] ) ) {
50  $this->blockDisablesLogin = (bool)$params['blockDisablesLogin'];
51  }
52  }
53 
55  protected function postInitSetup() {
56  $this->blockDisablesLogin ??= $this->config->get( MainConfigNames::BlockDisablesLogin );
57  }
58 
60  public function getAuthenticationRequests( $action, array $options ) {
61  return [];
62  }
63 
65  public function beginSecondaryAuthentication( $user, array $reqs ) {
66  if ( !$this->blockDisablesLogin ) {
68  }
69  $block = $user->getBlock();
70  // Ignore IP blocks and partial blocks, $wgBlockDisablesLogin was meant for
71  // blocks banning specific users.
72  if ( $block && $block->isSitewide() && $block->isBlocking( $user ) ) {
74  new \Message( 'login-userblocked', [ $user->getName() ] )
75  );
76  } else {
78  }
79  }
80 
82  public function beginSecondaryAccountCreation( $user, $creator, array $reqs ) {
84  }
85 
87  public function testUserForCreation( $user, $autocreate, array $options = [] ) {
88  // isBlockedFromCreateAccount() does not return non-accountcreation blocks, but we need them
89  // in the $wgBlockDisablesLogin case; getBlock() is unreliable for IP blocks. So we need both.
90  $blocks = [
91  'local-createaccount' => $user->isBlockedFromCreateAccount(),
92  'local' => $user->getBlock(),
93  ];
94  foreach ( $blocks as $block ) {
96  if ( $block && $block->isSitewide()
97  // This method is for checking a given account/username, not the current user, so
98  // ignore IP blocks; they will be checked elsewhere via authorizeCreateAccount().
99  // FIXME: special-case autocreation which doesn't do that check. Should it?
100  && ( $block->isBlocking( $user ) || $autocreate )
101  && (
102  // Should blocks that prevent account creation also prevent autocreation?
103  // We'll go with yes here.
104  $block->isCreateAccountBlocked()
105  // A successful autocreation means the user is logged in, so we must make sure to
106  // honor $wgBlockDisablesLogin. If it's enabled, sitewide blocks are expected to
107  // prevent login regardless of their flags.
108  || ( $autocreate && $this->blockDisablesLogin )
109  )
110  // FIXME: ideally on autocreation we'd figure out if the user has the ipblock-exempt
111  // or globalblock-exempt right via some central authorization system like
112  // CentralAuth global groups. But at this point the local account doesn't exist
113  // yet so there is no way to do that. There should probably be some separate hook
114  // to fetch user rights for a central user.
115  // FIXME: T249444: there should probably be a way to force autocreation through blocks
116  ) {
117  $formatter = MediaWikiServices::getInstance()->getBlockErrorFormatter();
118 
119  $context = \RequestContext::getMain();
120 
121  $language = $context->getUser()->isSafeToLoad() ?
122  \RequestContext::getMain()->getLanguage() :
123  MediaWikiServices::getInstance()->getContentLanguage();
124 
125  $ip = $context->getRequest()->getIP();
126 
127  return StatusValue::newFatal(
128  $formatter->getMessage( $block, $user, $language, $ip )
129  );
130  }
131  }
132  return StatusValue::newGood();
133  }
134 
135 }
A base class that implements some of the boilerplate for a SecondaryAuthenticationProvider.
static newFail(Message $msg, array $failReasons=[])
Check if the user is blocked, and prevent authentication if so.
beginSecondaryAccountCreation( $user, $creator, array $reqs)
Start an account creation flow.There is no guarantee this will be called in a successful account crea...
testUserForCreation( $user, $autocreate, array $options=[])
Determine whether an account may be created.User being created (not added to the database yet)....
getAuthenticationRequests( $action, array $options)
Return the applicable list of AuthenticationRequests.Possible values for $action depend on whether th...
beginSecondaryAuthentication( $user, array $reqs)
Start an authentication flow.Note that this may be called for a user even if beginSecondaryAccountCre...
postInitSetup()
A provider can override this to do any necessary setup after init() is called.1.37 Stability: stablet...
A class containing constants representing the names of configuration variables.
const BlockDisablesLogin
Name constant for the BlockDisablesLogin setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition: Message.php:144
static getMain()
Get the RequestContext object associated with the main request.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: StatusValue.php:46
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:73
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:85