From 472665ad4a4f2d8f5dd3df3f21567089cc29a508 Mon Sep 17 00:00:00 2001
From: Felipe Carasso <felipe@carassonet.org>
Date: Fri, 25 Dec 2020 21:13:19 -0500
Subject: [PATCH 1/3] chore(MDL-70381): turns resend confirmation
 plugin-dependent

---
 admin/user.php      | 26 +++++++++++++-------------
 auth/email/auth.php | 26 ++++++++++++++++++++++++++
 lib/authlib.php     | 20 ++++++++++++++++++++
 3 files changed, 59 insertions(+), 13 deletions(-)

diff --git a/admin/user.php b/admin/user.php
index a06b2edc047..203253ba41f 100644
--- a/admin/user.php
+++ b/admin/user.php
@@ -71,14 +71,10 @@
             print_error('alreadyconfirmed');
         }
 
-        $returnmsg = get_string('emailconfirmsentsuccess');
-        $messagetype = \core\output\notification::NOTIFY_SUCCESS;
-        if (!send_confirmation_email($user)) {
-            $returnmsg = get_string('emailconfirmsentfailure');
-            $messagetype = \core\output\notification::NOTIFY_ERROR;
-        }
+        $auth = get_auth_plugin($user->auth);
+
+        $auth->user_resend_confirmation($user->username);
 
-        redirect($returnurl, $returnmsg, null, $messagetype);
     } else if ($delete and confirm_sesskey()) {              // Delete a selected user, after confirmation
         require_capability('moodle/user:delete', $sitecontext);
 
@@ -383,12 +379,16 @@
                     $lastcolumn = "<span class=\"dimmed_text\">".get_string('confirm')."</span>";
                 }
 
-                $lastcolumn .= ' | ' . html_writer::link(new moodle_url($returnurl,
-                    [
-                        'resendemail' => $user->id,
-                        'sesskey' => sesskey()
-                    ]
-                ), $strresendemail);
+                $auth = get_auth_plugin($user->auth);
+
+                if ($auth->can_resend_confirmation()) {
+                    $lastcolumn .= ' | ' . html_writer::link(new moodle_url($returnurl,
+                        [
+                            'resendemail' => $user->id,
+                            'sesskey' => sesskey()
+                        ]
+                    ), $strresendemail);
+                }
             }
 
             if ($user->lastaccess) {
diff --git a/auth/email/auth.php b/auth/email/auth.php
index f207f030a22..71b19778d40 100644
--- a/auth/email/auth.php
+++ b/auth/email/auth.php
@@ -155,6 +155,32 @@ class auth_plugin_email extends auth_plugin_base {
         }
     }
 
