Skip to content

Commit

Permalink
Adding this (optional) feature to 1.4 stable because it seems quite
Browse files Browse the repository at this point in the history
a few people really need it.
  • Loading branch information
moodler committed Sep 7, 2004
1 parent 7c2e85f commit a50ded8
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
7 changes: 7 additions & 0 deletions config-dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@
// Seconds for files to remain in caches. Decrease this if you are worried
// about students being served outdated versions of uploaded files.
// $CFG->filelifetime = 86400;
//
// The following two settings allow you to specify allowed domains for
// email addresses. If the first one is set, then Moodle will DISALLOW
// all domains EXCEPT those listed. Otherwise, if the second one is set
// then all addresses are ALLOWED EXCEPT those listed.
// $CFG->allowemailaddresses = "myschool.edu.au hotmail.com";
// $CFG->denyemailaddresses = "hotmail.com yahoo.com";

//=========================================================================
// ALL DONE! To continue installation, visit your main page with a browser
Expand Down
2 changes: 2 additions & 0 deletions lang/en/moodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@
$string['emailexists'] = 'This email address is already registered.';
$string['emailformat'] = 'Email format';
$string['emailmustbereal'] = 'Note: your email address must be a real one';
$string['emailnotallowed'] = 'Email addresses in these domains are not allowed ($a)';
$string['emailonlyallowed'] = 'This email is not one of those that are allowed ($a)';
$string['emailpasswordconfirmation'] = 'Hi $a->firstname,
Someone (probably you) has requested a new password for your
Expand Down
40 changes: 40 additions & 0 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,12 @@ function create_user_record($username, $password, $auth='') {
}
}

if (!empty($newuser->email)) {
if (email_is_not_allowed($newuser->email)) {
unset($newuser->email);
}
}

$newuser->auth = (empty($auth)) ? $CFG->auth : $auth;
$newuser->username = $username;
$newuser->password = md5($password);
Expand Down Expand Up @@ -1630,6 +1636,40 @@ function send_password_change_confirmation_email($user) {
}


function email_is_not_allowed($email) {
/// Check that an email is allowed. It returns an error message if there
/// was a problem.

global $CFG;

if (!empty($CFG->allowemailaddresses)) {
$allowed = explode(' ', $CFG->allowemailaddresses);
foreach ($allowed as $allowedpattern) {
$allowedpattern = trim($allowedpattern);
if (!$allowedpattern) {
continue;
}
if (strpos($email, $allowedpattern) !== false) { // Match!
return false;
}
}
return get_string("emailonlyallowed", '', $CFG->allowemailaddresses);

} else if (!empty($CFG->denyemailaddresses)) {
$denied = explode(' ', $CFG->denyemailaddresses);
foreach ($denied as $deniedpattern) {
$deniedpattern = trim($deniedpattern);
if (!$deniedpattern) {
continue;
}
if (strpos($email, $deniedpattern) !== false) { // Match!
return get_string("emailnotallowed", '', $CFG->denyemailaddresses);
}
}
}

return false;
}


/// FILE HANDLING /////////////////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions login/signup.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ function validate_form($user, &$err) {
$err->country = get_string("missingcountry");
}

if (empty($err->email)) {
if ($error = email_is_not_allowed($user->email)) {
$err->email = $error;
}
}

return;
}

Expand Down
10 changes: 8 additions & 2 deletions user/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,21 @@ function find_form_errors(&$user, &$usernew, &$err) {
if (empty($usernew->country))
$err["country"] = get_string("missingcountry");

if (! validate_email($usernew->email))
if (! validate_email($usernew->email)) {
$err["email"] = get_string("invalidemail");

else if ($otheruser = get_record("user", "email", $usernew->email)) {
} else if ($otheruser = get_record("user", "email", $usernew->email)) {
if ($otheruser->id <> $user->id) {
$err["email"] = get_string("emailexists");
}
}

if (empty($err["email"]) and !isadmin()) {
if ($error = email_is_not_allowed($usernew->email)) {
$err["email"] = $error;
}
}

$user->email = $usernew->email;

return count($err);
Expand Down

0 comments on commit a50ded8

Please sign in to comment.