Merge "Use AuthManager on special pages"
[lhc/web/wiklou.git] / includes / auth / EmailNotificationSecondaryAuthenticationProvider.php
1 <?php
2
3 namespace MediaWiki\Auth;
4
5 use Config;
6 use StatusValue;
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 class EmailNotificationSecondaryAuthenticationProvider
15 extends AbstractSecondaryAuthenticationProvider
16 {
17 /** @var bool */
18 protected $sendConfirmationEmail;
19
20 /**
21 * @param array $params
22 * - sendConfirmationEmail: (bool) send an email asking the user to confirm their email
23 * address after a successful registration
24 */
25 public function __construct( $params = [] ) {
26 if ( isset( $params['sendConfirmationEmail'] ) ) {
27 $this->sendConfirmationEmail = (bool)$params['sendConfirmationEmail'];
28 }
29 }
30
31 public function setConfig( Config $config ) {
32 parent::setConfig( $config );
33
34 if ( $this->sendConfirmationEmail === null ) {
35 $this->sendConfirmationEmail = $this->config->get( 'EnableEmail' )
36 && $this->config->get( 'EmailAuthentication' );
37 }
38 }
39
40 public function getAuthenticationRequests( $action, array $options ) {
41 return [];
42 }
43
44 public function beginSecondaryAuthentication( $user, array $reqs ) {
45 return AuthenticationResponse::newAbstain();
46 }
47
48 public function beginSecondaryAccountCreation( $user, $creator, array $reqs ) {
49 if (
50 $this->sendConfirmationEmail
51 && $user->getEmail()
52 && !$this->manager->getAuthenticationSessionData( 'no-email' )
53 ) {
54 $status = $user->sendConfirmationMail();
55 $user->saveSettings();
56 if ( $status->isGood() ) {
57 // TODO show 'confirmemail_oncreate' success message
58 } else {
59 // TODO show 'confirmemail_sendfailed' error message
60 $this->logger->warning( 'Could not send confirmation email: ' .
61 $status->getWikiText( false, false, 'en' ) );
62 }
63 }
64
65 return AuthenticationResponse::newPass();
66 }
67 }