Merge branch 'MDL-25881' of git://git.luns.net.uk/moodle
[moodle.git] / auth / email / auth.php
CommitLineData
b9ddb2d5 1<?php
2
3/**
4 * @author Martin Dougiamas
5 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
6 * @package moodle multiauth
7 *
8 * Authentication Plugin: Email Authentication
9 *
10 * Standard authentication function.
11 *
12 * 2006-08-28 File created.
13 */
14
139ebfdb 15if (!defined('MOODLE_INTERNAL')) {
16 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
17}
b9ddb2d5 18
6bc1e5d5 19require_once($CFG->libdir.'/authlib.php');
b9ddb2d5 20
21/**
22 * Email authentication plugin.
23 */
6bc1e5d5 24class auth_plugin_email extends auth_plugin_base {
b9ddb2d5 25
26 /**
27 * Constructor.
28 */
29 function auth_plugin_email() {
6bc1e5d5 30 $this->authtype = 'email';
b9ddb2d5 31 $this->config = get_config('auth/email');
32 }
33
34 /**
35 * Returns true if the username and password work and false if they are
36 * wrong or don't exist.
37 *
38 * @param string $username The username
39 * @param string $password The password
139ebfdb 40 * @return bool Authentication success or failure.
b9ddb2d5 41 */
42 function user_login ($username, $password) {
be544ec3 43 global $CFG, $DB;
44 if ($user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
b9ddb2d5 45 return validate_internal_user_password($user, $password);
46 }
47 return false;
48 }
49
fb5c7739 50 /**
b9ddb2d5 51 * Updates the user's password.
52 *
53 * called when the user password is updated.
54 *
c57dcb62 55 * @param object $user User table object (with system magic quotes)
56 * @param string $newpassword Plaintext password (with system magic quotes)
b9ddb2d5 57 * @return boolean result
58 *
59 */
da249a30 60 function user_update_password($user, $newpassword) {
61 $user = get_complete_user_data('id', $user->id);
b9ddb2d5 62 return update_internal_user_password($user, $newpassword);
63 }
64
6bc1e5d5 65 function can_signup() {
66 return true;
67 }
68
430759a5 69 /**
b9ddb2d5 70 * Sign up a new user ready for confirmation.
dd0feda5 71 * Password is passed in plaintext.
72 *
5d910388 73 * @param object $user new user object
dd0feda5 74 * @param boolean $notify print notice with link and terminate
b9ddb2d5 75 */
6b8ad965 76 function user_signup($user, $notify=true) {
5d910388 77 global $CFG, $DB;
831d450e 78 require_once($CFG->dirroot.'/user/profile/lib.php');
5117d598 79
dd0feda5 80 $user->password = hash_internal_user_password($user->password);
81
a9637e7d 82 $user->id = $DB->insert_record('user', $user);
5117d598 83
831d450e 84 /// Save any custom profile field information
85 profile_save_data($user);
86
14482ff9 87 $user = $DB->get_record('user', array('id'=>$user->id));
2942a5cd 88 events_trigger('user_created', $user);
89
b9ddb2d5 90 if (! send_confirmation_email($user)) {
2b06294b 91 print_error('auth_emailnoemail','auth_email');
b9ddb2d5 92 }
93
94 if ($notify) {
07ed083e 95 global $CFG, $PAGE, $OUTPUT;
b9ddb2d5 96 $emailconfirm = get_string('emailconfirm');
cfc5b79b 97 $PAGE->navbar->add($emailconfirm);
98 $PAGE->set_title($emailconfirm);
c93fdc7b 99 $PAGE->set_heading($PAGE->course->fullname);
cfc5b79b 100 echo $OUTPUT->header();
b9ddb2d5 101 notice(get_string('emailconfirmsent', '', $user->email), "$CFG->wwwroot/index.php");
dd0feda5 102 } else {
103 return true;
139ebfdb 104 }
b9ddb2d5 105 }
106
6bc1e5d5 107 /**
108 * Returns true if plugin allows confirming of new users.
109 *
110 * @return bool
111 */
112 function can_confirm() {
113 return true;
114 }
115
430759a5 116 /**
b9ddb2d5 117 * Confirm the new user as registered.
dd0feda5 118 *
b9a66360 119 * @param string $username
120 * @param string $confirmsecret
b9ddb2d5 121 */
122 function user_confirm($username, $confirmsecret) {
b9a66360 123 global $DB;
b9ddb2d5 124 $user = get_complete_user_data('username', $username);
125
126 if (!empty($user)) {
127 if ($user->confirmed) {
128 return AUTH_CONFIRM_ALREADY;
dd0feda5 129
130 } else if ($user->auth != 'email') {
131 return AUTH_CONFIRM_ERROR;
132
b9a66360 133 } else if ($user->secret == $confirmsecret) { // They have provided the secret key to get in
f685e830
PS
134 $DB->set_field("user", "confirmed", 1, array("id"=>$user->id));
135 $DB->set_field("user", "firstaccess", time(), array("id"=>$user->id));
b9ddb2d5 136 return AUTH_CONFIRM_OK;
137 }
dd0feda5 138 } else {
139 return AUTH_CONFIRM_ERROR;
b9ddb2d5 140 }
141 }
142
edb5da83
PS
143 function prevent_local_passwords() {
144 return false;
145 }
146
b9ddb2d5 147 /**
148 * Returns true if this authentication plugin is 'internal'.
149 *
139ebfdb 150 * @return bool
b9ddb2d5 151 */
152 function is_internal() {
153 return true;
154 }
139ebfdb 155
b9ddb2d5 156 /**
157 * Returns true if this authentication plugin can change the user's
158 * password.
159 *
139ebfdb 160 * @return bool
b9ddb2d5 161 */
162 function can_change_password() {
163 return true;
164 }
139ebfdb 165
b9ddb2d5 166 /**
430759a5 167 * Returns the URL for changing the user's pw, or empty if the default can
b9ddb2d5 168 * be used.
169 *
99f9f85f 170 * @return moodle_url
b9ddb2d5 171 */
172 function change_password_url() {
99f9f85f 173 return null; // use default internal method
430759a5 174 }
175
176 /**
177 * Returns true if plugin allows resetting of internal password.
178 *
179 * @return bool
180 */
181 function can_reset_password() {
182 return true;
b9ddb2d5 183 }
139ebfdb 184
b9ddb2d5 185 /**
186 * Prints a form for configuring this authentication plugin.
187 *
188 * This function is called from admin/auth.php, and outputs a full page with
189 * a form for configuring this plugin.
190 *
191 * @param array $page An object containing all the data for this page.
192 */
139ebfdb 193 function config_form($config, $err, $user_fields) {
b9ddb2d5 194 include "config.html";
195 }
196
197 /**
198 * Processes and stores configuration data for this authentication plugin.
199 */
200 function process_config($config) {
5d6308d4 201 // set to defaults if undefined
5117d598
PS
202 if (!isset($config->recaptcha)) {
203 $config->recaptcha = false;
5d6308d4 204 }
5117d598 205
5d6308d4 206 // save settings
207 set_config('recaptcha', $config->recaptcha, 'auth/email');
b9ddb2d5 208 return true;
209 }
5117d598 210
9b5f87d2 211 /**
212 * Returns whether or not the captcha element is enabled, and the admin settings fulfil its requirements.
213 * @return bool
214 */
215 function is_captcha_enabled() {
216 global $CFG;
217 return isset($CFG->recaptchapublickey) && isset($CFG->recaptchaprivatekey) && get_config("auth/{$this->authtype}", 'recaptcha');
218 }
139ebfdb 219
b9ddb2d5 220}
221
5117d598 222