+    /**
+     * Returns true if plugin allows resending confirmation of new users.
+     *
+     * @return bool
+     */
+    function can_resend_confirmation() {
+        return true;
+    }
+
+    /**
+     * Resend confirmation email for the new user.
+     *
+     * @param string $username
+     */
+    function user_resend_confirmation($username) {
+        $returnmsg = get_string('emailconfirmsentfailure');
+        $messagetype = \core\output\notification::NOTIFY_ERROR;
+        if ($user->secret == $confirmsecret) {   // They have provided the secret key to get in
+            if (!send_confirmation_email($user)) {
+                $returnmsg = get_string('emailconfirmsentsuccess');
+                $messagetype = \core\output\notification::NOTIFY_SUCCESS;
+            }
+        }
+        redirect($returnurl, $returnmsg, null, $messagetype);
+    }
+
     /**
      * Returns true if plugin allows confirming of new users.
      *
diff --git a/lib/authlib.php b/lib/authlib.php
index d1604e6a18a..80aa97d817a 100644
--- a/lib/authlib.php
+++ b/lib/authlib.php
@@ -338,6 +338,26 @@ class auth_plugin_base {
         print_error('mustbeoveride', 'debug', '', 'user_confirm()' );
     }
 
+    /**
+     * Returns true if plugin allows resending confirmation of new users.
+     *
+     * @return bool
+     */
+    function can_resend_confirmation() {
+        //override if needed
+        return false;
+    }
+
+    /**
+     * Resend user confirmation email.
+     *
+     * @param string $username
+     */
+    function user_resend_confirmation($username) {
+        //override when can confirm
+        print_error('mustbeoveride', 'debug', '', 'user_resend_confirmation()' );
+    }
+
     /**
      * Checks if user exists in external db
      *
-- 
2.17.1


From d3f1fbab2800284b624b89a302dd131157232202 Mon Sep 17 00:00:00 2001
From: Felipe Carasso <felipe@carassonet.org>
Date: Sat, 26 Dec 2020 13:48:15 -0500
Subject: [PATCH 2/3] chore(MDL-70381): adds check for
 auth->can_resend_confirmation to login/index.php

---
 login/index.php | 33 ++++++++++++++++++---------------
 1 file changed, 18 insertions(+), 15 deletions(-)

diff --git a/login/index.php b/login/index.php
index 0966e92d3d2..e62da259169 100644
--- a/login/index.php
+++ b/login/index.php
@@ -186,23 +186,26 @@ if ($frm and isset($frm->username)) {                             // Login WITH
             $PAGE->set_heading($site->fullname);
             echo $OUTPUT->header();
             echo $OUTPUT->heading(get_string("mustconfirm"));
-            if ($resendconfirmemail) {
-                if (!send_confirmation_email($user)) {
-                    echo $OUTPUT->notification(get_string('emailconfirmsentfailure'), \core\output\notification::NOTIFY_ERROR);
-                } else {
-                    echo $OUTPUT->notification(get_string('emailconfirmsentsuccess'), \core\output\notification::NOTIFY_SUCCESS);
+            $userauth = get_auth_plugin($USER->auth);
+            if ($userauth->can_resend_confirmation()) {
+                if ($resendconfirmemail) {
+                    if (!send_confirmation_email($user)) {
+                        echo $OUTPUT->notification(get_string('emailconfirmsentfailure'), \core\output\notification::NOTIFY_ERROR);
+                    } else {
+                        echo $OUTPUT->notification(get_string('emailconfirmsentsuccess'), \core\output\notification::NOTIFY_SUCCESS);
+                    }
                 }
+                echo $OUTPUT->box(get_string("emailconfirmsent", "", s($user->email)), "generalbox boxaligncenter");
+                $resendconfirmurl = new moodle_url('/login/index.php',
+                    [
+                        'username' => $frm->username,
+                        'password' => $frm->password,
+                        'resendconfirmemail' => true,
+                        'logintoken' => \core\session\manager::get_login_token()
+                    ]
+                );
+                echo $OUTPUT->single_button($resendconfirmurl, get_string('emailconfirmationresend'));
             }
-            echo $OUTPUT->box(get_string("emailconfirmsent", "", s($user->email)), "generalbox boxaligncenter");
-            $resendconfirmurl = new moodle_url('/login/index.php',
-                [
-                    'username' => $frm->username,
-                    'password' => $frm->password,
-                    'resendconfirmemail' => true,
-                    'logintoken' => \core\session\manager::get_login_token()
-                ]
-            );
-            echo $OUTPUT->single_button($resendconfirmurl, get_string('emailconfirmationresend'));
             echo $OUTPUT->footer();
             die;
         }
-- 
2.17.1


From bc5a0e0af93830b0cf5eee5921f909372fd70aaa Mon Sep 17 00:00:00 2001
From: Felipe Carasso <felipe@carassonet.org>
Date: Sun, 27 Dec 2020 21:05:26 -0500
Subject: [PATCH 3/3] chore(MDL-70381): checks auth->can_resend_confirmation()
 before allowing it on login page

---
 lib/datalib.php | 2 +-
 login/index.php | 7 +++++--
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/lib/datalib.php b/lib/datalib.php
index 2ca8e54d80c..e2f0c0e2909 100644
--- a/lib/datalib.php
+++ b/lib/datalib.php
@@ -522,7 +522,7 @@ function get_users_listing($sort='lastaccess', $dir='ASC', $page=0, $recordsperp
     $extrafields = "$extrafields, $namefields";
 
     // warning: will return UNCONFIRMED USERS
-    return $DB->get_records_sql("SELECT id, username, email, city, country, lastaccess, confirmed, mnethostid, suspended $extrafields
+    return $DB->get_records_sql("SELECT id, username, email, city, country, lastaccess, confirmed, mnethostid, suspended, auth $extrafields
                                    FROM {user}
                                   WHERE $select
                                   $sort", $params, $page, $recordsperpage);
diff --git a/login/index.php b/login/index.php
index e62da259169..441cb47c93b 100644
--- a/login/index.php
+++ b/login/index.php
@@ -186,7 +186,8 @@ if ($frm and isset($frm->username)) {                             // Login WITH
             $PAGE->set_heading($site->fullname);
             echo $OUTPUT->header();
             echo $OUTPUT->heading(get_string("mustconfirm"));
-            $userauth = get_auth_plugin($USER->auth);
+            $userauth = get_auth_plugin($user->auth);
+            $resendmsg = $OUTPUT->box(get_string("emailconfirmsent", "", s($user->email)), "generalbox boxaligncenter");
             if ($userauth->can_resend_confirmation()) {
                 if ($resendconfirmemail) {
                     if (!send_confirmation_email($user)) {
@@ -195,7 +196,7 @@ if ($frm and isset($frm->username)) {                             // Login WITH
                         echo $OUTPUT->notification(get_string('emailconfirmsentsuccess'), \core\output\notification::NOTIFY_SUCCESS);
                     }
                 }
-                echo $OUTPUT->box(get_string("emailconfirmsent", "", s($user->email)), "generalbox boxaligncenter");
+                echo $resendmsg;
                 $resendconfirmurl = new moodle_url('/login/index.php',
                     [
                         'username' => $frm->username,
@@ -205,6 +206,8 @@ if ($frm and isset($frm->username)) {                             // Login WITH
                     ]
                 );
                 echo $OUTPUT->single_button($resendconfirmurl, get_string('emailconfirmationresend'));
+            } else {
+                echo $resendmsg;
             }
             echo $OUTPUT->footer();
             die;
-- 
2.17.1

