diff --git a/auth/ldap/auth.php b/auth/ldap/auth.php index 8ec2bd7..35fda67 100644 --- a/auth/ldap/auth.php +++ b/auth/ldap/auth.php @@ -273,7 +273,7 @@ class auth_plugin_ldap extends auth_plugin_base { $newuser['userpassword'] = $extpassword; break; default: - print_error('auth_ldap_unsupportedusertype','auth',$this->config->user_type); + print_error('auth_ldap_unsupportedusertype','auth','',$this->config->user_type); } $uadd = $this->ldap_add($ldapconnection, $this->config->user_attribute.'="'.$this->ldap_addslashes($userobject->username).','.$this->config->create_context.'"', $newuser); ldap_close($ldapconnection); @@ -281,6 +281,92 @@ class auth_plugin_ldap extends auth_plugin_base { } + function can_signup() { + return (!empty($this->config->auth_user_create) and !empty($this->config->create_context)); + } + + /** + * Sign up a new user ready for confirmation. + * Password is passed in plaintext. + * + * @param object $user new user object (with system magic quotes) + * @param boolean $notify print notice with link and terminate + */ + function user_signup($user, $notify=true) { + if ($this->user_exists($user->username)) { + print_error('auth_ldap_user_exists', 'auth'); + } + + $plainslashedpassword = $user->password; + unset($user->password); + + if (! $this->user_create($user, $plainslashedpassword)) { + print_error('auth_ldap_create_error', 'auth'); + } + + if (! ($user->id = insert_record('user', $user)) ) { + print_error('auth_emailnoinsert', 'auth'); + } + + $this->update_user_record($user->username); + update_internal_user_password($user, $plainslashedpassword); + + if (! send_confirmation_email($user)) { + print_error('auth_emailnoemail', 'auth'); + } + + if ($notify) { + global $CFG; + $emailconfirm = get_string('emailconfirm'); + print_header($emailconfirm, $emailconfirm, $emailconfirm); + notice(get_string('emailconfirmsent', '', $user->email), "$CFG->wwwroot/index.php"); + } else { + return true; + } + } + + /** + * Returns true if plugin allows confirming of new users. + * + * @return bool + */ + function can_confirm() { + return $this->can_signup(); + } + + /** + * Confirm the new user as registered. + * + * @param string $username (with system magic quotes) + * @param string $confirmsecret (with system magic quotes) + */ + function user_confirm($username, $confirmsecret) { + $user = get_complete_user_data('username', $username); + + if (!empty($user)) { + if ($user->confirmed) { + return AUTH_CONFIRM_ALREADY; + + } else if ($user->auth != 'ldap') { + return AUTH_CONFIRM_ERROR; + + } else if ($user->secret == stripslashes($confirmsecret)) { // They have provided the secret key to get in + if (!$this->user_activate($username)) { + return AUTH_CONFIRM_FAIL; + } + if (!set_field("user", "confirmed", 1, "id", $user->id)) { + return AUTH_CONFIRM_FAIL; + } + if (!set_field("user", "firstaccess", time(), "id", $user->id)) { + return AUTH_CONFIRM_FAIL; + } + return AUTH_CONFIRM_OK; + } + } else { + return AUTH_CONFIRM_ERROR; + } + } + /** * return number of days to user password expires * diff --git a/lang/en_utf8/auth.php b/lang/en_utf8/auth.php index 1a6b689..f92c5eb 100644 --- a/lang/en_utf8/auth.php +++ b/lang/en_utf8/auth.php @@ -155,6 +155,7 @@ $string['auth_ldap_bind_pw'] = 'Password for bind-user.'; $string['auth_ldap_bind_settings'] = 'Bind settings'; $string['auth_ldap_contexts'] = 'List of contexts where users are located. Separate different contexts with \';\'. For example: \'ou=users,o=org; ou=others,o=org\''; $string['auth_ldap_create_context'] = 'If you enable user creation with email confirmation, specify the context where users are created. This context should be different from other users to prevent security issues. You don\'t need to add this context to ldap_context-variable, Moodle will search for users from this context automatically.
Note! You have to modify the method user_create() in file auth/ldap/auth.php to make user creation work'; +$string['auth_ldap_create_error'] = 'Error creating user in LDAP.'; $string['auth_ldap_creators'] = 'List of groups whose members are allowed to create new courses. Separate multiple groups with \';\'. Usually something like \'cn=teachers,ou=staff,o=myorg\''; $string['auth_ldap_expiration_desc'] = 'Select No to disable expired password checking or LDAP to read passwordexpiration time directly from LDAP'; $string['auth_ldap_expiration_warning_desc'] = 'Number of days before password expiration warning is issued.'; @@ -174,6 +175,7 @@ $string['auth_ldap_preventpassindb'] = 'Select yes to prevent passwords from bei $string['auth_ldap_search_sub'] = 'Search users from subcontexts.'; $string['auth_ldap_server_settings'] = 'LDAP server settings'; $string['auth_ldap_update_userinfo'] = 'Update user information (firstname, lastname, address..) from LDAP to Moodle. Specify \"Data mapping\" settings as you need.'; +$string['auth_ldap_user_exists'] = 'LDAP username already exists.'; $string['auth_ldap_user_attribute'] = 'Optional: Overrides the attribute used to name/search users. Usually \'cn\'.'; $string['auth_ldap_user_settings'] = 'User lookup settings'; $string['auth_ldap_user_type'] = 'Select how users are stored in LDAP. This setting also specifies how login expiration, grace logins and user creation will work.'; @@ -213,7 +215,7 @@ $string['auth_ldap_create_context_key'] = 'Context for new users'; $string['auth_ldap_creators_key'] = 'Creators'; $string['auth_ldap_noconnect'] = 'LDAP-module cannot connect to server: $a'; $string['auth_ldap_noconnect_all'] = 'LDAP-module cannot connect to any servers: $a'; -$string['auth_ldap_unsupportedusertype'] = 'auth: ldap user_create() does not support selected usertype:"$a" (..yet)'; +$string['auth_ldap_unsupportedusertype'] = 'auth: ldap user_create() does not support selected usertype: $a (..yet)'; $string['auth_ldap_usertypeundefined'] = 'config.user_type not defined or function ldap_expirationtime2unix does not support selected type!'; $string['auth_ldap_usertypeundefined2'] = 'config.user_type not defined or function ldap_unixi2expirationtime does not support selected type!'; $string['auth_ldap_noextension'] = 'Warning: The PHP LDAP module does not seem to be present. Please ensure it is installed and enabled.';