Commit 9fccfe9f authored by Robert Lyon's avatar Robert Lyon Committed by Doris Tam
Browse files

Bug 1966138: Better seting of email profile field



Normally when we use set_profile_field() to set an email it will make
the existing primary email be renamed to the new one.
But if you already have a secondary email with the same value then
things go wrong as it tries to set your existing primary to the new
value rather than just make the secondary one the primary one

Change-Id: Ia82c24c7f8e18eaac8c5c7cfd648c6a4b50ec3c0
Signed-off-by: default avatarRobert Lyon <robertl@catalyst.net.nz>
parent 1b211b9c
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -825,5 +825,26 @@ function xmldb_core_upgrade($oldversion=0) {
            )");
    }

    if ($oldversion < 2022041300) {
        if ($records = get_records_sql_array("
            SELECT a.id, ae.email FROM {artefact} a
            JOIN {artefact_internal_profile_email} ae ON ae.artefact = a.id
            WHERE a.artefacttype = ?
            AND a.title != ae.email", array('email'))) {
            log_debug('Need to fix up email info drift');
            $count = 0;
            $limit = 100;
            $total = count($records);
            foreach ($records as $record) {
                execute_sql("UPDATE artefact SET title = ? WHERE id = ?", array($record->email, $record->id));
                $count++;
                if (($count % $limit) == 0 || $count == $total) {
                    log_debug("$count/$total");
                    set_time_limit(30);
                }
            }
        }
    }

    return $status;
}
+21 −10
Original line number Diff line number Diff line
@@ -552,6 +552,16 @@ function set_profile_field($userid, $field, $value, $new = FALSE) {
    // specified one
    if ($field == 'email') {
        if (!$new) {
            $is_already_secondary = get_field_sql("
                SELECT a.id FROM {artefact} a
                JOIN {artefact_internal_profile_email} ae ON ae.artefact = a.id
                WHERE a.owner = ? AND ae.principal != 1 AND ae.email = ?", array($userid, $value));
            if ($is_already_secondary) {
                // Secondary email matches the value so lets set that one as primary
                set_user_primary_email($userid, $value);
                return;
            }
            else {
                try {
                    $email = artefact_instance_from_type('email', $userid);
                }
@@ -559,6 +569,7 @@ function set_profile_field($userid, $field, $value, $new = FALSE) {
                    // We'll create a new artefact then.
                }
            }
        }
        if (!isset($email)) {
            $email = new ArtefactTypeEmail(0, null, TRUE);
            $email->set('owner', $userid);
@@ -607,8 +618,9 @@ function set_profile_field($userid, $field, $value, $new = FALSE) {
 *
 * @param int     $userid   The user ID
 * @param string  $newemail The new valid email address
 * @param boolean $ignore   Ignore the $user->email check
 */
function set_user_primary_email($userid, $newemail) {
function set_user_primary_email($userid, $newemail, $ignore=false) {
    safe_require('artefact', 'internal');

    $user = new User();
@@ -616,8 +628,8 @@ function set_user_primary_email($userid, $newemail) {

    db_begin();
    // Update user's primary email address
    if ($newemail !== $user->email) {
        // Set the current email address to be secondary
    if ($newemail !== $user->email || $ignore) {
        // Set the current primary email address to be secondary
        update_record(
            'artefact_internal_profile_email',
            (object)array(
@@ -625,7 +637,6 @@ function set_user_primary_email($userid, $newemail) {
            ),
            (object)array(
                'owner' => $user->id,
                'email' => $user->email,
            )
        );
        // If the new primary email address is to be verified, remove it
+1 −1
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@ $config = new stdClass();
// See https://wiki.mahara.org/wiki/Developer_Area/Version_Numbering_Policy
// For upgrades on dev branches, increment the version by one. On main, use the date.

$config->version = 2022032200;
$config->version = 2022041300;
$config->series = '22.10';
$config->release = '22.10dev';
$config->minupgradefrom = 2020013006;