/
auth.php
392 lines (359 loc) · 12.2 KB
/
auth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Moodle Development Kit Authentication plugin
*
* @package auth
* @subpackage mdk
* @copyright 2012 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/authlib.php');
/**
* Moodle Development Kit Authentication plugin
*
* @package auth
* @subpackage mdk
*/
class auth_plugin_mdk extends auth_plugin_base {
/**
* Constructor.
*/
function __construct() {
$this->authtype = 'mdk';
$this->config = $this->init_config(get_config('auth/mdk'));
}
/**
* Creates the config entries
*
* @return void
*/
public function init_config($config) {
if (!isset($config->useusernameas)) {
$config->useusernameas = '0';
}
if (!isset($config->adduserpicture)) {
$config->adduserpicture = '1';
}
if (!isset($config->downloadgravatar)) {
$config->downloadgravatar = '1';
}
if (!isset($config->gravatardefault)) {
$config->gravatardefault = 'wavatar';
}
foreach ($this->userfields as $field) {
if (!isset($pluginconfig->{"field_map_$field"})) {
$config->{"field_map_$field"} = '';
}
if (!isset($config->{"field_updatelocal_$field"})) {
$config->{"field_updatelocal_$field"} = '';
}
if (!isset($config->{"field_updateremote_$field"})) {
$config->{"field_updateremote_$field"} = '';
}
if (!isset($config->{"field_lock_$field"})) {
$config->{"field_lock_$field"} = '';
}
}
return $config;
}
/**
* Returns true if the username and password work or don't exist and false
* if the user exists and the password is wrong.
*
* @param string $username The username
* @param string $password The password
* @return bool Authentication success or failure.
*/
function user_login ($username, $password) {
global $CFG, $DB;
if ($user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
return validate_internal_user_password($user, $password);
}
return true;
}
/**
* Updates the user's password.
*
* called when the user password is updated.
*
* @param object $user User table object
* @param string $newpassword Plaintext password
* @return boolean result
*
*/
function user_update_password($user, $newpassword) {
$user = get_complete_user_data('id', $user->id);
return update_internal_user_password($user, $newpassword);
}
/**
* Do we forbid saved passwords locally?
*
* @return boolean
*/
function prevent_local_passwords() {
return false;
}
/**
* Returns true if this authentication plugin is 'internal'.
*
* @return bool
*/
function is_internal() {
return true;
}
/**
* Returns true if this authentication plugin can change the user's
* password.
*
* @return bool
*/
function can_change_password() {
return true;
}
/**
* Returns the URL for changing the user's pw, or empty if the default can
* be used.
*
* @return moodle_url
*/
function change_password_url() {
return null;
}
/**
* Returns true if plugin allows resetting of internal password.
*
* @return bool
*/
function can_reset_password() {
return true;
}
/**
* Indicates if moodle should automatically update internal user
* records with data from external sources using the information
* from get_userinfo() method.
*
* @return bool true means automatically copy data from ext to user table
*/
function is_synchronised_with_external() {
return true;
}
/**
* Read user information from external database and returns it as array().
* Function should return all information available. If you are saving
* this information to moodle user-table you should honour synchronisation flags
*
* @param string $username username
*
* @return mixed array with no magic quotes or false on error
*/
function get_userinfo($username) {
include_once('data/userinfos.php');
$userinfos = new auth_mdk_userinfos();
$default = $userinfos->get('default');
$data = array();
// Name
$loop = 0;
if ($name = $userinfos->get('name')) {
$name = explode(',', $name, 2);
if (isset($name[1])) {
$firstname = trim($name[0]);
$lastname = trim($name[1]);
}
}
while (!isset($firstname) || $this->user_exists_with_name($firstname, $lastname)) {
$firstname = $userinfos->get('firstname');
$lastname = $userinfos->get('lastname');
// Avoid infinite loop
if ($loop++ > 10) {
break;
}
}
$data['firstname'] = $this->parse_value($firstname, $username);
$data['lastname'] = $this->parse_value($lastname, $username);
// Any other field
$missing = array_diff($this->userfields, array_keys($data));
foreach ($missing as $field) {
if ($value = $userinfos->get($field)) {
$data[$field] = $this->parse_value($value, $username);
}
}
$data = array_merge($default, $data);
$data = $this->do_use_username_as($username, $data);
return $data;
}
/**
* Returns the role of a user based on its username
*
* @param string $username
* @return int
*/
public function get_role($username) {
$letter = substr($username, 0, 1);
switch ($letter) {
case 's':
$archetype = 'student';
break;
case 't':
$archetype = 'editingteacher';
break;
case 'm':
$archetype = 'manager';
break;
default:
$archetype = 'user';
break;
}
$role = get_archetype_roles($archetype);
return reset($role);
}
/**
* Sync roles for this user
*
* @param $user object user object (without system magic quotes)
*/
public function sync_roles($user) {
global $CFG, $DB, $PAGE;
require_once($CFG->dirroot . '/enrol/manual/lib.php');
$plugin = new enrol_manual_plugin();
$courses = $DB->get_records_select('course', 'id > ?', array(1), '', 'id, startdate');
foreach ($courses as $course) {
$role = $this->get_role($user->username);
if (!in_array($role->archetype, array('user', 'guest', 'frontpage'))) {
$instance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'));
if ($instance) {
$plugin->enrol_user($instance, $user->id, $role->id, $course->startdate, 0);
}
}
}
}
/**
* Prints a form for configuring this authentication plugin.
*
* This function is called from admin/auth.php, and outputs a full page with
* a form for configuring this plugin.
*
* @param array $page An object containing all the data for this page.
*/
function config_form($config, $err, $user_fields) {
include('config.html');
}
/**
* Check if a user exists with that name
*
* @param string $firstname
* @param string $lastname
* @return bool
*/
public function user_exists_with_name($firstname, $lastname) {
global $DB;
return $DB->record_exists('user', array('deleted' => 0, 'firstname' => $firstname, 'lastname' => $lastname));
}
/**
* Does the logic to use the user name in the user details (first and last names).
*
* @param string $username the username of the user.
* @param array $data the complete array of details of the user.
* @return array of details.
*/
public function do_use_username_as($username, $data) {
switch($this->config->useusernameas) {
case 'first':
$data['firstname'] = $username;
break;
case 'last':
$data['lastname'] = $username;
break;
case 'both':
$data['firstname'] = $username;
$data['lastname'] = $username;
break;
case 'prefix_first':
$data['firstname'] = $username . ' ' . $data['firstname'];
break;
case 'prefix_last':
$data['lastname'] = $username . ' ' . $data['lastname'];
break;
case 'prefix_both':
$data['firstname'] = $username . ' ' . $data['firstname'];
$data['lastname'] = $username . ' ' . $data['lastname'];
break;
case 'suffix_first':
$data['firstname'] .= ' ' . $username;
break;
case 'suffix_last':
$data['lastname'] .= ' ' . $username;
break;
case 'suffix_both':
$data['firstname'] .= ' ' . $username;
$data['lastname'] .= ' ' . $username;
default:
break;
}
return $data;
}
/**
* Returns the list of options for the config usernameas.
*
* @return array of options
*/
public function get_usernameas_options() {
return array(
0 => get_string('nothing', 'auth_mdk'),
'first' => get_string('firstname'),
'last' => get_string('lastname'),
'both' => get_string('bothfirstandlastname', 'auth_mdk'),
'prefix_first' => get_string('prefixfirstname', 'auth_mdk'),
'prefix_last' => get_string('prefixlastname', 'auth_mdk'),
'prefix_both' => get_string('prefixboth', 'auth_mdk'),
'suffix_first' => get_string('suffixfirstname', 'auth_mdk'),
'suffix_last' => get_string('suffixlastname', 'auth_mdk'),
'suffix_both' => get_string('suffixboth', 'auth_mdk'),
);
}
/**
* Check for patterns and replace them
*
* @param string $value the string to work on
* @param string $username the username
* @return string modified
*/
public function parse_value($value, $username) {
global $CFG;
if (!empty($CFG->auth_mdk_hostname)) {
$host = $CFG->auth_mdk_hostname;
} else {
$url = parse_url($CFG->wwwroot);
$host = $url['host'];
}
$value = str_replace('{username}', $username, $value);
$value = str_replace('{hostname}', $host, $value);
return $value;
}
/**
* Processes and stores configuration data for this authentication plugin.
* @param srdClass $config
* @return bool always true or exception
*/
function process_config($config) {
set_config('useusernameas', $config->useusernameas, 'auth/mdk');
set_config('adduserpicture', $config->adduserpicture, 'auth/mdk');
set_config('downloadgravatar', $config->downloadgravatar, 'auth/mdk');
set_config('gravatardefault', $config->gravatardefault, 'auth/mdk');
return true;
}
}