Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
EmailableUser
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 newFromName
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
 sendConfirmAndMigrateMail
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Extension\CentralAuth\User;
22
23use MediaWiki\MediaWikiServices;
24use MediaWiki\Status\Status;
25use MediaWiki\Title\Title;
26use MediaWiki\User\User;
27use MediaWiki\User\UserNameUtils;
28
29class EmailableUser extends User {
30
31    /**
32     * Static factory method for creation from username.
33     *
34     * @param string $name Username, validated by Title::newFromText()
35     * @param string|bool $validate Type of validation to use
36     *   Takes the same parameters as UserNameUtils::getCanonical(),
37     *   except that true is accepted as an alias for RIGOR_VALID
38     *   Use of UserNameUtils' class public constants RIGOR_* is preferred
39     *   - RIGOR_NONE        No validation
40     *   - RIGOR_VALID       Valid for batch processes
41     *   - RIGOR_USABLE      Valid for batch processes and login
42     *   - RIGOR_CREATABLE   Valid for batch processes, login and account creation
43     *
44     * @return EmailableUser|false EmailableUser object, or false if the
45     *    username is invalid (e.g. if it contains illegal characters or is an IP address).
46     *    If the username is not present in the database, the result will be a EmailableUser
47     *    object with a name, zero user ID and default settings.
48     */
49    public static function newFromName( $name, $validate = UserNameUtils::RIGOR_VALID ) {
50        $userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils();
51        if ( $validate === true ) {
52            $validate = UserNameUtils::RIGOR_VALID;
53        }
54        $name = $userNameUtils->getCanonical( $name, $validate );
55        if ( $name === false ) {
56            return false;
57        } else {
58            # Create unloaded user object
59            $u = new EmailableUser;
60            $u->mName = $name;
61            $u->mFrom = 'name';
62            $u->setItemLoaded( 'name' );
63            return $u;
64        }
65    }
66
67    /**
68     * Generate a new e-mail confirmation token and send a confirmation/invalidation
69     * mail to the user's given address.
70     *
71     * @return Status
72     */
73    public function sendConfirmAndMigrateMail() {
74        global $wgLang;
75        // 14 days
76        $tokenLife = 14 * 24 * 60 * 60;
77
78        $token = $this->confirmationToken( $expiration );
79
80        // we want this token to last a little bit longer since we are cold-emailing
81        // users and we really want as many responses as possible
82        $now = time();
83        $expires = $now + $tokenLife;
84        $expiration = wfTimestamp( TS_MW, $expires );
85        $this->mEmailTokenExpires = $expiration;
86
87        if ( $this->isEmailConfirmed() ) {
88            // Hack to bypass localization of 'Special:'
89            // @see User::getTokenUrl
90            $mergeAccountUrl = Title::makeTitle( NS_MAIN, 'Special:MergeAccount' )->getCanonicalURL();
91        } else {
92            // create a "token url" for MergeAccount since we have added email
93            // confirmation there
94            $mergeAccountUrl = $this->getTokenUrl( 'MergeAccount', $token );
95        }
96
97        $invalidateURL = $this->invalidationTokenUrl( $token );
98        $this->saveSettings();
99
100        return $this->sendMail(
101            wfMessage( 'centralauth-finishglobaliseemail_subject' )->text(),
102            wfMessage( "centralauth-finishglobaliseemail_body",
103                $this->getRequest()->getIP(),
104                $this->getName(),
105                $mergeAccountUrl,
106                $wgLang->timeanddate( $expiration, false ),
107                $invalidateURL,
108                $wgLang->date( $expiration, false ),
109                $wgLang->time( $expiration, false )
110            )->text()
111        );
112    }
113}