weekly release 3.5dev
[moodle.git] / auth / email / auth.php
CommitLineData
b9ddb2d5 1<?php
a2f10958
PS
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
b9ddb2d5 16
17/**
b9ddb2d5 18 * Authentication Plugin: Email Authentication
19 *
a2f10958
PS
20 * @author Martin Dougiamas
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22 * @package auth_email
b9ddb2d5 23 */
24
60d7078a 25defined('MOODLE_INTERNAL') || die();
b9ddb2d5 26
6bc1e5d5 27require_once($CFG->libdir.'/authlib.php');
b9ddb2d5 28
29/**
30 * Email authentication plugin.
31 */
6bc1e5d5 32class auth_plugin_email extends auth_plugin_base {
b9ddb2d5 33
34 /**
35 * Constructor.
36 */
4a89e83b 37 public function __construct() {
6bc1e5d5 38 $this->authtype = 'email';
037273d8 39 $this->config = get_config('auth_email');
b9ddb2d5 40 }
41
4a89e83b
MG
42 /**
43 * Old syntax of class constructor. Deprecated in PHP7.
44 *
45 * @deprecated since Moodle 3.1
46 */
47 public function auth_plugin_email() {
48 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
49 self::__construct();
50 }
51
b9ddb2d5 52 /**
53 * Returns true if the username and password work and false if they are
54 * wrong or don't exist.
55 *
56 * @param string $username The username
57 * @param string $password The password
139ebfdb 58 * @return bool Authentication success or failure.
b9ddb2d5 59 */
60 function user_login ($username, $password) {
be544ec3 61 global $CFG, $DB;
62 if ($user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
b9ddb2d5 63 return validate_internal_user_password($user, $password);
64 }
65 return false;
66 }
67
fb5c7739 68 /**
b9ddb2d5 69 * Updates the user's password.
70 *
71 * called when the user password is updated.
72 *
c57dcb62 73 * @param object $user User table object (with system magic quotes)
74 * @param string $newpassword Plaintext password (with system magic quotes)
b9ddb2d5 75 * @return boolean result
76 *
77 */
da249a30 78 function user_update_password($user, $newpassword) {
79 $user = get_complete_user_data('id', $user->id);
ec2d8ceb
SC
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).
b9ddb2d5 83 return update_internal_user_password($user, $newpassword);
84 }
85
6bc1e5d5 86 function can_signup() {
87 return true;
88 }
89
430759a5 90 /**
b9ddb2d5 91 * Sign up a new user ready for confirmation.
dd0feda5 92 * Password is passed in plaintext.
93 *
5d910388 94 * @param object $user new user object
dd0feda5 95 * @param boolean $notify print notice with link and terminate
b9ddb2d5 96 */
6b8ad965 97 function user_signup($user, $notify=true) {
d6a25bc4
JL
98 // Standard signup, without custom confirmatinurl.
99 return $this->user_signup_with_confirmation($user, $notify);
100 }
101
102 /**
103 * Sign up a new user ready for confirmation.
104 *
105 * Password is passed in plaintext.
106 * A custom confirmationurl could be used.
107 *
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
113 * @since Moodle 3.2
114 */
115 public function user_signup_with_confirmation($user, $notify=true, $confirmationurl = null) {
5d910388 116 global $CFG, $DB;
831d450e 117 require_once($CFG->dirroot.'/user/profile/lib.php');
bb78e249 118 require_once($CFG->dirroot.'/user/lib.php');
5117d598 119
1d658535 120 $plainpassword = $user->password;
dd0feda5 121 $user->password = hash_internal_user_password($user->password);
8bf0f207
MN
122 if (empty($user->calendartype)) {
123 $user->calendartype = $CFG->calendartype;
124 }
dd0feda5 125
9363073b 126 $user->id = user_create_user($user, false, false);
5117d598 127
1d658535
PS
128 user_add_password_history($user->id, $plainpassword);
129
bb78e249 130 // Save any custom profile field information.
831d450e 131 profile_save_data($user);
132
9363073b
RT
133 // Trigger event.
134 \core\event\user_created::create_from_userid($user->id)->trigger();
135
d6a25bc4
JL
136 if (! send_confirmation_email($user, $confirmationurl)) {
137 print_error('auth_emailnoemail', 'auth_email');
b9ddb2d5 138 }
139
140 if ($notify) {
07ed083e 141 global $CFG, $PAGE, $OUTPUT;
b9ddb2d5 142 $emailconfirm = get_string('emailconfirm');
cfc5b79b 143 $PAGE->navbar->add($emailconfirm);
144 $PAGE->set_title($emailconfirm);
c93fdc7b 145 $PAGE->set_heading($PAGE->course->fullname);
cfc5b79b 146 echo $OUTPUT->header();
b9ddb2d5 147 notice(get_string('emailconfirmsent', '', $user->email), "$CFG->wwwroot/index.php");
dd0feda5 148 } else {
149 return true;
139ebfdb 150 }
b9ddb2d5 151 }
152
6bc1e5d5 153 /**
154 * Returns true if plugin allows confirming of new users.
155 *
156 * @return bool
157 */
158 function can_confirm() {
159 return true;
160 }
161
430759a5 162 /**
b9ddb2d5 163 * Confirm the new user as registered.
dd0feda5 164 *
b9a66360 165 * @param string $username
166 * @param string $confirmsecret
b9ddb2d5 167 */
168 function user_confirm($username, $confirmsecret) {
b9a66360 169 global $DB;
b9ddb2d5 170 $user = get_complete_user_data('username', $username);
171
172 if (!empty($user)) {
4f8b6d56 173 if ($user->auth != $this->authtype) {
dd0feda5 174 return AUTH_CONFIRM_ERROR;
175
4f8b6d56
MG
176 } else if ($user->secret == $confirmsecret && $user->confirmed) {
177 return AUTH_CONFIRM_ALREADY;
178
b9a66360 179 } else if ($user->secret == $confirmsecret) { // They have provided the secret key to get in
f685e830 180 $DB->set_field("user", "confirmed", 1, array("id"=>$user->id));
b9ddb2d5 181 return AUTH_CONFIRM_OK;
182 }
dd0feda5 183 } else {
184 return AUTH_CONFIRM_ERROR;
b9ddb2d5 185 }
186 }
187
edb5da83
PS
188 function prevent_local_passwords() {
189 return false;
190 }
191
b9ddb2d5 192 /**
193 * Returns true if this authentication plugin is 'internal'.
194 *
139ebfdb 195 * @return bool
b9ddb2d5 196 */
197 function is_internal() {
198 return true;
199 }
139ebfdb 200
b9ddb2d5 201 /**
202 * Returns true if this authentication plugin can change the user's
203 * password.
204 *
139ebfdb 205 * @return bool
b9ddb2d5 206 */
207 function can_change_password() {
208 return true;
209 }
139ebfdb 210
b9ddb2d5 211 /**
430759a5 212 * Returns the URL for changing the user's pw, or empty if the default can
b9ddb2d5 213 * be used.
214 *
99f9f85f 215 * @return moodle_url
b9ddb2d5 216 */
217 function change_password_url() {
99f9f85f 218 return null; // use default internal method
430759a5 219 }
220
221 /**
222 * Returns true if plugin allows resetting of internal password.
223 *
224 * @return bool
225 */
226 function can_reset_password() {
227 return true;
b9ddb2d5 228 }
139ebfdb 229
9b29f686
MN
230 /**
231 * Returns true if plugin can be manually set.
232 *
233 * @return bool
234 */
235 function can_be_manually_set() {
236 return true;
237 }
238
9b5f87d2 239 /**
0f7f3002 240 * Returns whether or not the captcha element is enabled.
9b5f87d2 241 * @return bool
242 */
243 function is_captcha_enabled() {
037273d8 244 return get_config("auth_{$this->authtype}", 'recaptcha');
9b5f87d2 245 }
139ebfdb 246
b9ddb2d5 247}
248
5117d598 249