MDL-32572 fix notice when changing internal auth_db passwords
[moodle.git] / auth / db / auth.php
CommitLineData
b9ddb2d5 1<?php
b9ddb2d5 2/**
b9ddb2d5 3 * Authentication Plugin: External Database Authentication
4 *
5 * Checks against an external database.
6 *
7415aed1
PS
7 * @package auth
8 * @subpackage db
9 * @author Martin Dougiamas
10 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
b9ddb2d5 11 */
12
7415aed1 13defined('MOODLE_INTERNAL') || die();
b9ddb2d5 14
6bc1e5d5 15require_once($CFG->libdir.'/authlib.php');
e7721485 16require_once($CFG->libdir.'/adodb/adodb.inc.php');
6bc1e5d5 17
b9ddb2d5 18/**
19 * External database authentication plugin.
20 */
6bc1e5d5 21class auth_plugin_db extends auth_plugin_base {
b9ddb2d5 22
23 /**
24 * Constructor.
25 */
26 function auth_plugin_db() {
6bc1e5d5 27 $this->authtype = 'db';
b9ddb2d5 28 $this->config = get_config('auth/db');
8ae42b8d 29 if (empty($this->config->extencoding)) {
30 $this->config->extencoding = 'utf-8';
31 }
b9ddb2d5 32 }
33
34 /**
35 * Returns true if the username and password work and false if they are
36 * wrong or don't exist.
37 *
576c063b 38 * @param string $username The username
39 * @param string $password The password
8ae42b8d 40 *
41 * @return bool Authentication success or failure.
b9ddb2d5 42 */
139ebfdb 43 function user_login($username, $password) {
576c063b 44 global $CFG, $DB;
b9ddb2d5 45
ab6e0848
PS
46 $extusername = textlib::convert($username, 'utf-8', $this->config->extencoding);
47 $extpassword = textlib::convert($password, 'utf-8', $this->config->extencoding);
8ae42b8d 48
139ebfdb 49 $authdb = $this->db_init();
b9ddb2d5 50
7415aed1 51 if ($this->is_internal()) {
b9ddb2d5 52 // lookup username externally, but resolve
53 // password locally -- to support backend that
54 // don't track passwords
8ae42b8d 55 $rs = $authdb->Execute("SELECT * FROM {$this->config->table}
56 WHERE {$this->config->fielduser} = '".$this->ext_addslashes($extusername)."' ");
b9ddb2d5 57 if (!$rs) {
03cedd62 58 $authdb->Close();
03ea0b32 59 debugging(get_string('auth_dbcantconnect','auth_db'));
b9ddb2d5 60 return false;
61 }
8ae42b8d 62
7415aed1 63 if (!$rs->EOF) {
03cedd62 64 $rs->Close();
65 $authdb->Close();
7415aed1 66 // user exists externally
b9ddb2d5 67 // check username/password internally
45a6d31c 68 if ($user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id, 'auth'=>$this->authtype))) {
b9ddb2d5 69 return validate_internal_user_password($user, $password);
70 }
71 } else {
03cedd62 72 $rs->Close();
73 $authdb->Close();
b9ddb2d5 74 // user does not exist externally
75 return false;
8ae42b8d 76 }
b9ddb2d5 77
8ae42b8d 78 } else {
7415aed1 79 // normal case: use external db for both usernames and passwords
b9ddb2d5 80
81 if ($this->config->passtype === 'md5') { // Re-format password accordingly
8ae42b8d 82 $extpassword = md5($extpassword);
83 } else if ($this->config->passtype === 'sha1') {
84 $extpassword = sha1($extpassword);
b9ddb2d5 85 }
86
8ae42b8d 87 $rs = $authdb->Execute("SELECT * FROM {$this->config->table}
88 WHERE {$this->config->fielduser} = '".$this->ext_addslashes($extusername)."'
89 AND {$this->config->fieldpass} = '".$this->ext_addslashes($extpassword)."' ");
b9ddb2d5 90 if (!$rs) {
03cedd62 91 $authdb->Close();
03ea0b32 92 debugging(get_string('auth_dbcantconnect','auth_db'));
b9ddb2d5 93 return false;
94 }
8ae42b8d 95
03cedd62 96 if (!$rs->EOF) {
97 $rs->Close();
98 $authdb->Close();
b9ddb2d5 99 return true;
100 } else {
03cedd62 101 $rs->Close();
102 $authdb->Close();
b9ddb2d5 103 return false;
8ae42b8d 104 }
105
b9ddb2d5 106 }
107 }
108
139ebfdb 109 function db_init() {
93901eb4 110 // Connect to the external database (forcing new connection)
ab6e0848 111 $authdb = ADONewConnection($this->config->type);
8ae42b8d 112 if (!empty($this->config->debugauthdb)) {
113 $authdb->debug = true;
114 ob_start();//start output buffer to allow later use of the page headers
115 }
116 $authdb->Connect($this->config->host, $this->config->user, $this->config->pass, $this->config->name, true);
b9ddb2d5 117 $authdb->SetFetchMode(ADODB_FETCH_ASSOC);
8ae42b8d 118 if (!empty($this->config->setupsql)) {
119 $authdb->Execute($this->config->setupsql);
120 }
b9ddb2d5 121
139ebfdb 122 return $authdb;
123 }
7415aed1 124
139ebfdb 125 /**
ab6e0848 126 * Returns user attribute mappings between moodle and ldap
139ebfdb 127 *
128 * @return array
129 */
130 function db_attributes() {
139ebfdb 131 $moodleattributes = array();
4105caff 132 foreach ($this->userfields as $field) {
139ebfdb 133 if (!empty($this->config->{"field_map_$field"})) {
134 $moodleattributes[$field] = $this->config->{"field_map_$field"};
0f02788f 135 }
136 }
5261baf1 137 $moodleattributes['username'] = $this->config->fielduser;
139ebfdb 138 return $moodleattributes;
139 }
140
141 /**
142 * Reads any other information for a user from external database,
143 * then returns it in an array
144 *
be544ec3 145 * @param string $username
139ebfdb 146 *
147 * @return array without magic quotes
148 */
149 function get_userinfo($username) {
139ebfdb 150 global $CFG;
151
ab6e0848 152 $extusername = textlib::convert($username, 'utf-8', $this->config->extencoding);
139ebfdb 153
154 $authdb = $this->db_init();
155
156 //Array to map local fieldnames we want, to external fieldnames
157 $selectfields = $this->db_attributes();
158
0f02788f 159 $result = array();
160 //If at least one field is mapped from external db, get that mapped data:
161 if ($selectfields) {
162 $select = '';
163 foreach ($selectfields as $localname=>$externalname) {
164 $select .= ", $externalname AS $localname";
165 }
166 $select = 'SELECT ' . substr($select,1);
167 $sql = $select .
168 " FROM {$this->config->table}" .
8ae42b8d 169 " WHERE {$this->config->fielduser} = '".$this->ext_addslashes($extusername)."'";
0f02788f 170 if ($rs = $authdb->Execute($sql)) {
03cedd62 171 if ( !$rs->EOF ) {
245ac557 172 $fields_obj = $rs->FetchObj();
1ed6ae07 173 $fields_obj = (object)array_change_key_case((array)$fields_obj , CASE_LOWER);
0f02788f 174 foreach ($selectfields as $localname=>$externalname) {
ab6e0848 175 $result[$localname] = textlib::convert($fields_obj->{$localname}, $this->config->extencoding, 'utf-8');
0f02788f 176 }
177 }
245ac557 178 $rs->Close();
b9ddb2d5 179 }
180 }
181 $authdb->Close();
b9ddb2d5 182 return $result;
183 }
184
fb5c7739 185 /**
186 * Change a user's password
187 *
ae040d4b 188 * @param object $user User table object
189 * @param string $newpassword Plaintext password
8ae42b8d 190 *
fb5c7739 191 * @return bool True on success
192 */
da249a30 193 function user_update_password($user, $newpassword) {
88fbc9a7
PS
194 global $DB;
195
7415aed1 196 if ($this->is_internal()) {
88fbc9a7
PS
197 $puser = $DB->get_record('user', array('id'=>$user->id), '*', MUST_EXIST);
198 if (update_internal_user_password($puser, $newpassword)) {
199 $user->password = $puser->password;
200 return true;
201 } else {
202 return false;
203 }
b9ddb2d5 204 } else {
205 // we should have never been called!
206 return false;
207 }
208 }
209
210 /**
ab6e0848 211 * synchronizes user from external db to moodle user table
b9ddb2d5 212 *
ab6e0848 213 * Sync should be done by using idnumber attribute, not username.
b9ddb2d5 214 * You need to pass firstsync parameter to function to fill in
ab6e0848 215 * idnumbers if they don't exists in moodle user table.
8ae42b8d 216 *
ab6e0848 217 * Syncing users removes (disables) users that don't exists anymore in external db.
8ae42b8d 218 * Creates new users and updates coursecreator status of users.
219 *
b9ddb2d5 220 * This implementation is simpler but less scalable than the one found in the LDAP module.
221 *
7415aed1 222 * @param bool $do_updates Optional: set to true to force an update of existing accounts
ab6e0848
PS
223 * @param bool $verbose
224 * @return int 0 means success, 1 means failure
b9ddb2d5 225 */
ab6e0848 226 function sync_users($do_updates=false, $verbose=false) {
70ca450a 227 global $CFG, $DB;
b9ddb2d5 228
ab6e0848 229 // list external users
b9ddb2d5 230 $userlist = $this->get_userlist();
b9ddb2d5 231
ab6e0848 232 // delete obsolete internal users
139ebfdb 233 if (!empty($this->config->removeuser)) {
b9ddb2d5 234
139ebfdb 235 // find obsolete users
236 if (count($userlist)) {
20d8d5c7
EL
237 list($notin_sql, $params) = $DB->get_in_or_equal($userlist, SQL_PARAMS_NAMED, 'u', false);
238 $params['authtype'] = $this->authtype;
f91f3f63 239 $sql = "SELECT u.*
bc31625a
PS
240 FROM {user} u
241 WHERE u.auth=:authtype AND u.deleted=0 AND u.username $notin_sql";
139ebfdb 242 } else {
f91f3f63 243 $sql = "SELECT u.*
bc31625a
PS
244 FROM {user} u
245 WHERE u.auth=:authtype AND u.deleted=0";
246 $params = array();
20d8d5c7 247 $params['authtype'] = $this->authtype;
139ebfdb 248 }
bc31625a 249 $remove_users = $DB->get_records_sql($sql, $params);
139ebfdb 250
251 if (!empty($remove_users)) {
ab6e0848
PS
252 if ($verbose) {
253 mtrace(print_string('auth_dbuserstoremove','auth_db', count($remove_users)));
254 }
139ebfdb 255
139ebfdb 256 foreach ($remove_users as $user) {
6f87ef52 257 if ($this->config->removeuser == AUTH_REMOVEUSER_FULLDELETE) {
ab6e0848
PS
258 delete_user($user);
259 if ($verbose) {
260 mtrace("\t".get_string('auth_dbdeleteuser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)));
139ebfdb 261 }
6f87ef52 262 } else if ($this->config->removeuser == AUTH_REMOVEUSER_SUSPEND) {
1dffbae2 263 $updateuser = new stdClass();
139ebfdb 264 $updateuser->id = $user->id;
265 $updateuser->auth = 'nologin';
a4d25731 266 $updateuser->timemodified = time();
dd88de0e 267 $DB->update_record('user', $updateuser);
ab6e0848
PS
268 if ($verbose) {
269 mtrace("\t".get_string('auth_dbsuspenduser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)));
270 }
139ebfdb 271 }
b9ddb2d5 272 }
8ae42b8d 273 }
139ebfdb 274 unset($remove_users); // free mem!
8ae42b8d 275 }
b9ddb2d5 276
277 if (!count($userlist)) {
278 // exit right here
279 // nothing else to do
ab6e0848 280 return 0;
b9ddb2d5 281 }
282
283 ///
284 /// update existing accounts
285 ///
286 if ($do_updates) {
287 // narrow down what fields we need to update
288 $all_keys = array_keys(get_object_vars($this->config));
289 $updatekeys = array();
290 foreach ($all_keys as $key) {
291 if (preg_match('/^field_updatelocal_(.+)$/',$key, $match)) {
292 if ($this->config->{$key} === 'onlogin') {
293 array_push($updatekeys, $match[1]); // the actual key name
294 }
295 }
296 }
297 // print_r($all_keys); print_r($updatekeys);
298 unset($all_keys); unset($key);
299
300 // only go ahead if we actually
301 // have fields to update locally
302 if (!empty($updatekeys)) {
20d8d5c7 303 list($in_sql, $params) = $DB->get_in_or_equal($userlist, SQL_PARAMS_NAMED, 'u', true);
bc31625a
PS
304 $params['authtype'] = $this->authtype;
305 $sql = "SELECT u.id, u.username
306 FROM {user} u
307 WHERE u.auth=:authtype AND u.deleted=0 AND u.username {$in_sql}";
308 if ($update_users = $DB->get_records_sql($sql, $params)) {
ab6e0848
PS
309 if ($verbose) {
310 mtrace("User entries to update: ".count($update_users));
311 }
8ae42b8d 312
313 foreach ($update_users as $user) {
ab6e0848
PS
314 if ($this->update_user_record($user->username, $updatekeys)) {
315 if ($verbose) {
316 mtrace("\t".get_string('auth_dbupdatinguser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)));
317 }
318 } else {
319 if ($verbose) {
320 mtrace("\t".get_string('auth_dbupdatinguser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id))." - ".get_string('skipped'));
321 }
139ebfdb 322 }
8ae42b8d 323 }
324 unset($update_users); // free memory
b9ddb2d5 325 }
b9ddb2d5 326 }
327 }
328
329
330 ///
331 /// create missing accounts
332 ///
333 // NOTE: this is very memory intensive
334 // and generally inefficient
8ae42b8d 335 $sql = 'SELECT u.id, u.username
70ca450a 336 FROM {user} u
5211c7ec 337 WHERE u.auth=\'' . $this->authtype . '\' AND u.deleted=\'0\'';
b9ddb2d5 338
70ca450a 339 $users = $DB->get_records_sql($sql);
8ae42b8d 340
b9ddb2d5 341 // simplify down to usernames
342 $usernames = array();
2b214bc1 343 if (!empty($users)) {
344 foreach ($users as $user) {
345 array_push($usernames, $user->username);
346 }
347 unset($users);
b9ddb2d5 348 }
b9ddb2d5 349
350 $add_users = array_diff($userlist, $usernames);
351 unset($usernames);
352
353 if (!empty($add_users)) {
ab6e0848
PS
354 if ($verbose) {
355 mtrace(get_string('auth_dbuserstoadd','auth_db',count($add_users)));
356 }
3f22e756 357 // Do not use transactions around this foreach, we want to skip problematic users, not revert everything.
b9ddb2d5 358 foreach($add_users as $user) {
359 $username = $user;
360 $user = $this->get_userinfo_asobj($user);
8ae42b8d 361
b9ddb2d5 362 // prep a few params
b7b50143 363 $user->username = $username;
b7b50143 364 $user->confirmed = 1;
5211c7ec 365 $user->auth = $this->authtype;
b7b50143 366 $user->mnethostid = $CFG->mnet_localhost_id;
8ae42b8d 367 if (empty($user->lang)) {
368 $user->lang = $CFG->lang;
369 }
370
b9ddb2d5 371 // maybe the user has been deleted before
a4d25731
PS
372 if ($old_user = $DB->get_record('user', array('username'=>$user->username, 'deleted'=>1, 'mnethostid'=>$user->mnethostid, 'auth'=>$user->auth))) {
373 // note: this undeleting is deprecated and will be eliminated soon
374 $DB->set_field('user', 'deleted', 0, array('id'=>$old_user->id));
375 $DB->set_field('user', 'timemodified', time(), array('id'=>$old_user->id));
ab6e0848 376 if ($verbose) {
a4d25731 377 mtrace("\t".get_string('auth_dbreviveduser', 'auth_db', array('name'=>$old_user->username, 'id'=>$old_user->id)));
ab6e0848 378 }
6b8ad965 379
a9637e7d 380 } else {
a4d25731
PS
381 $user->timecreated = time();
382 $user->timemodified = $user->timecreated;
3f22e756
PS
383 try {
384 $id = $DB->insert_record('user', $user); // it is truly a new user
385 if ($verbose) {
386 mtrace("\t".get_string('auth_dbinsertuser', 'auth_db', array('name'=>$user->username, 'id'=>$id)));
387 }
388 } catch (moodle_exception $e) {
389 if ($verbose) {
390 mtrace("\t".get_string('auth_dbinsertusererror', 'auth_db', $user->username));
391 }
392 continue;
ab6e0848 393 }
b9ddb2d5 394 // if relevant, tag for password generation
7415aed1 395 if ($this->is_internal()) {
b9ddb2d5 396 set_user_preference('auth_forcepasswordchange', 1, $id);
397 set_user_preference('create_password', 1, $id);
398 }
b9ddb2d5 399 }
b9ddb2d5 400 }
b9ddb2d5 401 unset($add_users); // free mem
402 }
ab6e0848 403 return 0;
b9ddb2d5 404 }
405
139ebfdb 406 function user_exists($username) {
93901eb4 407
a7e32367 408 /// Init result value
409 $result = false;
410
ab6e0848 411 $extusername = textlib::convert($username, 'utf-8', $this->config->extencoding);
8ae42b8d 412
139ebfdb 413 $authdb = $this->db_init();
b9ddb2d5 414
8ae42b8d 415 $rs = $authdb->Execute("SELECT * FROM {$this->config->table}
416 WHERE {$this->config->fielduser} = '".$this->ext_addslashes($extusername)."' ");
b9ddb2d5 417
418 if (!$rs) {
2b06294b 419 print_error('auth_dbcantconnect','auth_db');
7415aed1 420 } else if (!$rs->EOF) {
ab6e0848 421 // user exists externally
03cedd62 422 $result = true;
8ae42b8d 423 }
a7e32367 424
425 $authdb->Close();
426 return $result;
b9ddb2d5 427 }
428
429
430 function get_userlist() {
93901eb4 431
a7e32367 432 /// Init result value
433 $result = array();
434
139ebfdb 435 $authdb = $this->db_init();
b9ddb2d5 436
437 // fetch userlist
438 $rs = $authdb->Execute("SELECT {$this->config->fielduser} AS username
439 FROM {$this->config->table} ");
b9ddb2d5 440
441 if (!$rs) {
2b06294b 442 print_error('auth_dbcantconnect','auth_db');
7415aed1 443 } else if (!$rs->EOF) {
245ac557 444 while ($rec = $rs->FetchRow()) {
e9366bf8 445 $rec = (object)array_change_key_case((array)$rec , CASE_LOWER);
446 array_push($result, $rec->username);
b9ddb2d5 447 }
8ae42b8d 448 }
a7e32367 449
450 $authdb->Close();
451 return $result;
b9ddb2d5 452 }
453
454 /**
ab6e0848 455 * reads user information from DB and return it in an object
b9ddb2d5 456 *
8ae42b8d 457 * @param string $username username (with system magic quotes)
b9ddb2d5 458 * @return array
459 */
460 function get_userinfo_asobj($username) {
461 $user_array = truncate_userinfo($this->get_userinfo($username));
1dffbae2 462 $user = new stdClass();
b9ddb2d5 463 foreach($user_array as $key=>$value) {
464 $user->{$key} = $value;
465 }
466 return $user;
467 }
468
8ae42b8d 469 /**
470 * will update a local user record from an external source.
471 * is a lighter version of the one in moodlelib -- won't do
b9ddb2d5 472 * expensive ops such as enrolment
473 *
8ae42b8d 474 * If you don't pass $updatekeys, there is a performance hit and
b9ddb2d5 475 * values removed from DB won't be removed from moodle.
8ae42b8d 476 *
185721a4 477 * @param string $username username
ab6e0848
PS
478 * @param bool $updatekeys
479 * @return stdClass
b9ddb2d5 480 */
139ebfdb 481 function update_user_record($username, $updatekeys=false) {
185721a4 482 global $CFG, $DB;
b9ddb2d5 483
b9ddb2d5 484 //just in case check text case
ab6e0848 485 $username = trim(textlib::strtolower($username));
8ae42b8d 486
b9ddb2d5 487 // get the current user record
185721a4 488 $user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id));
b9ddb2d5 489 if (empty($user)) { // trouble
490 error_log("Cannot update non-existent user: $username");
2b06294b 491 print_error('auth_dbusernotexist','auth_db',$username);
b9ddb2d5 492 die;
493 }
494
b7b50143 495 // Ensure userid is not overwritten
496 $userid = $user->id;
a4d25731 497 $updated = false;
b7b50143 498
b9ddb2d5 499 if ($newinfo = $this->get_userinfo($username)) {
500 $newinfo = truncate_userinfo($newinfo);
8ae42b8d 501
b9ddb2d5 502 if (empty($updatekeys)) { // all keys? this does not support removing values
503 $updatekeys = array_keys($newinfo);
504 }
8ae42b8d 505
b9ddb2d5 506 foreach ($updatekeys as $key) {
b9ddb2d5 507 if (isset($newinfo[$key])) {
508 $value = $newinfo[$key];
b9ddb2d5 509 } else {
510 $value = '';
511 }
8ae42b8d 512
513 if (!empty($this->config->{'field_updatelocal_' . $key})) {
bc31625a 514 if (isset($user->{$key}) and $user->{$key} != $value) { // only update if it's changed
185721a4 515 $DB->set_field('user', $key, $value, array('id'=>$userid));
a4d25731 516 $updated = true;
139ebfdb 517 }
b9ddb2d5 518 }
519 }
520 }
a4d25731
PS
521 if ($updated) {
522 $DB->set_field('user', 'timemodified', time(), array('id'=>$userid));
523 }
185721a4 524 return $DB->get_record('user', array('id'=>$userid, 'deleted'=>0));
139ebfdb 525 }
526
527 /**
528 * Called when the user record is updated.
529 * Modifies user in external database. It takes olduser (before changes) and newuser (after changes)
ab6e0848 530 * compares information saved modified information to external db.
139ebfdb 531 *
70ca450a 532 * @param mixed $olduser Userobject before modifications
533 * @param mixed $newuser Userobject new modified userobject
139ebfdb 534 * @return boolean result
535 *
536 */
537 function user_update($olduser, $newuser) {
538 if (isset($olduser->username) and isset($newuser->username) and $olduser->username != $newuser->username) {
539 error_log("ERROR:User renaming not allowed in ext db");
540 return false;
541 }
542
5211c7ec 543 if (isset($olduser->auth) and $olduser->auth != $this->authtype) {
139ebfdb 544 return true; // just change auth and skip update
545 }
546
547 $curruser = $this->get_userinfo($olduser->username);
548 if (empty($curruser)) {
549 error_log("ERROR:User $olduser->username found in ext db");
550 return false;
551 }
552
ab6e0848 553 $extusername = textlib::convert($olduser->username, 'utf-8', $this->config->extencoding);
139ebfdb 554
555 $authdb = $this->db_init();
556
557 $update = array();
558 foreach($curruser as $key=>$value) {
559 if ($key == 'username') {
560 continue; // skip this
561 }
562 if (empty($this->config->{"field_updateremote_$key"})) {
563 continue; // remote update not requested
564 }
565 if (!isset($newuser->$key)) {
566 continue;
567 }
70ca450a 568 $nuvalue = $newuser->$key;
139ebfdb 569 if ($nuvalue != $value) {
ab6e0848 570 $update[] = $this->config->{"field_map_$key"}."='".$this->ext_addslashes(textlib::convert($nuvalue, 'utf-8', $this->config->extencoding))."'";
139ebfdb 571 }
572 }
573 if (!empty($update)) {
574 $authdb->Execute("UPDATE {$this->config->table}
7415aed1
PS
575 SET ".implode(',', $update)."
576 WHERE {$this->config->fielduser}='".$this->ext_addslashes($extusername)."'");
139ebfdb 577 }
578 $authdb->Close();
579 return true;
b9ddb2d5 580 }
581
8ae42b8d 582 /**
583 * A chance to validate form data, and last chance to
584 * do stuff before it is inserted in config_plugin
ab6e0848
PS
585 *
586 * @param stfdClass config form
587 * @param array $error errors
588 * @return void
8ae42b8d 589 */
ab6e0848 590 function validate_form($form, &$err) {
150b5fb0 591 if ($form->passtype === 'internal') {
b9ddb2d5 592 $this->config->changepasswordurl = '';
593 set_config('changepasswordurl', '', 'auth/db');
594 }
b9ddb2d5 595 }
596
edb5da83 597 function prevent_local_passwords() {
7415aed1 598 return !$this->is_internal();
edb5da83
PS
599 }
600
b9ddb2d5 601 /**
7415aed1
PS
602 * Returns true if this authentication plugin is "internal".
603 *
604 * Internal plugins use password hashes from Moodle user table for authentication.
b9ddb2d5 605 *
139ebfdb 606 * @return bool
b9ddb2d5 607 */
608 function is_internal() {
e79781f7
PS
609 if (!isset($this->config->passtype)) {
610 return true;
611 }
7415aed1
PS
612 return ($this->config->passtype === 'internal');
613 }
614
615 /**
616 * Indicates if moodle should automatically update internal user
617 * records with data from external sources using the information
618 * from auth_plugin_base::get_userinfo().
619 *
620 * @return bool true means automatically copy data from ext to user table
621 */
622 function is_synchronised_with_external() {
623 return true;
b9ddb2d5 624 }
625
626 /**
627 * Returns true if this authentication plugin can change the user's
628 * password.
629 *
139ebfdb 630 * @return bool
b9ddb2d5 631 */
632 function can_change_password() {
7415aed1 633 return ($this->is_internal() or !empty($this->config->changepasswordurl));
b9ddb2d5 634 }
635
636 /**
430759a5 637 * Returns the URL for changing the user's pw, or empty if the default can
b9ddb2d5 638 * be used.
639 *
99f9f85f 640 * @return moodle_url
b9ddb2d5 641 */
642 function change_password_url() {
7415aed1 643 if ($this->is_internal()) {
430759a5 644 // standard form
99f9f85f 645 return null;
430759a5 646 } else {
7415aed1 647 // use admin defined custom url
99f9f85f 648 return new moodle_url($this->config->changepasswordurl);
430759a5 649 }
b9ddb2d5 650 }
651
ab6ff8a4 652 /**
653 * Returns true if plugin allows resetting of internal password.
654 *
655 * @return bool
656 */
657 function can_reset_password() {
7415aed1 658 return $this->is_internal();
ab6ff8a4 659 }
660
b9ddb2d5 661 /**
662 * Prints a form for configuring this authentication plugin.
663 *
664 * This function is called from admin/auth.php, and outputs a full page with
665 * a form for configuring this plugin.
666 *
ab6e0848
PS
667 * @param stdClass $config
668 * @param array $err errors
669 * @param array $user_fields
670 * @return void
b9ddb2d5 671 */
139ebfdb 672 function config_form($config, $err, $user_fields) {
8ae42b8d 673 include 'config.html';
b9ddb2d5 674 }
675
676 /**
677 * Processes and stores configuration data for this authentication plugin.
ab6e0848
PS
678 * @param srdClass $config
679 * @return bool always true or exception
b9ddb2d5 680 */
681 function process_config($config) {
682 // set to defaults if undefined
683 if (!isset($config->host)) {
8ae42b8d 684 $config->host = 'localhost';
b9ddb2d5 685 }
686 if (!isset($config->type)) {
8ae42b8d 687 $config->type = 'mysql';
688 }
689 if (!isset($config->sybasequoting)) {
690 $config->sybasequoting = 0;
b9ddb2d5 691 }
692 if (!isset($config->name)) {
8ae42b8d 693 $config->name = '';
b9ddb2d5 694 }
695 if (!isset($config->user)) {
8ae42b8d 696 $config->user = '';
b9ddb2d5 697 }
698 if (!isset($config->pass)) {
8ae42b8d 699 $config->pass = '';
b9ddb2d5 700 }
701 if (!isset($config->table)) {
8ae42b8d 702 $config->table = '';
b9ddb2d5 703 }
704 if (!isset($config->fielduser)) {
8ae42b8d 705 $config->fielduser = '';
b9ddb2d5 706 }
707 if (!isset($config->fieldpass)) {
8ae42b8d 708 $config->fieldpass = '';
b9ddb2d5 709 }
710 if (!isset($config->passtype)) {
8ae42b8d 711 $config->passtype = 'plaintext';
712 }
713 if (!isset($config->extencoding)) {
714 $config->extencoding = 'utf-8';
715 }
716 if (!isset($config->setupsql)) {
717 $config->setupsql = '';
718 }
719 if (!isset($config->debugauthdb)) {
720 $config->debugauthdb = 0;
b9ddb2d5 721 }
139ebfdb 722 if (!isset($config->removeuser)) {
6f87ef52 723 $config->removeuser = AUTH_REMOVEUSER_KEEP;
139ebfdb 724 }
b9ddb2d5 725 if (!isset($config->changepasswordurl)) {
726 $config->changepasswordurl = '';
727 }
728
729 // save settings
8ae42b8d 730 set_config('host', $config->host, 'auth/db');
731 set_config('type', $config->type, 'auth/db');
732 set_config('sybasequoting', $config->sybasequoting, 'auth/db');
733 set_config('name', $config->name, 'auth/db');
734 set_config('user', $config->user, 'auth/db');
735 set_config('pass', $config->pass, 'auth/db');
736 set_config('table', $config->table, 'auth/db');
737 set_config('fielduser', $config->fielduser, 'auth/db');
738 set_config('fieldpass', $config->fieldpass, 'auth/db');
739 set_config('passtype', $config->passtype, 'auth/db');
740 set_config('extencoding', trim($config->extencoding), 'auth/db');
139ebfdb 741 set_config('setupsql', trim($config->setupsql),'auth/db');
8ae42b8d 742 set_config('debugauthdb', $config->debugauthdb, 'auth/db');
139ebfdb 743 set_config('removeuser', $config->removeuser, 'auth/db');
8ae42b8d 744 set_config('changepasswordurl', trim($config->changepasswordurl), 'auth/db');
745
b9ddb2d5 746 return true;
747 }
748
8ae42b8d 749 function ext_addslashes($text) {
750 // using custom made function for now
751 if (empty($this->config->sybasequoting)) {
752 $text = str_replace('\\', '\\\\', $text);
753 $text = str_replace(array('\'', '"', "\0"), array('\\\'', '\\"', '\\0'), $text);
754 } else {
755 $text = str_replace("'", "''", $text);
756 }
757 return $text;
758 }
b9ddb2d5 759}
760
5117d598 761