Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / auth / EmailNotificationSecondaryAuthenticationProvider.php
bloba315f4a563da80a7e28af23db201de0c8b8bc450
1 <?php
3 namespace MediaWiki\Auth;
5 use MediaWiki\MainConfigNames;
6 use Wikimedia\Rdbms\IConnectionProvider;
8 /**
9 * Handles email notification / email address confirmation for account creation.
11 * Set 'no-email' to true (via AuthManager::setAuthenticationSessionData) to skip this provider.
12 * Primary providers doing so are expected to take care of email address confirmation.
14 class EmailNotificationSecondaryAuthenticationProvider
15 extends AbstractSecondaryAuthenticationProvider
17 /** @var bool */
18 protected $sendConfirmationEmail;
20 private IConnectionProvider $dbProvider;
22 /**
23 * @param IConnectionProvider $dbProvider
24 * @param array $params
25 * - sendConfirmationEmail: (bool) send an email asking the user to confirm their email
26 * address after a successful registration
28 public function __construct( IConnectionProvider $dbProvider, $params = [] ) {
29 if ( isset( $params['sendConfirmationEmail'] ) ) {
30 $this->sendConfirmationEmail = (bool)$params['sendConfirmationEmail'];
32 $this->dbProvider = $dbProvider;
35 protected function postInitSetup() {
36 $this->sendConfirmationEmail ??= $this->config->get( MainConfigNames::EnableEmail )
37 && $this->config->get( MainConfigNames::EmailAuthentication );
40 public function getAuthenticationRequests( $action, array $options ) {
41 return [];
44 public function beginSecondaryAuthentication( $user, array $reqs ) {
45 return AuthenticationResponse::newAbstain();
48 public function beginSecondaryAccountCreation( $user, $creator, array $reqs ) {
49 if (
50 $this->sendConfirmationEmail
51 && $user->getEmail()
52 && !$this->manager->getAuthenticationSessionData( 'no-email' )
53 ) {
54 // TODO show 'confirmemail_oncreate'/'confirmemail_sendfailed' message
55 $this->dbProvider->getPrimaryDatabase()->onTransactionCommitOrIdle(
56 function () use ( $user ) {
57 $user = $user->getInstanceForUpdate();
58 $status = $user->sendConfirmationMail();
59 $user->saveSettings();
60 if ( !$status->isGood() ) {
61 $this->logger->warning( 'Could not send confirmation email: ' .
62 $status->getWikiText( false, false, 'en' ) );
65 __METHOD__
69 return AuthenticationResponse::newPass();