MDL-10875 Corrected bug in averaging plus a couple of typos
[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) {
b7b50143 43 global $CFG;
44 if ($user = get_record('user', '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 *
73 * @param object $user new user object (with system magic quotes)
74 * @param boolean $notify print notice with link and terminate
b9ddb2d5 75 */
6bc1e5d5 76 function user_signup($user, $notify=true) {
dd0feda5 77 $user->password = hash_internal_user_password($user->password);
78
b9ddb2d5 79 if (! ($user->id = insert_record('user', $user)) ) {
a9ad3633 80 print_error('auth_emailnoinsert','auth');
b9ddb2d5 81 }
82 if (! send_confirmation_email($user)) {
a9ad3633 83 print_error('auth_emailnoemail','auth');
b9ddb2d5 84 }
85
86 if ($notify) {
87 global $CFG;
88 $emailconfirm = get_string('emailconfirm');
e295df44 89 $navlinks = array();
90 $navlinks[] = array('name' => $emailconfirm, 'link' => null, 'type' => 'misc');
91 $navigation = build_navigation($navlinks);
92
93 print_header($emailconfirm, $emailconfirm, $navigation);
b9ddb2d5 94 notice(get_string('emailconfirmsent', '', $user->email), "$CFG->wwwroot/index.php");
dd0feda5 95 } else {
96 return true;
139ebfdb 97 }
b9ddb2d5 98 }
99
6bc1e5d5 100 /**
101 * Returns true if plugin allows confirming of new users.
102 *
103 * @return bool
104 */
105 function can_confirm() {
106 return true;
107 }
108
430759a5 109 /**
b9ddb2d5 110 * Confirm the new user as registered.
dd0feda5 111 *
112 * @param string $username (with system magic quotes)
113 * @param string $confirmsecret (with system magic quotes)
b9ddb2d5 114 */
115 function user_confirm($username, $confirmsecret) {
116 $user = get_complete_user_data('username', $username);
117
118 if (!empty($user)) {
119 if ($user->confirmed) {
120 return AUTH_CONFIRM_ALREADY;
dd0feda5 121
122 } else if ($user->auth != 'email') {
123 return AUTH_CONFIRM_ERROR;
124
125 } else if ($user->secret == stripslashes($confirmsecret)) { // They have provided the secret key to get in
b9ddb2d5 126 if (!set_field("user", "confirmed", 1, "id", $user->id)) {
127 return AUTH_CONFIRM_FAIL;
128 }
129 if (!set_field("user", "firstaccess", time(), "id", $user->id)) {
130 return AUTH_CONFIRM_FAIL;
131 }
132 return AUTH_CONFIRM_OK;
133 }
dd0feda5 134 } else {
135 return AUTH_CONFIRM_ERROR;
b9ddb2d5 136 }
137 }
138
139 /**
140 * Returns true if this authentication plugin is 'internal'.
141 *
139ebfdb 142 * @return bool
b9ddb2d5 143 */
144 function is_internal() {
145 return true;
146 }
139ebfdb 147
b9ddb2d5 148 /**
149 * Returns true if this authentication plugin can change the user's
150 * password.
151 *
139ebfdb 152 * @return bool
b9ddb2d5 153 */
154 function can_change_password() {
155 return true;
156 }
139ebfdb 157
b9ddb2d5 158 /**
430759a5 159 * Returns the URL for changing the user's pw, or empty if the default can
b9ddb2d5 160 * be used.
161 *
430759a5 162 * @return mixed
b9ddb2d5 163 */
164 function change_password_url() {
430759a5 165 return ''; // use dafult internal method
166 }
167
168 /**
169 * Returns true if plugin allows resetting of internal password.
170 *
171 * @return bool
172 */
173 function can_reset_password() {
174 return true;
b9ddb2d5 175 }
139ebfdb 176
b9ddb2d5 177 /**
178 * Prints a form for configuring this authentication plugin.
179 *
180 * This function is called from admin/auth.php, and outputs a full page with
181 * a form for configuring this plugin.
182 *
183 * @param array $page An object containing all the data for this page.
184 */
139ebfdb 185 function config_form($config, $err, $user_fields) {
b9ddb2d5 186 include "config.html";
187 }
188
189 /**
190 * Processes and stores configuration data for this authentication plugin.
191 */
192 function process_config($config) {
193 return true;
194 }
139ebfdb 195
b9ddb2d5 196}
197
198?>