Auth::set_user(): make method optional. If not provided, do not change session and do not trigger the hook

This commit is contained in:
Benjamin Renard 2023-02-28 16:51:32 +01:00
parent 37eab5d49f
commit 4ff9aa71ad
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -2,6 +2,8 @@
namespace EesyPHP; namespace EesyPHP;
use EesyPHP\Auth\User;
class Auth { class Auth {
@ -264,14 +266,23 @@ class Auth {
/** /**
* Helper to set current authenticated user * Helper to set current authenticated user
* @param \EesyPHP\Auth\User $user The current authenticated user object * @param \EesyPHP\Auth\User $user The current authenticated user object
* @param string $method Method used to authenticate the user * @param string|null $method Method used to authenticate the user. If not null, user information
* will be changed in session.
* @return void * @return void
*/ */
public static function set_user($user, $method) { public static function set_user($user, $method=null) {
if (!$user instanceof User)
throw new \Exception(
"Provided user is not an instance of \\EesyPHP\\Auth\\User: ".vardump($user)
);
self :: $user = $user;
if (!$method) return;
// Method provided: update user info in session and trigger hook
Log :: debug( Log :: debug(
'User %s authenticated using method %s and backend %s', 'User %s authenticated using method %s and backend %s',
$user->username, $method, $user->backend); $user, $method, $user->backend);
self :: $user = $user;
self :: $logged_method = $method; self :: $logged_method = $method;
$_SESSION['user'] = serialize($user); $_SESSION['user'] = serialize($user);
$_SESSION['auth_method'] = $method; $_SESSION['auth_method'] = $method;