Skip to content

Commit

Permalink
MDL-13811 Completed the required changes. Also added a "Cancel email …
Browse files Browse the repository at this point in the history
…change" link.
  • Loading branch information
nicolasconnault committed Jul 5, 2008
1 parent 2b03299 commit e7f720c
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 17 deletions.
1 change: 1 addition & 0 deletions admin/settings/security.php
Expand Up @@ -50,6 +50,7 @@
$temp->add(new admin_setting_configtext('minpasswordupper', get_string('minpasswordupper', 'admin'), get_string('configminpasswordupper', 'admin'), 1, PARAM_INT));
$temp->add(new admin_setting_configtext('minpasswordnonalphanum', get_string('minpasswordnonalphanum', 'admin'), get_string('configminpasswordnonalphanum', 'admin'), 1, PARAM_INT));
$temp->add(new admin_setting_configcheckbox('disableuserimages', get_string('disableuserimages', 'admin'), get_string('configdisableuserimages', 'admin'), 0));
$temp->add(new admin_setting_configcheckbox('emailchangeconfirmation', get_string('emailchangeconfirmation', 'admin'), get_string('configemailchangeconfirmation', 'admin'), 0));
$ADMIN->add('security', $temp);


Expand Down
2 changes: 2 additions & 0 deletions lang/en_utf8/admin.php
Expand Up @@ -98,6 +98,7 @@
$string['configdoctonewwindow'] = 'If you enable this, then links to Moodle Docs will be shown in a new window.';
$string['configeditorfontlist'] = 'Select the fonts that should appear in the editor\'s drop-down list.';
$string['configeditordictionary'] = 'This value will be used if aspell doesn\'t have dictionary for users own language.';
$string['configemailchangeconfirmation'] = 'Require an email confirmation step when users change their email address in their profile.';
$string['configemoticons'] = 'Change the code on the left that relates to the name of the emoticon on the right. To add new emoticons, add a code and a name, then add an image as name.gif in /pix/s.';
$string['configenableajax'] = 'This setting allows you to control the use of AJAX (advanced client/server interfaces using Javascript) across the whole site. With this setting enabled users can still make a choice in their profile, otherwise AJAX is disabled for everybody.';
$string['configdisablecourseajax'] = 'Do not use AJAX when editing main course pages.';
Expand Down Expand Up @@ -316,6 +317,7 @@
$string['editorspellinghelp'] = 'Enable or disable spell-checking. When enabled, <strong>aspell</strong> must be installed on the server.';
$string['editorspelling'] = 'Editor spelling';
$string['editstrings'] = 'Edit words or phrases';
$string['emailchangeconfirmation'] = 'Email change confirmation';
$string['emoticons'] = 'Emoticons';
$string['emptysettingvalue'] = 'Empty';
$string['enableajax'] = 'Enable AJAX';
Expand Down
24 changes: 19 additions & 5 deletions lang/en_utf8/auth.php
@@ -1,4 +1,4 @@
<?php // $Id$
<?php // $Id$
// auth.php - created with Moodle 1.5 UNSTABLE DEVELOPMENT (2005010100)


Expand Down Expand Up @@ -113,13 +113,27 @@
$string['auth_dbchangepasswordurl_key'] = 'Password-change URL';

// Email plugin
$string['auth_emailchangecancel'] = 'Cancel email change';
$string['auth_emailchangepending'] = 'Change pending. Open the link sent to you at $a->preference_newemail.';
$string['auth_emaildescription'] = 'Email confirmation is the default authentication method. When the user signs up, choosing their own new username and password, a confirmation email is sent to the user\'s email address. This email contains a secure link to a page where the user can confirm their account. Future logins just check the username and password against the stored values in the Moodle database.';
$string['auth_emailnowexists'] = 'The email address you tried to assign to your profile has been assigned to someone else since your original request. Your request for change of email address is hereby cancelled, but you may try again with a different address.';
$string['auth_emailtitle'] = 'Email-based self-registration';
$string['auth_emailnoinsert'] = 'Could not add your record to the database!';
$string['auth_emailnoemail'] = 'Tried to send you an email but failed!';
$string['auth_emailrecaptcha'] = 'Adds a visual/audio confirmation form element to the signup page for email self-registering users. This protects your site against spammers and contributes to a worthwhile cause. See http://recaptcha.net/learnmore.html for more details.';
$string['auth_emailrecaptcha_key'] = 'Enable reCAPTCHA element';
$string['auth_emailsettings'] = 'Settings';
$string['auth_emailupdatemessage'] = 'Dear $a->fullname,
You have requested a change of your email address for your user account at $a->site. Please open the following URL in your browser in order to confirm this change.
$a->url';
$string['auth_emailupdatetitle'] = 'Confirmation of email update at $a->site';
$string['auth_invalidnewemailkey'] = 'Error: if you are trying to confirm a change of email address, you may have made a mistake in copying the URL we sent you by email. Please copy the address and try again.';
$string['auth_emailupdatesuccess'] = 'Your email address was successfully updated to $a->email.';
$string['auth_outofnewemailupdateattempts'] = 'You have run out of allowed attempts to update your email address. Your update request has been cancelled.';
$string['auth_emailupdate'] = 'Email address update';
$string['auth_changingemailaddress'] = 'You have requested a change of email address, from $a->oldemail to $a->newemail. For security reasons, we are sending you an email message at the new address to confirm that it belongs to you. Your email address will be updated as soon as you open the URL sent to you in that message.';

