Merge branch 'install_25_STABLE' of https://git.in.moodle.com/amosbot/moodle-install...
[moodle.git] / auth / email / auth.php
blob9ac59bdaa583bc930c8acd3d4c8abe72047ccbf1
1 <?php
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.
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/>.
17 /**
18 * Authentication Plugin: Email Authentication
20 * @author Martin Dougiamas
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22 * @package auth_email
25 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->libdir.'/authlib.php');
29 /**
30 * Email authentication plugin.
32 class auth_plugin_email extends auth_plugin_base {
34 /**
35 * Constructor.
37 function auth_plugin_email() {
38 $this->authtype = 'email';
39 $this->config = get_config('auth/email');
42 /**
43 * Returns true if the username and password work and false if they are
44 * wrong or don't exist.
46 * @param string $username The username
47 * @param string $password The password
48 * @return bool Authentication success or failure.
50 function user_login ($username, $password) {
51 global $CFG, $DB;
52 if ($user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
53 return validate_internal_user_password($user, $password);
55 return false;
58 /**
59 * Updates the user's password.
61 * called when the user password is updated.
63 * @param object $user User table object (with system magic quotes)
64 * @param string $newpassword Plaintext password (with system magic quotes)
65 * @return boolean result
68 function user_update_password($user, $newpassword) {
69 $user = get_complete_user_data('id', $user->id);
70 // This will also update the stored hash to the latest algorithm
71 // if the existing hash is using an out-of-date algorithm (or the
72 // legacy md5 algorithm).
73 return update_internal_user_password($user, $newpassword);
76 function can_signup() {
77 return true;
80 /**
81 * Sign up a new user ready for confirmation.
82 * Password is passed in plaintext.
84 * @param object $user new user object
85 * @param boolean $notify print notice with link and terminate
87 function user_signup($user, $notify=true) {
88 global $CFG, $DB;
89 require_once($CFG->dirroot.'/user/profile/lib.php');
91 $user->password = hash_internal_user_password($user->password);
93 $user->id = $DB->insert_record('user', $user);
95 /// Save any custom profile field information
96 profile_save_data($user);
98 $user = $DB->get_record('user', array('id'=>$user->id));
99 events_trigger('user_created', $user);
101 if (! send_confirmation_email($user)) {
102 print_error('auth_emailnoemail','auth_email');
105 if ($notify) {
106 global $CFG, $PAGE, $OUTPUT;
107 $emailconfirm = get_string('emailconfirm');
108 $PAGE->navbar->add($emailconfirm);
109 $PAGE->set_title($emailconfirm);
110 $PAGE->set_heading($PAGE->course->fullname);
111 echo $OUTPUT->header();
112 notice(get_string('emailconfirmsent', '', $user->email), "$CFG->wwwroot/index.php");
113 } else {
114 return true;
119 * Returns true if plugin allows confirming of new users.
121 * @return bool
123 function can_confirm() {
124 return true;
128 * Confirm the new user as registered.
130 * @param string $username
131 * @param string $confirmsecret
133 function user_confirm($username, $confirmsecret) {
134 global $DB;
135 $user = get_complete_user_data('username', $username);
137 if (!empty($user)) {
138 if ($user->confirmed) {
139 return AUTH_CONFIRM_ALREADY;
141 } else if ($user->auth != $this->authtype) {
142 return AUTH_CONFIRM_ERROR;
144 } else if ($user->secret == $confirmsecret) { // They have provided the secret key to get in
145 $DB->set_field("user", "confirmed", 1, array("id"=>$user->id));
146 if ($user->firstaccess == 0) {
147 $DB->set_field("user", "firstaccess", time(), array("id"=>$user->id));
149 return AUTH_CONFIRM_OK;
151 } else {
152 return AUTH_CONFIRM_ERROR;
156 function prevent_local_passwords() {
157 return false;
161 * Returns true if this authentication plugin is 'internal'.
163 * @return bool
165 function is_internal() {
166 return true;
170 * Returns true if this authentication plugin can change the user's
171 * password.
173 * @return bool
175 function can_change_password() {
176 return true;
180 * Returns the URL for changing the user's pw, or empty if the default can
181 * be used.
183 * @return moodle_url
185 function change_password_url() {
186 return null; // use default internal method
190 * Returns true if plugin allows resetting of internal password.
192 * @return bool
194 function can_reset_password() {
195 return true;
199 * Prints a form for configuring this authentication plugin.
201 * This function is called from admin/auth.php, and outputs a full page with
202 * a form for configuring this plugin.
204 * @param array $page An object containing all the data for this page.
206 function config_form($config, $err, $user_fields) {
207 include "config.html";
211 * Processes and stores configuration data for this authentication plugin.
213 function process_config($config) {
214 // set to defaults if undefined
215 if (!isset($config->recaptcha)) {
216 $config->recaptcha = false;
219 // save settings
220 set_config('recaptcha', $config->recaptcha, 'auth/email');
221 return true;
225 * Returns whether or not the captcha element is enabled, and the admin settings fulfil its requirements.
226 * @return bool
228 function is_captcha_enabled() {
229 global $CFG;
230 return isset($CFG->recaptchapublickey) && isset($CFG->recaptchaprivatekey) && get_config("auth/{$this->authtype}", 'recaptcha');