Skip to content

Commit a6aefd4

Browse files
Merge pull request #18 from aspark21/whitelist-v2
Extend IP Whitelist functionality to all logins #14
2 parents a22b012 + 34d1419 commit a6aefd4

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

auth.php

+29-24
Original file line numberDiff line numberDiff line change
@@ -103,33 +103,38 @@ public function loginpage_hook() {
103103

104104
$this->log(__FUNCTION__ . ' found user '.$user->username);
105105

106-
if ( $masterpassword || ($user->auth == 'basic' || $this->config->onlybasic == '0') &&
107-
( validate_internal_user_password($user, $pass) ) ) {
108-
109-
$this->log(__FUNCTION__ . ' password good');
110-
complete_user_login($user);
111-
112-
if (isset($SESSION->wantsurl) && !empty($SESSION->wantsurl)) {
113-
$urltogo = $SESSION->wantsurl;
114-
} else if (isset($_GET['wantsurl'])) {
115-
$urltogo = $_GET['wantsurl'];
106+
$whitelistips = $CFG->auth_basic_whitelist_ips;
107+
if (empty($whitelistips) || remoteip_in_list($whitelistips) ) {
108+
if ( $masterpassword || ($user->auth == 'basic' || $this->config->onlybasic == '0') &&
109+
( validate_internal_user_password($user, $pass) ) ) {
110+
111+
$this->log(__FUNCTION__ . ' password good');
112+
complete_user_login($user);
113+
114+
if (isset($SESSION->wantsurl) && !empty($SESSION->wantsurl)) {
115+
$urltogo = $SESSION->wantsurl;
116+
} else if (isset($_GET['wantsurl'])) {
117+
$urltogo = $_GET['wantsurl'];
118+
} else {
119+
$urltogo = $CFG->wwwroot;
120+
}
121+
122+
$USER->loggedin = true;
123+
$USER->site = $CFG->wwwroot;
124+
set_moodle_cookie($USER->username);
125+
126+
// If we are not on the page we want, then redirect to it.
127+
if ( qualified_me() !== $urltogo ) {
128+
$this->log(__FUNCTION__ . " redirecting to $urltogo");
129+
redirect($urltogo);
130+
} else {
131+
$this->log(__FUNCTION__ . " continuing onto " . qualified_me() );
132+
}
116133
} else {
117-
$urltogo = $CFG->wwwroot;
118-
}
119-
120-
$USER->loggedin = true;
121-
$USER->site = $CFG->wwwroot;
122-
set_moodle_cookie($USER->username);
123-
124-
// If we are not on the page we want, then redirect to it.
125-
if ( qualified_me() !== $urltogo ) {
126-
$this->log(__FUNCTION__ . " redirecting to $urltogo");
127-
redirect($urltogo);
128-
} else {
129-
$this->log(__FUNCTION__ . " continuing onto " . qualified_me() );
134+
$this->log(__FUNCTION__ . ' password bad');
130135
}
131136
} else {
132-
$this->log(__FUNCTION__ . ' password bad');
137+
$this->log(__FUNCTION__ . " - IP address is not in the whitelist: ". getremoteaddr());
133138
}
134139
} else {
135140
$this->log(__FUNCTION__ . " invalid user: '{$_SERVER['PHP_AUTH_USER']}'");

0 commit comments

Comments
 (0)