// FirstClass plugin
$string['auth_fccreators'] = 'List of groups whose members are allowed to create new courses. Separate multiple groups with \';\'. Names must be spelled exactly as on FirstClass server. System is case-sensitive.';
Expand Down Expand Up @@ -194,11 +208,11 @@
$string['auth_ldap_version'] = 'The version of the LDAP protocol your server is using.';
$string['auth_ldapdescription'] = 'This method provides authentication against an external LDAP server.
If the given username and password are valid, Moodle creates a new user
If the given username and password are valid, Moodle creates a new user
entry in its database. This module can read user attributes from LDAP and prefill
entry in its database. This module can read user attributes from LDAP and prefill
wanted fields in Moodle. For following logins only the username and
wanted fields in Moodle. For following logins only the username and
password are checked.';
$string['auth_ldap_ldap_encoding_key'] = 'LDAP encoding';
Expand Down Expand Up @@ -323,7 +337,7 @@
$string['auth_shibboleth_login_long'] = 'Login to Moodle via Shibboleth';
$string['auth_shibboleth_select_organization'] = 'For authentication via Shibboleth, please select your organization from the drop down list:';
$string['auth_shibboleth_contact_administrator'] = 'In case you are not associated with the given organizations and you need access to a course on this server, please contact the';
$string['auth_shibboleth_select_member'] = 'I\'m a member of ...';
$string['auth_shibboleth_select_member'] = 'I\'m a member of ...';
$string['auth_shibboleth_errormsg'] ='Please select the organization you are member of!';
$string['auth_shib_no_organizations_warning'] ='If you want to use the integrated WAYF service, you must provide a coma-separated list of Identity Provider entityIDs, their names and optionally a session initiator.';

Expand Down
61 changes: 57 additions & 4 deletions user/edit.php
Expand Up @@ -10,6 +10,7 @@

$userid = optional_param('id', $USER->id, PARAM_INT); // user id
$course = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
$cancelemailchange = optional_param('cancelemailchange', false, PARAM_INT); // course id (defaults to Site)

if (!$course = get_record('course', 'id', $course)) {
error('Course ID was incorrect');
Expand All @@ -34,8 +35,17 @@
error('User ID was incorrect');
}

// Process email change cancellation
if ($cancelemailchange) {
useredit_load_preferences($user);
$user->preference_newemail = null;
$user->preference_newemailkey = null;
$user->preference_newemailattemptsleft = null;
useredit_update_user_preference($user);
}

