MDL-35769 Course formats: Added function format_base::default_blocks() to replace...
[moodle.git] / auth / email / auth.php
blobe50c09e44ee87fad41b4efa195de4261621f55cb
1 <?php
3 /**
4 * @author Martin Dougiamas
5 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
6 * @package moodle multiauth
8 * Authentication Plugin: Email Authentication
10 * Standard authentication function.
12 * 2006-08-28 File created.
15 if (!defined('MOODLE_INTERNAL')) {
16 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
19 require_once($CFG->libdir.'/authlib.php');
21 /**
22 * Email authentication plugin.
24 class auth_plugin_email extends auth_plugin_base {
26 /**
27 * Constructor.
29 function auth_plugin_email() {
30 $this->authtype = 'email';
31 $this->config = get_config('auth/email');
34 /**
35 * Returns true if the username and password work and false if they are
36 * wrong or don't exist.
38 * @param string $username The username
39 * @param string $password The password
40 * @return bool Authentication success or failure.
42 function user_login ($username, $password) {
43 global $CFG, $DB;
44 if ($user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
45 return validate_internal_user_password($user, $password);
47 return false;
50 /**
51 * Updates the user's password.
53 * called when the user password is updated.
55 * @param object $user User table object (with system magic quotes)
56 * @param string $newpassword Plaintext password (with system magic quotes)
57 * @return boolean result
60 function user_update_password($user, $newpassword) {
61 $user = get_complete_user_data('id', $user->id);
62 return update_internal_user_password($user, $newpassword);
65 function can_signup() {
66 return true;
69 /**
70 * Sign up a new user ready for confirmation.
71 * Password is passed in plaintext.
73 * @param object $user new user object
74 * @param boolean $notify print notice with link and terminate
76 function user_signup($user, $notify=true) {
77 global $CFG, $DB;
78 require_once($CFG->dirroot.'/user/profile/lib.php');
80 $user->password = hash_internal_user_password($user->password);
82 $user->id = $DB->insert_record('user', $user);
84 /// Save any custom profile field information
85 profile_save_data($user);
87 $user = $DB->get_record('user', array('id'=>$user->id));
88 events_trigger('user_created', $user);
90 if (! send_confirmation_email($user)) {
91 print_error('auth_emailnoemail','auth_email');
94 if ($notify) {
95 global $CFG, $PAGE, $OUTPUT;
96 $emailconfirm = get_string('emailconfirm');
97 $PAGE->navbar->add($emailconfirm);
98 $PAGE->set_title($emailconfirm);
99 $PAGE->set_heading($PAGE->course->fullname);
100 echo $OUTPUT->header();
101 notice(get_string('emailconfirmsent', '', $user->email), "$CFG->wwwroot/index.php");
102 } else {
103 return true;
108 * Returns true if plugin allows confirming of new users.
110 * @return bool
112 function can_confirm() {
113 return true;
117 * Confirm the new user as registered.
119 * @param string $username
120 * @param string $confirmsecret
122 function user_confirm($username, $confirmsecret) {
123 global $DB;
124 $user = get_complete_user_data('username', $username);
126 if (!empty($user)) {
127 if ($user->confirmed) {
128 return AUTH_CONFIRM_ALREADY;
130 } else if ($user->auth != $this->authtype) {
131 return AUTH_CONFIRM_ERROR;
133 } else if ($user->secret == $confirmsecret) { // They have provided the secret key to get in
134 $DB->set_field("user", "confirmed", 1, array("id"=>$user->id));
135 if ($user->firstaccess == 0) {
136 $DB->set_field("user", "firstaccess", time(), array("id"=>$user->id));
138 return AUTH_CONFIRM_OK;
140 } else {
141 return AUTH_CONFIRM_ERROR;
145 function prevent_local_passwords() {
146 return false;
150 * Returns true if this authentication plugin is 'internal'.
152 * @return bool
154 function is_internal() {
155 return true;
159 * Returns true if this authentication plugin can change the user's
160 * password.
162 * @return bool
164 function can_change_password() {
165 return true;
169 * Returns the URL for changing the user's pw, or empty if the default can
170 * be used.
172 * @return moodle_url
174 function change_password_url() {
175 return null; // use default internal method
179 * Returns true if plugin allows resetting of internal password.
181 * @return bool
183 function can_reset_password() {
184 return true;
188 * Prints a form for configuring this authentication plugin.
190 * This function is called from admin/auth.php, and outputs a full page with
191 * a form for configuring this plugin.
193 * @param array $page An object containing all the data for this page.
195 function config_form($config, $err, $user_fields) {
196 include "config.html";
200 * Processes and stores configuration data for this authentication plugin.
202 function process_config($config) {
203 // set to defaults if undefined
204 if (!isset($config->recaptcha)) {
205 $config->recaptcha = false;
208 // save settings
209 set_config('recaptcha', $config->recaptcha, 'auth/email');
210 return true;
214 * Returns whether or not the captcha element is enabled, and the admin settings fulfil its requirements.
215 * @return bool
217 function is_captcha_enabled() {
218 global $CFG;
219 return isset($CFG->recaptchapublickey) && isset($CFG->recaptchaprivatekey) && get_config("auth/{$this->authtype}", 'recaptcha');