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 */
14class EmailNotificationSecondaryAuthenticationProvider
15    extends AbstractSecondaryAuthenticationProvider
16{
17    /** @var bool */
18    protected $sendConfirmationEmail;
19
20    /** @var IConnectionProvider */
21    private $dbProvider;
22
23    /**
24     * @param IConnectionProvider $dbProvider
25     * @param array $params
26     *  - sendConfirmationEmail: (bool) send an email asking the user to confirm their email
27     *    address after a successful registration
28     */
29    public function __construct( IConnectionProvider $dbProvider, $params = [] ) {
30        if ( isset( $params['sendConfirmationEmail'] ) ) {
31            $this->sendConfirmationEmail = (bool)$params['sendConfirmationEmail'];
32        }
33        $this->dbProvider = $dbProvider;
34    }
35
36    protected function postInitSetup() {
37        $this->sendConfirmationEmail ??= $this->config->get( MainConfigNames::EnableEmail )
38                && $this->config->get( MainConfigNames::EmailAuthentication );
39    }
40
41    public function getAuthenticationRequests( $action, array $options ) {
42        return [];
43    }
44
45    public function beginSecondaryAuthentication( $user, array $reqs ) {
46        return AuthenticationResponse::newAbstain();
47    }
48
49    public function beginSecondaryAccountCreation( $user, $creator, array $reqs ) {
50        if (
51            $this->sendConfirmationEmail
52            && $user->getEmail()
53            && !$this->manager->getAuthenticationSessionData( 'no-email' )
54        ) {
55            // TODO show 'confirmemail_oncreate'/'confirmemail_sendfailed' message
56            $this->dbProvider->getPrimaryDatabase()->onTransactionCommitOrIdle(
57                function () use ( $user ) {
58                    $user = $user->getInstanceForUpdate();
59                    $status = $user->sendConfirmationMail();
60                    $user->saveSettings();
61                    if ( !$status->isGood() ) {
62                        $this->logger->warning( 'Could not send confirmation email: ' .
63                            $status->getWikiText( false, false, 'en' ) );
64                    }
65                },
66                __METHOD__
67            );
68        }
69
70        return AuthenticationResponse::newPass();
71    }
72}