on demand release 2.5beta+
[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);
ec2d8ceb
SC
62 // This will also update the stored hash to the latest algorithm
63 // if the existing hash is using an out-of-date algorithm (or the
64 // legacy md5 algorithm).
b9ddb2d5 65 return update_internal_user_password($user, $newpassword);
66 }
67
6bc1e5d5 68 function can_signup() {
69 return true;
70 }
71
430759a5 72 /**
b9ddb2d5 73 * Sign up a new user ready for confirmation.
dd0feda5 74 * Password is passed in plaintext.
75 *
5d910388 76 * @param object $user new user object
dd0feda5 77 * @param boolean $notify print notice with link and terminate
b9ddb2d5 78 */
6b8ad965 79 function user_signup($user, $notify=true) {
5d910388 80 global $CFG, $DB;
831d450e 81 require_once($CFG->dirroot.'/user/profile/lib.php');
5117d598 82
dd0feda5 83 $user->password = hash_internal_user_password($user->password);
84
a9637e7d 85 $user->id = $DB->insert_record('user', $user);
5117d598 86
831d450e 87 /// Save any custom profile field information
88 profile_save_data($user);
89
14482ff9 90 $user = $DB->get_record('user', array('id'=>$user->id));
2942a5cd 91 events_trigger('user_created', $user);
92
b9ddb2d5 93 if (! send_confirmation_email($user)) {
2b06294b 94 print_error('auth_emailnoemail','auth_email');
b9ddb2d5 95 }
96
97 if ($notify) {
07ed083e 98 global $CFG, $PAGE, $OUTPUT;
b9ddb2d5 99 $emailconfirm = get_string('emailconfirm');
cfc5b79b 100 $PAGE->navbar->add($emailconfirm);
101 $PAGE->set_title($emailconfirm);
c93fdc7b 102 $PAGE->set_heading($PAGE->course->fullname);
cfc5b79b 103 echo $OUTPUT->header();
b9ddb2d5 104 notice(get_string('emailconfirmsent', '', $user->email), "$CFG->wwwroot/index.php");
dd0feda5 105 } else {
106 return true;
139ebfdb 107 }
b9ddb2d5 108 }
109
6bc1e5d5 110 /**
111 * Returns true if plugin allows confirming of new users.
112 *
113 * @return bool
114 */
115 function can_confirm() {
116 return true;
117 }
118
430759a5 119 /**
b9ddb2d5 120 * Confirm the new user as registered.
dd0feda5 121 *
b9a66360 122 * @param string $username
123 * @param string $confirmsecret
b9ddb2d5 124 */
125 function user_confirm($username, $confirmsecret) {
b9a66360 126 global $DB;
b9ddb2d5 127 $user = get_complete_user_data('username', $username);
128
129 if (!empty($user)) {
130 if ($user->confirmed) {
131 return AUTH_CONFIRM_ALREADY;
dd0feda5 132
351d55b3 133 } else if ($user->auth != $this->authtype) {
dd0feda5 134 return AUTH_CONFIRM_ERROR;
135
b9a66360 136 } else if ($user->secret == $confirmsecret) { // They have provided the secret key to get in
f685e830 137 $DB->set_field("user", "confirmed", 1, array("id"=>$user->id));
fcb46048
PS
138 if ($user->firstaccess == 0) {
139 $DB->set_field("user", "firstaccess", time(), array("id"=>$user->id));
140 }
b9ddb2d5 141 return AUTH_CONFIRM_OK;
142 }
dd0feda5 143 } else {
144 return AUTH_CONFIRM_ERROR;
b9ddb2d5 145 }
146 }
147
edb5da83
PS
148 function prevent_local_passwords() {
149 return false;
150 }
151
b9ddb2d5 152 /**
153 * Returns true if this authentication plugin is 'internal'.
154 *
139ebfdb 155 * @return bool
b9ddb2d5 156 */
157 function is_internal() {
158 return true;
159 }
139ebfdb 160
b9ddb2d5 161 /**
162 * Returns true if this authentication plugin can change the user's
163 * password.
164 *
139ebfdb 165 * @return bool
b9ddb2d5 166 */
167 function can_change_password() {
168 return true;
169 }
139ebfdb 170
b9ddb2d5 171 /**
430759a5 172 * Returns the URL for changing the user's pw, or empty if the default can
b9ddb2d5 173 * be used.
174 *
99f9f85f 175 * @return moodle_url
b9ddb2d5 176 */
177 function change_password_url() {
99f9f85f 178 return null; // use default internal method
430759a5 179 }
180
181 /**
182 * Returns true if plugin allows resetting of internal password.
183 *
184 * @return bool
185 */
186 function can_reset_password() {
187 return true;
b9ddb2d5 188 }
139ebfdb 189
b9ddb2d5 190 /**
191 * Prints a form for configuring this authentication plugin.
192 *
193 * This function is called from admin/auth.php, and outputs a full page with
194 * a form for configuring this plugin.
195 *
196 * @param array $page An object containing all the data for this page.
197 */
139ebfdb 198 function config_form($config, $err, $user_fields) {
b9ddb2d5 199 include "config.html";
200 }
201
202 /**
203 * Processes and stores configuration data for this authentication plugin.
204 */
205 function process_config($config) {
5d6308d4 206 // set to defaults if undefined
5117d598
PS
207 if (!isset($config->recaptcha)) {
208 $config->recaptcha = false;
5d6308d4 209 }
5117d598 210
5d6308d4 211 // save settings
212 set_config('recaptcha', $config->recaptcha, 'auth/email');
b9ddb2d5 213 return true;
214 }
5117d598 215
9b5f87d2 216 /**
217 * Returns whether or not the captcha element is enabled, and the admin settings fulfil its requirements.
218 * @return bool
219 */
220 function is_captcha_enabled() {
221 global $CFG;
222 return isset($CFG->recaptchapublickey) && isset($CFG->recaptchaprivatekey) && get_config("auth/{$this->authtype}", 'recaptcha');
223 }
139ebfdb 224
b9ddb2d5 225}
226
5117d598 227