Set $active_user immediately after creating user when logging in for first time

Relates to: #55
This commit is contained in:
Thomas Pike
2021-11-07 21:05:51 +01:00
parent a5a1f7c16d
commit 4641ce0166
3 changed files with 10 additions and 5 deletions

View File

@@ -295,10 +295,11 @@ class User extends Entity {
/**
* Retrieve the user's details from LDAP.
* @param bool $login true if getting user details as part of login process
* @throws UserNotFoundException if the user is not found in LDAP
*/
public function get_details_from_ldap() {
global $config, $group_dir, $user_dir;
public function get_details_from_ldap($login = false) {
global $config, $group_dir, $user_dir, $active_user;
$attributes = array();
$attributes[] = 'dn';
$attributes[] = $config['ldap']['user_id'];
@@ -345,6 +346,9 @@ class User extends Entity {
$this->update();
} else {
$user_dir->add_user($this);
if($login) {
$active_user = $this;
}
}
if(isset($config['ldap']['sync_groups']) && is_array($config['ldap']['sync_groups'])) {
$syncgroups = $config['ldap']['sync_groups'];

View File

@@ -78,10 +78,11 @@ class UserDirectory extends DBDirectory {
* Get a user from the database by its uid. If it does not exist in the database, retrieve it
* from LDAP and store in the database.
* @param string $uid of user
* @param bool $login true if getting user as part of login process
* @return User with specified entity uid
* @throws UserNotFoundException if no user with that uid exists
*/
public function get_user_by_uid($uid) {
public function get_user_by_uid($uid, $login = false) {
if(isset($this->cache_uid[$uid])) {
return $this->cache_uid[$uid];
}
@@ -96,7 +97,7 @@ class UserDirectory extends DBDirectory {
$user = new User;
$user->uid = $uid;
$this->cache_uid[$uid] = $user;
$user->get_details_from_ldap();
$user->get_details_from_ldap($login);
}
$stmt->close();
return $user;

View File

@@ -21,7 +21,7 @@ ob_start();
set_exception_handler('exception_handler');
if(isset($_SERVER['PHP_AUTH_USER'])) {
$active_user = $user_dir->get_user_by_uid($_SERVER['PHP_AUTH_USER']);
$active_user = $user_dir->get_user_by_uid($_SERVER['PHP_AUTH_USER'], true);
} else {
throw new Exception("Not logged in.");
}