Skip to content

Commit 4641ce0

Browse files
committed
Set $active_user immediately after creating user when logging in for first time
Relates to: #55
1 parent a5a1f7c commit 4641ce0

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

model/user.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,11 @@ public function check_csrf_token($token) {
295295

296296
/**
297297
* Retrieve the user's details from LDAP.
298+
* @param bool $login true if getting user details as part of login process
298299
* @throws UserNotFoundException if the user is not found in LDAP
299300
*/
300-
public function get_details_from_ldap() {
301-
global $config, $group_dir, $user_dir;
301+
public function get_details_from_ldap($login = false) {
302+
global $config, $group_dir, $user_dir, $active_user;
302303
$attributes = array();
303304
$attributes[] = 'dn';
304305
$attributes[] = $config['ldap']['user_id'];
@@ -345,6 +346,9 @@ public function get_details_from_ldap() {
345346
$this->update();
346347
} else {
347348
$user_dir->add_user($this);
349+
if($login) {
350+
$active_user = $this;
351+
}
348352
}
349353
if(isset($config['ldap']['sync_groups']) && is_array($config['ldap']['sync_groups'])) {
350354
$syncgroups = $config['ldap']['sync_groups'];

model/userdirectory.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@ public function get_user_by_id($id) {
7878
* Get a user from the database by its uid. If it does not exist in the database, retrieve it
7979
* from LDAP and store in the database.
8080
* @param string $uid of user
81+
* @param bool $login true if getting user as part of login process
8182
* @return User with specified entity uid
8283
* @throws UserNotFoundException if no user with that uid exists
8384
*/
84-
public function get_user_by_uid($uid) {
85+
public function get_user_by_uid($uid, $login = false) {
8586
if(isset($this->cache_uid[$uid])) {
8687
return $this->cache_uid[$uid];
8788
}
@@ -96,7 +97,7 @@ public function get_user_by_uid($uid) {
9697
$user = new User;
9798
$user->uid = $uid;
9899
$this->cache_uid[$uid] = $user;
99-
$user->get_details_from_ldap();
100+
$user->get_details_from_ldap($login);
100101
}
101102
$stmt->close();
102103
return $user;

requesthandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
set_exception_handler('exception_handler');
2222

2323
if(isset($_SERVER['PHP_AUTH_USER'])) {
24-
$active_user = $user_dir->get_user_by_uid($_SERVER['PHP_AUTH_USER']);
24+
$active_user = $user_dir->get_user_by_uid($_SERVER['PHP_AUTH_USER'], true);
2525
} else {
2626
throw new Exception("Not logged in.");
2727
}

0 commit comments

Comments
 (0)