2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Authentication Plugin: Email Authentication
20 * @author Martin Dougiamas
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
25 defined('MOODLE_INTERNAL') ||
die();
27 require_once($CFG->libdir
.'/authlib.php');
30 * Email authentication plugin.
32 class auth_plugin_email
extends auth_plugin_base
{
37 public function __construct() {
38 $this->authtype
= 'email';
39 $this->config
= get_config('auth_email');
43 * Old syntax of class constructor. Deprecated in PHP7.
45 * @deprecated since Moodle 3.1
47 public function auth_plugin_email() {
48 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER
);
53 * Returns true if the username and password work and false if they are
54 * wrong or don't exist.
56 * @param string $username The username
57 * @param string $password The password
58 * @return bool Authentication success or failure.
60 function user_login ($username, $password) {
62 if ($user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id
))) {
63 return validate_internal_user_password($user, $password);
69 * Updates the user's password.
71 * called when the user password is updated.
73 * @param object $user User table object (with system magic quotes)
74 * @param string $newpassword Plaintext password (with system magic quotes)
75 * @return boolean result
78 function user_update_password($user, $newpassword) {
79 $user = get_complete_user_data('id', $user->id
);
80 // This will also update the stored hash to the latest algorithm
81 // if the existing hash is using an out-of-date algorithm (or the
82 // legacy md5 algorithm).
83 return update_internal_user_password($user, $newpassword);
86 function can_signup() {
91 * Sign up a new user ready for confirmation.
92 * Password is passed in plaintext.
94 * @param object $user new user object
95 * @param boolean $notify print notice with link and terminate
97 function user_signup($user, $notify=true) {
98 // Standard signup, without custom confirmatinurl.
99 return $this->user_signup_with_confirmation($user, $notify);
103 * Sign up a new user ready for confirmation.
105 * Password is passed in plaintext.
106 * A custom confirmationurl could be used.
108 * @param object $user new user object
109 * @param boolean $notify print notice with link and terminate
110 * @param string $confirmationurl user confirmation URL
111 * @return boolean true if everything well ok and $notify is set to true
112 * @throws moodle_exception
115 public function user_signup_with_confirmation($user, $notify=true, $confirmationurl = null) {
116 global $CFG, $DB, $SESSION;
117 require_once($CFG->dirroot
.'/user/profile/lib.php');
118 require_once($CFG->dirroot
.'/user/lib.php');
120 $plainpassword = $user->password
;
121 $user->password
= hash_internal_user_password($user->password
);
122 if (empty($user->calendartype
)) {
123 $user->calendartype
= $CFG->calendartype
;
126 $user->id
= user_create_user($user, false, false);
128 user_add_password_history($user->id
, $plainpassword);
130 // Save any custom profile field information.
131 profile_save_data($user);
133 // Save wantsurl against user's profile, so we can return them there upon confirmation.
134 if (!empty($SESSION->wantsurl
)) {
135 set_user_preference('auth_email_wantsurl', $SESSION->wantsurl
, $user);
139 \core\event\user_created
::create_from_userid($user->id
)->trigger();
141 if (! send_confirmation_email($user, $confirmationurl)) {
142 print_error('auth_emailnoemail', 'auth_email');
146 global $CFG, $PAGE, $OUTPUT;
147 $emailconfirm = get_string('emailconfirm');
148 $PAGE->navbar
->add($emailconfirm);
149 $PAGE->set_title($emailconfirm);
150 $PAGE->set_heading($PAGE->course
->fullname
);
151 echo $OUTPUT->header();
152 notice(get_string('emailconfirmsent', '', $user->email
), "$CFG->wwwroot/index.php");
159 * Returns true if plugin allows confirming of new users.
163 function can_confirm() {
168 * Confirm the new user as registered.
170 * @param string $username
171 * @param string $confirmsecret
173 function user_confirm($username, $confirmsecret) {
174 global $DB, $SESSION;
175 $user = get_complete_user_data('username', $username);
178 if ($user->auth
!= $this->authtype
) {
179 return AUTH_CONFIRM_ERROR
;
181 } else if ($user->secret
== $confirmsecret && $user->confirmed
) {
182 return AUTH_CONFIRM_ALREADY
;
184 } else if ($user->secret
== $confirmsecret) { // They have provided the secret key to get in
185 $DB->set_field("user", "confirmed", 1, array("id"=>$user->id
));
187 if ($wantsurl = get_user_preferences('auth_email_wantsurl', false, $user)) {
188 // Ensure user gets returned to page they were trying to access before signing up.
189 $SESSION->wantsurl
= $wantsurl;
190 unset_user_preference('auth_email_wantsurl', $user);
193 return AUTH_CONFIRM_OK
;
196 return AUTH_CONFIRM_ERROR
;
200 function prevent_local_passwords() {
205 * Returns true if this authentication plugin is 'internal'.
209 function is_internal() {
214 * Returns true if this authentication plugin can change the user's
219 function can_change_password() {
224 * Returns the URL for changing the user's pw, or empty if the default can
229 function change_password_url() {
230 return null; // use default internal method
234 * Returns true if plugin allows resetting of internal password.
238 function can_reset_password() {
243 * Returns true if plugin can be manually set.
247 function can_be_manually_set() {
252 * Returns whether or not the captcha element is enabled.
255 function is_captcha_enabled() {
256 return get_config("auth_{$this->authtype}", 'recaptcha');