// Guest can not be edited
if (isguestuser($user)) {
if (isguestuser($user)) {
print_error('guestnoeditprofile');
}

Expand Down Expand Up @@ -89,10 +99,29 @@
$userform = new user_edit_form();
$userform->set_data($user);

$email_changed = false;

if ($usernew = $userform->get_data()) {

add_to_log($course->id, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');

$email_changed_html = '';

if ($CFG->emailchangeconfirmation) {
// Handle change of email carefully for non-trusted users
if ($user->email != $usernew->email && !has_capability('moodle/user:update', $systemcontext)) {
$a = new stdClass();
$a->newemail = $usernew->preference_newemail = $usernew->email;
$usernew->preference_newemailkey = random_string(20);
$usernew->preference_newemailattemptsleft = 3;
$a->oldemail = $usernew->email = $user->email;

$email_changed_html = print_box(get_string('auth_changingemailaddress', 'auth', $a), 'generalbox', 'notice', true);
$email_changed_html .= print_continue("$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id", true);
$email_changed = true;
}
}

$authplugin = get_auth_plugin($user->auth);

$usernew->timemodified = time();
Expand Down Expand Up @@ -131,6 +160,23 @@
// save custom profile fields data
profile_save_data($usernew);

// If email was changed, send confirmation email now
if ($email_changed && $CFG->emailchangeconfirmation) {
$temp_user = fullclone($user);
$temp_user->email = $usernew->preference_newemail;

$a = new stdClass();
$a->url = $CFG->wwwroot . '/user/emailupdate.php?key=' . $usernew->preference_newemailkey . '&id=' . $user->id;
$a->site = $SITE->fullname;

$emailupdatemessage = get_string('auth_emailupdatemessage', 'auth', $a);
$emailupdatetitle = get_string('auth_emailupdatetitle', 'auth', $a);

if(!$mail_results = email_to_user($temp_user, get_admin(), $emailupdatetitle, $emailupdatemessage)) {
die("could not send email!");
}
}

if ($USER->id == $user->id) {
// Override old $USER session variable if needed
$usernew = (array)get_record('user', 'id', $user->id); // reload from db
Expand All @@ -139,7 +185,10 @@
}
}
events_trigger('user_updated', $usernew);
redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");

if (!$email_changed || !$CFG->emailchangeconfirmation) {
redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
}
}


Expand All @@ -164,8 +213,12 @@
$currenttab = 'editprofile';
require('tabs.php');

/// Finally display THE form
$userform->display();
if ($email_changed) {
echo $email_changed_html;
} else {
/// Finally display THE form
$userform->display();
}

/// and proper footer
print_footer($course);
Expand Down
25 changes: 18 additions & 7 deletions user/editlib.php
Expand Up @@ -59,7 +59,10 @@ function useredit_update_interests($user, $csv_tag_names) {
}

function useredit_shared_definition(&$mform) {
global $CFG;
global $CFG, $USER;

$user = get_record('user', 'id', $USER->id);
useredit_load_preferences($user);

$strrequired = get_string('required');

Expand All @@ -80,8 +83,16 @@ function useredit_shared_definition(&$mform) {
$mform->addRule('lastname', $strrequired, 'required', null, 'client');
$mform->setType('lastname', PARAM_NOTAGS);

$mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
$mform->addRule('email', $strrequired, 'required', null, 'client');
// Do not show email field if change confirmation is pending
if ($CFG->emailchangeconfirmation && !empty($user->preference_newemail)) {
$notice = get_string('auth_emailchangepending', 'auth', $user);
$notice .= '<br /><a href="edit.php?cancelemailchange=1&amp;id='.$user->id.'">'
. get_string('auth_emailchangecancel', 'auth') . '</a>';
$mform->addElement('static', 'emailpending', get_string('email'), $notice);
} else {
$mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
$mform->addRule('email', $strrequired, 'required', null, 'client');
}

$choices = array();
$choices['0'] = get_string('emaildisplayno');
Expand Down Expand Up @@ -225,9 +236,9 @@ function useredit_shared_definition(&$mform) {
$mform->addElement('header', 'moodle_interests', get_string('interests'));
$mform->addElement('textarea', 'interests', get_string('interestslist'), 'cols="45" rows="3"');
$mform->setHelpButton('interests', array('interestslist', get_string('helpinterestslist'),
false, true, false));
false, true, false));
}

/// Moodle optional fields
$mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
$mform->setAdvanced('moodle_optional');
Expand Down Expand Up @@ -267,8 +278,8 @@ function useredit_shared_definition(&$mform) {

$mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"');
$mform->setType('address', PARAM_MULTILANG);


}

?>
58 changes: 58 additions & 0 deletions user/emailupdate.php
@@ -0,0 +1,58 @@
<?php // $Id$
require_once('../config.php');
require_once($CFG->libdir.'/adminlib.php');

$key = required_param('key', PARAM_ALPHANUM);
$id = required_param('id', PARAM_INT);

if (!$user = get_record('user', 'id', $id)) {
error("Unknown user ID");
}

$preferences = get_user_preferences(null, null, $id);
$a = new stdClass();
$a->fullname = fullname($user, true);
$stremailupdate = get_string('auth_emailupdate', 'auth', $a);
print_header(format_string($SITE->fullname) . ": $stremailupdate", format_string($SITE->fullname) . ": $stremailupdate");

$cancel_email_update = false;

if (empty($preferences['newemailattemptsleft'])) {
redirect("$CFG->wwwroot/user/view.php?id=$user->id");

} elseif ($preferences['newemailattemptsleft'] < 1) {
$cancel_email_update = true;
$stroutofattempts = get_string('auth_outofnewemailupdateattempts', 'auth');
print_box($stroutofattempts, 'center');

} elseif ($key == $preferences['newemailkey']) {
$user->email = $preferences['newemail'];

// Detect duplicate before saving
if (get_record('user', 'email', addslashes($user->email))) {
$stremailnowexists = get_string('auth_emailnowexists', 'auth');
print_box($stremailnowexists, 'center');
$cancel_email_update = true;
print_continue("$CFG->wwwroot/user/view.php?id=$user->id");
} else {
// update user email
if (!set_field('user', 'email', addslashes($user->email), 'id', $user->id)) {
error('Error updating user record');

} else {
events_trigger('user_updated', $user);
$stremailupdatesuccess = get_string('auth_emailupdatesuccess', 'auth', $user);
print_box($stremailupdatesuccess, 'center');
print_continue("$CFG->wwwroot/user/view.php?id=$user->id");

$cancel_email_update = true;
}
}

} else {
$preferences['newemailattemptsleft']--;
set_user_preference('newemailattemptsleft', $preferences['newemailattemptsleft'], $user->id);
$strinvalidkey = get_string('auth_invalidnewemailkey', 'auth');
print_box($strinvalidkey, 'center');
}

2 changes: 1 addition & 1 deletion version.php
Expand Up @@ -6,7 +6,7 @@
// This is compared against the values stored in the database to determine
// whether upgrades should be performed (see lib/db/*.php)

$version = 2007101515; // YYYYMMDD = date of the 1.9 branch (don't change)
$version = 2007101516; // YYYYMMDD = date of the 1.9 branch (don't change)
// X = release number 1.9.[0,1,2,3...]
// Y.YY = micro-increments between releases

Expand Down

0 comments on commit e7f720c

Please sign in to comment.