Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
EmailNotificationSecondaryAuthenticationProvider
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
5 / 5
11
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 postInitSetup
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getAuthenticationRequests
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 beginSecondaryAuthentication
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 beginSecondaryAccountCreation
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3namespace MediaWiki\Auth;
4
5use MediaWiki\MainConfigNames;
6use Wikimedia\Rdbms\IConnectionProvider;
7
8/**
9 * Handles email notification / email address confirmation for account creation.
10 *
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.
13 *
14 * @ingroup Auth
15 */
16class EmailNotificationSecondaryAuthenticationProvider
17    extends AbstractSecondaryAuthenticationProvider
18{
19    /** @var bool */
20    protected $sendConfirmationEmail;
21
22    private IConnectionProvider $dbProvider;
23
24    /**
25     * @param IConnectionProvider $dbProvider
26     * @param array $params
27     *  - sendConfirmationEmail: (bool) send an email asking the user to confirm their email
28     *    address after a successful registration
29     */
30    public function __construct( IConnectionProvider $dbProvider, $params = [] ) {
31        if ( isset( $params['sendConfirmationEmail'] ) ) {
32            $this->sendConfirmationEmail = (bool)$params['sendConfirmationEmail'];
33        }
34        $this->dbProvider = $dbProvider;
35    }
36
37    protected function postInitSetup() {
38        $this->sendConfirmationEmail ??= $this->config->get( MainConfigNames::EnableEmail )
39                && $this->config->get( MainConfigNames::EmailAuthentication );
40    }
41
42    /** @inheritDoc */
43    public function getAuthenticationRequests( $action, array $options ) {
44        return [];
45    }
46
47    /** @inheritDoc */
48    public function beginSecondaryAuthentication( $user, array $reqs ) {
49        return AuthenticationResponse::newAbstain();
50    }
51
52    /** @inheritDoc */
53    public function beginSecondaryAccountCreation( $user, $creator, array $reqs ) {
54        if (
55            $this->sendConfirmationEmail
56            && $user->getEmail()
57            && !$this->manager->getAuthenticationSessionData( 'no-email' )
58        ) {
59            // TODO show 'confirmemail_oncreate'/'confirmemail_sendfailed' message
60            $this->dbProvider->getPrimaryDatabase()->onTransactionCommitOrIdle(
61                function () use ( $user ) {
62                    $user = $user->getInstanceFromPrimary() ?? $user;
63                    $status = $user->sendConfirmationMail();
64                    $user->saveSettings();
65                    if ( !$status->isGood() ) {
66                        $this->logger->warning( 'Could not send confirmation email: ' .
67                            $status->getWikiText( false, false, 'en' ) );
68                    }
69                },
70                __METHOD__
71            );
72        }
73
74        return AuthenticationResponse::newPass();
75    }
76}